-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
263 lines (235 loc) · 7.77 KB
/
action.yml
File metadata and controls
263 lines (235 loc) · 7.77 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
name: "SecScore"
description: "Security scoring engine for CI/CD pipelines. Convert SARIF findings into a single score and security decision."
author: "Cássio B. Pereira a.k.a. @cassiodeveloper"
branding:
icon: "shield"
color: "gray-dark"
inputs:
findings:
description: "Path to findings.json (generated by your scanners/normalizers)"
required: false
policy:
description: "Path to SecScore policy YAML"
required: false
default: policy/policy-default.yml
sarif:
description: >
Path to one or more SARIF files. Accepts a single path or a comma-separated list of paths. Example: "semgrep.sarif,trivy.sarif"
required: false
no_diff_aware:
description: "Set to 'true' to disable diff-aware filtering (enabled by default)"
required: false
default: "false"
base_ref:
description: "Git ref to diff against for diff-aware filtering (default: origin/main)"
required: false
default: "origin/main"
max_findings:
description: "Max findings to show in PR comment"
required: false
default: "5"
label_review:
description: "Label to apply when decision is REVIEW"
required: false
default: "secscore-review"
check_name:
description: "Name of the GitHub Check Run"
required: false
default: "SecScore"
provider:
description: "Provider name for context (e.g. 'checkmarx', 'snyk')"
required: false
checkmarx-project:
description: "Checkmarx project name for context"
required: false
checkmarx-base-url:
description: "Checkmarx base URL for context"
required: false
checkmarx-token:
description: "Checkmarx token for context"
required: false
branch:
description: "Branch name for context"
required: false
outputs:
decision:
description: "PASS | REVIEW | FAIL"
value: ${{ steps.read_result.outputs.decision }}
score:
description: "0-100 score"
value: ${{ steps.read_result.outputs.score }}
exit_code:
description: "0=PASS, 1=REVIEW, 2=FAIL"
value: ${{ steps.secscore.outputs.exit_code }}
runs:
using: "composite"
steps:
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install SecScore (from this action repo)
shell: bash
run: |
python -m pip install --upgrade pip
pip install -r "${GITHUB_ACTION_PATH}/requirements.txt"
pip install -e "${GITHUB_ACTION_PATH}"
- name: Run SecScore
id: secscore
shell: bash
run: |
POLICY_PATH="${{ inputs.policy }}"
if [ -z "$POLICY_PATH" ]; then
POLICY_PATH="${GITHUB_ACTION_PATH}/policy/policy-default.yml"
echo "Using default policy from action"
else
echo "Using custom policy: $POLICY_PATH"
fi
# Monta argumento --sarif (pode ser lista separada por vírgula)
SARIF_ARG=""
if [ -n "${{ inputs.sarif }}" ]; then
SARIF_ARG="--sarif ${{ inputs.sarif }}"
fi
# Diff-aware opt-out
DIFF_ARG=""
if [ "${{ inputs.no_diff_aware }}" = "true" ]; then
DIFF_ARG="--no-diff-aware"
fi
# Base ref
BASE_REF_ARG=""
if [ -n "${{ inputs.base_ref }}" ]; then
BASE_REF_ARG="--base-ref ${{ inputs.base_ref }}"
fi
set +e
python3 -m secscore.cli.main pr \
${SARIF_ARG} \
--policy "$POLICY_PATH" \
--out pr-comment.md \
--json-out secscore-result.json \
${DIFF_ARG} \
${BASE_REF_ARG}
code=$?
echo "exit_code=$code" >> $GITHUB_OUTPUT
exit 0
- name: Read SecScore result
id: read_result
shell: bash
run: |
python3 - << 'PY'
import json, os, sys
path = "secscore-result.json"
if not os.path.exists(path):
print("decision=FAIL")
print("score=0")
sys.exit(0)
data = json.load(open(path, "r", encoding="utf-8"))
print(f"decision={data.get('decision','FAIL')}")
print(f"score={data.get('score',0)}")
PY >> "$GITHUB_OUTPUT"
- name: Label PR based on SecScore
if: ${{ always() }}
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const label = '${{ inputs.label_review }}';
if (!fs.existsSync('secscore-result.json')) return;
const result = JSON.parse(fs.readFileSync('secscore-result.json', 'utf8'));
const issue = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
const hasLabel = issue.data.labels.some(l => l.name === label);
if (result.decision === 'REVIEW' && !hasLabel) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: [label]
});
}
if (result.decision !== 'REVIEW' && hasLabel) {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
name: label
});
}
- name: Create SecScore status check
if: ${{ always() }}
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const checkName = '${{ inputs.check_name }}';
if (!fs.existsSync('secscore-result.json')) {
await github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name: checkName,
head_sha: context.payload.pull_request.head.sha,
status: 'completed',
conclusion: 'failure',
output: {
title: `${checkName} FAIL`,
summary: 'SecScore did not produce secscore-result.json'
}
});
return;
}
const result = JSON.parse(fs.readFileSync('secscore-result.json', 'utf8'));
const summary = fs.existsSync('pr-comment.md')
? fs.readFileSync('pr-comment.md', 'utf8')
: `${checkName} ${result.decision} — Score ${result.score}/100`;
const conclusion =
result.decision === 'PASS' ? 'success' :
result.decision === 'REVIEW' ? 'success' :
'failure';
await github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name: checkName,
head_sha: context.payload.pull_request.head.sha,
status: 'completed',
conclusion,
output: {
title: `${checkName} ${result.decision}`,
summary
}
});
- name: Publish PR comment (upsert)
if: ${{ always() }}
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
if (!fs.existsSync('pr-comment.md')) return;
const body = fs.readFileSync('pr-comment.md', 'utf8');
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
const marker = '<!-- SECSCORE_COMMENT -->';
const existing = comments.data.find(c => (c.body || '').includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body
});
}
- name: Fail job on SecScore FAIL
if: ${{ steps.secscore.outputs.exit_code == '2' }}
shell: bash
run: exit 1