-
Notifications
You must be signed in to change notification settings - Fork 64
61 lines (53 loc) · 2.05 KB
/
discussion_auto_close.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
name: Auto-Close 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 }
);