Skip to content

Commit

Permalink
scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jdanielllopis committed Oct 31, 2024
1 parent f6abde9 commit a28ef59
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/scripts/download.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
##
# Copyright (c) 2024 Analog Devices, Inc.
#
# SPDX-License-Identifier: GPL-2.0
##

readonly CHECKPATCH_SCRIPT="checkpatch.pl"
readonly SPELLING_TXT="spelling.txt"
readonly CONST_STRUCTS="const_structs.checkpatch"
readonly CHECKPATCH_SCRIPT_LINUX="./scripts/checkpatch.pl"

readonly KERNEL_RAW_URL="https://raw.githubusercontent.com/torvalds/linux/master"
readonly CHECKPATCH_URL="${KERNEL_RAW_URL}/scripts/${CHECKPATCH_SCRIPT}"
readonly SPELLING_URL="${KERNEL_RAW_URL}/scripts/${SPELLING_TXT}"
readonly CONST_STRUCTS_URL="${KERNEL_RAW_URL}/scripts/${CONST_STRUCTS}"

if [ ! -f ${CHECKPATCH_SCRIPT_LINUX} ]; then
for download in "${CHECKPATCH_URL}:${CHECKPATCH_SCRIPT}" "${SPELLING_URL}:${SPELLING_TXT}" \
"${CONST_STRUCTS_URL}:${CONST_STRUCTS}"; do
echo "Downloading '${download##*:}'..."
curl -f "${download%:*}" -s -S -O || \
exit 1
done
fi

chmod 755 "${CHECKPATCH_SCRIPT}"
73 changes: 73 additions & 0 deletions .github/scripts/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash
##
# Copyright (c) 2024 Analog Devices, Inc.
#
# SPDX-License-Identifier: GPL-2.0
##

sudo apt-get install -y jq

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"
pwd
ls -la `pwd`
id

ls -l /

# Add safe directory option for github workspace to disable fatal error
# - docker container uid is 0(root)
# - github workspace directory owner is 1001
git config --global --add safe.directory /github/workspace

RESULT=0

# Check the input parameter
echo
if [[ -z "$GITHUB_TOKEN" ]]; then
echo -e "\e[0;34mToken is empty. Review PR without comments.\e[0m"
HEADERS=()
else
echo -e "\e[0;34mReview PR with comments.\e[0m"
HEADERS=(-H "Authorization: Bearer $GITHUB_TOKEN")
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
echo " - API endpoint: $URL"

list=$(curl $URL "${HEADERS[@]}" -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 "-------------------------------------------------------------"
echo -e "[$i/$len] Check commit - \e[1;34m$sha1\e[0m"
echo "-------------------------------------------------------------"
./review.sh ${sha1} || RESULT=1;
echo
((i++))
done

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

exit $RESULT
118 changes: 118 additions & 0 deletions .github/scripts/review.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash
##
# Copyright (c) 2024 Analog Devices, Inc.
#
# SPDX-License-Identifier: GPL-2.0
##

# download checkpacth.pl from the kernel repository
chmod +x ./download.sh
./download.sh

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

if [[ -z "$CHECKPATCH_COMMAND" ]] ; then
CHECKPATCH_COMMAND="./checkpatch.pl --no-tree"
fi

# Generate email style commit message
PATCH_FILE=$(git format-patch $1 -1)
PATCHMAIL=$($CHECKPATCH_COMMAND $PATCH_FILE)

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

#
# 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
echo -e "\e[1m$row\e[0m"
break
fi

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

# The row is started with "#"
if [[ "$row" =~ ^\# ]]; then
# Split the string using ':' separator
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
echo "::error ::${MESSAGE}"
else
echo "::error file=${FILE},line=${LINE}::${MESSAGE}"
fi
# Output empty line
echo

# 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=

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 a28ef59

Please sign in to comment.