Skip to content

Commit

Permalink
Fixed posting only when GITHUB_TOKEN is present.
Browse files Browse the repository at this point in the history
  • Loading branch information
webispy committed Oct 23, 2019
1 parent 005cc8a commit 9fb45b1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ jobs:
- name: Run checkpatch review
uses: webispy/checkpatch-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,51 @@ From [zephyr](https://github.com/zephyrproject-rtos/zephyr) project:
### Disable warning for "No structs that should be const ..."

- https://github.com/nugulinux/docker-devenv/blob/master/patches/0002-ignore_const_struct_warning.patch

## Action setup guide

### Pull Request from owned repository

.github/workflows/main.yml

```yml
name: checkpatch review
on: [pull_request]
jobs:
my_review:
name: checkpatch review
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Run checkpatch review
uses: webispy/checkpatch-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
The checkpatch action will posting comments for error/warning result to the PR conversation.
### Pull Request from forked repository
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.
.github/workflows/main.yml
```yml
name: checkpatch review
on: [pull_request]
jobs:
my_review:
name: checkpatch review
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Run checkpatch review
uses: webispy/checkpatch-action@master
```
You can find the error/waring result from Github action console log.
# License
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.
34 changes: 26 additions & 8 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,40 @@ pwd

RESULT=0

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

# Get commit list using Github API
echo
echo -e "\e[0;34mGet the list of commits included in the PR($GITHUB_REF).\e[0m"
PR=${GITHUB_REF#"refs/pull/"}
PRNUM=${PR%"/merge"}
URL=https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${PRNUM}/commits
list=$(curl $URL -X GET -s -H "Authorization: token ${GITHUB_TOKEN}" | jq '.[].sha' -r)
echo $list
echo " - API endpoint: $URL"

list=$(curl $URL -X GET -s | jq '.[].sha' -r)
len=$(echo "$list" | wc -l)
echo " - Commits $len: $list"

# Run review.sh on each commit in the PR
echo
echo -e "\e[0;34mStart review for each commits.\e[0m"

i=1
for sha1 in $list; do
echo "Check - Commit id $sha1"
echo "-------------------------------------------------------------"
echo -e "[$i/$len] Check commit - \e[1;34m$sha1\e[0m"
echo "-------------------------------------------------------------"
/review.sh ${sha1} || RESULT=1;
echo "Result: ${RESULT}"
echo
((i++))
done

echo "Done"
echo -e "\e[1;34mDone\e[0m"

exit $RESULT
30 changes: 25 additions & 5 deletions review.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ MESSAGE=
function post_code_message()
{
echo "POST to ${CODE_URL} with ${MESSAGE}"
curl ${CODE_URL} -H "Authorization: token ${GITHUB_TOKEN}" \
curl ${CODE_URL} -s \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
-X POST --data "$(cat <<EOF
{
"commit_id": "$COMMIT",
Expand All @@ -42,7 +44,8 @@ EOF
function post_comment_message()
{
echo "POST to ${BODY_URL} with ${MESSAGE}"
curl ${BODY_URL} -H "Authorization: token ${GITHUB_TOKEN}" \
curl ${BODY_URL} -s \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
-X POST --data "$(cat <<EOF
{
Expand Down Expand Up @@ -84,6 +87,7 @@ while read -r row
do
# End of checkpatch.pl message
if [[ "$row" =~ ^total: ]]; then
echo -e "\e[1m$row\e[0m"
break
fi

Expand All @@ -103,10 +107,16 @@ do
else
# An empty line means the paragraph is over.
if [[ -z $row ]]; then
if [[ -z $FILE ]]; then
post_comment_message
if [[ ! -z "$GITHUB_TOKEN" ]]; then
echo "Post comment to Github"
if [[ -z $FILE ]]; then
post_comment_message
else
post_code_message
fi
else
post_code_message
# Output empty line
echo
fi

# Code review found a problem.
Expand All @@ -125,8 +135,18 @@ do
FOUND=1
FILE=
LINE=

echo -e "\e[1;31m$row\e[0m"
else
echo $row
fi

done <<<"$PATCHMAIL"

if [[ "$RESULT" == "0" ]]; then
echo -e "\e[1;32m>> Success\e[0m"
else
echo -e "\e[1;31m>> Failure\e[0m"
fi

exit $RESULT

0 comments on commit 9fb45b1

Please sign in to comment.