-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
214 lines (191 loc) · 8.06 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# yaml-language-server: $schema=https://json.schemastore.org/github-action.json
name: 'Benedi.kt'
author: 'The diKTat Team'
description: 'A GitHub Action to check your code with diKTat'
# Note: when inverting the default values of boolean flags, be sure to also
# invert the values they're compared to, i.e. if a certain flag is `true` by
# default, then it should be compared with `false`, because `false` is the only
# value which users of our action can explicitly set. Cases when a flag is
# merely inset (i.e. is not equal to either `true` or `false`) are quite common.
inputs:
config:
description: >
The location of the YAML configuration file. By default,
diktat-analysis.yml in the current directory is used.
default: 'diktat-analysis.yml'
required: false
reporter:
description: >
The reporter to use, one of: "sarif" (the default) or "checkstyle".
default: 'sarif'
required: false
input-paths:
description: >
One or more patterns which indicate the files or directories to check.
Use a multiline string to specify multiple inputs.
If an input is a path to a file, it is passed to diKTat as-is.
If an input is a path to a directory, the directory is recursively
traversed, and all *.kt and *.kts files are passed to diKTat.
If an input is an Ant-like path pattern (such as "**/*.kt"), diKTat
expands it into the list of files that match the path pattern. Path
patterns may be negated, e. g.: "!src/**/*Test.kt" or
"!src/**/generated/**".
required: false
java-distribution:
description: >
The Java distribution. See the list of supported distributions at
<https://github.com/actions/setup-java/blob/main/README.md#supported-distributions>.
The default is "temurin".
Note that setting just this property in order to use a custom JDK is not
sufficient: please set "java-version", too.
default: 'temurin'
required: false
java-version:
description: >
The Java version to set up. Takes a whole or semver Java version. See
examples of supported syntax at
<https://github.com/actions/setup-java/blob/main/README.md#supported-version-syntax>.
required: false
debug:
description: >
Whether debug logging should be enabled
default: ${{ false }}
required: false
fail-on-error:
description: >
Whether linter errors are considered fatal (the default is true)
default: ${{ true }}
required: false
outputs:
exit-code:
description: "The exit code of diKTat"
value: ${{ steps.diktat.outputs.exit-code }}
runs:
using: "composite"
steps:
- id: setup-java
if: ${{ inputs.java-version != null }}
uses: actions/setup-java@v4
with:
distribution: '${{ inputs.java-distribution }}'
java-version: ${{ inputs.java-version }}
- id: download-diktat
uses: robinraju/[email protected]
with:
repository: "saveourtool/diktat"
tag: "v2.0.0"
fileName: "diktat"
# GitHub seems to set `-e` internally, so don't fail immediately if diKTat
# returns a non-zero exit code but store the exit code in a variable for
# further processing.
#
# This step is always successful but records the exit code in the
# `steps.diktat.outputs.exit-code` variable.
- id: diktat
run: |
if [[ '${{ inputs.debug }}' == 'true' ]]
then
set -x
fi
DIKTAT_ARGS=('--config' '${{ inputs.config }}' '--reporter' '${{ inputs.reporter }}')
if [[ '${{ inputs.reporter }}' == 'sarif' ]]
then
report_file=${GITHUB_WORKSPACE}/report.sarif
elif [[ '${{ inputs.reporter }}' == 'checkstyle' ]]
then
report_file=${GITHUB_WORKSPACE}/checkstyle-report.xml
else
echo "<reporter> should be set to <sarif> or <checkstyle>"
exit 1
fi
DIKTAT_ARGS+=('--output' "${report_file}")
if [[ '${{ inputs.debug }}' == 'true' ]]
then
DIKTAT_ARGS+=('--log-level' 'DEBUG')
fi
set -o pipefail
IFS=$'\n'
INPUT_PATHS=(${{ inputs.input-paths }})
DIKTAT_CMD=${GITHUB_WORKSPACE}/diktat
chmod +x ${DIKTAT_CMD}
{ ${DIKTAT_CMD} "${DIKTAT_ARGS[@]}" "${INPUT_PATHS[@]}" | tee diktat.log; } && exit_code=$? || exit_code=$?
total_lines=$(wc -l diktat.log | cut -d ' ' -f1)
summary_line_number=$(grep -n 'Summary error count (descending) by rule:' diktat.log | cut -d: -f1)
summary_line=$(tail -n $((total_lines - summary_line_number)) diktat.log | sed -e 's/^ //g' | awk '{ printf("%s,", $0) }' | sed -e 's/,$//g')
echo "summary-line=${summary_line}" >>$GITHUB_OUTPUT
rm -f diktat.log
echo "exit-code=${exit_code}" >>$GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ github.token }}
shell: bash
# Creates a summary report and fails the job if necessary.
#
# If `diktat` exited with code 1 (linter errors), return 0 or 1 depending
# on the value of the `fail-on-error` flag. If the exit code is 2 or
# greater, return it as-is, ignoring the flag.
#
# Note: BSD `awk` apparently doesn't understand `\\s`, that's why we're
# splitting on `: *` (a colon followed by a zero or more space characters).
- id: diktat-summary
if: ${{ always() }}
run: |
if (( ${{ steps.diktat.outputs.exit-code }} == 0 ))
then
status_icon=':heavy_check_mark:'
message='completed successfully'
else
status_icon=':x:'
message='exited with code **${{ steps.diktat.outputs.exit-code }}**'
fi
diktat='[_diKTat_](https://github.com/saveourtool/diktat)'
echo "${status_icon} ${diktat} ${message}" >>${GITHUB_STEP_SUMMARY}
if (( ${{ steps.diktat.outputs.exit-code }} != 0 ))
then
echo '```console' >>${GITHUB_STEP_SUMMARY}
echo "${{ steps.diktat.outputs.summary-line }}" | xargs -d ',' -n1 echo >>${GITHUB_STEP_SUMMARY}
echo '```' >>${GITHUB_STEP_SUMMARY}
fi
if (( ${{ steps.diktat.outputs.exit-code }} <= 1 ))
then
${GITHUB_WORKSPACE}/diktat --version | awk '{ split($0, versions, ": *"); print " - _" versions[1] "_: **" versions[2] "**" }' >>${GITHUB_STEP_SUMMARY}
fi
if (( ${{ steps.diktat.outputs.exit-code }} != 1 ))
then
exit ${{ steps.diktat.outputs.exit-code }}
elif [[ '${{ inputs.fail-on-error }}' != 'false' ]]
then
exit 1
fi
shell: bash
# When in SARIF reporting mode, upload SARIF reports to GitHub.
- id: upload-sarif
if: ${{ always() && steps.diktat.outputs.exit-code == 1 && inputs.reporter == 'sarif' }}
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: ${{ github.workspace }}/report.sarif
# Alternatively, when in Checkstyle reporting mode, upload Checkstyle XML
# using `reviewdog`.
#
# `reviewdog` doesn't play well with manual runs (`workflow_dispatch`):
# it fails with
#
# post failed for %s: failed to create check: POST https://api.github.com/repos/%s/%s/check-runs: 422 No commit found for SHA: []
- id: reviewdog-install
if: ${{ always() && steps.diktat.outputs.exit-code == 1 && inputs.reporter == 'checkstyle' && github.event_name != 'workflow_dispatch' }}
uses: reviewdog/action-setup@v1
- id: reviewdog
if: ${{ always() && steps.diktat.outputs.exit-code == 1 && inputs.reporter == 'checkstyle' && github.event_name != 'workflow_dispatch' }}
run: |
if [[ '${{ github.event_name }}' == 'pull_request' ]]
then
reviewdog_reporter='github-pr-check'
else
reviewdog_reporter='github-check'
fi
reviewdog -f=checkstyle -fail-on-error=false -level=info -name='diKTat errors reported by reviewdog' -reporter="${reviewdog_reporter}" <${GITHUB_WORKSPACE}/checkstyle-report.xml
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ github.token }}
shell: bash
branding:
icon: 'check-circle'
color: 'purple'