Auto-Delete Very Short Discussions #1
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: Auto-Delete Very Short Discussions | |
on: | |
discussion: | |
types: [created] | |
permissions: | |
issues: write | |
discussions: write | |
jobs: | |
validate-discussion: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check word and character count | |
id: validation | |
run: | | |
MIN_WORDS=5 | |
MIN_CHARS=10 | |
BODY=$(jq -r '.discussion.body' "$GITHUB_EVENT_PATH") | |
WORD_COUNT=$(echo "$BODY" | wc -w) | |
CHAR_COUNT=$(echo -n "$BODY" | wc -m) | |
if [ "$WORD_COUNT" -lt $MIN_WORDS ] || [ "$CHAR_COUNT" -lt $MIN_CHARS ]; then | |
echo "too_short=true" >> "$GITHUB_OUTPUT" | |
echo "words=$WORD_COUNT" >> "$GITHUB_OUTPUT" | |
echo "chars=$CHAR_COUNT" >> "$GITHUB_OUTPUT" | |
else | |
echo "too_short=false" >> "$GITHUB_OUTPUT" | |
fi | |
echo "$GITHUB_OUTPUT" | |
- name: Notify User via GitHub Mention (Generates Email) then Delete Discussion | |
if: steps.validation.outputs.too_short == 'true' | |
uses: actions/github-script@v7 | |
env: | |
WORDS: ${{ steps.validation.outputs.words }} | |
CHARS: ${{ steps.validation.outputs.chars }} | |
with: | |
script: | | |
const { discussion } = context.payload; | |
await github.graphql(` | |
mutation($discussionId: ID!, $body: String!) { | |
addDiscussionComment(input: { | |
discussionId: $discussionId, | |
body: $body | |
}) { comment { id } } | |
}`, | |
{ | |
discussionId: discussion.node_id, | |
body: `⚠️ @${discussion.user.login}\nDiscussion closed: To protect against spam, discussion posts that do not meet a minimum word and character count are automatically deleted. Please provide more details in a new post.` | |
} | |
); | |
# await github.graphql(` | |
# mutation($discussionId: ID!) { | |
# deleteDiscussion(input: {id: $discussionId}) { discussion { id } } | |
# }`, { discussionId: discussion.node_id } | |
# ); |