Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
webispy committed Oct 21, 2019
1 parent 3ffa855 commit cbc9d8b
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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 }}
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM webispy/checkpatch

COPY entrypoint.sh /entrypoint.sh
COPY review.sh /review.sh

ENTRYPOINT ["/entrypoint.sh"]
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
# helloworld-docker-action
# Github action for `checkpatch.pl`

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.

## Resources

Following files are used to this project.

- https://raw.githubusercontent.com/torvalds/linux/master/scripts/checkpatch.pl
- https://raw.githubusercontent.com/torvalds/linux/master/scripts/spelling.txt

## Patch

### add option for excluding directories

From [zephyr](https://github.com/zephyrproject-rtos/zephyr) project:

- https://github.com/zephyrproject-rtos/zephyr/commit/92a12a19ae5ac5fdf441c690c48eed0052df326d

### Disable warning for "No structs that should be const ..."

- https://github.com/nugulinux/docker-devenv/blob/master/patches/0002-ignore_const_struct_warning.patch
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: 'checkpatch.pl PR review'
description: 'Codew review for PR using checkpatch.pl'
branding:
icon: 'check-square'
color: 'purple'
runs:
using: 'docker'
image: 'Dockerfile'
29 changes: 29 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

echo "Start..."
echo "Workflow: $GITHUB_WORKFLOW"
echo "Action: $GITHUB_ACTION"
echo "Actor: $GITHUB_ACTOR"
echo "Repository: $GITHUB_REPOSITORY"
echo "Event-name: $GITHUB_EVENT_NAME"
echo "Event-path: $GITHUB_EVENT_PATH"
echo "Workspace: $GITHUB_WORKSPACE"
echo "SHA: $GITHUB_SHA"
echo "REF: $GITHUB_REF"
echo "HEAD-REF: $GITHUB_HEAD_REF"
echo "BASE-REF: $GITHUB_BASE_REF"
echo "TOKEN: $GITHUB_TOKEN"
pwd

RESULT=0

# Run review.sh on each commit in the PR
for sha1 in $(git rev-list origin/$GITHUB_BASE_REF..origin/$GITHUB_HEAD_REF); do
echo "Check - Commit id $sha1"
/review.sh ${sha1} || RESULT=1;
echo "Result: ${RESULT}"
done

echo "Done"

exit $RESULT
130 changes: 130 additions & 0 deletions review.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/bin/bash

# To debug the current script, please uncomment the following 'set -x' line
#set -x

# Argument
COMMIT=$1

# Get PR number
PR=${GITHUB_REF#"refs/pull/"}
PRNUM=${PR%"/merge"}

# Generate email style commit message
PATCHMAIL=$(git show --format=email $1 | checkpatch.pl --no-tree -)

# Github REST API endpoints
BODY_URL=https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${PRNUM}/comments
CODE_URL=https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${PRNUM}/comments

# Internal state variables
RESULT=0
FOUND=0
MESSAGE=

# Write message to specific file and line
function post_code_message()
{
curl $CODE_URL -s -H "Authorization: token ${GITHUB_TOKEN}" \
-X POST --data "$(cat <<EOF
{
"commit_id": "$COMMIT",
"path": "${FILE}",
"position": ${LINE},
"body": "${MESSAGE}"
}
EOF
)"
}

# Write message to pull-request comment
function post_comment_message()
{
curl $BODY_URL -s -H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
-X POST --data "$(cat <<EOF
{
"body": ":warning: ${COMMIT} - ${MESSAGE}"
}
EOF
)"
}

#
# checkpatch.pl result format
# ---------------------------
#
# Template:
# ---------
#
# [WARNING/ERROR]: [message for code line]
# #[id]: FILE: [filename]:[line-number]
# +[code]
# [empty line]
#
# [WARNING/ERROR]: [message for commit itself]
#
# total: [n] erros, [n] warnings, [n] lines checked
#
# example:
# --------
#
# ERROR: xxxx
# #15: FILE: a.c:3:
# +int main() {
#
# ERROR: Missing Signed-off-by: line(s)
#
# total: ...
#

while read -r row
do
# End of checkpatch.pl message
if [[ "$row" =~ ^total: ]]; then
break
fi

# Additional parsing is needed
if [[ "$FOUND" == "1" ]]; then

# The row is started with "#"
if [[ "$row" =~ ^\# ]]; then
# Split the string using ':' seperator
IFS=':' read -r -a list <<< "$row"

# Get file-name after removing spaces.
FILE=$(echo ${list[2]} | xargs)

# Get line-number
LINE=${list[3]}
else
# An empty line means the paragraph is over.
if [[ -z $row ]]; then
if [[ -z $FILE ]]; then
post_comment_message
else
post_code_message
fi

# Code review found a problem.
RESULT=1

FOUND=0
FILE=
LINE=
fi
fi
fi

# Found warning or error paragraph
if [[ "$row" =~ ^(WARNING|ERROR) ]]; then
MESSAGE=$row
FOUND=1
FILE=
LINE=
fi

done <<<"$PATCHMAIL"

exit $RESULT

0 comments on commit cbc9d8b

Please sign in to comment.