Skip to content

Commit c50cec1

Browse files
committed
Adding a job that checks the PR and commit formats.
This job will make sure that: * All commits in a PR were squashed to a single commit * The PR title and commit title are identical * the PR description and commit message are identical Signed-off-by: Yoni Bettan <[email protected]>
1 parent 515e44d commit c50cec1

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

ci/prow/check-commit

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bash
2+
3+
set -euxo pipefail
4+
5+
#FIXME: check if there is a prow github token that can be use to increase the api rate limit from 60 req/h to 5000 req/h
6+
7+
header='Accept: application/vnd.github+json'
8+
9+
api_call() {
10+
res=$(curl -H 'Accept: application/vnd.github+json' https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/${1})
11+
echo ${res}
12+
}
13+
14+
get_field_from_json() {
15+
echo ${1} | jq ".${2}"
16+
}
17+
18+
pr=$(api_call pulls/${PULL_NUMBER})
19+
20+
number_of_commits=$(get_field_from_json "${pr}" "commits")
21+
if [[ ${number_of_commits} != 1 ]]; then
22+
echo '
23+
All PRs must contain a single commit. Please refer to https://github.com/kubernetes-sigs/kernel-module-management/blob/main/CONTRIBUTING.md
24+
'
25+
exit 1
26+
fi
27+
28+
commits=$(api_call pulls/${PULL_NUMBER}/commits)
29+
30+
# The PR title and body are interpreted differently than the commit message in the folowing ways:
31+
# 1. The PR title and body are different json objects while the commit message is a single json.
32+
# - We combine the PR title and body for the comparison.
33+
# - We remove quots because we combined multiple strings.
34+
# 2. PR data is using \r\n for new paragraphs while the commit message is using \n\n.
35+
# - We replace all instances of \r\n and \n\n with spaces for the comparison.
36+
# 3. Those manipulation messes the number of spaces in the strings.
37+
# - We trim all the redundant occurences of spaces for the comparison.
38+
pr_title=$(get_field_from_json "${pr}" "title" | sed 's/"//g' | sed 's/\\r\\n/ /g' | sed 's/ \+/ /g')
39+
pr_body=$(get_field_from_json "${pr}" "body" | sed 's/"//g' | sed 's/\\r\\n/ /g' | sed 's/ \+/ /g')
40+
pr_title_and_body="${pr_title} ${pr_body}"
41+
commit_message=$(get_field_from_json "${commits}" "[].commit.message" | sed 's/"//g' | sed 's/\\n\\n/ /g' | sed 's/\\n/ /g' | sed 's/ \+/ /g')
42+
43+
if [[ "${pr_title_and_body}" != "${commit_message}" ]]; then
44+
echo "
45+
Commit and PR are out of sync. Make sure the following are identical:
46+
* PR title <--> commit title
47+
* PR description <--> commit message
48+
"
49+
50+
echo "
51+
PR:
52+
53+
${pr_title_and_body}
54+
55+
Commit:
56+
57+
${commit_message}
58+
"
59+
exit 1
60+
fi

0 commit comments

Comments
 (0)