Check for Spammy Issue Comments #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check for Spammy Issue Comments | |
on: | |
issue_comment: | |
types: [created, edited] | |
permissions: | |
issues: write | |
jobs: | |
link-check: | |
name: Remove suspicious links from issue comments | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const comment = context.payload.comment | |
const { owner, repo } = context.repo | |
console.log('Repository owner:', owner) | |
console.log('Repository name:', repo) | |
console.log('Comment body:', comment.body) | |
// Array of regex patterns and their replacements | |
const regexReplacements = [ | |
// File sharing websites (general) | |
{ | |
pattern: /https?:\/\/(www\.)?(box|dropbox|mediafire|sugarsync|tresorit|hightail|opentext|sharefile|citrixsharefile|icloud|onedrive|1drv)\.com\/[^\s\)]+/g, | |
replacement: '[Link removed for safety reasons]' | |
}, | |
// Google Drive (needs to match subdomain) | |
{ | |
pattern: /https?:\/\/drive\.google\.com\/[^\s\)]+/g, | |
replacement: '[Link removed for safety reasons]' | |
}, | |
// Link shorteners | |
{ | |
pattern: /https?:\/\/(www\.)?(bit\.ly|t\.co|tinyurl\.com|goo\.gl|ow\.ly|buff\.ly|is\.gd|soo\.gd|t2mio|bl\.ink|clck\.ru|shorte\.st|cutt\.ly|v\.gd|qr\.ae|rb\.gy|rebrand\.ly|tr\.im|shorturl\.at|lnkd\.in)\/[^\s\)]+/g | |
replacement: '[Shorten link removed for safety reasons]' | |
}, | |
// Add more as needed | |
]; | |
// Iterate through each regex and replace matches in the comment body | |
let updatedBody = comment.body; | |
regexReplacements.forEach(({ pattern, replacement }) => { | |
if (pattern.test(updatedBody)) { | |
console.log(`Pattern found: ${pattern}`); | |
updatedBody = updatedBody.replace(pattern, replacement); | |
} | |
}); | |
// If the comment body was updated, edit the comment | |
if (updatedBody !== comment.body) { | |
console.log('Updated comment body:', updatedBody); | |
// Edit the comment with the updated body | |
await github.rest.issues.updateComment({ | |
owner: owner, | |
repo: repo, | |
comment_id: comment.id, | |
body: updatedBody | |
}); | |
} else { | |
console.log('No suspicious links found.') | |
} |