forked from erzz/codeclimate-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
186 lines (174 loc) · 8.15 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
name: "Code Climate Standalone"
author: "Karan Shah & Sean Erswell-Liljefelt"
description: "Runs a detached version of CodeClimate which reports only in the workflow and does not require the CodeClimate Service"
branding:
color: gray-dark
icon: chevrons-up
inputs:
config_file:
description: "Optionally provide a path to your codeclimate.yml relative to your project"
required: false
default: ".codeclimate.yml"
html_report:
description: "Should a faster, second, execution occur in order to generate an HTML report"
required: false
default: "false"
info_threshold:
description: "The number of findings of this severity allowed before the job returns a failure"
required: false
default: "0"
minor_threshold:
description: "The number of findings of this severity allowed before the job returns a failure"
required: false
default: "0"
major_threshold:
description: "The number of findings of this severity allowed before the job returns a failure"
required: false
default: "0"
critical_threshold:
description: "The number of findings of this severity allowed before the job returns a failure"
required: false
default: "0"
blocker_threshold:
description: "The number of findings of this severity allowed before the job returns a failure"
required: false
default: "0"
exit_on_complete:
description: "Return exit code on action completion. Use `false` if you want to perform some custom actions and exit later. (Hint: Use the result output to determine exit code later)"
required: false
default: "true"
outputs:
info_findings:
description: "The number of findings of severity INFO"
value: ${{ steps.cc.outputs.info }}
minor_findings:
description: "The number of findings of severity MINOR"
value: ${{ steps.cc.outputs.minor }}
major_findings:
description: "The number of findings of severity MAJOR"
value: ${{ steps.cc.outputs.major }}
critical_findings:
description: "The number of findings of severity CRITICAL"
value: ${{ steps.cc.outputs.critical }}
blocker_findings:
description: "The number of findings of severity BLOCKER"
value: ${{ steps.cc.outputs.blocker }}
total_findings:
description: "The total number of findings"
value: ${{ steps.cc.outputs.total }}
result:
description: "Final result of codeclimate based on thresholds"
value: ${{ steps.parse_result.outputs.result }}
runs:
using: "composite"
steps:
# Initial Run is performed to get the results in a parseable format
- name: Code Climate
shell: bash
id: cc
env:
CC_CONF: ${{ inputs.config_file }}
CC_BLOCKERS_ALLOWED: ${{ inputs.blocker_threshold }}
CC_CRITICAL_ALLOWED: ${{ inputs.critical_threshold }}
CC_MAJOR_ALLOWED: ${{ inputs.major_threshold }}
CC_MINOR_ALLOWED: ${{ inputs.minor_threshold }}
CC_INFO_ALLOWED: ${{ inputs.info_threshold }}
run: |
# If no configuration supplied the job will run with Code Climate's default settings
# and language detection. Providing your own config is highly recommended for speed
# and accuracy
echo "#### CONFIG ####"
if [ -f .codeclimate.yml ] || cp "$CC_CONF" .codeclimate.yml; then
echo "Found codeclimate config, using that"
else
echo "::warning::No configuration found, using Code Climate's default configuration"
fi
git switch $GITHUB_REF_NAME
git switch $GITHUB_HEAD_REF
files_changed=$(git --no-pager diff --name-only $GITHUB_HEAD_REF..$GITHUB_REF_NAME)
files_changed="./${files_changed//$'\n'/ ./}"
# Run once for JSON output
echo "#### INITIAL RUN ####"
docker run \
--env CODECLIMATE_CODE="$PWD" \
--volume "$PWD":/code \
--volume /var/run/docker.sock:/var/run/docker.sock \
--volume /tmp/cc:/tmp/cc \
--entrypoint /bin/sh codeclimate/codeclimate -c "cd /code && /usr/src/app/bin/codeclimate prepare > /dev/null && /usr/src/app/bin/codeclimate analyze $files_changed -f json" > raw.json
# Strip the json to only issues
jq -c 'map(select(.type | test("issue"; "i")))' raw.json > codeclimate-report.json
# Parse to provide simple job output
TOTAL_ISSUES=$(jq '. | length' codeclimate-report.json)
TOTAL_BLOCKER=$(jq 'map(select(.severity == "blocker")) | length' codeclimate-report.json)
TOTAL_CRITICAL=$(jq 'map(select(.severity == "critical")) | length' codeclimate-report.json)
TOTAL_MAJOR=$(jq 'map(select(.severity == "major")) | length' codeclimate-report.json)
TOTAL_MINOR=$(jq 'map(select(.severity == "minor")) | length' codeclimate-report.json)
TOTAL_INFO=$(jq 'map(select(.severity == "info")) | length' codeclimate-report.json)
# Set outputs
echo "::set-output name=total::$TOTAL_ISSUES"
echo "::set-output name=info::$TOTAL_INFO"
echo "::set-output name=minor::$TOTAL_MINOR"
echo "::set-output name=major::$TOTAL_MAJOR"
echo "::set-output name=critical::$TOTAL_CRITICAL"
echo "::set-output name=blocker::$TOTAL_BLOCKER"
echo "::set-output name=files_changed::$files_changed"
# Second run purely to get the readable HTML report. The second run is much faster than the first as it does not need to redownload the images already pulled by the first run
- name: Generate HTML Report
shell: bash
env:
CC_CONF: ${{ inputs.config_file }}
HTML_REPORT: ${{ inputs.html_report }}
run: |
if [ "$HTML_REPORT" = true ]; then
# If no configuration supplied the job will run with Code Climate's default settings
# and language detection. Providing your own config is highly recommended for speed
# and accuracy
echo "#### CONFIG ####"
if [ -f .codeclimate.yml ] || cp "$CC_CONF" .codeclimate.yml; then
echo "Found codeclimate.yml at project root"
else
echo "::warning::No configuration found, using Code Climate's default configuration"
fi
# Run for HTML output
echo "#### GENERATING HTML VERSION ####"
docker run \
--env CODECLIMATE_CODE="$PWD" \
--volume "$PWD":/code \
--volume /var/run/docker.sock:/var/run/docker.sock \
--volume /tmp/cc:/tmp/cc \
--entrypoint /bin/sh codeclimate/codeclimate -c "cd /code && /usr/src/app/bin/codeclimate prepare > /dev/null && /usr/src/app/bin/codeclimate analyze ${{ steps.cc.outputs.files_changed }} -f html" > codeclimate-report.html
else
echo "HTML REPORT not requested, skipping..."
fi
# Determine the result
- name: Parse Result
id: parse_result
shell: bash
env:
CC_BLOCKERS_ALLOWED: ${{ inputs.blocker_threshold }}
CC_CRITICAL_ALLOWED: ${{ inputs.critical_threshold }}
CC_MAJOR_ALLOWED: ${{ inputs.major_threshold }}
CC_MINOR_ALLOWED: ${{ inputs.minor_threshold }}
CC_INFO_ALLOWED: ${{ inputs.info_threshold }}
EXIT_ON_COMPLETE: ${{ inputs.exit_on_complete }}
run: |
# Output in logs
echo "#### RESULT ####"
echo "total_issues: ${{ steps.cc.outputs.total }}"
echo "info: ${{ steps.cc.outputs.info }} allowed: $CC_INFO_ALLOWED"
echo "minor: ${{ steps.cc.outputs.minor }} allowed: $CC_MINOR_ALLOWED"
echo "major: ${{ steps.cc.outputs.major }} allowed: $CC_MAJOR_ALLOWED"
echo "critical: ${{ steps.cc.outputs.critical }} allowed: $CC_CRITICAL_ALLOWED"
echo "blocker: ${{ steps.cc.outputs.blocker }} allowed: $CC_BLOCKERS_ALLOWED"
# Pass or Fail the job depending on the findings / inputs
if [ ${{ steps.cc.outputs.blocker }} -gt "$CC_BLOCKERS_ALLOWED" ] || [ ${{ steps.cc.outputs.critical }} -gt "$CC_CRITICAL_ALLOWED" ] || [ ${{ steps.cc.outputs.major }} -gt "$CC_MAJOR_ALLOWED" ]; then
echo "::set-output name=result::'FAIL'"
if [ "$EXIT_ON_COMPLETE" = "true" ]; then
exit 1
fi
else
echo "::set-output name=result::'PASS'"
if [ "$EXIT_ON_COMPLETE" = "true" ]; then
exit 0
fi
fi