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 {
1110 callCommand ,
1211 callLaceworkCli ,
1312 debug ,
13+ generateUILink ,
1414 getActionRef ,
1515 getMsSinceStart ,
1616 getOptionalEnvVariable ,
@@ -21,10 +21,9 @@ import {
2121
2222import path from 'path'
2323
24- const scaSarifReport = 'scaReport/output.sarif'
25- const scaReport = 'sca.sarif'
26- const scaLWJSONReport = 'scaReport/output-lw.json'
27- const scaDir = 'scaReport'
24+ const outputDirectory = 'output_directory'
25+ const outputSarifReport = path . join ( outputDirectory , 'output.sarif' )
26+ const sarifReport = 'results.sarif'
2827
2928async function runAnalysis ( ) {
3029 const target = getInput ( 'target' )
@@ -40,11 +39,20 @@ async function runAnalysis() {
4039 }
4140
4241 info ( 'Analyzing ' + target )
43- telemetryCollector . addField ( 'tools' , 'sca' )
4442 const toUpload : string [ ] = [ ]
4543
4644 // command to print both sarif and lwjson formats
47- var args = [ 'sca' , 'scan' , '.' , '-o' , scaDir , '--formats' , 'sarif,lw-json' , '--deployment' , 'ci' ]
45+ var args = [
46+ 'sca' ,
47+ 'scan' ,
48+ '.' ,
49+ '--output' ,
50+ outputDirectory ,
51+ '--formats' ,
52+ 'sarif,lw-json' ,
53+ '--deployment' ,
54+ 'ci' ,
55+ ]
4856 if ( target === 'push' ) {
4957 args . push ( '--save-results' )
5058 }
@@ -53,10 +61,14 @@ async function runAnalysis() {
5361 }
5462 await callLaceworkCli ( ...args )
5563 // make a copy of the sarif file
56- args = [ scaSarifReport , scaReport ]
57- await callCommand ( 'cp' , ...args )
5864
59- toUpload . push ( scaReport )
65+ const sarifReportPath = getInput ( 'code-scanning-path' )
66+ if ( sarifReportPath === '' ) {
67+ throw new Error ( 'code-scanning-path input cannot be empty' )
68+ }
69+ await callCommand ( 'cp' , outputSarifReport , sarifReportPath )
70+
71+ toUpload . push ( sarifReportPath )
6072
6173 const uploadStart = Date . now ( )
6274 const artifactPrefix = getInput ( 'artifact-prefix' )
@@ -69,6 +81,31 @@ async function runAnalysis() {
6981 setOutput ( `${ target } -completed` , true )
7082}
7183
84+ export async function compareResults ( oldReport : string , newReport : string ) : Promise < string > {
85+ var comparisonFile = 'comparison.md'
86+ const args = [
87+ 'sca' ,
88+ 'compare' ,
89+ '--old' ,
90+ oldReport ,
91+ '--new' ,
92+ newReport ,
93+ '--markdown' ,
94+ comparisonFile ,
95+ '--markdown-variant' ,
96+ 'GitHub' ,
97+ '--deployment' ,
98+ 'ci' ,
99+ ]
100+
101+ const uiLink = generateUILink ( )
102+ if ( uiLink ) args . push ( ...[ '--ui-link' , uiLink ] )
103+
104+ if ( debug ( ) ) args . push ( '--debug' )
105+ await callLaceworkCli ( ...args )
106+ return existsSync ( comparisonFile ) ? readFileSync ( comparisonFile , 'utf8' ) : ''
107+ }
108+
72109async function displayResults ( ) {
73110 info ( 'Displaying results' )
74111 const downloadStart = Date . now ( )
@@ -78,30 +115,24 @@ async function displayResults() {
78115 'duration.download-artifacts' ,
79116 ( Date . now ( ) - downloadStart ) . toString ( )
80117 )
81- const sarifFileOld = path . join ( artifactOld , scaReport )
82- const sarifFileNew = path . join ( artifactNew , scaReport )
118+ const sarifFileOld = path . join ( artifactOld , sarifReport )
119+ const sarifFileNew = path . join ( artifactNew , sarifReport )
83120
84- const issuesByTool : { [ tool : string ] : string } = { }
121+ var compareMessage : string
85122 if ( existsSync ( sarifFileOld ) && existsSync ( sarifFileNew ) ) {
86- issuesByTool [ 'sca' ] = await compareResults ( 'sca' , sarifFileOld , sarifFileNew )
123+ compareMessage = await compareResults ( sarifFileOld , sarifFileNew )
87124 } else {
88- throw new Error ( 'SARIF file not found for SCA ' )
125+ throw new Error ( 'SARIF file not found' )
89126 }
90127
91128 const commentStart = Date . now ( )
92- if ( Object . values ( issuesByTool ) . some ( ( x ) => x . length > 0 ) && getInput ( 'token' ) . length > 0 ) {
129+ if ( compareMessage . length > 0 && getInput ( 'token' ) . length > 0 ) {
93130 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- }
100131 if ( getInput ( 'footer' ) !== '' ) {
101- message += '\n\n' + getInput ( 'footer' )
132+ compareMessage += '\n\n' + getInput ( 'footer' )
102133 }
103- info ( message )
104- const commentUrl = await postCommentIfInPr ( message )
134+ info ( compareMessage )
135+ const commentUrl = await postCommentIfInPr ( compareMessage )
105136 if ( commentUrl !== undefined ) {
106137 setOutput ( 'posted-comment' , commentUrl )
107138 }
0 commit comments