Skip to content

Commit 9fb45b1

Browse files
committed
Fixed posting only when GITHUB_TOKEN is present.
1 parent 005cc8a commit 9fb45b1

File tree

4 files changed

+100
-14
lines changed

4 files changed

+100
-14
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ jobs:
99
- name: Run checkpatch review
1010
uses: webispy/checkpatch-action@master
1111
env:
12-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,51 @@ From [zephyr](https://github.com/zephyrproject-rtos/zephyr) project:
2020
### Disable warning for "No structs that should be const ..."
2121

2222
- https://github.com/nugulinux/docker-devenv/blob/master/patches/0002-ignore_const_struct_warning.patch
23+
24+
## Action setup guide
25+
26+
### Pull Request from owned repository
27+
28+
.github/workflows/main.yml
29+
30+
```yml
31+
name: checkpatch review
32+
on: [pull_request]
33+
jobs:
34+
my_review:
35+
name: checkpatch review
36+
runs-on: ubuntu-latest
37+
steps:
38+
- uses: actions/checkout@v1
39+
- name: Run checkpatch review
40+
uses: webispy/checkpatch-action@master
41+
env:
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
```
44+
45+
The checkpatch action will posting comments for error/warning result to the PR conversation.
46+
47+
### Pull Request from forked repository
48+
49+
The Github action has a limitation that doesn't have write permission for PR from forked repository. So the action cannot write a comment to the PR.
50+
51+
.github/workflows/main.yml
52+
53+
```yml
54+
name: checkpatch review
55+
on: [pull_request]
56+
jobs:
57+
my_review:
58+
name: checkpatch review
59+
runs-on: ubuntu-latest
60+
steps:
61+
- uses: actions/checkout@v1
62+
- name: Run checkpatch review
63+
uses: webispy/checkpatch-action@master
64+
```
65+
66+
You can find the error/waring result from Github action console log.
67+
68+
# License
69+
70+
Since the `checkpatch.pl` file is a script in the Linux kernel source tree, you must follow the [GPL-2.0](https://github.com/torvalds/linux/blob/master/COPYING) license, which is your kernel license.

entrypoint.sh

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,40 @@ pwd
1616

1717
RESULT=0
1818

19-
# Get PR number
20-
PR=${GITHUB_REF#"refs/pull/"}
21-
PRNUM=${PR%"/merge"}
19+
# Check the input parameter
20+
echo
21+
if [[ -z "$GITHUB_TOKEN" ]]; then
22+
echo -e "\e[0;34mToken is empty. Review PR without comments.\e[0m"
23+
else
24+
echo -e "\e[0;34mReview PR with comments.\e[0m"
25+
fi
2226

2327
# Get commit list using Github API
28+
echo
29+
echo -e "\e[0;34mGet the list of commits included in the PR($GITHUB_REF).\e[0m"
30+
PR=${GITHUB_REF#"refs/pull/"}
31+
PRNUM=${PR%"/merge"}
2432
URL=https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${PRNUM}/commits
25-
list=$(curl $URL -X GET -s -H "Authorization: token ${GITHUB_TOKEN}" | jq '.[].sha' -r)
26-
echo $list
33+
echo " - API endpoint: $URL"
34+
35+
list=$(curl $URL -X GET -s | jq '.[].sha' -r)
36+
len=$(echo "$list" | wc -l)
37+
echo " - Commits $len: $list"
2738

2839
# Run review.sh on each commit in the PR
40+
echo
41+
echo -e "\e[0;34mStart review for each commits.\e[0m"
42+
43+
i=1
2944
for sha1 in $list; do
30-
echo "Check - Commit id $sha1"
45+
echo "-------------------------------------------------------------"
46+
echo -e "[$i/$len] Check commit - \e[1;34m$sha1\e[0m"
47+
echo "-------------------------------------------------------------"
3148
/review.sh ${sha1} || RESULT=1;
32-
echo "Result: ${RESULT}"
49+
echo
50+
((i++))
3351
done
3452

35-
echo "Done"
53+
echo -e "\e[1;34mDone\e[0m"
3654

3755
exit $RESULT

review.sh

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ MESSAGE=
2626
function post_code_message()
2727
{
2828
echo "POST to ${CODE_URL} with ${MESSAGE}"
29-
curl ${CODE_URL} -H "Authorization: token ${GITHUB_TOKEN}" \
29+
curl ${CODE_URL} -s \
30+
-H "Authorization: token ${GITHUB_TOKEN}" \
31+
-H "Content-Type: application/json" \
3032
-X POST --data "$(cat <<EOF
3133
{
3234
"commit_id": "$COMMIT",
@@ -42,7 +44,8 @@ EOF
4244
function post_comment_message()
4345
{
4446
echo "POST to ${BODY_URL} with ${MESSAGE}"
45-
curl ${BODY_URL} -H "Authorization: token ${GITHUB_TOKEN}" \
47+
curl ${BODY_URL} -s \
48+
-H "Authorization: token ${GITHUB_TOKEN}" \
4649
-H "Content-Type: application/json" \
4750
-X POST --data "$(cat <<EOF
4851
{
@@ -84,6 +87,7 @@ while read -r row
8487
do
8588
# End of checkpatch.pl message
8689
if [[ "$row" =~ ^total: ]]; then
90+
echo -e "\e[1m$row\e[0m"
8791
break
8892
fi
8993

@@ -103,10 +107,16 @@ do
103107
else
104108
# An empty line means the paragraph is over.
105109
if [[ -z $row ]]; then
106-
if [[ -z $FILE ]]; then
107-
post_comment_message
110+
if [[ ! -z "$GITHUB_TOKEN" ]]; then
111+
echo "Post comment to Github"
112+
if [[ -z $FILE ]]; then
113+
post_comment_message
114+
else
115+
post_code_message
116+
fi
108117
else
109-
post_code_message
118+
# Output empty line
119+
echo
110120
fi
111121

112122
# Code review found a problem.
@@ -125,8 +135,18 @@ do
125135
FOUND=1
126136
FILE=
127137
LINE=
138+
139+
echo -e "\e[1;31m$row\e[0m"
140+
else
141+
echo $row
128142
fi
129143

130144
done <<<"$PATCHMAIL"
131145

146+
if [[ "$RESULT" == "0" ]]; then
147+
echo -e "\e[1;32m>> Success\e[0m"
148+
else
149+
echo -e "\e[1;31m>> Failure\e[0m"
150+
fi
151+
132152
exit $RESULT

0 commit comments

Comments
 (0)