From c9041d26ffa87c57c6e1e18120eeb1a53f923625 Mon Sep 17 00:00:00 2001 From: Keith Date: Wed, 18 Dec 2024 23:24:23 +0800 Subject: [PATCH] ci: add a spam comment detection When a comment is added in an issue 1. if the comment has specific words, it will be deleted 2. if the comment has unexpected links, an annotation about safety will be appended. --- .github/workflows/spam-comment-detection.yaml | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .github/workflows/spam-comment-detection.yaml diff --git a/.github/workflows/spam-comment-detection.yaml b/.github/workflows/spam-comment-detection.yaml new file mode 100644 index 000000000..a3a1f0492 --- /dev/null +++ b/.github/workflows/spam-comment-detection.yaml @@ -0,0 +1,45 @@ +name: Spam detection + +on: + issue_comment: + types: [created, edited] + +jobs: + detect-spam: + runs-on: ubuntu-latest + steps: + - name: Set up Node.js + uses: actions/setup-node@main + + - name: Check for Spam + uses: actions/github-script@v7 + with: + script: | + const comment = process.env.COMMENT_BODY + const spam_words = ['Neuron Support Request'] + const comment_id = process.env.COMMENT_ID + const issue_number = process.env.ISSUE_NUMBER + const owner = process.env.REPO_OWNER + const repo = process.env.REPO_NAME + const EXTERNAL_LINK_REGEXT = /https:\/\/(?!((\w+\.)?github\.com|github\.com|(\w+\.)?magickbase\.com|(\w+\.)?nervos\.org))/gi + if (spam_words.some(w => comment.includes(w))) { + console.info(`Spam comment: ${comment}`) + github.rest.issues.deleteComment({ owner, repo, comment_id }) + } else if (EXTERNAL_LINK_REGEXT.test(comment)) { + console.info(`External link detected, append an annotation`) + github.rest.issues.createComment({ + owner, + repo, + issue_number, + body: `An external link is mentioned in the comment above. Please verify the link's safety before visiting.` + }) + } else { + console.info("No spam detected") + } + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + COMMENT_BODY: ${{ github.event.comment.body }} + COMMENT_ID: ${{ github.event.comment.id }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + REPO_OWNER: ${{github.repository_owner }} + REPO_NAME: ${{ github.event.repository.name }}