@@ -11,7 +11,9 @@ import * as utility from '../../src/blackduck-security-action/utility'
11
11
import { GitHubClientServiceFactory } from '../../src/blackduck-security-action/factory/github-client-service-factory'
12
12
import { GithubClientServiceCloud } from '../../src/blackduck-security-action/service/impl/cloud/github-client-service-cloud'
13
13
import fs from 'fs'
14
+ import * as core from '@actions/core'
14
15
16
+ jest . mock ( '@actions/core' )
15
17
jest . mock ( '@actions/io' , ( ) => ( {
16
18
rmRF : jest . fn ( )
17
19
} ) )
@@ -32,6 +34,90 @@ afterEach(() => {
32
34
jest . restoreAllMocks ( )
33
35
} )
34
36
37
+ describe ( 'Black Duck Security Action: Handling isBridgeExecuted and Exit Code Information Messages' , ( ) => {
38
+ const setupBlackDuckInputs = ( extraInputs : Record < string , any > = { } ) => {
39
+ Object . defineProperty ( inputs , 'BLACKDUCKSCA_URL' , { value : 'BLACKDUCKSCA_URL' } )
40
+ Object . defineProperty ( inputs , 'BLACKDUCKSCA_TOKEN' , { value : 'BLACKDUCKSCA_TOKEN' } )
41
+ Object . defineProperty ( inputs , 'DETECT_INSTALL_DIRECTORY' , { value : 'DETECT_INSTALL_DIRECTORY' } )
42
+ Object . defineProperty ( inputs , 'DETECT_SCAN_FULL' , { value : 'TRUE' } )
43
+ Object . defineProperty ( inputs , 'BLACKDUCKSCA_SCAN_FAILURE_SEVERITIES' , { value : 'ALL' } )
44
+ Object . defineProperty ( inputs , 'BLACKDUCKSCA_FIXPR_ENABLED' , { value : 'false' } )
45
+ Object . defineProperty ( inputs , 'BLACKDUCKSCA_PRCOMMENT_ENABLED' , { value : true } )
46
+ Object . defineProperty ( inputs , 'RETURN_STATUS' , { value : true } )
47
+ for ( const [ key , value ] of Object . entries ( extraInputs ) ) {
48
+ Object . defineProperty ( inputs , key , { value, writable : true } )
49
+ }
50
+ }
51
+
52
+ const setupMocks = ( exitCode : number ) => {
53
+ jest . spyOn ( Bridge . prototype , 'getBridgeVersionFromLatestURL' ) . mockResolvedValueOnce ( '0.1.0' )
54
+ const downloadFileResp : DownloadFileResponse = {
55
+ filePath : 'C://user/temp/download/' ,
56
+ fileName : 'C://user/temp/download/bridge-win.zip'
57
+ }
58
+ jest . spyOn ( downloadUtility , 'getRemoteFile' ) . mockResolvedValueOnce ( downloadFileResp )
59
+ jest . spyOn ( downloadUtility , 'extractZipped' ) . mockResolvedValueOnce ( true )
60
+ jest . spyOn ( configVariables , 'getGitHubWorkspaceDir' ) . mockReturnValueOnce ( '/home/bridge' )
61
+ jest . spyOn ( Bridge . prototype , 'executeBridgeCommand' ) . mockResolvedValueOnce ( exitCode )
62
+ const uploadResponse : UploadArtifactResponse = { size : 0 , id : 123 }
63
+ jest . spyOn ( diagnostics , 'uploadDiagnostics' ) . mockResolvedValueOnce ( uploadResponse )
64
+ }
65
+
66
+ afterEach ( ( ) => {
67
+ Object . defineProperty ( inputs , 'BLACKDUCKSCA_URL' , { value : null } )
68
+ } )
69
+
70
+ it ( 'handles successful execution with exitCode 0' , async ( ) => {
71
+ setupBlackDuckInputs ( )
72
+ setupMocks ( 0 )
73
+ const response = await run ( )
74
+
75
+ expect ( response ) . toBe ( 0 )
76
+ expect ( core . info ) . toHaveBeenCalledWith ( 'Black Duck Security Action workflow execution completed successfully.' )
77
+ expect ( core . setOutput ) . toHaveBeenCalledWith ( 'status' , 0 )
78
+ expect ( core . debug ) . toHaveBeenCalledWith ( 'Bridge CLI execution completed: true' )
79
+ } )
80
+
81
+ it ( 'handles issues detected but marked as success with exitCode 8' , async ( ) => {
82
+ setupBlackDuckInputs ( { MARK_BUILD_STATUS : 'success' } )
83
+ setupMocks ( 8 )
84
+ jest . spyOn ( utility , 'checkJobResult' ) . mockReturnValue ( 'success' )
85
+
86
+ const response = await run ( )
87
+
88
+ expect ( response ) . toBe ( 8 )
89
+ expect ( core . info ) . toHaveBeenCalledWith ( 'Marking the build success as configured in the task.' )
90
+ expect ( core . setOutput ) . toHaveBeenCalledWith ( 'status' , 8 )
91
+ expect ( core . debug ) . toHaveBeenCalledWith ( 'Bridge CLI execution completed: true' )
92
+ } )
93
+
94
+ it ( 'handles failure case with exitCode 2' , async ( ) => {
95
+ setupBlackDuckInputs ( )
96
+ setupMocks ( 2 )
97
+
98
+ const response = await run ( )
99
+ expect ( response ) . toBe ( 2 )
100
+ expect ( core . setOutput ) . toHaveBeenCalledWith ( 'status' , 2 )
101
+ expect ( core . debug ) . toHaveBeenCalledWith ( 'Bridge CLI execution completed: false' )
102
+ } )
103
+
104
+ it ( 'uploads SARIF report for exitCode 8' , async ( ) => {
105
+ setupBlackDuckInputs ( {
106
+ BLACKDUCKSCA_REPORTS_SARIF_CREATE : 'true' ,
107
+ BLACKDUCKSCA_REPORTS_SARIF_FILE_PATH : '/' ,
108
+ MARK_BUILD_STATUS : 'success'
109
+ } )
110
+ setupMocks ( 8 )
111
+ jest . spyOn ( utility , 'checkJobResult' ) . mockReturnValue ( 'success' )
112
+ jest . spyOn ( utility , 'isPullRequestEvent' ) . mockReturnValue ( false )
113
+ const uploadResponse : UploadArtifactResponse = { size : 0 , id : 123 }
114
+ jest . spyOn ( diagnostics , 'uploadSarifReportAsArtifact' ) . mockResolvedValueOnce ( uploadResponse )
115
+
116
+ await run ( )
117
+ expect ( diagnostics . uploadSarifReportAsArtifact ) . toHaveBeenCalledWith ( 'Blackduck SCA SARIF Generator' , '/' , 'blackduck_sarif_report' )
118
+ } )
119
+ } )
120
+
35
121
test ( 'Not supported flow error - run' , async ( ) => {
36
122
Object . defineProperty ( inputs , 'POLARIS_SERVER_URL' , { value : null } )
37
123
Object . defineProperty ( inputs , 'BLACKDUCKSCA_URL' , { value : null } )
0 commit comments