From 6a4598f7067a7ea794f7288d84b2f99708b96fc8 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Wed, 13 Jan 2021 13:12:57 +0900 Subject: [PATCH] infra coding_style: Add coding style check bot Add a bot that checks the coding style using cpplint and reports the result as a review comment. This feature refers to the repository where cpplint and docker settings with github-action. This action works whenever a PR is created and updated. In cpplint, only the violation of ThorVG's coding style is reported as a warning using several options. whitespace/parens whitespace/indent whitespace/end_of_line whitespace/blank_line The bot will review all of the contents of all files in the current commit file list, However, in the future, we will improve Bot to only review the changes. --- .github/workflows/actions_cpplint.yml | 33 +++++++++++++++ .github/workflows/cpp_lint_check.sh | 59 +++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 .github/workflows/actions_cpplint.yml create mode 100644 .github/workflows/cpp_lint_check.sh diff --git a/.github/workflows/actions_cpplint.yml b/.github/workflows/actions_cpplint.yml new file mode 100644 index 00000000..da706169 --- /dev/null +++ b/.github/workflows/actions_cpplint.yml @@ -0,0 +1,33 @@ +name: PullRequest_Checker + +on: + pull_request: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + submodules: true + + - name: Install Packages + run: | + sudo apt-get update + sudo apt-get install curl + sudo apt-get install cmake jq clang + sudo apt-get install software-properties-common + sudo apt-get install python3-pip + pip3 install wheel --user + pip3 install cpplint --user + + - name: Run Cpplint Script + run: | + export PATH=$PATH:~/.local/bin/ + chmod +x "${GITHUB_WORKSPACE}/.github/workflows/cpp_lint_check.sh" + "${GITHUB_WORKSPACE}/.github/workflows/cpp_lint_check.sh" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + diff --git a/.github/workflows/cpp_lint_check.sh b/.github/workflows/cpp_lint_check.sh new file mode 100644 index 00000000..c130da3f --- /dev/null +++ b/.github/workflows/cpp_lint_check.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +if [[ -z "$GITHUB_TOKEN" ]]; then + echo "The GITHUB_TOKEN is required." + exit 1 +fi + +FILES_LINK=`jq -r '.pull_request._links.self.href' "$GITHUB_EVENT_PATH"`/files +echo "Files = $FILES_LINK" + +curl $FILES_LINK > files.json +FILES_URLS_STRING=`jq -r '.[].raw_url' files.json` + +readarray -t URLS <<<"$FILES_URLS_STRING" + +echo "File names: $URLS" + +mkdir files +cd files +for i in "${URLS[@]}" +do + echo "Downloading $i" + curl -LOk --remote-name $i +done + +echo "Files downloaded!" +echo "Performing checkup:" + +cpplint --filter=-,\ ++whitespace/parens,\ ++whitespace/indent,\ ++whitespace/end_of_line,\ ++whitespace/blank_line \ +--extension=cpp,h,c \ +--recursive \ +./ > cpp-report.txt 2>&1 + +PAYLOAD_CPPLINT=`cat cpp-report.txt` +COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url) + +echo $COMMENTS_URL +echo "Cppcheck errors:" +echo $PAYLOAD_CPPLINT + +if [[ $PAYLOAD_CPPLINT == *"Total errors found: "* ]]; then + OUTPUT+=$'\n**CODING STYLE CHECK**:\n' + OUTPUT+=$'\n```\n' + OUTPUT+="$PAYLOAD_CPPLINT" + OUTPUT+=$'\n```\n' + +else + OUTPUT+=$'\n**CODING STYLE CHECK**:\n' + OUTPUT+=$'\n```\n' + OUTPUT+="Perfect." + OUTPUT+=$'\n```\n' +fi +PAYLOAD=$(echo '{}' | jq --arg body "$OUTPUT" '.body = $body') + +curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/vnd.github.VERSION.text+json" --data "$PAYLOAD" "$COMMENTS_URL"