Skip to content

Commit cbc9d8b

Browse files
committed
Initial commit
1 parent 3ffa855 commit cbc9d8b

6 files changed

Lines changed: 207 additions & 1 deletion

File tree

.github/workflows/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: checkpatch review
2+
on: [pull_request]
3+
jobs:
4+
my_review:
5+
name: checkpatch review
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v1
9+
- name: Run checkpatch review
10+
uses: webispy/checkpatch-action@master
11+
env:
12+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM webispy/checkpatch
2+
3+
COPY entrypoint.sh /entrypoint.sh
4+
COPY review.sh /review.sh
5+
6+
ENTRYPOINT ["/entrypoint.sh"]

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
# helloworld-docker-action
1+
# Github action for `checkpatch.pl`
2+
3+
The `checkpatch.pl` is a perl script to verify that your code conforms to the Linux kernel coding style. This project uses `checkpatch.pl` to automatically review and leave comments on pull requests.
4+
5+
## Resources
6+
7+
Following files are used to this project.
8+
9+
- https://raw.githubusercontent.com/torvalds/linux/master/scripts/checkpatch.pl
10+
- https://raw.githubusercontent.com/torvalds/linux/master/scripts/spelling.txt
11+
12+
## Patch
13+
14+
### add option for excluding directories
15+
16+
From [zephyr](https://github.com/zephyrproject-rtos/zephyr) project:
17+
18+
- https://github.com/zephyrproject-rtos/zephyr/commit/92a12a19ae5ac5fdf441c690c48eed0052df326d
19+
20+
### Disable warning for "No structs that should be const ..."
21+
22+
- https://github.com/nugulinux/docker-devenv/blob/master/patches/0002-ignore_const_struct_warning.patch

action.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: 'checkpatch.pl PR review'
2+
description: 'Codew review for PR using checkpatch.pl'
3+
branding:
4+
icon: 'check-square'
5+
color: 'purple'
6+
runs:
7+
using: 'docker'
8+
image: 'Dockerfile'

entrypoint.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
echo "Start..."
4+
echo "Workflow: $GITHUB_WORKFLOW"
5+
echo "Action: $GITHUB_ACTION"
6+
echo "Actor: $GITHUB_ACTOR"
7+
echo "Repository: $GITHUB_REPOSITORY"
8+
echo "Event-name: $GITHUB_EVENT_NAME"
9+
echo "Event-path: $GITHUB_EVENT_PATH"
10+
echo "Workspace: $GITHUB_WORKSPACE"
11+
echo "SHA: $GITHUB_SHA"
12+
echo "REF: $GITHUB_REF"
13+
echo "HEAD-REF: $GITHUB_HEAD_REF"
14+
echo "BASE-REF: $GITHUB_BASE_REF"
15+
echo "TOKEN: $GITHUB_TOKEN"
16+
pwd
17+
18+
RESULT=0
19+
20+
# Run review.sh on each commit in the PR
21+
for sha1 in $(git rev-list origin/$GITHUB_BASE_REF..origin/$GITHUB_HEAD_REF); do
22+
echo "Check - Commit id $sha1"
23+
/review.sh ${sha1} || RESULT=1;
24+
echo "Result: ${RESULT}"
25+
done
26+
27+
echo "Done"
28+
29+
exit $RESULT

review.sh

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)