Skip to content

Add inputs #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Test action

on: [pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
# - uses: actions/checkout@v2
- uses: shenxianpeng/cpp-linter-action@master
with:
style: file
extensions: 'cpp'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
RUN apt-get -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.sh
ENTRYPOINT [ "/entrypoint.sh" ]
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
12 changes: 12 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ 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 have clang-format use the closest relative .clang-format file."
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'."
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 }}
3 changes: 3 additions & 0 deletions demo/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
Language: Cpp
BasedOnStyle: WebKit
7 changes: 7 additions & 0 deletions demo/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"directory": ".",
"command": "/usr/bin/g++ -Wall -Werror demo.cpp",
"file": "/demo.cpp"
}
]
15 changes: 15 additions & 0 deletions demo/demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#include <stdio.h>




int main(){

printf("Hello world!\n");

return 0;}



/* This is an ugly test code */
28 changes: 24 additions & 4 deletions runchecks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ if [[ -z "$GITHUB_TOKEN" ]]; then
exit 1
fi

args=("$@")
FMT_STYLE=${args[0]}
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"

Expand All @@ -13,14 +17,30 @@ 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
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[@]}"
do
echo "Downloading $i"
curl -LOk --remote-name $i
curl -LOk --remote-name $i
done

echo "Files downloaded!"
Expand All @@ -31,13 +51,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="$FMT_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
Expand Down