11import { error , getInput , info , setOutput , warning } from '@actions/core'
2- import { appendFileSync , existsSync } from 'fs'
2+ import { appendFileSync , existsSync , readFileSync } from 'fs'
33import {
44 downloadArtifact ,
55 postCommentIfInPr ,
66 resolveExistingCommentIfFound ,
77 uploadArtifact ,
88} from './actions'
9- import { compareResults } from './tool'
109import {
11- callCommand ,
1210 callLaceworkCli ,
1311 debug ,
12+ generateUILink ,
1413 getActionRef ,
1514 getMsSinceStart ,
1615 getOptionalEnvVariable ,
@@ -21,10 +20,9 @@ import {
2120
2221import path from 'path'
2322
24- const scaSarifReport = 'scaReport/output.sarif'
25- const scaReport = 'sca.sarif'
26- const scaLWJSONReport = 'scaReport/output-lw.json'
27- const scaDir = 'scaReport'
23+ const artifactPrefix = getInput ( 'artifact-prefix' )
24+ const sarifReportPath = getInput ( 'code-scanning-path' )
25+ const comparisonMarkdownPath = 'comparison.md'
2826
2927async function runAnalysis ( ) {
3028 const target = getInput ( 'target' )
@@ -40,68 +38,91 @@ async function runAnalysis() {
4038 }
4139
4240 info ( 'Analyzing ' + target )
43- telemetryCollector . addField ( 'tools' , 'sca' )
4441 const toUpload : string [ ] = [ ]
4542
4643 // command to print both sarif and lwjson formats
47- var args = [ 'sca' , 'scan' , '.' , '-o' , scaDir , '--formats' , 'sarif,lw-json' , '--deployment' , 'ci' ]
44+ var args = [
45+ 'sca' ,
46+ 'scan' ,
47+ '.' ,
48+ '--formats' ,
49+ 'sarif' ,
50+ '--output' ,
51+ sarifReportPath ,
52+ '--deployment' ,
53+ 'ci' ,
54+ ]
4855 if ( target === 'push' ) {
4956 args . push ( '--save-results' )
5057 }
5158 if ( debug ( ) ) {
5259 args . push ( '--debug' )
5360 }
5461 await callLaceworkCli ( ...args )
55- // make a copy of the sarif file
56- args = [ scaSarifReport , scaReport ]
57- await callCommand ( 'cp' , ...args )
58-
59- toUpload . push ( scaReport )
62+ toUpload . push ( sarifReportPath )
6063
6164 const uploadStart = Date . now ( )
62- const artifactPrefix = getInput ( 'artifact-prefix' )
63- if ( artifactPrefix !== '' ) {
64- await uploadArtifact ( artifactPrefix + '-results-' + target , ...toUpload )
65- } else {
66- await uploadArtifact ( 'results-' + target , ...toUpload )
67- }
65+
66+ await uploadArtifact ( getArtifactName ( target ) , ...toUpload )
67+
6868 telemetryCollector . addField ( 'duration.upload-artifacts' , ( Date . now ( ) - uploadStart ) . toString ( ) )
6969 setOutput ( `${ target } -completed` , true )
7070}
7171
72+ export async function compareResults ( oldReport : string , newReport : string ) : Promise < string > {
73+ const args = [
74+ 'sca' ,
75+ 'compare' ,
76+ '--old' ,
77+ oldReport ,
78+ '--new' ,
79+ newReport ,
80+ '--output' ,
81+ sarifReportPath ,
82+ '--markdown' ,
83+ comparisonMarkdownPath ,
84+ '--markdown-variant' ,
85+ 'GitHub' ,
86+ '--deployment' ,
87+ 'ci' ,
88+ ]
89+ const uiLink = generateUILink ( )
90+ if ( uiLink ) args . push ( ...[ '--ui-link' , uiLink ] )
91+ if ( debug ( ) ) args . push ( '--debug' )
92+
93+ await callLaceworkCli ( ...args )
94+ await uploadArtifact ( getArtifactName ( 'compare' ) , sarifReportPath , comparisonMarkdownPath )
95+
96+ return existsSync ( comparisonMarkdownPath ) ? readFileSync ( comparisonMarkdownPath , 'utf8' ) : ''
97+ }
98+
7299async function displayResults ( ) {
73100 info ( 'Displaying results' )
74101 const downloadStart = Date . now ( )
75- const artifactOld = await downloadArtifact ( 'results- old')
76- const artifactNew = await downloadArtifact ( 'results- new')
102+ const artifactOld = await downloadArtifact ( getArtifactName ( ' old') )
103+ const artifactNew = await downloadArtifact ( getArtifactName ( ' new') )
77104 telemetryCollector . addField (
78105 'duration.download-artifacts' ,
79106 ( Date . now ( ) - downloadStart ) . toString ( )
80107 )
81- const sarifFileOld = path . join ( artifactOld , scaReport )
82- const sarifFileNew = path . join ( artifactNew , scaReport )
108+ const sarifFileOld = path . join ( artifactOld , sarifReportPath )
109+ const sarifFileNew = path . join ( artifactNew , sarifReportPath )
83110
84- const issuesByTool : { [ tool : string ] : string } = { }
111+ var compareMessage : string
85112 if ( existsSync ( sarifFileOld ) && existsSync ( sarifFileNew ) ) {
86- issuesByTool [ 'sca' ] = await compareResults ( 'sca' , sarifFileOld , sarifFileNew )
113+ compareMessage = await compareResults ( sarifFileOld , sarifFileNew )
87114 } else {
88- throw new Error ( 'SARIF file not found for SCA ' )
115+ throw new Error ( 'SARIF file not found' )
89116 }
90117
91118 const commentStart = Date . now ( )
92- if ( Object . values ( issuesByTool ) . some ( ( x ) => x . length > 0 ) && getInput ( 'token' ) . length > 0 ) {
119+ if ( compareMessage . length > 0 && getInput ( 'token' ) . length > 0 ) {
93120 info ( 'Posting comment to GitHub PR as there were new issues introduced:' )
94- let message = ''
95- for ( const [ , issues ] of Object . entries ( issuesByTool ) ) {
96- if ( issues . length > 0 ) {
97- message += issues
98- }
99- }
100121 if ( getInput ( 'footer' ) !== '' ) {
101- message += '\n\n' + getInput ( 'footer' )
122+ compareMessage += '\n\n' + getInput ( 'footer' )
102123 }
103- info ( message )
104- const commentUrl = await postCommentIfInPr ( message )
124+ info ( compareMessage )
125+ const commentUrl = await postCommentIfInPr ( compareMessage )
105126 if ( commentUrl !== undefined ) {
106127 setOutput ( 'posted-comment' , commentUrl )
107128 }
@@ -112,6 +133,14 @@ async function displayResults() {
112133 setOutput ( `display-completed` , true )
113134}
114135
136+ function getArtifactName ( target : string ) : string {
137+ var artifactName = 'results-'
138+ if ( artifactPrefix !== '' ) {
139+ artifactName = artifactPrefix + '-' + artifactName
140+ }
141+ return artifactName + target
142+ }
143+
115144async function main ( ) {
116145 telemetryCollector . addField ( 'duration.install' , getMsSinceStart ( ) )
117146 telemetryCollector . addField ( 'version' , getActionRef ( ) )
0 commit comments