@@ -21,7 +21,7 @@ async function securityAnalysis() {
21
21
22
22
const log = new TwoColumnLogger ( "Diffblue Gitlab Integration" ) ;
23
23
try {
24
- const projectName = `gitlab -${ Date . now ( ) } ` ;
24
+ const projectName = `${ process . env . CI_PROJECT_NAMESPACE } -${ process . env . CI_PROJECT_NAME } - ${ process . env . CI_JOB_ID } ` ;
25
25
let localMachine : models . Instance = {
26
26
name : "local-executor" ,
27
27
ip : "localhost" ,
@@ -40,51 +40,47 @@ async function securityAnalysis() {
40
40
zoneIndex : 0 ,
41
41
diskImg : { name : "nodiskimgname" , link : "nodiskimglink" } ,
42
42
username : "diffblue" ,
43
+ password : "password123" ,
43
44
duration : { start : "nodurationstart" } ,
44
45
} ;
45
46
47
+ console . log ( `Running against commit with title: ${ process . env . CI_COMMIT_TITLE } ` ) ;
48
+ console . log ( "Initialising Diffblue platform" ) ;
46
49
const startTime = Date . now ( ) ;
47
-
48
50
localMachine = await platform . createProject ( localMachine ) ;
49
51
localMachine = await platform . initialiseProject ( localMachine ) ;
50
52
localMachine = await platform . startAnalysis ( localMachine ) ;
53
+ console . log ( "Analysis started" ) ;
51
54
52
- do {
55
+ let complete = 0 ;
56
+ while ( true ) {
53
57
localMachine = await platform . getAnalysisProgress ( localMachine ) ;
54
- if ( localMachine . analysisStatus === "RUNNING" || localMachine . analysisStatus === "WAITING" ) {
55
- const elapsed = ( Date . now ( ) - startTime ) / 1000 ;
56
- const nextWait = Math . ceil ( elapsed / 10 ) ; // Wait for 10% of time elapsed so far
57
- log . debug ( `Waiting ${ nextWait } seconds before checking again whether Diffblue platform is ready` ) ;
58
- await wait ( nextWait ) ;
58
+ if ( localMachine . analysisProgress !== undefined && localMachine . analysisProgress !== complete ) {
59
+ complete = localMachine . analysisProgress ;
60
+ console . log ( `Analysis ${ complete . toFixed ( 2 ) } % complete` ) ;
59
61
}
60
- } while ( localMachine . analysisStatus === "RUNNING" || localMachine . analysisStatus === "WAITING" ) ;
62
+ if ( localMachine . analysisStatus !== "RUNNING" && localMachine . analysisStatus !== "WAITING" )
63
+ break ;
64
+ const elapsed = ( Date . now ( ) - startTime ) / 1000 ;
65
+ const nextWait = Math . ceil ( elapsed / 10 ) ; // Wait for 10% of time elapsed so far
66
+ log . debug ( `Waiting ${ nextWait } seconds before checking again whether Diffblue platform is ready` ) ;
67
+ await wait ( nextWait ) ;
68
+ }
61
69
62
70
const issues = await platform . getIssues ( localMachine ) ;
71
+ const issueLogLines = issues . map ( ( issue : any ) => `* ${ issue . testName } \n Trace: ${ getIssueUri ( localMachine , issue . id ) } ` ) ;
72
+ console . log ( `Identified ${ issues . length } issues:\n${ issueLogLines . join ( "\n" ) } ` ) ;
63
73
64
- log . debug ( `Identified ${ issues . length } issues` ) ;
65
- log . debug ( JSON . stringify ( issues ) ) ;
66
-
67
- const report = issues . map (
68
- function ( issue : any ) {
69
-
70
- // TODO: query the platform's public-facing URL
71
- const testUrl = `http://localhost/${ projectName } /${ localMachine . analysisNum } /tests/${ issue . id } /trace` ;
72
-
73
- return {
74
- file : `${ issue . testClassDir } /${ issue . testClass } ` ,
75
- message : issue . testName ,
76
- priority : "High" ,
77
- tool : "Diffblue-security" ,
78
- url : testUrl ,
79
- cve : `Click here ${ testUrl } ` ,
80
- // cve: testUrl // Mandatory key
81
- } ;
82
- }
83
- ) ;
84
-
85
- const reportFileName = path . join ( process . env . INIT_CWD , "gl-sast-report.json" ) ;
86
- log . debug ( `Writing issue report to ${ reportFileName } ` ) ;
87
- await fs . writeFile ( reportFileName , JSON . stringify ( report ) ) ;
74
+ const report = issues . map ( ( issue : any ) => ( {
75
+ file : `${ issue . testClassDir } /${ issue . testClass } ` ,
76
+ message : issue . testName ,
77
+ priority : "High" ,
78
+ tool : "Diffblue security analyser" ,
79
+ // TODO: query the platform's public-facing URL
80
+ url : getIssueUri ( localMachine , issue . id ) ,
81
+ cve : "N/A" , // Mandatory key when provide URL
82
+ } ) ) ;
83
+ await fs . writeFile ( path . join ( process . env . INIT_CWD , "gl-sast-report.json" ) , JSON . stringify ( report ) ) ;
88
84
} catch ( err ) {
89
85
if ( err . message ) {
90
86
log . debug ( err . message ) ;
@@ -97,4 +93,9 @@ async function securityAnalysis() {
97
93
}
98
94
}
99
95
96
+ function getIssueUri ( localMachine : models . Instance , issueId : any ) : string {
97
+ return `http://localhost/${ localMachine . project . name } /${ localMachine . analysisNum } /tests/${ issueId } /trace` ;
98
+ }
99
+
100
+ // Run async entry point
100
101
securityAnalysis ( ) ;
0 commit comments