Skip to content

Commit ac1d2d7

Browse files
committed
fix: add summary comment on failure when warn-only: true
1 parent 526b7f2 commit ac1d2d7

File tree

5 files changed

+50
-36
lines changed

5 files changed

+50
-36
lines changed

dist/index.js

Lines changed: 22 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/comment-pr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ const COMMENT_MARKER = '<!-- dependency-review-pr-comment-marker -->'
1717

1818
export async function commentPr(
1919
commentContent: string,
20-
config: ConfigurationOptions
20+
config: ConfigurationOptions,
21+
failureCount: number
2122
): Promise<void> {
2223
if (
2324
!(
2425
config.comment_summary_in_pr === 'always' ||
25-
(config.comment_summary_in_pr === 'on-failure' &&
26-
process.exitCode === core.ExitCode.Failure)
26+
(config.comment_summary_in_pr === 'on-failure' && failureCount > 0)
2727
)
2828
) {
2929
return

src/deny.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ export async function getDeniedChanges(
3535
}
3636
}
3737

38-
if (hasDeniedPackage) {
39-
core.setFailed('Dependency review detected denied packages.')
40-
} else {
41-
core.info('Dependency review did not detect any denied packages')
42-
}
43-
4438
return changesDenied
4539
}
4640

src/main.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,25 @@ async function run(): Promise<void> {
141141
summary.addSnapshotWarnings(config, snapshot_warnings)
142142
}
143143

144+
let failureCount = 0;
145+
144146
if (config.vulnerability_check) {
145147
core.setOutput('vulnerable-changes', JSON.stringify(vulnerableChanges))
146148
summary.addChangeVulnerabilitiesToSummary(vulnerableChanges, minSeverity)
147-
printVulnerabilitiesBlock(vulnerableChanges, minSeverity, warnOnly)
149+
failureCount += printVulnerabilitiesBlock(vulnerableChanges, minSeverity, warnOnly)
148150
}
149151
if (config.license_check) {
150152
core.setOutput(
151153
'invalid-license-changes',
152154
JSON.stringify(invalidLicenseChanges)
153155
)
154156
summary.addLicensesToSummary(invalidLicenseChanges, config)
155-
printLicensesBlock(invalidLicenseChanges, warnOnly)
157+
failureCount += printLicensesBlock(invalidLicenseChanges, warnOnly)
156158
}
157159
if (config.deny_packages || config.deny_groups) {
158160
core.setOutput('denied-changes', JSON.stringify(deniedChanges))
159161
summary.addDeniedToSummary(deniedChanges)
160-
printDeniedDependencies(deniedChanges, config)
162+
failureCount += printDeniedDependencies(deniedChanges, config)
161163
}
162164
if (config.show_openssf_scorecard) {
163165
summary.addScorecardToSummary(scorecard, config)
@@ -182,7 +184,7 @@ async function run(): Promise<void> {
182184
}
183185

184186
// update the PR comment if needed with the right-sized summary
185-
await commentPr(rendered, config)
187+
await commentPr(rendered, config, failureCount)
186188
} catch (error) {
187189
if (error instanceof RequestError && error.status === 404) {
188190
core.setFailed(
@@ -208,17 +210,17 @@ function printVulnerabilitiesBlock(
208210
addedChanges: Changes,
209211
minSeverity: Severity,
210212
warnOnly: boolean
211-
): void {
212-
let vulFound = false
213+
): number {
214+
let vulCount = 0
213215
core.group('Vulnerabilities', async () => {
214216
if (addedChanges.length > 0) {
215217
for (const change of addedChanges) {
216218
printChangeVulnerabilities(change)
219+
vulCount += change.vulnerabilities.length;
217220
}
218-
vulFound = true
219221
}
220222

221-
if (vulFound) {
223+
if (vulCount > 0) {
222224
const msg = 'Dependency review detected vulnerable packages.'
223225
if (warnOnly) {
224226
core.warning(msg)
@@ -231,6 +233,7 @@ function printVulnerabilitiesBlock(
231233
)
232234
}
233235
})
236+
return vulCount
234237
}
235238

236239
function printChangeVulnerabilities(change: Change): void {
@@ -249,9 +252,11 @@ function printChangeVulnerabilities(change: Change): void {
249252
function printLicensesBlock(
250253
invalidLicenseChanges: Record<string, Changes>,
251254
warnOnly: boolean
252-
): void {
255+
): number {
256+
let failureCount = 0;
253257
core.group('Licenses', async () => {
254258
if (invalidLicenseChanges.forbidden.length > 0) {
259+
failureCount += invalidLicenseChanges.forbidden.length;
255260
core.info('\nThe following dependencies have incompatible licenses:')
256261
printLicensesError(invalidLicenseChanges.forbidden)
257262
const msg = 'Dependency review detected incompatible licenses.'
@@ -262,6 +267,7 @@ function printLicensesBlock(
262267
}
263268
}
264269
if (invalidLicenseChanges.unresolved.length > 0) {
270+
failureCount += invalidLicenseChanges.unresolved.length;
265271
core.warning(
266272
'\nThe validity of the licenses of the dependencies below could not be determined. Ensure that they are valid SPDX licenses:'
267273
)
@@ -272,6 +278,7 @@ function printLicensesBlock(
272278
}
273279
printNullLicenses(invalidLicenseChanges.unlicensed)
274280
})
281+
return failureCount;
275282
}
276283

277284
function printLicensesError(changes: Changes): void {
@@ -373,7 +380,7 @@ function printScannedDependencies(changes: Changes): void {
373380
function printDeniedDependencies(
374381
changes: Changes,
375382
config: ConfigurationOptions
376-
): void {
383+
): number {
377384
core.group('Denied', async () => {
378385
for (const denied of config.deny_packages) {
379386
core.info(`Config: ${denied}`)
@@ -383,7 +390,14 @@ function printDeniedDependencies(
383390
core.info(`Change: ${change.name}@${change.version} is denied`)
384391
core.info(`Change: ${change.package_url} is denied`)
385392
}
393+
394+
if (changes.length > 0) {
395+
core.setFailed('Dependency review detected denied packages.')
396+
} else {
397+
core.info('Dependency review did not detect any denied packages')
398+
}
386399
})
400+
return changes.length
387401
}
388402

389403
function getScorecardChanges(changes: Changes): Changes {

0 commit comments

Comments
 (0)