Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add a spam comment detection #3288

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/spam-comment-detection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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.toLowerCase()
const spam_words = process.env.SPAM_WORDS.split(',').map(w => w.toLowerCase())
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)) {
yanguoyu marked this conversation as resolved.
Show resolved Hide resolved
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 }}
SPAM_WORDS: ${{ github.secrets.SPAM_WORDS }}
Loading