-
-
Notifications
You must be signed in to change notification settings - Fork 89
200 lines (159 loc) · 7.31 KB
/
spam-remover.yml
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
name: Spam issues handler
on:
issues:
types: [opened, edited, labeled, unlabeled]
jobs:
check-issue:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v4
timeout-minutes: 3
with:
script: |
/* https://octokit.github.io/rest.js/v20/#issues */
/* console.log(context); */
const config = {
labelsToAbort: ['no-bot'],
commonSpamSentences: [
'Read damn arch-wiki before borking your computer',
],
labelViolation: 'spambot',
commentsIdentification: {
startToken: '<!-- Do not change! SpamRemoverBot identifier -->',
userLogins: [ 'github-actions[bot]' ],
ignoreCommentsOlderThanDays: 60
}
};
const issueTitle = context.payload.issue.title;
const issueLabels = context.payload.issue.labels.map(i => i.name);
const issueBody = context.payload.issue.body;
const issueLocked = context.payload.issue.locked;
console.log('--- Issue Info ---');
console.log('Title', issueTitle);
console.log('Labels', issueLabels);
/*
console.log('Body', issueBody);
*/
console.log('---------');
// Check for immediate abort
if (config.labelsToAbort.some(label => issueLabels.includes(label))) {
console.log('Detected a abort label, will abort workflow');
return;
}
if (issueLocked) {
console.log('*** Issue is already locked ***');
} else {
const validationResult = isIssueValid(issueTitle, issueLabels, issueBody, config);
console.log('*** Manage label ***');
manageLabel(config, issueLabels, validationResult);
console.log('*** Manage comments ***');
manageComments(config, config.commentsIdentification, validationResult);
}
function isIssueValid(issueTitle, issueLabels, issueBody, config) {
console.log('Performing checks...');
let failedChecks = [];
for(const spamSentence of config.commonSpamSentences) {
console.log('Checking for ' + spamSentence + '...');
if (issueTitle.includes(spamSentence) || issueBody.includes(spamSentence)) {
console.log('FAILED');
failedChecks.push(spamSentence);
} else {
console.log('OK');
}
}
return {
valid: failedChecks.length == 0,
failedChecks: failedChecks
};
}
function manageLabel(config, issueLabels, validationResult) {
if (validationResult.valid) {
/*
if (issueLabels.includes(config.labelViolation)) {
console.log('Removing label')
github.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: config.labelViolation
})
} else {
console.log('No label to remove')
}
*/
} else {
if (!issueLabels.includes(config.labelViolation)) {
console.log('Adding label');
github.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [ config.labelViolation ]
});
} else {
console.log('Label already exists');
}
}
}
async function manageComments(config, commentsIdentificationConfig, validationResult) {
console.log('Trying to remove the same existing comments')
let since_date = new Date()
since_date.setDate(new Date().getDate() - commentsIdentificationConfig.ignoreCommentsOlderThanDays)
const comments_payload = await github.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
since: since_date.toISOString()
})
console.log('Found ' + comments_payload.data.length + 'x comments')
const commentsToDelete = comments_payload.data.filter(c => {
let matchingComment =
// Check if the user who commented is the expected one
commentsIdentificationConfig.userLogins.includes(c.user.login) &&
// Check if the comment body starts with the startToken
c.body.startsWith(commentsIdentificationConfig.startToken);
return matchingComment
})
console.log('The following comments will be deleted:', commentsToDelete)
let counter = 0
for(const commentToDelete of commentsToDelete) {
counter++
if (counter > 3) {
core.warning('Detected potential malfunction: Deleting an unusual high amount of comments - Aborting')
console.log('Aborting comment deletion')
break
}
console.log('Deleting comment with id', commentToDelete.id)
github.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: commentToDelete.id
})
}
if (!validationResult.valid) {
console.log('Creating new comment');
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body:
commentsIdentificationConfig.startToken + '\r\n'
+ '#### Spambot\r\n'
+ 'Spambot detected that this issue is spam.'
});
/*+ validationResult.failedChecks.map(c => '* ' + c.errorMsg).join('\r\n')*/
console.log('Closing the ticket');
github.issues.update({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed',
});
console.log('Locking the ticket');
github.issues.lock({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
}
}