-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
290 lines (273 loc) · 10 KB
/
Copy pathaction.yml
File metadata and controls
290 lines (273 loc) · 10 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# SwiftStaticAnalysis (swa) composite GitHub Action.
#
# Usage example:
#
# - uses: g-cqd/SwiftStaticAnalysis@main
# with:
# path: Sources
# mode: reachability
# clone-types: exact,near
# min-tokens: '50'
# format: sarif
# fail-on: error
#
# Inputs are mapped to the real `swa` CLI surface:
# - `mode` -> `swa unused --mode <mode>`
# - `clone-types` -> `swa duplicates --types <type> [<type>...]`
# - `min-tokens` -> `swa duplicates --min-tokens <n>`
# - `format` -> `-f text|json|xcode`, or `sarif` (post-processed from JSON)
# - `fail-on` -> implemented in this action (`swa` has no such flag)
#
# Outputs:
# - `unused-count` number of unused-code findings
# - `duplicates-count` number of duplicate clone groups
# - `sarif-path` path to the generated SARIF file (when format=sarif)
name: SwiftStaticAnalysis (swa)
description: Run swa on a Swift codebase to find unused code and duplicates, with SARIF output.
author: g-cqd
branding:
icon: search
color: orange
inputs:
path:
description: Path to scan (directory or file).
required: false
default: '.'
mode:
description: Unused-code detection mode (reachability, simple, indexStore).
required: false
default: reachability
clone-types:
description: Comma-separated clone types to detect (exact, near, semantic).
required: false
default: exact,near
min-tokens:
description: Minimum tokens for a duplicate clone group.
required: false
default: '50'
format:
description: Output format (sarif, text, json, xcode). `sarif` is post-processed from JSON.
required: false
default: sarif
fail-on:
description: Severity threshold to fail the step (error, warning, never). Implemented in-action; swa itself has no fail-on flag.
required: false
default: error
swa-version:
description: Optional git ref to check out before building (tag, branch, or SHA). When empty, uses the workspace as-is.
required: false
default: ''
working-directory:
description: Working directory in which to invoke swa.
required: false
default: ${{ github.workspace }}
outputs:
unused-count:
description: Number of unused-code findings reported by swa.
value: ${{ steps.run.outputs.unused-count }}
duplicates-count:
description: Number of duplicate clone groups reported by swa.
value: ${{ steps.run.outputs.duplicates-count }}
sarif-path:
description: Path to the generated SARIF file (only when format=sarif).
value: ${{ steps.run.outputs.sarif-path }}
runs:
using: composite
steps:
- name: Build swa (release)
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
# Indirect through env so a ref containing shell metacharacters
# cannot break out of the `git checkout` argument. Avoids the
# standard `${{ inputs.* }}` direct-interpolation injection
# pattern called out by GitHub's hardening guide.
SWA_VERSION: ${{ inputs.swa-version }}
run: |
set -euo pipefail
if [[ -n "$SWA_VERSION" ]]; then
echo "Checking out swa ref: $SWA_VERSION"
git fetch --tags --quiet origin -- "$SWA_VERSION" || true
git checkout --quiet -- "$SWA_VERSION"
fi
echo "Building swa in release configuration..."
swift build -c release --product swa
BIN_DIR="$(swift build -c release --show-bin-path)"
echo "$BIN_DIR" >> "$GITHUB_PATH"
echo "SWA_BIN=$BIN_DIR/swa" >> "$GITHUB_ENV"
- name: Run swa
id: run
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
SWA_PATH: ${{ inputs.path }}
SWA_MODE: ${{ inputs.mode }}
SWA_CLONE_TYPES: ${{ inputs.clone-types }}
SWA_MIN_TOKENS: ${{ inputs.min-tokens }}
SWA_FORMAT: ${{ inputs.format }}
SWA_FAIL_ON: ${{ inputs.fail-on }}
run: |
set -euo pipefail
case "$SWA_FORMAT" in
sarif)
SWA_FORMAT_FLAG=json
;;
text|json|xcode)
SWA_FORMAT_FLAG="$SWA_FORMAT"
;;
*)
echo "::error::Unknown format '$SWA_FORMAT' (expected sarif|text|json|xcode)"
exit 2
;;
esac
CLONE_TYPES_ARR=()
IFS=',' read -ra _CTS <<<"$SWA_CLONE_TYPES"
for t in "${_CTS[@]}"; do
t="${t// /}"
[[ -z "$t" ]] && continue
case "$t" in
exact|near|semantic) CLONE_TYPES_ARR+=("$t") ;;
*)
echo "::error::Unknown clone type '$t' (expected exact|near|semantic)"
exit 2
;;
esac
done
if [[ "${#CLONE_TYPES_ARR[@]}" -eq 0 ]]; then
CLONE_TYPES_ARR=(exact)
fi
case "$SWA_MODE" in
reachability|simple|indexStore) ;;
*)
echo "::error::Unknown mode '$SWA_MODE' (expected reachability|simple|indexStore)"
exit 2
;;
esac
case "$SWA_FAIL_ON" in
error|warning|never) ;;
*)
echo "::error::Unknown fail-on '$SWA_FAIL_ON' (expected error|warning|never)"
exit 2
;;
esac
OUT_DIR="${RUNNER_TEMP:-/tmp}/swa-out"
mkdir -p "$OUT_DIR"
DUP_JSON="$OUT_DIR/duplicates.json"
UNUSED_JSON="$OUT_DIR/unused.json"
echo "Running: swa duplicates $SWA_PATH --types ${CLONE_TYPES_ARR[*]} --min-tokens $SWA_MIN_TOKENS -f json"
"$SWA_BIN" duplicates "$SWA_PATH" \
--types "${CLONE_TYPES_ARR[@]}" \
--min-tokens "$SWA_MIN_TOKENS" \
-f json > "$DUP_JSON" || {
echo "::error::swa duplicates failed"
exit 1
}
echo "Running: swa unused $SWA_PATH --mode $SWA_MODE -f json"
"$SWA_BIN" unused "$SWA_PATH" \
--mode "$SWA_MODE" \
-f json > "$UNUSED_JSON" || {
echo "::error::swa unused failed"
exit 1
}
DUP_COUNT=$(jq 'length' "$DUP_JSON" 2>/dev/null || echo 0)
UNUSED_COUNT=$(jq 'length' "$UNUSED_JSON" 2>/dev/null || echo 0)
echo "Duplicate groups: $DUP_COUNT"
echo "Unused items: $UNUSED_COUNT"
echo "duplicates-count=$DUP_COUNT" >> "$GITHUB_OUTPUT"
echo "unused-count=$UNUSED_COUNT" >> "$GITHUB_OUTPUT"
SARIF_OUT="$OUT_DIR/swa.sarif"
case "$SWA_FORMAT" in
sarif)
jq -n \
--slurpfile dup "$DUP_JSON" \
--slurpfile unused "$UNUSED_JSON" \
'
def dup_results:
($dup[0] // []) | map(
. as $g
| (.clones // []) | map(
{
ruleId: ("duplicate-\($g.type)"),
level: "warning",
message: { text: ("Duplicate code (\($g.type) clone, \($g.occurrences) occurrences, similarity \($g.similarity))") },
locations: [{
physicalLocation: {
artifactLocation: { uri: .file },
region: { startLine: .startLine, endLine: .endLine }
}
}]
}
)
) | add // [];
def unused_results:
($unused[0] // []) | map({
ruleId: ("unused-\(.declaration.kind)"),
level: "warning",
message: { text: (.suggestion // "Unused declaration") },
locations: [{
physicalLocation: {
artifactLocation: { uri: .declaration.location.file },
region: {
startLine: .declaration.location.line,
startColumn: .declaration.location.column
}
}
}]
});
{
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/Schemata/sarif-schema-2.1.0.json",
version: "2.1.0",
runs: [{
tool: {
driver: {
name: "swa",
informationUri: "https://github.com/g-cqd/SwiftStaticAnalysis",
rules: []
}
},
results: (dup_results + unused_results)
}]
}
' > "$SARIF_OUT"
echo "SARIF written to $SARIF_OUT"
echo "sarif-path=$SARIF_OUT" >> "$GITHUB_OUTPUT"
;;
text|xcode)
"$SWA_BIN" duplicates "$SWA_PATH" \
--types "${CLONE_TYPES_ARR[@]}" \
--min-tokens "$SWA_MIN_TOKENS" \
-f "$SWA_FORMAT_FLAG" || true
"$SWA_BIN" unused "$SWA_PATH" \
--mode "$SWA_MODE" \
-f "$SWA_FORMAT_FLAG" || true
;;
json)
echo "::group::swa duplicates (json)"
cat "$DUP_JSON"
echo "::endgroup::"
echo "::group::swa unused (json)"
cat "$UNUSED_JSON"
echo "::endgroup::"
;;
esac
TOTAL=$((DUP_COUNT + UNUSED_COUNT))
case "$SWA_FAIL_ON" in
never)
;;
warning|error)
if [[ "$TOTAL" -gt 0 ]]; then
echo "::error::swa reported $TOTAL findings (duplicates=$DUP_COUNT unused=$UNUSED_COUNT); failing because fail-on=$SWA_FAIL_ON"
exit 1
fi
;;
esac
- name: Upload SARIF artifact
if: ${{ inputs.format == 'sarif' && steps.run.outputs.sarif-path != '' }}
# Pinned by SHA per https://docs.github.com/en/actions/security-guides
# `actions/upload-artifact@v4` resolved to v4.4.3 at audit time.
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
with:
name: swa-sarif
path: ${{ steps.run.outputs.sarif-path }}
if-no-files-found: warn
retention-days: 30