From 0004b228378eef1dc005a9834441378ab9c93faa Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 06:36:54 -0700 Subject: [PATCH 01/14] style input added --- .github/workflows/test.yml | 12 ++++++++++++ Dockerfile | 8 ++++---- action.yml | 7 +++++++ demo/demo.cpp | 15 +++++++++++++++ runchecks.sh | 6 +++--- 5 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 demo/demo.cpp diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..30a570ef --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,12 @@ +name: Test action + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: shenxianpeng/cpp-linter-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Dockerfile b/Dockerfile index d58b80b4..9ced1c94 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,10 +8,10 @@ LABEL com.github.actions.color="gray-dark" LABEL repository="https://github.com/shenxianpeng/cpp-linter-action" LABEL maintainer="shenxianpeng <20297606+shenxianpeng@users.noreply.github.com>" -WORKDIR /build +# WORKDIR /build RUN apt-get update RUN apt-get -qq -y install curl clang-tidy cmake jq clang clang-format -ADD runchecks.sh /entrypoint.sh -COPY . . -CMD ["bash", "/entrypoint.sh"] +COPY runchecks.sh /entrypoint.sh +RUN chmod +x /entrypoint.ch +CMD ["bash", "/entrypoint.sh", "$@"] diff --git a/action.yml b/action.yml index e53fcb24..2fb1513b 100644 --- a/action.yml +++ b/action.yml @@ -4,6 +4,13 @@ author: shenxianpeng branding: icon: 'check-circle' color: 'green' +inputs: + style: # the specific style rules + description: "The style rules to use (defaults to 'LLVM'). Set this to file to make 'file' to have clang-format use the closest relative '.clang-format' file." + required: false + default: 'llvm' runs: using: 'docker' image: 'Dockerfile' + args: + - ${{ inputs.style }} diff --git a/demo/demo.cpp b/demo/demo.cpp new file mode 100644 index 00000000..c003979f --- /dev/null +++ b/demo/demo.cpp @@ -0,0 +1,15 @@ + +#include + + + + +int main(){ + + printf("Hello world!\n"); + + return 0;} + + + +/* This is an ugly test code */ diff --git a/runchecks.sh b/runchecks.sh index 2588d5e3..b697bc23 100644 --- a/runchecks.sh +++ b/runchecks.sh @@ -20,7 +20,7 @@ cd files for i in "${URLS[@]}" do echo "Downloading $i" - curl -LOk --remote-name $i + curl -LOk --remote-name $i done echo "Files downloaded!" @@ -31,13 +31,13 @@ for i in "${URLS[@]}" do filename=`basename $i` clang-tidy $filename -checks=boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-cplusplus-*,clang-analyzer-*,cppcoreguidelines-* >> clang-tidy-report.txt - clang-format --dry-run -Werror $filename || echo "File: $filename not formatted!" >> clang-format-report.txt + clang-format -style=$INPUT_STYLE--dry-run -Werror $filename || echo "File: $filename not formatted!" >> clang-format-report.txt done PAYLOAD_TIDY=`cat clang-tidy-report.txt` PAYLOAD_FORMAT=`cat clang-format-report.txt` COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url) - + echo $COMMENTS_URL echo "Clang-tidy errors:" echo $PAYLOAD_TIDY From 7a6d5835b20dd6cad8d0be37c2d968282fc8b154 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 07:04:57 -0700 Subject: [PATCH 02/14] switch action to my fork's branch --- .github/workflows/test.yml | 2 +- Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 30a570ef..97b17203 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,6 +7,6 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: shenxianpeng/cpp-linter-action@master + - uses: 2bndy5/cpp-linter-action@add-input env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Dockerfile b/Dockerfile index 9ced1c94..4ea94955 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ LABEL maintainer="shenxianpeng <20297606+shenxianpeng@users.noreply.github.com>" # WORKDIR /build RUN apt-get update -RUN apt-get -qq -y install curl clang-tidy cmake jq clang clang-format +RUN apt-get -y install curl clang-tidy cmake jq clang clang-format COPY runchecks.sh /entrypoint.sh RUN chmod +x /entrypoint.ch From 9840272c428c19d23c97751fb6c9c8aab692fabe Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 00:06:34 -0700 Subject: [PATCH 03/14] typo --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 97b17203..43d9ff56 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,6 +7,6 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: 2bndy5/cpp-linter-action@add-input + - uses: 2bndy5/cpp-linter-action@add-inputs env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 3f2fb2a0637bcb528baf03ed91b67bfaab7473d7 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 00:11:05 -0700 Subject: [PATCH 04/14] forgot indent in yml --- action.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/action.yml b/action.yml index 2fb1513b..b36f3573 100644 --- a/action.yml +++ b/action.yml @@ -6,9 +6,9 @@ branding: color: 'green' inputs: style: # the specific style rules - description: "The style rules to use (defaults to 'LLVM'). Set this to file to make 'file' to have clang-format use the closest relative '.clang-format' file." - required: false - default: 'llvm' + description: "The style rules to use (defaults to 'llvm'). Set this to file to make 'file' to have clang-format use the closest relative .clang-format file." + required: false + default: 'llvm' runs: using: 'docker' image: 'Dockerfile' From 44afa1c2621d007e28b91e44144ac3e57f083cd7 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 00:17:01 -0700 Subject: [PATCH 05/14] another typo --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4ea94955..478438f7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,5 +13,5 @@ RUN apt-get update RUN apt-get -y install curl clang-tidy cmake jq clang clang-format COPY runchecks.sh /entrypoint.sh -RUN chmod +x /entrypoint.ch -CMD ["bash", "/entrypoint.sh", "$@"] +RUN chmod +x /entrypoint.sh +CMD ["bash", "/entrypoint.sh"] From d58e5b6746bae569d3601a010d224fc73d3fd2d8 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 00:55:22 -0700 Subject: [PATCH 06/14] args from yml is passed to ENTRYPOINT --- Dockerfile | 2 +- runchecks.sh | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 478438f7..204f8cf7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,4 +14,4 @@ RUN apt-get -y install curl clang-tidy cmake jq clang clang-format COPY runchecks.sh /entrypoint.sh RUN chmod +x /entrypoint.sh -CMD ["bash", "/entrypoint.sh"] +ENTRYPOINT [ "/entrypoint.sh" ] diff --git a/runchecks.sh b/runchecks.sh index b697bc23..f0c43e3c 100644 --- a/runchecks.sh +++ b/runchecks.sh @@ -4,7 +4,8 @@ if [[ -z "$GITHUB_TOKEN" ]]; then echo "The GITHUB_TOKEN is required." exit 1 fi - +args=("$@") +FMT_STYLE=${args[0]} FILES_LINK=`jq -r '.pull_request._links.self.href' "$GITHUB_EVENT_PATH"`/files echo "Files = $FILES_LINK" @@ -31,7 +32,7 @@ for i in "${URLS[@]}" do filename=`basename $i` clang-tidy $filename -checks=boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-cplusplus-*,clang-analyzer-*,cppcoreguidelines-* >> clang-tidy-report.txt - clang-format -style=$INPUT_STYLE--dry-run -Werror $filename || echo "File: $filename not formatted!" >> clang-format-report.txt + clang-format -style=$FMT_STYLE --dry-run -Werror $filename || echo "File: $filename not formatted!" >> clang-format-report.txt done PAYLOAD_TIDY=`cat clang-tidy-report.txt` From 0163b29427a5828b509d2017816043cc8235dacd Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 03:52:19 -0700 Subject: [PATCH 07/14] add file filtering based on extensions --- action.yml | 5 +++++ runchecks.sh | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index b36f3573..db2a3cfc 100644 --- a/action.yml +++ b/action.yml @@ -9,8 +9,13 @@ inputs: description: "The style rules to use (defaults to 'llvm'). Set this to file to make 'file' to have clang-format use the closest relative .clang-format file." required: false default: 'llvm' + extensions: + desciption: "The file extensions to run the action against. This defaults to 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx'". + required: false + default: 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx' runs: using: 'docker' image: 'Dockerfile' args: - ${{ inputs.style }} + - ${{ inputs.extensions }} diff --git a/runchecks.sh b/runchecks.sh index f0c43e3c..46af5afd 100644 --- a/runchecks.sh +++ b/runchecks.sh @@ -4,8 +4,11 @@ if [[ -z "$GITHUB_TOKEN" ]]; then echo "The GITHUB_TOKEN is required." exit 1 fi + args=("$@") FMT_STYLE=${args[0]} +IFS=',' read -a FILE_EXT_LIST <<< ${args[1]} + FILES_LINK=`jq -r '.pull_request._links.self.href' "$GITHUB_EVENT_PATH"`/files echo "Files = $FILES_LINK" @@ -14,8 +17,25 @@ FILES_URLS_STRING=`jq -r '.[].raw_url' files.json` readarray -t URLS <<<"$FILES_URLS_STRING" -echo "File names: $URLS" +# exclude undesired files +for index in "${!URLS[@]}" +do + is_supported=0 + for i in "${FILE_EXT_LIST[@]}" + do + echo "testing ${URLS[index]} vs *$i" + if [[ ${URLS[index]} == *".$i" ]] + then + is_supported=1 + fi + done + if [ $is_supported == 0 ] + then + unset -v "URLS[index]" + fi +done +echo "File names: $URLS" mkdir files cd files for i in "${URLS[@]}" From aeeb2bde5b33e6e58e633e05bff2ffb24b9562c4 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 03:56:44 -0700 Subject: [PATCH 08/14] typo --- action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index db2a3cfc..48eeb18a 100644 --- a/action.yml +++ b/action.yml @@ -10,9 +10,9 @@ inputs: required: false default: 'llvm' extensions: - desciption: "The file extensions to run the action against. This defaults to 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx'". + description: "The file extensions to run the action against. This comma-separated string defaults to 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx'". required: false - default: 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx' + default: "c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx" runs: using: 'docker' image: 'Dockerfile' From 8d842d050a0f8553419e9242628c62c53ec6578d Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 03:58:06 -0700 Subject: [PATCH 09/14] [typo] a rogue '.' --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 48eeb18a..f19bf3d8 100644 --- a/action.yml +++ b/action.yml @@ -10,7 +10,7 @@ inputs: required: false default: 'llvm' extensions: - description: "The file extensions to run the action against. This comma-separated string defaults to 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx'". + description: "The file extensions to run the action against. This comma-separated string defaults to 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx'." required: false default: "c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx" runs: From f8c5644cb2acaba12ebae79373d2ca1b4614ed1a Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 04:04:07 -0700 Subject: [PATCH 10/14] delete debug output and trigger CI only for PRs --- .github/workflows/test.yml | 2 +- runchecks.sh | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 43d9ff56..730e9762 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,6 @@ name: Test action -on: [push, pull_request] +on: [pull_request] jobs: test: diff --git a/runchecks.sh b/runchecks.sh index 46af5afd..bdd9c958 100644 --- a/runchecks.sh +++ b/runchecks.sh @@ -23,7 +23,6 @@ do is_supported=0 for i in "${FILE_EXT_LIST[@]}" do - echo "testing ${URLS[index]} vs *$i" if [[ ${URLS[index]} == *".$i" ]] then is_supported=1 From 7fbc86f0aa8dc05aeb095d94670b0430a489bdc0 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 05:08:08 -0700 Subject: [PATCH 11/14] actually test new action inputs --- .github/workflows/test.yml | 3 +++ demo/.clang-format | 3 +++ demo/compile_commands.json | 7 +++++++ runchecks.sh | 2 +- 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 demo/.clang-format create mode 100644 demo/compile_commands.json diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 730e9762..643827b7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,5 +8,8 @@ jobs: steps: - uses: actions/checkout@v2 - uses: 2bndy5/cpp-linter-action@add-inputs + with: + style: file + extensions: 'cpp' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/demo/.clang-format b/demo/.clang-format new file mode 100644 index 00000000..1dd236cb --- /dev/null +++ b/demo/.clang-format @@ -0,0 +1,3 @@ +--- +Language: Cpp +BasedOnStyle: WebKit diff --git a/demo/compile_commands.json b/demo/compile_commands.json new file mode 100644 index 00000000..2643be40 --- /dev/null +++ b/demo/compile_commands.json @@ -0,0 +1,7 @@ +[ +{ + "directory": ".", + "command": "/usr/bin/g++ -Wall -Werror demo.cpp", + "file": "/demo.cpp" +} +] diff --git a/runchecks.sh b/runchecks.sh index bdd9c958..a973f5c2 100644 --- a/runchecks.sh +++ b/runchecks.sh @@ -34,7 +34,7 @@ do fi done -echo "File names: $URLS" +echo "File names: ${URLS[*]}" mkdir files cd files for i in "${URLS[@]}" From ba0318a91cfce6278ba78980459023e80a3b3ea6 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 05:42:05 -0700 Subject: [PATCH 12/14] README augmented. --- .github/workflows/test.yml | 4 ++-- README.md | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 643827b7..b2d6376b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,8 +6,8 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: 2bndy5/cpp-linter-action@add-inputs + # - uses: actions/checkout@v2 + - uses: shenxianpeng/cpp-linter-action@master with: style: file extensions: 'cpp' diff --git a/README.md b/README.md index 99f606d9..ec947c89 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ Github Actions for linting the C/C++ code. Integrated clang-tidy, clang-format c Just create a `yml` file under your GitHub repository. For example `.github/workflows/cpp-linter.yml` +!!! Requires `secrets.GITHUB_TOKEN` set to an environment variable name "GITHUB_TOKEN". + ```yml name: cpp-linter @@ -16,10 +18,18 @@ jobs: runs-on: ubuntu-latest steps: - name: C/C++ Lint Action + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} uses: shenxianpeng/cpp-linter-action@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + style: 'file' ``` +## Optional Inputs + +| Input name | default value | Description | +|------------|---------------|-------------| +| style | 'llvm' | The style rules to use. Set this to 'file' to have clang-format use the closest relative .clang-format file. | +| extensions | 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx' | The file extensions to run the action against. This is a comma-separated string. | ## Results of GitHub Actions From 1388e718b0b20e7ed13078db321a38e6883c4f44 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 05:48:35 -0700 Subject: [PATCH 13/14] remove typo --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index f19bf3d8..f50f994f 100644 --- a/action.yml +++ b/action.yml @@ -6,7 +6,7 @@ branding: color: 'green' inputs: style: # the specific style rules - description: "The style rules to use (defaults to 'llvm'). Set this to file to make 'file' to have clang-format use the closest relative .clang-format file." + description: "The style rules to use (defaults to 'llvm'). Set this to 'file' to have clang-format use the closest relative .clang-format file." required: false default: 'llvm' extensions: From b0bf4b574e82a81cc86992fe39ef70409912f277 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Wed, 18 Aug 2021 05:58:56 -0700 Subject: [PATCH 14/14] review changes --- runchecks.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runchecks.sh b/runchecks.sh index a973f5c2..f2e901dc 100644 --- a/runchecks.sh +++ b/runchecks.sh @@ -7,7 +7,7 @@ fi args=("$@") FMT_STYLE=${args[0]} -IFS=',' read -a FILE_EXT_LIST <<< ${args[1]} +IFS=',' read -r -a FILE_EXT_LIST <<< "${args[1]}" FILES_LINK=`jq -r '.pull_request._links.self.href' "$GITHUB_EVENT_PATH"`/files echo "Files = $FILES_LINK" @@ -51,7 +51,7 @@ for i in "${URLS[@]}" do filename=`basename $i` clang-tidy $filename -checks=boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-cplusplus-*,clang-analyzer-*,cppcoreguidelines-* >> clang-tidy-report.txt - clang-format -style=$FMT_STYLE --dry-run -Werror $filename || echo "File: $filename not formatted!" >> clang-format-report.txt + clang-format -style="$FMT_STYLE" --dry-run -Werror "$filename" || echo "File: $filename not formatted!" >> clang-format-report.txt done PAYLOAD_TIDY=`cat clang-tidy-report.txt`