Skip to content

Commit 7f36438

Browse files
2bndy5shenxianpeng
andauthored
Output exit code (#10)
* solution for #7 * parameterize EXIT_CODE * fix bash function calls; switch to the new docker image * fix bash array length syntax * oops need that apt update * ok now change the test code a bit * check output is working * fix bad yml syntax * is it being set at all? * use a string instead of an integer * fix bash logic syntax * single square brackets are the old way to do logic * review changes * add output var info to the readme * Update demo.cpp * Revert action to upstream master Co-authored-by: shenxianpeng <xianpeng.shen@gmail.com>
1 parent 180d0c2 commit 7f36438

File tree

7 files changed

+242
-11
lines changed

7 files changed

+242
-11
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ jobs:
88
steps:
99
# - uses: actions/checkout@v2
1010
- uses: shenxianpeng/cpp-linter-action@master
11+
id: linter
1112
with:
1213
style: file
1314
extensions: 'cpp'
1415
env:
1516
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17+
18+
- name: Fail fast?!
19+
if: steps.linter.outputs.checks-failed > 0
20+
run: |
21+
echo "Some files failed the linting checks!"
22+
# for actual deployment
23+
# run: exit 1

Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:latest
1+
FROM xianpengshen/clang-tool
22

33
LABEL com.github.actions.name="cpp-linter check"
44
LABEL com.github.actions.description="Lint your code with clang-tidy in parallel to your builds"
@@ -8,9 +8,8 @@ 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
1211
RUN apt-get update
13-
RUN apt-get -y install curl clang-tidy cmake jq clang clang-format
12+
RUN apt-get -y install curl jq
1413

1514
COPY runchecks.sh /entrypoint.sh
1615
RUN chmod +x /entrypoint.sh

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ jobs:
3131
| style | 'llvm' | The style rules to use. Set this to 'file' to have clang-format use the closest relative .clang-format file. |
3232
| 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. |
3333
34+
### Outputs
35+
36+
This action creates 1 output variable named `checks-failed`. Even if the linting checks fail for source files this action will still pass, but users' CI workflows can use this action's output to exit the workflow early if that is desired.
3437
## Results of GitHub Actions
3538

3639
Here is a test repository [cpp-linter-action-demo](https://github.com/shenxianpeng/cpp-linter-action-demo) which has added `cpp-linter.yml`. when an unformatted C/C++ source file was committed and create a Pull Request will automatically recognize and add warning comments.

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ inputs:
1313
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'."
1414
required: false
1515
default: "c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx"
16+
outputs:
17+
checks-failed:
18+
description: An integer that can be used as a boolean value to indicate if all checks failed.
1619
runs:
1720
using: 'docker'
1821
image: 'Dockerfile'

demo/.clang-tidy

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
Checks: 'clang-diagnostic-*,clang-analyzer-*,-*,performance-*,bugprone-*,clang-analyzer-*,mpi-*,misc-*,readability-*'
3+
WarningsAsErrors: ''
4+
HeaderFilterRegex: ''
5+
AnalyzeTemporaryDtors: false
6+
FormatStyle: 'file'
7+
CheckOptions:
8+
- key: bugprone-argument-comment.CommentBoolLiterals
9+
value: '0'
10+
- key: bugprone-argument-comment.CommentCharacterLiterals
11+
value: '0'
12+
- key: bugprone-argument-comment.CommentFloatLiterals
13+
value: '0'
14+
- key: bugprone-argument-comment.CommentIntegerLiterals
15+
value: '0'
16+
- key: bugprone-argument-comment.CommentNullPtrs
17+
value: '0'
18+
- key: bugprone-argument-comment.CommentStringLiterals
19+
value: '0'
20+
- key: bugprone-argument-comment.CommentUserDefinedLiterals
21+
value: '0'
22+
- key: bugprone-argument-comment.IgnoreSingleArgument
23+
value: '0'
24+
- key: bugprone-argument-comment.StrictMode
25+
value: '0'
26+
- key: bugprone-assert-side-effect.AssertMacros
27+
value: assert
28+
- key: bugprone-assert-side-effect.CheckFunctionCalls
29+
value: '0'
30+
- key: bugprone-dangling-handle.HandleClasses
31+
value: 'std::basic_string_view;std::experimental::basic_string_view'
32+
- key: bugprone-dynamic-static-initializers.HeaderFileExtensions
33+
value: ',h,hh,hpp,hxx'
34+
- key: bugprone-exception-escape.FunctionsThatShouldNotThrow
35+
value: ''
36+
- key: bugprone-exception-escape.IgnoredExceptions
37+
value: ''
38+
- key: bugprone-misplaced-widening-cast.CheckImplicitCasts
39+
value: '0'
40+
- key: bugprone-not-null-terminated-result.WantToUseSafeFunctions
41+
value: '1'
42+
- key: bugprone-signed-char-misuse.CharTypdefsToIgnore
43+
value: ''
44+
- key: bugprone-sizeof-expression.WarnOnSizeOfCompareToConstant
45+
value: '1'
46+
- key: bugprone-sizeof-expression.WarnOnSizeOfConstant
47+
value: '1'
48+
- key: bugprone-sizeof-expression.WarnOnSizeOfIntegerExpression
49+
value: '0'
50+
- key: bugprone-sizeof-expression.WarnOnSizeOfThis
51+
value: '1'
52+
- key: bugprone-string-constructor.LargeLengthThreshold
53+
value: '8388608'
54+
- key: bugprone-string-constructor.WarnOnLargeLength
55+
value: '1'
56+
- key: bugprone-suspicious-enum-usage.StrictMode
57+
value: '0'
58+
- key: bugprone-suspicious-missing-comma.MaxConcatenatedTokens
59+
value: '5'
60+
- key: bugprone-suspicious-missing-comma.RatioThreshold
61+
value: '0.200000'
62+
- key: bugprone-suspicious-missing-comma.SizeThreshold
63+
value: '5'
64+
- key: bugprone-suspicious-string-compare.StringCompareLikeFunctions
65+
value: ''
66+
- key: bugprone-suspicious-string-compare.WarnOnImplicitComparison
67+
value: '1'
68+
- key: bugprone-suspicious-string-compare.WarnOnLogicalNotComparison
69+
value: '0'
70+
- key: bugprone-too-small-loop-variable.MagnitudeBitsUpperLimit
71+
value: '16'
72+
- key: bugprone-unhandled-self-assignment.WarnOnlyIfThisHasSuspiciousField
73+
value: '1'
74+
- key: bugprone-unused-return-value.CheckedFunctions
75+
value: '::std::async;::std::launder;::std::remove;::std::remove_if;::std::unique;::std::unique_ptr::release;::std::basic_string::empty;::std::vector::empty'
76+
- key: cert-dcl16-c.NewSuffixes
77+
value: 'L;LL;LU;LLU'
78+
- key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
79+
value: '0'
80+
- key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors
81+
value: '1'
82+
- key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
83+
value: '1'
84+
- key: google-readability-braces-around-statements.ShortStatementLines
85+
value: '1'
86+
- key: google-readability-function-size.StatementThreshold
87+
value: '800'
88+
- key: google-readability-namespace-comments.ShortNamespaceLines
89+
value: '10'
90+
- key: google-readability-namespace-comments.SpacesBeforeComments
91+
value: '2'
92+
- key: misc-definitions-in-headers.HeaderFileExtensions
93+
value: ',h,hh,hpp,hxx'
94+
- key: misc-definitions-in-headers.UseHeaderFileExtension
95+
value: '1'
96+
- key: misc-throw-by-value-catch-by-reference.CheckThrowTemporaries
97+
value: '1'
98+
- key: misc-unused-parameters.StrictMode
99+
value: '0'
100+
- key: modernize-loop-convert.MaxCopySize
101+
value: '16'
102+
- key: modernize-loop-convert.MinConfidence
103+
value: reasonable
104+
- key: modernize-loop-convert.NamingStyle
105+
value: CamelCase
106+
- key: modernize-pass-by-value.IncludeStyle
107+
value: llvm
108+
- key: modernize-replace-auto-ptr.IncludeStyle
109+
value: llvm
110+
- key: modernize-use-nullptr.NullMacros
111+
value: 'NULL'
112+
- key: performance-faster-string-find.StringLikeClasses
113+
value: 'std::basic_string'
114+
- key: performance-for-range-copy.AllowedTypes
115+
value: ''
116+
- key: performance-for-range-copy.WarnOnAllAutoCopies
117+
value: '0'
118+
- key: performance-inefficient-string-concatenation.StrictMode
119+
value: '0'
120+
- key: performance-inefficient-vector-operation.EnableProto
121+
value: '0'
122+
- key: performance-inefficient-vector-operation.VectorLikeClasses
123+
value: '::std::vector'
124+
- key: performance-move-const-arg.CheckTriviallyCopyableMove
125+
value: '1'
126+
- key: performance-move-constructor-init.IncludeStyle
127+
value: llvm
128+
- key: performance-no-automatic-move.AllowedTypes
129+
value: ''
130+
- key: performance-type-promotion-in-math-fn.IncludeStyle
131+
value: llvm
132+
- key: performance-unnecessary-copy-initialization.AllowedTypes
133+
value: ''
134+
- key: performance-unnecessary-value-param.AllowedTypes
135+
value: ''
136+
- key: performance-unnecessary-value-param.IncludeStyle
137+
value: llvm
138+
- key: readability-braces-around-statements.ShortStatementLines
139+
value: '0'
140+
- key: readability-else-after-return.WarnOnUnfixable
141+
value: '1'
142+
- key: readability-function-size.BranchThreshold
143+
value: '4294967295'
144+
- key: readability-function-size.LineThreshold
145+
value: '4294967295'
146+
- key: readability-function-size.NestingThreshold
147+
value: '4294967295'
148+
- key: readability-function-size.ParameterThreshold
149+
value: '4294967295'
150+
- key: readability-function-size.StatementThreshold
151+
value: '800'
152+
- key: readability-function-size.VariableThreshold
153+
value: '4294967295'
154+
- key: readability-identifier-naming.IgnoreFailedSplit
155+
value: '0'
156+
- key: readability-implicit-bool-conversion.AllowIntegerConditions
157+
value: '0'
158+
- key: readability-implicit-bool-conversion.AllowPointerConditions
159+
value: '0'
160+
- key: readability-inconsistent-declaration-parameter-name.IgnoreMacros
161+
value: '1'
162+
- key: readability-inconsistent-declaration-parameter-name.Strict
163+
value: '0'
164+
- key: readability-magic-numbers.IgnoredFloatingPointValues
165+
value: '1.0;100.0;'
166+
- key: readability-magic-numbers.IgnoredIntegerValues
167+
value: '1;2;3;4;'
168+
- key: readability-redundant-member-init.IgnoreBaseInCopyConstructors
169+
value: '0'
170+
- key: readability-redundant-smartptr-get.IgnoreMacros
171+
value: '1'
172+
- key: readability-redundant-string-init.StringNames
173+
value: '::std::basic_string'
174+
- key: readability-simplify-boolean-expr.ChainedConditionalAssignment
175+
value: '0'
176+
- key: readability-simplify-boolean-expr.ChainedConditionalReturn
177+
value: '0'
178+
- key: readability-simplify-subscript-expr.Types
179+
value: '::std::basic_string;::std::basic_string_view;::std::vector;::std::array'
180+
- key: readability-static-accessed-through-instance.NameSpecifierNestingThreshold
181+
value: '3'
182+
- key: readability-uppercase-literal-suffix.IgnoreMacros
183+
value: '1'
184+
- key: readability-uppercase-literal-suffix.NewSuffixes
185+
value: ''
186+
...

demo/demo.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** This is a very ugly test code (doomed to fail linting) */
12

23
#include <stdio.h>
34

@@ -6,10 +7,8 @@
67

78
int main(){
89

10+
for (;;) break;
11+
912
printf("Hello world!\n");
1013

1114
return 0;}
12-
13-
14-
15-
/* This is an ugly test code */

runchecks.sh

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
#!/bin/bash
22

3-
if [[ -z "$GITHUB_TOKEN" ]]; then
3+
EXIT_CODE="0"
4+
PAYLOAD_FORMAT=""
5+
PAYLOAD_TIDY=""
6+
7+
function set_exit_code () {
8+
if [[ $# -gt 0 ]]
9+
then
10+
EXIT_CODE="$1"
11+
else
12+
if [[ "$PAYLOAD_FORMAT" != "" || "$PAYLOAD_TIDY" != "" ]]
13+
then
14+
EXIT_CODE="1"
15+
fi
16+
fi
17+
echo "::set-output name=checks-failed::$EXIT_CODE"
18+
}
19+
20+
# check for access token (ENV VAR needed for git API calls)
21+
if [[ -z "$GITHUB_TOKEN" ]]
22+
then
423
echo "The GITHUB_TOKEN is required."
5-
exit 1
24+
set_exit_code "1"
25+
exit "$EXIT_CODE"
626
fi
727

28+
# parse CLI args
829
args=("$@")
930
FMT_STYLE=${args[0]}
1031
IFS=',' read -r -a FILE_EXT_LIST <<< "${args[1]}"
1132

33+
# use git API payload
1234
FILES_LINK=`jq -r '.pull_request._links.self.href' "$GITHUB_EVENT_PATH"`/files
1335
echo "Files = $FILES_LINK"
1436

37+
# setup download URLS
1538
curl $FILES_LINK > files.json
1639
FILES_URLS_STRING=`jq -r '.[].raw_url' files.json`
17-
1840
readarray -t URLS <<<"$FILES_URLS_STRING"
1941

2042
# exclude undesired files
@@ -34,7 +56,16 @@ do
3456
fi
3557
done
3658

37-
echo "File names: ${URLS[*]}"
59+
# exit early if nothing to do
60+
if [ ${#URLS[@]} == 0 ]
61+
then
62+
set_exit_code "0"
63+
echo "No source files need checking!"
64+
exit $EXIT_CODE
65+
else
66+
echo "File names: ${URLS[*]}"
67+
fi
68+
3869
mkdir files
3970
cd files
4071
for i in "${URLS[@]}"
@@ -78,6 +109,8 @@ if [ "$PAYLOAD_FORMAT" != "" ]; then
78109
OUTPUT+=$'\n```\n'
79110
fi
80111

112+
set_exit_code
113+
81114
echo "OUTPUT is: \n $OUTPUT"
82115

83116
PAYLOAD=$(echo '{}' | jq --arg body "$OUTPUT" '.body = $body')

0 commit comments

Comments
 (0)