-
-
Notifications
You must be signed in to change notification settings - Fork 509
87 lines (75 loc) · 3.23 KB
/
Issue-Handler.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Name of the GitHub Action
name: Check and Close Issues
# Trigger the action on issue events, specifically when an issue is opened
on:
issues:
types: [opened]
# Job definitions
jobs:
handle-issues:
# Run this job only for issues
if: github.event_name == 'issues'
# Specify the runner environment
runs-on: ubuntu-latest
steps:
# Step 1: Check out the repository code
- name: Check out code
uses: actions/checkout@v4
# Step 2: Set up Node.js environment (version 16)
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 16
# Step 3: Custom script to check and close issues
- name: Check and close issues
id: close-issues
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
// Define keywords and labels for issue filtering
const keywordsToCheck = ['instagram', 'facebook', 'twitter', 'HTTP Error 403', 'not a bot'];
const requiredLabel = 'new issue';
const referenceIssueNumber = 1399;
const actionClosedLabel = 'action-closed'; // Unique label to track action-closed issues
// Function to process each issue
async function processIssue(issue) {
const issueBody = issue.body.toLowerCase();
const issueLabels = issue.labels.map(label => label.name);
const wasClosedByAction = issueLabels.includes(actionClosedLabel);
// Determine if the issue should be closed
const shouldCloseIssue = !wasClosedByAction &&
keywordsToCheck.some(keyword => issueBody.includes(keyword)) && issueLabels.includes(requiredLabel);
// Close the issue if it meets the criteria
if (shouldCloseIssue) {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: 'closed'
});
// Add labels and comment to the closed issue
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
labels: ['duplicate', actionClosedLabel]
});
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
name: requiredLabel
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: `This issue has been closed and labeled as duplicate. Please see issue #${referenceIssueNumber} for more details. If you believe this is not the case, you can reopen this issue.`
});
}
}
// Process newly opened issues
if (context.payload.action === 'opened') {
await processIssue(context.payload.issue);
}