-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path04-commit-msg.sh
More file actions
executable file
·261 lines (225 loc) · 10.8 KB
/
04-commit-msg.sh
File metadata and controls
executable file
·261 lines (225 loc) · 10.8 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/bash
CHECKS_DIR=$(dirname $(realpath $0))
SCRIPT_DIR=$(dirname ${CHECKS_DIR})
source ${SCRIPT_DIR}/ci.functions
source ${CHECKS_DIR}/checks.functions
#set -e
shopt -s extglob
assert_env_variables --print PR_NUMBER PR_PATH PR_COMMITS_PATH PR_COMMENTS_PATH || exit 1
: ${PR_CHECKLIST_PATH:=/dev/stderr}
pr_title=$(jq -j -r '.title' ${PR_PATH} | sed -r -e 's/^\s*//g' -e 's/\s*$//g')
# We need to strip out any carriage returns('\r') so we can do
# more accurate comparisons.
pr_body=$(jq -j -r '.body | sub("\r";"";"g")' ${PR_PATH})
commit_count=$(jq -r '. | length' ${PR_COMMITS_PATH})
multiple_commits=$(jq -r "[ .[].body | match(\"(^|\r?\n)multiple-commits:[[:blank:]]*(standalone|interim)(\r?\n|$)\"; \"g\") | .captures[1].string ][0]" ${PR_COMMENTS_PATH})
readarray -d "" -t commit_msgs < <( jq --raw-output0 '.[].commit.message | sub("\r";"";"g")' ${PR_COMMITS_PATH})
# The commit title isn't split out for us like the PR title
# so we need to get the first line ourselves.
readarray -d "" -t commit_titles < <( jq --raw-output0 '.[].commit.message | sub("\r";"";"g") | split("\n")[0]' ${PR_COMMITS_PATH})
# The second line of the commit message should be a blank line.
readarray -d "" -t commit_blanks < <( jq --raw-output0 '.[].commit.message | sub("\r";"";"g") | split("\n")[1] // ""' ${PR_COMMITS_PATH})
# The rest of the commit message is the body.
readarray -d "" -t commit_bodies < <( jq --raw-output0 '.[].commit.message | sub("\r";"";"g") | split("\n")[2:] | join("\n")' ${PR_COMMITS_PATH})
debug_out "Checking for PR description/Commit msg title mismatches"
checklist_added=false
commit_title=$(echo "${commit_titles[0]}" | sed -r -e 's/^\s*//g' -e 's/\s*$//g')
if [ $commit_count -eq 1 ] && [ "$pr_title" != "${commit_title}" ] ; then
debug_out "PR title and commit title mismatch"
cat <<-EOF | print_checklist_item --append-newline
- [ ] The PR title does not match the commit title. This can cause
confusion for reviewers and future maintainers.
GitHub doesn't automatically update the PR title when you update
the commit message so if you've updated the commit with a force-push,
please update the PR title to match the new commit message body.
EOF
checklist_added=true
fi
echo -e $"${commit_bodies[0]}" | tr '[:cntrl:]' ' ' > /tmp/pr-commit-body-${PR_NUMBER}.txt
echo -e $"${pr_body}" | tr '[:cntrl:]' ' ' > /tmp/pr-desc-body-${PR_NUMBER}.txt
sed -i -r -e '/^$/d' \
-e $'s/[[:blank:]]+/ /g' \
-e 's@https://github.com/asterisk/[^/]+/issues/([0-9]+)@#\1@g' \
/tmp/pr-{desc,commit}-body-${PR_NUMBER}.txt
debug_out "Checking for PR description/Commit msg body mismatches"
if [ $commit_count -eq 1 ] && ! diff -qEZBbw --strip-trailing-cr /tmp/pr-{desc,commit}-body-${PR_NUMBER}.txt ; then
debug_out "PR description and commit message body mismatch."
cat <<-EOF | print_checklist_item --append-newline
- [ ] The PR description does not match the commit message body.
This can cause confusion for reviewers and future maintainers.
GitHub doesn't automatically update the PR description when you update
the commit message so if you've updated the commit with a force-push,
please update the PR description to match the new commit message body.
EOF
checklist_added=true
fi
debug_out "Checking for Commit msg blank line"
no_blank_line=false
for (( commit=0 ; commit < commit_count ; commit+=1 )) ; do
if [ -n "${commit_blanks[$commit]}" ] ; then
no_blank_line=true
fi
done
if $no_blank_line ; then
debug_out "Commit message doesn't contain a blank line after the title."
cat <<-EOF | print_checklist_item --append-newline
- [ ] A commit message doesn't contain a blank line after the title.
EOF
checklist_added=true
fi
declare -A has_fixes=( ["commit"]=false ["pr"]=false )
has_extra_trailers=false
check_for_extra_trailers() {
bad_trailers=$(echo "$2" | grep -A999 -E '^(http|https|Resolves|Closes|Fixes|DeveloperNote|UserNote|UpgradeNote)' | sed -n -r -e '/^[^ :]+:/!d;/(http|https|Resolves|Closes|Fixes|DeveloperNote|UpgradeNote|UserNote)/!p')
if [ -n "$bad_trailers" ] ; then
debug_out "${1} has extra trailers: ${bad_trailers}"
has_extra_trailers=true
fi
}
debug_out "Checking PR and commits for extra trailers."
check_for_extra_trailers "pr" "${pr_body}"
for (( commit=0 ; commit < commit_count ; commit+=1 )) ; do
check_for_extra_trailers "commit" "${commit_bodies[$commit]}"
done
if $has_extra_trailers ; then
cat <<-EOF | print_checklist_item --append-newline
- [ ] The PR description and/or commit message has unsupported trailers after
the \`Resolves\`, \`Closes\`, \`Fixes\`, \`UserNote\`, \`UpgradeNote\` and/or \`DeveloperNote\` trailers.
Please refrain from adding unsupported trailers as they will confuse the
release change log generation. If you really need them, please move them
before any of the supportred trailers and ensure there's a blank line after them.
EOF
checklist_added=true
fi
debug_out "Checking PR and commits for Fixes/Closes/Resolves."
has_bad_fixes=false
declare -A issues_resolved=()
check_for_bad_fixes() {
while read LINE ; do
# Skip the check if someone typed "Resolves an issue..."
[[ "$LINE" =~ ^(Resolves|Closes|Fixes)[[:blank:]][a-zA-Z] ]] && continue
[[ "$LINE" =~ (^|[[:cntrl:]])(Fixes|Closes|Resolves)([^[:cntrl:]]+) ]] || continue
keyword=${BASH_REMATCH[2]}
value=${BASH_REMATCH[3]}
has_fixes[$1]=true
debug_out "${1} has a '${keyword}' trailer. Checking value '${value}'."
if [[ ! "${value}" =~ ^[:][[:blank:]]([#]([0-9]+|GHSA))|(https://github.com/[^/]+/[^/]+/issues/[0-9]+) ]] || [[ ! "${2}" =~ (^|[[:cntrl:]][[:cntrl:]])${keyword} ]] ; then
debug_out "${1} '${keyword}' trailer is malformed."
has_bad_fixes=true
else
if [[ "${value}" =~ ^[:][[:blank:]]([#]([0-9]+|GHSA-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+))|(https://github.com/[^/]+/[^/]+/issues/([0-9]+)) ]] ; then
issue="${BASH_REMATCH[2]:-${BASH_REMATCH[4]}}"
issues_resolved[$issue]=true
fi
fi
done <<< $2
}
# Check PR and commits for bad fixes.
check_for_bad_fixes "pr" "${pr_body}"
for (( commit=0 ; commit < commit_count ; commit+=1 )) ; do
check_for_bad_fixes "commit" "${commit_bodies[$commit]}"
done
if $has_bad_fixes ; then
cat <<-EOF | print_checklist_item --append-newline
- [ ] The PR description and/or commit message has a malformed
\`Fixes\`, \`Closes\` or \`Resolves\` trailer.
The \`Fixes\`, \`Closes\` and \`Resolves\` keywords MUST be preceeded by a blank line
and followed immediately by a colon, a space, a hash sign(\`#\`), and the issue number.
If you have multiple issues to reference, you can add additional
\`Fixes\`, \`Closes\` and \`Resolves\` trailers on consecutive lines as long as the first
one has the preceeding blank line.
A malformed trailer will prevent the issue from being automatically closed when
the PR merges and from being listed in the release change logs.<br>
Regular expression: \`^(Fixes|Closes|Resolves): #[0-9]+$\`.<br>
Example: \`Fixes: #9999\`.
EOF
checklist_added=true
fi
debug_out "Checking PR for issue mentions. ${has_fixes[pr]} ${has_fixes['pr']}"
if ! ${has_fixes[pr]} ; then
pr_mentioned_by=( $(jq -r '.[] | select(.event == "cross-referenced") | select( .source.issue.pull_request == null) | .source.issue | ( .repository.owner.login + "/" + .repository.name + "#" + (.number |tostring))' $PR_TIMELINE_PATH) )
if [ ${#pr_mentioned_by[@]} -gt 0 ] ; then
cat <<-EOF | print_checklist_item --append-newline
- [ ] The PR is cross-referenced by one or more issues (${pr_mentioned_by[@]})
but doesn't contain any \`Fixes\`, \`Closes\` or \`Resolves\` trailers.
A missing trailer will prevent the issue from being automatically closed when
the PR merges and from being listed in the release change logs.<br>
Regular expression: \`^(Fixes|Closes|Resolves): #[0-9]+$\`.<br>
Example: \`Fixes: #9999\`.
EOF
debug_out "PR is cross-referenced by issues ${pr_mentioned_by[@]} but doesn't have any Fixes/Closes/Resolves trailers."
checklist_added=true
fi
fi
has_stray_refs=false
declare -a stray_issues
check_for_stray_refs() {
[[ "$2" =~ [[:blank:]](#[0-9]+)[[:blank:]] ]] || return 0
debug_out "${1} has a stray reference to issue ${BASH_REMATCH[1]}."
stray_issues+=("${BASH_REMATCH[1]}")
has_stray_refs=true
}
debug_out "Checking PR and commits for stray issue references."
if ! ${has_fixes[pr]} ; then
check_for_stray_refs "pr" "${pr_body}"
fi
if ! ${has_fixes[commit]} ; then
for (( commit=0 ; commit < commit_count ; commit+=1 )) ; do
check_for_stray_refs "commit" "${commit_bodies[$commit]}"
done
fi
if $has_stray_refs ; then
cat <<-EOF | print_checklist_item --append-newline
- [ ] The PR description and/or commit message references one or more
issues ( ${stray_issues[@]} ) without a \`Fixes:\`, \`Closes:\` or \`Resolves:\`
keyword. Without those keywords, the issues won't be automatically
closed when the PR merges and won't be listed in the release change logs.<br>
Regular expression: \`^(Fixes|Closes|Resolves): #[0-9]+$\`.<br>
Example: \`Fixes: #9999\`.
EOF
checklist_added=true
fi
has_bad_note=false
check_for_bad_notes() {
for keyword in UserNote UpgradeNote DeveloperNote ; do
[[ "${2}" =~ (^|[[:cntrl:]])(${keyword})([^[:cntrl:]]+) ]] || continue
debug_out "${1} has a '${keyword}' trailer. Checking for ':' and predeeding blank line."
if [[ ! "${2}" =~ (^|[[:cntrl:]][[:cntrl:]])${keyword}[:][[:blank:]] ]] ; then
debug_out "${1} '${keyword}' trailer either doesn't have a ':' after the keyword or doesn't have a preceeding blank line."
has_bad_note=true
fi
done
}
debug_out "Check PR and commits for notes."
check_for_bad_notes "pr" "${pr_body}"
for (( commit=0 ; commit < commit_count ; commit+=1 )) ; do
check_for_bad_notes "commit" "${commit_bodies[$commit]}"
done
if $has_bad_note ; then
cat <<-EOF | print_checklist_item --append-newline
- [ ] The PR description and/or commit message has malformed
\`UserNote\`, \`UpgradeNote\` and/or \`DeveloperNote\` trailers.
The \`UserNote\`, \`UpgradeNote\` and \`DeveloperNote\`
keywords MUST be predeeded by a blank line and
followed immediately by a colon and a space before
the actual note text. This is to ensure that the note is properly
formatted and displayed in the release change logs.
EOF
checklist_added=true
fi
if [ "${has_fixes[commit]}" != "${has_fixes[pr]}" ] ; then
debug_out "Commit has fixes but PR doesn't."
cat <<-EOF | print_checklist_item --append-newline
- [ ] Either the PR description has a \`Fixes\`, \`Closes\` or \`Resolves\` special trailer
but the commit mesage doesn't or the other way around.
A properly formatted \`Fixes\`, \`Closes\` or \`Resolves\`
trailer is required in the PR description to allow the issue and the PR
to be cross-linked and for the issue to be automatically closed when the
PR merges. It's also required in the commit message to allow the issue
to be listed in the release change logs.
EOF
checklist_added=true
fi
$checklist_added && exit $EXIT_CHECKLIST_ADDED
debug_out "No issues found."
exit $EXIT_OK