Skip to content

Commit 0d64329

Browse files
authored
ci: add a spam comment detection (nervosnetwork#3288)
1 parent 7ed48f2 commit 0d64329

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Spam detection
2+
3+
on:
4+
issue_comment:
5+
types: [created, edited]
6+
7+
jobs:
8+
detect-spam:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Set up Node.js
12+
uses: actions/setup-node@main
13+
14+
- name: Check for Spam
15+
uses: actions/github-script@v7
16+
with:
17+
script: |
18+
const comment = process.env.COMMENT_BODY.toLowerCase()
19+
const spam_words = process.env.SPAM_WORDS.split(',').map(w => w.toLowerCase())
20+
const comment_id = process.env.COMMENT_ID
21+
const issue_number = process.env.ISSUE_NUMBER
22+
const owner = process.env.REPO_OWNER
23+
const repo = process.env.REPO_NAME
24+
const EXTERNAL_LINK_REGEXT = /https:\/\/(?!((\w+\.)?github\.com|github\.com|(\w+\.)?magickbase\.com|(\w+\.)?nervos\.org))/gi
25+
if (spam_words.some(w => comment.includes(w))) {
26+
console.info(`Spam comment: ${comment}`)
27+
github.rest.issues.deleteComment({ owner, repo, comment_id })
28+
} else if (EXTERNAL_LINK_REGEXT.test(comment)) {
29+
console.info(`External link detected, append an annotation`)
30+
github.rest.issues.createComment({
31+
owner,
32+
repo,
33+
issue_number,
34+
body: `An external link is mentioned in the comment above. Please verify the link's safety before visiting.`
35+
})
36+
} else {
37+
console.info("No spam detected")
38+
}
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
COMMENT_BODY: ${{ github.event.comment.body }}
42+
COMMENT_ID: ${{ github.event.comment.id }}
43+
ISSUE_NUMBER: ${{ github.event.issue.number }}
44+
REPO_OWNER: ${{github.repository_owner }}
45+
REPO_NAME: ${{ github.event.repository.name }}
46+
SPAM_WORDS: ${{ github.secrets.SPAM_WORDS }}

0 commit comments

Comments
 (0)