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

Commit 1db93d7

Browse files
authored
feat: add support for custom PR comments (#16)
1 parent b87bf34 commit 1db93d7

File tree

6 files changed

+94
-20
lines changed

6 files changed

+94
-20
lines changed

action.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ inputs:
5151
description: 'Comment pull requests if no violations found'
5252
required: false
5353
default: 'true'
54+
no-policy-violations-found-comment:
55+
description: 'PR comment to post when no policy violations are found'
56+
required: false
57+
# language=markdown
58+
default: |-
59+
# :white_check_mark: Black Duck - None of your dependencies violate policy!
60+
policy-violations-found-comment-warning:
61+
description: 'Warning PR comment to post when policy violations are found'
62+
required: false
63+
# language=markdown
64+
default: |-
65+
# :warning: Black Duck - Found dependencies violating policy!
66+
policy-violations-found-comment-failure:
67+
description: 'Failure PR comment to post when policy violations are found'
68+
required: false
69+
# language=markdown
70+
default: |-
71+
# :x: Black Duck - Found dependencies violating policy!
72+
5473
outputs:
5574
detect-exit-code:
5675
description: 'A number indicating Detect exit code'

dist/index.js

Lines changed: 29 additions & 7 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ export class DetectFacade {
155155
const reportResult = await this.blackDuckReportGenerator.generateReport(
156156
scanJsonPaths[0],
157157
{
158+
noPolicyViolationsFoundComment:
159+
this.inputs.noPolicyViolationsFoundComment,
160+
policyViolationsFoundCommentWarning:
161+
this.inputs.policyViolationsFoundCommentWarning,
162+
policyViolationsFoundCommentFailure:
163+
this.inputs.policyViolationsFoundCommentFailure,
158164
failureConditionsMet,
159165
maxSize: MAX_REPORT_SIZE
160166
}

src/input/inputs.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export interface Inputs {
1111
detectTrustCertificate: string
1212
failIfDetectFails: boolean
1313
commentPrOnSuccess: boolean
14+
noPolicyViolationsFoundComment: string
15+
policyViolationsFoundCommentWarning: string
16+
policyViolationsFoundCommentFailure: string
1417
}
1518

1619
export enum Input {
@@ -24,7 +27,10 @@ export enum Input {
2427
OUTPUT_PATH_OVERRIDE = 'output-path-override',
2528
DETECT_TRUST_CERTIFICATE = 'detect-trust-cert',
2629
FAIL_IF_DETECT_FAILS = 'fail-if-detect-fails',
27-
COMMENT_PR_ON_SUCCESS = 'comment-pr-on-success'
30+
COMMENT_PR_ON_SUCCESS = 'comment-pr-on-success',
31+
NO_POLICY_VIOLATIONS_FOUND_COMMENT = 'no-policy-violations-found-comment',
32+
POLICY_VIOLATIONS_FOUND_COMMENT_WARNING = 'policy-violations-found-comment-warning',
33+
POLICY_VIOLATIONS_FOUND_COMMENT_FAILURE = 'policy-violations-found-comment-failure'
2834
}
2935

3036
export function gatherInputs(): Inputs {
@@ -38,6 +44,11 @@ export function gatherInputs(): Inputs {
3844
const detectTrustCertificate = getInputDetectTrustCertificate()
3945
const failIfDetectFails = getInputFailIfDetectFails()
4046
const commentPrOnSuccess = getInputCommentPrOnSuccess()
47+
const noPolicyViolationsFoundComment = getNoPolicyViolationsFoundComment()
48+
const policyViolationsFoundCommentWarning =
49+
getPolicyViolationsFoundCommentWarning()
50+
const policyViolationsFoundCommentFailure =
51+
getPolicyViolationsFoundCommentFailure()
4152
return {
4253
token,
4354
blackDuckUrl,
@@ -48,7 +59,10 @@ export function gatherInputs(): Inputs {
4859
outputPathOverride,
4960
detectTrustCertificate,
5061
failIfDetectFails,
51-
commentPrOnSuccess
62+
commentPrOnSuccess,
63+
noPolicyViolationsFoundComment,
64+
policyViolationsFoundCommentWarning,
65+
policyViolationsFoundCommentFailure
5266
}
5367
}
5468

@@ -91,3 +105,15 @@ function getInputFailIfDetectFails(): boolean {
91105
function getInputCommentPrOnSuccess(): boolean {
92106
return core.getBooleanInput(Input.COMMENT_PR_ON_SUCCESS)
93107
}
108+
109+
function getNoPolicyViolationsFoundComment(): string {
110+
return core.getInput(Input.NO_POLICY_VIOLATIONS_FOUND_COMMENT)
111+
}
112+
113+
function getPolicyViolationsFoundCommentWarning(): string {
114+
return core.getInput(Input.POLICY_VIOLATIONS_FOUND_COMMENT_WARNING)
115+
}
116+
117+
function getPolicyViolationsFoundCommentFailure(): string {
118+
return core.getInput(Input.POLICY_VIOLATIONS_FOUND_COMMENT_FAILURE)
119+
}

src/report/blackduck-report-generator.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ const HEADER =
1515
'| Policies Violated | Dependency | License(s) | Vulnerabilities | Short Term Recommended Upgrade | Long Term Recommended Upgrade |'
1616
const HEADER_ALIGNMENT = '|-|-|-|-|-|-|'
1717

18-
const SUCCESS_COMMENT =
19-
'# :white_check_mark: Black Duck - None of your dependencies violate policy!'
20-
const FAIL_COMMENT = (fail: boolean): string =>
21-
`# ${
22-
fail ? ':x:' : ':warning:'
23-
} Black Duck - Found dependencies violating policy!`
24-
2518
export class BlackDuckReportGenerator
2619
implements ReportGenerator<ReportProperties, ReportResult>
2720
{
@@ -39,7 +32,10 @@ export class BlackDuckReportGenerator
3932
textBuilder: TextBuilder,
4033
properties: ReportProperties
4134
): void {
42-
textBuilder.addLines(FAIL_COMMENT(properties.failureConditionsMet))
35+
const comment = properties.failureConditionsMet
36+
? properties.policyViolationsFoundCommentFailure
37+
: properties.policyViolationsFoundCommentWarning
38+
textBuilder.addLines(comment)
4339
}
4440

4541
private addHeaderToTextBuilder(textBuilder: TextBuilder): void {
@@ -78,9 +74,11 @@ export class BlackDuckReportGenerator
7874
return isContentTruncated
7975
}
8076

81-
private async generateSuccessReport(): Promise<ReportResult> {
77+
private async generateSuccessReport(
78+
properties: ReportProperties
79+
): Promise<ReportResult> {
8280
return {
83-
report: SUCCESS_COMMENT,
81+
report: properties.noPolicyViolationsFoundComment,
8482
failed: false,
8583
truncated: false,
8684
hasPolicyViolations: false
@@ -116,7 +114,7 @@ export class BlackDuckReportGenerator
116114
await this.blackDuckScanReportGenerator.generateReport(path)
117115
return blackDuckScanReport.hasPolicyViolations
118116
? this.generateFailureReport(blackDuckScanReport.reports, properties)
119-
: this.generateSuccessReport()
117+
: this.generateSuccessReport(properties)
120118
}
121119

122120
private getViolatedPolicies(violatedPolicies: string[]): string {

src/report/report-properties.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export interface ReportProperties {
2+
noPolicyViolationsFoundComment: string
3+
policyViolationsFoundCommentWarning: string
4+
policyViolationsFoundCommentFailure: string
25
failureConditionsMet: boolean
36
maxSize?: number
47
}

0 commit comments

Comments
 (0)