Skip to content

Commit 180d0c2

Browse files
authored
Add inputs style and extensions (#4)
* forgot indent in yml * args from yml is passed to ENTRYPOINT * add file filtering based on extensions * delete debug output and trigger CI only for PRs * actually test new action inputs * README augmented. * remove typo * review changes * style input added * switch action to my fork's branch
1 parent 5321296 commit 180d0c2

File tree

8 files changed

+93
-11
lines changed

8 files changed

+93
-11
lines changed

.github/workflows/test.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Test action
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
# - uses: actions/checkout@v2
10+
- uses: shenxianpeng/cpp-linter-action@master
11+
with:
12+
style: file
13+
extensions: 'cpp'
14+
env:
15+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ LABEL com.github.actions.color="gray-dark"
88
LABEL repository="https://github.com/shenxianpeng/cpp-linter-action"
99
LABEL maintainer="shenxianpeng <20297606+shenxianpeng@users.noreply.github.com>"
1010

11-
WORKDIR /build
11+
# WORKDIR /build
1212
RUN apt-get update
13-
RUN apt-get -qq -y install curl clang-tidy cmake jq clang clang-format
13+
RUN apt-get -y install curl clang-tidy cmake jq clang clang-format
1414

15-
ADD runchecks.sh /entrypoint.sh
16-
COPY . .
17-
CMD ["bash", "/entrypoint.sh"]
15+
COPY runchecks.sh /entrypoint.sh
16+
RUN chmod +x /entrypoint.sh
17+
ENTRYPOINT [ "/entrypoint.sh" ]

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Github Actions for linting the C/C++ code. Integrated clang-tidy, clang-format c
66

77
Just create a `yml` file under your GitHub repository. For example `.github/workflows/cpp-linter.yml`
88

9+
!!! Requires `secrets.GITHUB_TOKEN` set to an environment variable name "GITHUB_TOKEN".
10+
911
```yml
1012
name: cpp-linter
1113

@@ -16,10 +18,18 @@ jobs:
1618
runs-on: ubuntu-latest
1719
steps:
1820
- name: C/C++ Lint Action
21+
env:
22+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1923
uses: shenxianpeng/cpp-linter-action@master
20-
env:
21-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
with:
25+
style: 'file'
2226
```
27+
## Optional Inputs
28+
29+
| Input name | default value | Description |
30+
|------------|---------------|-------------|
31+
| style | 'llvm' | The style rules to use. Set this to 'file' to have clang-format use the closest relative .clang-format file. |
32+
| 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. |
2333
2434
## Results of GitHub Actions
2535

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ author: shenxianpeng
44
branding:
55
icon: 'check-circle'
66
color: 'green'
7+
inputs:
8+
style: # the specific style rules
9+
description: "The style rules to use (defaults to 'llvm'). Set this to 'file' to have clang-format use the closest relative .clang-format file."
10+
required: false
11+
default: 'llvm'
12+
extensions:
13+
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'."
14+
required: false
15+
default: "c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx"
716
runs:
817
using: 'docker'
918
image: 'Dockerfile'
19+
args:
20+
- ${{ inputs.style }}
21+
- ${{ inputs.extensions }}

demo/.clang-format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: WebKit

demo/compile_commands.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"directory": ".",
4+
"command": "/usr/bin/g++ -Wall -Werror demo.cpp",
5+
"file": "/demo.cpp"
6+
}
7+
]

demo/demo.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#include <stdio.h>
3+
4+
5+
6+
7+
int main(){
8+
9+
printf("Hello world!\n");
10+
11+
return 0;}
12+
13+
14+
15+
/* This is an ugly test code */

runchecks.sh

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ if [[ -z "$GITHUB_TOKEN" ]]; then
55
exit 1
66
fi
77

8+
args=("$@")
9+
FMT_STYLE=${args[0]}
10+
IFS=',' read -r -a FILE_EXT_LIST <<< "${args[1]}"
11+
812
FILES_LINK=`jq -r '.pull_request._links.self.href' "$GITHUB_EVENT_PATH"`/files
913
echo "Files = $FILES_LINK"
1014

@@ -13,14 +17,30 @@ FILES_URLS_STRING=`jq -r '.[].raw_url' files.json`
1317

1418
readarray -t URLS <<<"$FILES_URLS_STRING"
1519

16-
echo "File names: $URLS"
20+
# exclude undesired files
21+
for index in "${!URLS[@]}"
22+
do
23+
is_supported=0
24+
for i in "${FILE_EXT_LIST[@]}"
25+
do
26+
if [[ ${URLS[index]} == *".$i" ]]
27+
then
28+
is_supported=1
29+
fi
30+
done
31+
if [ $is_supported == 0 ]
32+
then
33+
unset -v "URLS[index]"
34+
fi
35+
done
1736

37+
echo "File names: ${URLS[*]}"
1838
mkdir files
1939
cd files
2040
for i in "${URLS[@]}"
2141
do
2242
echo "Downloading $i"
23-
curl -LOk --remote-name $i
43+
curl -LOk --remote-name $i
2444
done
2545

2646
echo "Files downloaded!"
@@ -31,13 +51,13 @@ for i in "${URLS[@]}"
3151
do
3252
filename=`basename $i`
3353
clang-tidy $filename -checks=boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-cplusplus-*,clang-analyzer-*,cppcoreguidelines-* >> clang-tidy-report.txt
34-
clang-format --dry-run -Werror $filename || echo "File: $filename not formatted!" >> clang-format-report.txt
54+
clang-format -style="$FMT_STYLE" --dry-run -Werror "$filename" || echo "File: $filename not formatted!" >> clang-format-report.txt
3555
done
3656

3757
PAYLOAD_TIDY=`cat clang-tidy-report.txt`
3858
PAYLOAD_FORMAT=`cat clang-format-report.txt`
3959
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
40-
60+
4161
echo $COMMENTS_URL
4262
echo "Clang-tidy errors:"
4363
echo $PAYLOAD_TIDY

0 commit comments

Comments
 (0)