Auto Respond to App Requests #112
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 Respond to App Requests | |
on: | |
discussion: | |
types: [created] | |
permissions: | |
contents: read | |
discussions: write | |
issues: write | |
jobs: | |
respond: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out repository | |
uses: actions/checkout@v4 | |
- 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: If spam, 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}\nTo protect against spam, this discussion will be deleted in a few minutes as it does not meet a minimum word and character count. Please create a more detailed post.` | |
} | |
); | |
await new Promise(resolve => setTimeout(resolve, 5000 * 60)) // Give GitHub time to email the user about the above comment | |
await github.graphql(` | |
mutation($discussionId: ID!) { | |
deleteDiscussion(input: {id: $discussionId}) { discussion { id } } | |
}`, { discussionId: discussion.node_id } | |
); | |
- name: If not spam, post response comment | |
if: steps.validation.outputs.too_short != 'true' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require('fs'); | |
const RESPONSE_CONTENT = fs.readFileSync('.github/discussion_auto_response.md', 'utf8'); | |
const discussionId = context.payload.discussion.node_id; | |
const categoryName = context.payload.discussion.category.name; | |
if (categoryName === 'App Requests') { | |
const query = ` | |
mutation($discussionId: ID!, $body: String!) { | |
addDiscussionComment(input: {discussionId: $discussionId, body: $body}) { | |
comment { | |
id | |
} | |
} | |
} | |
`; | |
await github.graphql(query, { | |
discussionId: discussionId, | |
body: RESPONSE_CONTENT | |
}); | |
} |