Skip to content

Commit f59e527

Browse files
feat: only comment on commits/PRs if CI breaks (#9)
1 parent 9c102e5 commit f59e527

File tree

2 files changed

+66
-52
lines changed

2 files changed

+66
-52
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ Add this workflow to your repository to analyze dependencies in your pull reques
66

77
The action will:
88

9-
<!-- TODO: make sure this first point is correct -->
10-
11-
1. Run on commits that modify dependency files (OR in the first commit after the action is added)
12-
2. Analyze dependencies for software supply chain issues
9+
1. Run on commits that modify dependency files
10+
2. Analyze dependencies for software supply chain smells
1311
3. Post results:
14-
1. If in a PR, will post the report as a comment
15-
2. Otherwise, results are available in the action logs; if high severity issues are found, the report will be posted as a comment in the commit, if enabled
12+
1. If in a PR, will post the report as a comment by default if CI fails
13+
2. Otherwise, results are available in the action logs; if CI fails, the report may also be posted as a comment in the commit, if enabled
1614
4. Break CI if high severity issues are found, if enabled
1715

1816
As an important note, **the first time you run this action, it _will_ take quite some time**!
@@ -45,8 +43,8 @@ SSC issues currently checked for:
4543
| no_gradual_report | Disable gradual report functionality | No | false |
4644
| fail_on_high_severity | Fail CI on high severity issues | No | true |
4745
| x_to_fail | Percentage threshold to break CI on non-high severity issues (per type of issue) | No | 5% of packages |
48-
| allow_pr_comment | Comment on PR if high severity issues found | No | true |
49-
| comment_on_commit | Comment on commit if high severity issues found | No | false |
46+
| allow_pr_comment | Post analysis results as a PR comment if CI breaks | No | true |
47+
| comment_on_commit | Post analysis results as a commit comment if CI breaks | No | false |
5048
| latest_commit_sha | Latest commit SHA, used to comment on commits | Yes | - |
5149
| github_event_before | GitHub event before SHA, to retrieve the previous cache key | Yes | - |
5250
| ignore_cache | Ignore the repository cache for this run (true/false) | No | false |

action.yml

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ inputs:
5858
required: false
5959
default: '5'
6060
allow_pr_comment:
61-
description: 'Post analysis results as a PR comment if high severity issues are found'
61+
description: 'Post analysis results as a PR comment if CI breaks'
6262
required: false
6363
default: 'true'
6464
comment_on_commit:
65-
description: 'Post analysis results as a commit comment if high severity issues are found'
65+
description: 'Post analysis results as a commit comment if CI breaks'
6666
required: false
6767
default: 'false'
6868
latest_commit_sha:
@@ -124,106 +124,122 @@ runs:
124124
GITHUB_API_TOKEN: ${{ inputs.github_token }}
125125
run: |
126126
cd dirty-waters/tool
127-
127+
128128
# Copy cache if it exists
129129
if [ -d "$GITHUB_WORKSPACE/tool/cache" ]; then
130130
cp -r $GITHUB_WORKSPACE/tool/cache/ .
131131
fi
132-
132+
133133
# Build command
134134
CMD="python main.py -p ${{ inputs.project_repo }} -v ${{ inputs.version_old }} -s -pm ${{ inputs.package_manager }}"
135-
135+
136136
if [ "${{ inputs.differential_analysis }}" == "true" ]; then
137137
CMD="$CMD -vn ${{ inputs.version_new }} -d"
138138
fi
139-
139+
140140
if [ -n "${{ inputs.name_match }}" ]; then
141141
CMD="$CMD -n"
142142
fi
143-
143+
144144
if [ -n "${{ inputs.pnpm_scope }}" ]; then
145145
CMD="$CMD --pnpm-scope ${{ inputs.pnpm_scope }}"
146146
fi
147-
147+
148148
if [ -n "${{ inputs.specified_smells }}" ]; then
149149
CMD="$CMD ${{ inputs.specified_smells }}"
150150
fi
151-
151+
152152
if [ "${{ inputs.debug }}" == "true" ]; then
153153
CMD="$CMD --debug"
154154
fi
155-
155+
156156
if [ "${{ inputs.no_gradual_report }}" == "true" ]; then
157157
CMD="$CMD --no-gradual-report"
158158
fi
159-
159+
160160
echo "Running command: $CMD"
161161
eval $CMD
162-
162+
163163
# Copy cache back
164164
cp -r cache/ "$GITHUB_WORKSPACE/tool/"
165-
165+
166166
# Process results
167167
if [ ! -d "results" ]; then
168168
echo "An error occurred: no reports were generated"
169169
exit 1
170170
fi
171-
171+
172172
if [ "${{ inputs.differential_analysis }}" == "true" ]; then
173173
latest_report=$(ls -t results/*/*_diff_summary.md | head -n1)
174174
else
175175
latest_report=$(ls -t results/*/*_static_summary.md | head -n1)
176176
fi
177-
177+
178178
COMMENT=$(cat "$latest_report")
179179
cat "$latest_report" # Debug purposes: we always paste it in the logs
180-
181-
# Handle PR comments
182-
PR_NUMBER=$(jq -r ".pull_request.number" "$GITHUB_EVENT_PATH")
183-
if [[ "$PR_NUMBER" != "null" && "${{ inputs.allow_pr_comment }}" == "true" ]]; then
184-
curl -s -X POST \
185-
-H "Accept: application/vnd.github.v3+json" \
186-
-H "Authorization: token $GITHUB_TOKEN" \
187-
"https://api.github.com/repos/${{ inputs.project_repo }}/issues/$PR_NUMBER/comments" \
188-
-d "$(jq -n --arg body "$COMMENT" '{body: $body}')"
189-
elif [ "${{ inputs.comment_on_commit }}" == "true" ]; then
180+
181+
# Check for CI failure conditions
182+
CI_WILL_FAIL=0
183+
184+
# Check for high severity issues
185+
if [ "${{ inputs.fail_on_high_severity }}" == "true" ]; then
190186
if [[ $(cat "$latest_report" | grep -o "(⚠️⚠️⚠️) [0-9]*" | grep -o "[0-9]*" | sort -nr | head -n1) -gt 0 ]]; then
187+
echo "High severity issues found. CI will fail."
188+
CI_WILL_FAIL=1
189+
fi
190+
fi
191+
192+
# Only check for threshold violations if we haven't already decided to fail
193+
if [ $CI_WILL_FAIL -eq 0 ]; then
194+
total_packages=$(cat "$latest_report" | grep -o "Total packages in the supply chain: [0-9]*" | grep -o "[0-9]*")
195+
for severity in "⚠️⚠️⚠️" "⚠️⚠️" "⚠️"; do
196+
for count in $(cat "$latest_report" | grep -o "($severity) [0-9]*" | grep -o "[0-9]*"); do
197+
if [[ $(echo "scale=2; $count / $total_packages * 100" | bc) -gt ${{ inputs.x_to_fail }} ]]; then
198+
echo "Number of $severity issues surpasses the threshold. CI will fail."
199+
CI_WILL_FAIL=1
200+
break 2 # Break both loops once we know we'll fail
201+
fi
202+
done
203+
done
204+
fi
205+
206+
# Handle comments only if CI will fail
207+
if [ $CI_WILL_FAIL -eq 1 ]; then
208+
# Handle PR comments
209+
PR_NUMBER=$(jq -r ".pull_request.number" "$GITHUB_EVENT_PATH")
210+
if [[ "$PR_NUMBER" != "null" && "${{ inputs.allow_pr_comment }}" == "true" ]]; then
211+
curl -s -X POST \
212+
-H "Accept: application/vnd.github.v3+json" \
213+
-H "Authorization: token $GITHUB_TOKEN" \
214+
"https://api.github.com/repos/${{ inputs.project_repo }}/issues/$PR_NUMBER/comments" \
215+
-d "$(jq -n --arg body "$COMMENT" '{body: $body}')"
216+
fi
217+
218+
# Handle commit comments
219+
if [ "${{ inputs.comment_on_commit }}" == "true" ]; then
191220
curl -s -X POST \
192221
-H "Accept: application/vnd.github.v3+json" \
193222
-H "Authorization: token $GITHUB_TOKEN" \
194223
"https://api.github.com/repos/${{ inputs.project_repo }}/commits/${{ inputs.latest_commit_sha }}/comments" \
195224
-d "$(jq -n --arg body "$COMMENT" '{body: $body}')"
196225
fi
197226
fi
198-
227+
199228
# Copy results
200229
cp -r results/* "$GITHUB_WORKSPACE/"
201-
202-
# Check for high severity issues
203-
if [ "${{ inputs.fail_on_high_severity }}" == "true" ]; then
204-
if [[ $(cat "$latest_report" | grep -o "(⚠️⚠️⚠️) [0-9]*" | grep -o "[0-9]*" | sort -nr | head -n1) -gt 0 ]]; then
205-
echo "High severity issues found. Failing the build"
206-
exit 1
207-
fi
230+
231+
# Exit with failure if CI_WILL_FAIL is set
232+
if [ $CI_WILL_FAIL -eq 1 ]; then
233+
exit 1
208234
fi
209-
210-
total_packages=$(cat "$latest_report" | grep -o "Total packages in the supply chain: [0-9]*" | grep -o "[0-9]*")
211-
for severity in "⚠️⚠️" "⚠️"; do
212-
for count in $(cat "$latest_report" | grep -o "($severity) [0-9]*" | grep -o "[0-9]*"); do
213-
if [[ $(echo "scale=2; $count / $total_packages * 100" | bc) -gt ${{ inputs.x_to_fail }} ]]; then
214-
echo "Number of $severity issues surpasses the threshold. Failing the build"
215-
exit 1
216-
fi
217-
done
218-
done
219235
220236
- name: Save cache
221237
uses: actions/cache/save@v4
222238
if: always()
223239
with:
224240
path: tool/cache
225241
key: dirty-waters-cache-${{ runner.os }}-${{ inputs.project_repo }}-${{ github.sha }}
226-
242+
227243
- name: Break CI if analyses fail
228244
run: exit $(( steps.analysis.outcome == 'failure' ))
229245
shell: bash

0 commit comments

Comments
 (0)