-
-
Notifications
You must be signed in to change notification settings - Fork 153
136 lines (116 loc) · 4.88 KB
/
Copy pathpr-template.yml
File metadata and controls
136 lines (116 loc) · 4.88 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
name: PR template guard
on:
pull_request_target:
types: [opened, reopened, edited, synchronize]
permissions:
contents: read
pull-requests: write
issues: write
jobs:
check:
runs-on: ubuntu-latest
if: >
github.repository == 'gnmyt/MySpeed' &&
!endsWith(github.event.pull_request.user.login, '[bot]')
steps:
- name: Check pull request body
id: check
env:
BODY: ${{ github.event.pull_request.body }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
run: |
strip() { tr -d '\r' | sed -e 's/<!--.*-->//g' -e '/<!--/,/-->/d'; }
printf '%s' "$BODY" | strip > body.md
gh api "repos/$REPO/contents/.github/PULL_REQUEST_TEMPLATE.md?ref=$BASE_SHA" \
--jq '.content' | base64 -d | strip > template.md
section() {
awk -v want="$2" '
/^#{1,6} / { keep = (index(tolower($0), tolower(want)) > 0); next }
keep { print }
' "$1"
}
items() {
section "$1" "$2" \
| grep -E '^[[:space:]]*- \[[ xX]\]' \
| sed -E -e 's/^[[:space:]]*- \[[ xX]\][[:space:]]*//' -e 's/[[:space:]]+$//' \
| tr -s ' ' | tr '[:upper:]' '[:lower:]' \
| grep -v 'other:' | sort || true
}
headings() {
grep -E '^#{1,6} ' "$1" \
| sed -E -e 's/^#{1,6}[[:space:]]*//' -e 's/[[:space:]]+$//' \
| tr -s ' ' | tr '[:upper:]' '[:lower:]' | sort || true
}
problems=""
add() { problems="${problems}- $1"$'\n'; }
tampered() {
items template.md "$1" > canonical.txt
items body.md "$1" > given.txt
[ -n "$(comm -3 canonical.txt given.txt || true)" ]
}
if ! grep -qi '^#\{1,6\} .*Changes made to' body.md || ! grep -qi '^#\{1,6\} .*Checklist' body.md; then
add "the pull request template was not used"
else
headings template.md > canonical_headings.txt
headings body.md > given_headings.txt
if [ -n "$(comm -3 canonical_headings.txt given_headings.txt || true)" ]; then
add "the section headings were edited, please leave them as they are"
fi
if grep -qi '^#\{1,6\} .*Description' body.md; then
description=$(section body.md "Description" | grep -v '^\s*$' || true)
if [ -z "$description" ]; then
add "the description is empty"
fi
fi
if tampered "Changes made to"; then
add "the options under \"Changes made to ...\" were edited, please leave them as they are"
elif ! section body.md "Changes made to" | grep -qi '^\s*- \[x\]'; then
add "no option is selected under \"Changes made to ...\""
fi
if tampered "Checklist"; then
add "the checklist was edited, please leave the items as they are"
elif section body.md "Checklist" | grep -q '^\s*- \[ \]'; then
add "not every item in the checklist is checked"
fi
fi
{
echo "problems<<EOF"
printf '%s' "$problems"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Update comment
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
AUTHOR: ${{ github.event.pull_request.user.login }}
PROBLEMS: ${{ steps.check.outputs.problems }}
MARKER: "<!-- pr-template-guard -->"
run: |
comment_id=$(gh api --paginate "repos/$REPO/issues/$PR/comments" \
--jq ".[] | select(.body | contains(\"$MARKER\")) | .id" | head -n1)
if [ -z "$PROBLEMS" ]; then
if [ -n "$comment_id" ]; then
gh api -X DELETE "repos/$REPO/issues/comments/$comment_id"
fi
gh pr edit "$PR" --repo "$REPO" --remove-label "incomplete template" || true
exit 0
fi
{
echo "$MARKER"
echo "Hey @$AUTHOR, thanks for the pull request!"
echo
echo "Before we can review it, could you complete the pull request template? Right now:"
echo
printf '%s' "$PROBLEMS"
echo
echo "You can edit the description at any time, this comment disappears once everything is filled in."
} > comment.md
if [ -n "$comment_id" ]; then
gh api -X PATCH "repos/$REPO/issues/comments/$comment_id" -F body=@comment.md >/dev/null
else
gh pr comment "$PR" --repo "$REPO" --body-file comment.md
fi
gh pr edit "$PR" --repo "$REPO" --add-label "incomplete template" || true