Skip to content

Commit a767cce

Browse files
committed
[antispam] experimenting (WIP)
1 parent 0d3b9b6 commit a767cce

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

.github/workflows/scripts/antispam.js

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,78 @@
1-
async function when_suspicious(){
1+
async function when_suspicious({ github, context, failed_checks }){
22

33
// might wanna use a score of confidence (how suspicious it is), then react on that
44

5-
await github.rest.issues.addLabels({
6-
owner,
7-
repo,
8-
issue_number: issueNumber,
9-
labels: ["suspicious"]
10-
});
5+
const { owner, repo } = context.repo;
6+
const issueNumber = context.payload.issue.number;
117

12-
// await github.rest.issues.update({
8+
// await github.rest.issues.addLabels({
139
// owner,
1410
// repo,
1511
// issue_number: issueNumber,
16-
// state: "closed"
12+
// labels: ["suspicious"]
1713
// });
1814
// await github.rest.issues.createComment({
1915
// owner,
2016
// repo,
2117
// issue_number: issueNumber,
2218
// body: "This issue/PR has been automatically closed as it does not meet our contribution guidelines. Please read our contribution guide before submitting."
2319
// });
20+
21+
22+
const reasons = failed_checks.map(check => `- ${check.reason}`).join("\n");
23+
const commentBody = `This issue/PR has been automatically flagged as [suspicious] as it might not meet contribution requirements.
24+
Please read our contribution guide before submitting.
25+
26+
Reason(s):
27+
28+
${reasons}
29+
`;
30+
31+
console.log(">>> DEBUG", commentBody);
32+
33+
// await github.rest.issues.createComment({
34+
// owner,
35+
// repo,
36+
// issue_number: issueNumber,
37+
// body: commentBody
38+
// });
39+
40+
41+
// TODO: if too many checks failed, then consider immediatly closing:
42+
43+
// await github.rest.issues.update({
44+
// owner,
45+
// repo,
46+
// issue_number: issueNumber,
47+
// state: "closed"
48+
// });
49+
50+
}
51+
52+
class Check {
53+
constructor(func, reason) {
54+
this.func = func;
55+
this.reason = reason;
56+
}
57+
58+
evaluate() { return this.func(); }
2459
}
2560

2661
module.exports = async ({ github, context, core }) => {
62+
63+
const { owner, repo } = context.repo;
2764
const {SHA} = process.env;
2865
const author = context.actor;
2966

3067
const { data: user } = await github.rest.users.getByUsername({ username: author });
3168

32-
console.log(">>> module: ", {
33-
user: user,
34-
author: author
35-
});
3669

37-
// check if the account was created within the last hour
38-
const is_author_account_recently_created = (() => {
70+
const was_author_account_recently_created = (() => {
3971
const createdAt = new Date(user.created_at);
4072
const now = new Date();
4173
const accountAgeInMinutes = (now - createdAt) / (1000 * 60);
4274

43-
console.log(">>> is_author_account_recently_created: ", {
75+
console.log(">>> was_author_account_recently_created: ", {
4476
now: now,
4577
createdAt: createdAt,
4678
accountAgeInMinutes: accountAgeInMinutes
@@ -63,8 +95,18 @@ module.exports = async ({ github, context, core }) => {
6395
return events.length === 0;
6496
})();
6597

66-
is_author_account_recently_created();
67-
is_author_only_contribution_on_GH();
98+
const checks = [
99+
new Check(() => was_author_account_recently_created, "Account is less than an hour old"),
100+
new Check(() => is_author_only_contribution_on_GH, "First contribution to any GitHub project"),
101+
new Check(() => user.followers === 0 && user.following === 0, "Author has no relationships"),
102+
new Check(() => user.public_repos === 0 && user.public_gists === 0, "Author has no public reop/gist"),
103+
];
104+
105+
const failed_checks = checks.filter(check => ! check.evaluate());
106+
107+
const threshold = 0;
108+
if (failed_checks.length <= threshold)
109+
return;
68110

69-
// when_suspicious();
111+
when_suspicious({ github, context, failed_checks});
70112
};

0 commit comments

Comments
 (0)