Check for Spammy Issue Comments #6
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@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 | |
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 = [ | |
{ | |
pattern: /https?:\/\/(www\.)?(example123456|mediafire|dropbox)\.com\/[^\s]+/g, | |
replacement: '[Link removed for safety reasons]' | |
} | |
// Add more regex-replacement pairs 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.') | |
} |