1+ #! /bin/bash
2+
3+ # To debug the current script, please uncomment the following 'set -x' line
4+ # set -x
5+
6+ # Argument
7+ COMMIT=$1
8+
9+ # Get PR number
10+ PR=${GITHUB_REF# " refs/pull/" }
11+ PRNUM=${PR% " /merge" }
12+
13+ # Generate email style commit message
14+ PATCHMAIL=$( git show --format=email $1 | checkpatch.pl --no-tree -)
15+
16+ # Github REST API endpoints
17+ BODY_URL=https://api.github.com/repos/${GITHUB_REPOSITORY} /issues/${PRNUM} /comments
18+ CODE_URL=https://api.github.com/repos/${GITHUB_REPOSITORY} /pulls/${PRNUM} /comments
19+
20+ # Internal state variables
21+ RESULT=0
22+ FOUND=0
23+ MESSAGE=
24+
25+ # Write message to specific file and line
26+ function post_code_message()
27+ {
28+ curl $CODE_URL -s -H " Authorization: token ${GITHUB_TOKEN} " \
29+ -X POST --data " $( cat << EOF
30+ {
31+ "commit_id": "$COMMIT ",
32+ "path": "${FILE} ",
33+ "position": ${LINE} ,
34+ "body": "${MESSAGE} "
35+ }
36+ EOF
37+ ) "
38+ }
39+
40+ # Write message to pull-request comment
41+ function post_comment_message()
42+ {
43+ curl $BODY_URL -s -H " Authorization: token ${GITHUB_TOKEN} " \
44+ -H " Content-Type: application/json" \
45+ -X POST --data " $( cat << EOF
46+ {
47+ "body": ":warning: ${COMMIT} - ${MESSAGE} "
48+ }
49+ EOF
50+ ) "
51+ }
52+
53+ #
54+ # checkpatch.pl result format
55+ # ---------------------------
56+ #
57+ # Template:
58+ # ---------
59+ #
60+ # [WARNING/ERROR]: [message for code line]
61+ # #[id]: FILE: [filename]:[line-number]
62+ # +[code]
63+ # [empty line]
64+ #
65+ # [WARNING/ERROR]: [message for commit itself]
66+ #
67+ # total: [n] erros, [n] warnings, [n] lines checked
68+ #
69+ # example:
70+ # --------
71+ #
72+ # ERROR: xxxx
73+ # #15: FILE: a.c:3:
74+ # +int main() {
75+ #
76+ # ERROR: Missing Signed-off-by: line(s)
77+ #
78+ # total: ...
79+ #
80+
81+ while read -r row
82+ do
83+ # End of checkpatch.pl message
84+ if [[ " $row " =~ ^total: ]]; then
85+ break
86+ fi
87+
88+ # Additional parsing is needed
89+ if [[ " $FOUND " == " 1" ]]; then
90+
91+ # The row is started with "#"
92+ if [[ " $row " =~ ^\# ]]; then
93+ # Split the string using ':' seperator
94+ IFS=' :' read -r -a list <<< " $row"
95+
96+ # Get file-name after removing spaces.
97+ FILE=$( echo ${list[2]} | xargs)
98+
99+ # Get line-number
100+ LINE=${list[3]}
101+ else
102+ # An empty line means the paragraph is over.
103+ if [[ -z $row ]]; then
104+ if [[ -z $FILE ]]; then
105+ post_comment_message
106+ else
107+ post_code_message
108+ fi
109+
110+ # Code review found a problem.
111+ RESULT=1
112+
113+ FOUND=0
114+ FILE=
115+ LINE=
116+ fi
117+ fi
118+ fi
119+
120+ # Found warning or error paragraph
121+ if [[ " $row " =~ ^(WARNING| ERROR) ]]; then
122+ MESSAGE=$row
123+ FOUND=1
124+ FILE=
125+ LINE=
126+ fi
127+
128+ done <<< " $PATCHMAIL"
129+
130+ exit $RESULT
0 commit comments