Skip to content
This repository was archived by the owner on Jan 26, 2025. It is now read-only.

Commit 98318b8

Browse files
authored
feat: improve debug log level and add fail-if-detect-fails (#9)
1 parent 9292525 commit 98318b8

File tree

5 files changed

+91
-18
lines changed

5 files changed

+91
-18
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.2.0] - 2023-09-12
9+
10+
### Changed
11+
12+
- Re-add `fail-on-all-policy-severities` input
13+
- Change log level on debug to another key
14+
- Auto-enable diagnostic mode when debug mode is enabled
15+
- Add `fail-if-detect-fails` input to propagate detect error as action failure
16+
817
## [1.1.0] - 2023-09-11
918

1019
### Added
@@ -36,7 +45,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3645
- Improve logging
3746
- Update dependencies and refactor action
3847

39-
[Unreleased]: https://github.com/mercedesbenzio/detect-action/compare/v1.1.0...main
48+
[Unreleased]: https://github.com/mercedesbenzio/detect-action/compare/v1.2.0...main
49+
[1.2.0]: https://github.com/mercedesbenzio/detect-action/compare/v1.1.0...v1.2.0
4050
[1.1.0]: https://github.com/mercedesbenzio/detect-action/compare/v1.0.0...v1.1.0
4151
[1.0.0]: https://github.com/mercedesbenzio/detect-action/compare/v0.4.0...v1.0.0
4252
[0.4.0]: https://github.com/mercedesbenzio/detect-action/releases/tag/v0.4.0

action.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,25 @@ inputs:
2828
- INTELLIGENT: persists the results and allows all features of Detect.
2929
required: false
3030
default: 'RAPID'
31+
fail-on-all-policy-severities:
32+
description: |-
33+
By default, Detect will only fail on policy violations with BLOCKER or CRITICAL severities.
34+
This flag will cause the action to fail on all policy severities.
35+
required: false
36+
default: 'false'
3137
output-path-override:
3238
description: 'Override for where to output Detect files, default is $RUNNER_TEMP/blackduck/'
3339
required: false
3440
detect-trust-cert:
3541
description: |-
3642
When set to true Detect will trust the Black Duck certificate
3743
even if the certificate is not in the keystore.
44+
required: false
3845
default: 'true'
46+
fail-if-detect-fails:
47+
description: 'Fail the action if detect exits with an error code'
3948
required: false
49+
default: 'false'
4050
outputs:
4151
detect-exit-code:
4252
description: 'A number indicating Detect exit code'

dist/index.js

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

src/detect/detect-facade.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,17 @@ export class DetectFacade {
102102
`--detect.scan.output.path=${outputPath}`
103103
]
104104
if (core.isDebug()) {
105-
detectArguments.push('--logging.level.detect=DEBUG')
105+
detectArguments.push('--logging.level.com.synopsys.integration=DEBUG')
106106
}
107107
return detectArguments
108108
}
109109

110+
private enableDiagnosticModeIfDebugEnabled(): void {
111+
if (core.isDebug()) {
112+
process.env[DetectEnvironmentProperties.DETECT_DIAGNOSTIC] = 'true'
113+
}
114+
}
115+
110116
private isDiagnosticModeEnabled(): boolean {
111117
const diagnosticMode =
112118
process.env[
@@ -136,7 +142,7 @@ export class DetectFacade {
136142
}
137143

138144
private async processRapidScanResult(
139-
exitedWithFailurePolicyViolation: boolean,
145+
failureConditionsMet: boolean,
140146
outputPath: string
141147
): Promise<boolean> {
142148
core.info(
@@ -149,7 +155,7 @@ export class DetectFacade {
149155
const reportResult = await this.blackDuckReportGenerator.generateReport(
150156
scanJsonPaths[0],
151157
{
152-
failureConditionsMet: exitedWithFailurePolicyViolation,
158+
failureConditionsMet,
153159
maxSize: MAX_REPORT_SIZE
154160
}
155161
)
@@ -172,15 +178,15 @@ export class DetectFacade {
172178

173179
private async processDetectResult(
174180
outputPath: string,
175-
exitedWithFailurePolicyViolation: boolean
181+
failureConditionsMet: boolean
176182
): Promise<boolean> {
177183
core.info(`${TOOL_NAME} executed successfully.`)
178184

179185
let hasPolicyViolations = false
180186

181187
if (this.inputs.scanMode === RAPID_SCAN) {
182188
hasPolicyViolations = await this.processRapidScanResult(
183-
exitedWithFailurePolicyViolation,
189+
failureConditionsMet,
184190
outputPath
185191
)
186192
}
@@ -228,6 +234,7 @@ export class DetectFacade {
228234
}
229235

230236
async run(): Promise<void> {
237+
this.enableDiagnosticModeIfDebugEnabled()
231238
this.setNodeTlsRejectUnauthorized()
232239

233240
const outputPath = this.getOutputPath()
@@ -254,7 +261,8 @@ export class DetectFacade {
254261
if (isSuccessOrPolicyFailure) {
255262
const hasPolicyViolations = await this.processDetectResult(
256263
outputPath,
257-
detectExitCode === ExitCode.FAILURE_POLICY_VIOLATION
264+
detectExitCode === ExitCode.FAILURE_POLICY_VIOLATION ||
265+
this.inputs.failOnAllPolicySeverities
258266
)
259267

260268
if (hasPolicyViolations) {
@@ -271,7 +279,14 @@ export class DetectFacade {
271279
const isFailureAndNotRapidScan =
272280
detectExitCode !== ExitCode.SUCCESS && this.inputs.scanMode !== RAPID_SCAN
273281

274-
if (!isSuccessOrPolicyFailure || isFailureAndNotRapidScan) {
282+
const isFailureAndFailIfDetectFails =
283+
detectExitCode !== ExitCode.SUCCESS && this.inputs.failIfDetectFails
284+
285+
if (
286+
isFailureAndFailIfDetectFails ||
287+
!isSuccessOrPolicyFailure ||
288+
isFailureAndNotRapidScan
289+
) {
275290
throw new Error(
276291
`Detect failed with exit code: ${detectExitCode} - ${getExitCodeName(
277292
detectExitCode

src/input/inputs.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ export interface Inputs {
66
blackDuckApiToken: string
77
detectVersion?: string
88
scanMode: string
9+
failOnAllPolicySeverities: boolean
910
outputPathOverride: string
1011
detectTrustCertificate: string
12+
failIfDetectFails: boolean
1113
}
1214

1315
export enum Input {
@@ -17,8 +19,10 @@ export enum Input {
1719
BLACKDUCK_API_TOKEN = 'blackduck-api-token',
1820
DETECT_VERSION = 'detect-version',
1921
SCAN_MODE = 'scan-mode',
22+
FAIL_ON_ALL_POLICY_SEVERITIES = 'fail-on-all-policy-severities',
2023
OUTPUT_PATH_OVERRIDE = 'output-path-override',
21-
DETECT_TRUST_CERTIFICATE = 'detect-trust-cert'
24+
DETECT_TRUST_CERTIFICATE = 'detect-trust-cert',
25+
FAIL_IF_DETECT_FAILS = 'fail-if-detect-fails'
2226
}
2327

2428
export function gatherInputs(): Inputs {
@@ -27,16 +31,20 @@ export function gatherInputs(): Inputs {
2731
const blackDuckApiToken = getInputBlackDuckApiToken()
2832
const detectVersion = getInputDetectVersion()
2933
const scanMode = getInputScanMode()
34+
const failOnAllPolicySeverities = getInputFailOnAllPolicySeverities()
3035
const outputPathOverride = getInputOutputPathOverride()
3136
const detectTrustCertificate = getInputDetectTrustCertificate()
37+
const failIfDetectFails = getInputFailIfDetectFails()
3238
return {
3339
token,
3440
blackDuckUrl,
3541
blackDuckApiToken,
3642
detectVersion,
3743
scanMode,
44+
failOnAllPolicySeverities,
3845
outputPathOverride,
39-
detectTrustCertificate
46+
detectTrustCertificate,
47+
failIfDetectFails
4048
}
4149
}
4250

@@ -60,10 +68,18 @@ function getInputScanMode(): string {
6068
return core.getInput(Input.SCAN_MODE).toUpperCase()
6169
}
6270

71+
function getInputFailOnAllPolicySeverities(): boolean {
72+
return core.getBooleanInput(Input.FAIL_ON_ALL_POLICY_SEVERITIES)
73+
}
74+
6375
function getInputOutputPathOverride(): string {
6476
return core.getInput(Input.OUTPUT_PATH_OVERRIDE)
6577
}
6678

6779
function getInputDetectTrustCertificate(): string {
6880
return core.getInput(Input.DETECT_TRUST_CERTIFICATE)
6981
}
82+
83+
function getInputFailIfDetectFails(): boolean {
84+
return core.getBooleanInput(Input.FAIL_IF_DETECT_FAILS)
85+
}

0 commit comments

Comments
 (0)