Auto-Close 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-Close Very Short Discussions | |
on: | |
discussion: | |
types: [created] | |
permissions: | |
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 | |
- name: Comment and close if too short | |
if: steps.validation.outputs.too_short == 'true' | |
run: | | |
curl -X POST \ | |
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"https://api.github.com/repos/${{ github.repository }}/discussions/${{ github.event.discussion.number }}/comments" \ | |
-d '{"body": "⚠️ Discussion closed: To protect against spam, discussion posts that do not meet a minimum word and character count are automatically closed. Please provide more details in your post."}' | |
curl -X PATCH \ | |
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"https://api.github.com/repos/${{ github.repository }}/discussions/${{ github.event.discussion.number }}" \ | |
-d '{"state": "closed"}' |