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,7 @@ 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 sarifReportPath = getInput ( 'code-scanning-path' )
2824
2925async function runAnalysis ( ) {
3026 const target = getInput ( 'target' )
@@ -40,23 +36,28 @@ async function runAnalysis() {
4036 }
4137
4238 info ( 'Analyzing ' + target )
43- telemetryCollector . addField ( 'tools' , 'sca' )
4439 const toUpload : string [ ] = [ ]
4540
4641 // command to print both sarif and lwjson formats
47- var args = [ 'sca' , 'scan' , '.' , '-o' , scaDir , '--formats' , 'sarif,lw-json' , '--deployment' , 'ci' ]
42+ var args = [
43+ 'sca' ,
44+ 'scan' ,
45+ '.' ,
46+ '--formats' ,
47+ 'sarif' ,
48+ '--output' ,
49+ sarifReportPath ,
50+ '--deployment' ,
51+ 'ci' ,
52+ ]
4853 if ( target === 'push' ) {
4954 args . push ( '--save-results' )
5055 }
5156 if ( debug ( ) ) {
5257 args . push ( '--debug' )
5358 }
5459 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 )
60+ toUpload . push ( sarifReportPath )
6061
6162 const uploadStart = Date . now ( )
6263 const artifactPrefix = getInput ( 'artifact-prefix' )
@@ -69,6 +70,31 @@ async function runAnalysis() {
6970 setOutput ( `${ target } -completed` , true )
7071}
7172
73+ export async function compareResults ( oldReport : string , newReport : string ) : Promise < string > {
74+ var comparisonFile = 'comparison.md'
75+ const args = [
76+ 'sca' ,
77+ 'compare' ,
78+ '--old' ,
79+ oldReport ,
80+ '--new' ,
81+ newReport ,
82+ '--markdown' ,
83+ comparisonFile ,
84+ '--markdown-variant' ,
85+ 'GitHub' ,
86+ '--deployment' ,
87+ 'ci' ,
88+ ]
89+
90+ const uiLink = generateUILink ( )
91+ if ( uiLink ) args . push ( ...[ '--ui-link' , uiLink ] )
92+
93+ if ( debug ( ) ) args . push ( '--debug' )
94+ await callLaceworkCli ( ...args )
95+ return existsSync ( comparisonFile ) ? readFileSync ( comparisonFile , 'utf8' ) : ''
96+ }
97+
7298async function displayResults ( ) {
7399 info ( 'Displaying results' )
74100 const downloadStart = Date . now ( )
@@ -78,30 +104,24 @@ async function displayResults() {
78104 'duration.download-artifacts' ,
79105 ( Date . now ( ) - downloadStart ) . toString ( )
80106 )
81- const sarifFileOld = path . join ( artifactOld , scaReport )
82- const sarifFileNew = path . join ( artifactNew , scaReport )
107+ const sarifFileOld = path . join ( artifactOld , sarifReportPath )
108+ const sarifFileNew = path . join ( artifactNew , sarifReportPath )
83109
84- const issuesByTool : { [ tool : string ] : string } = { }
110+ var compareMessage : string
85111 if ( existsSync ( sarifFileOld ) && existsSync ( sarifFileNew ) ) {
86- issuesByTool [ 'sca' ] = await compareResults ( 'sca' , sarifFileOld , sarifFileNew )
112+ compareMessage = await compareResults ( sarifFileOld , sarifFileNew )
87113 } else {
88- throw new Error ( 'SARIF file not found for SCA ' )
114+ throw new Error ( 'SARIF file not found' )
89115 }
90116
91117 const commentStart = Date . now ( )
92- if ( Object . values ( issuesByTool ) . some ( ( x ) => x . length > 0 ) && getInput ( 'token' ) . length > 0 ) {
118+ if ( compareMessage . length > 0 && getInput ( 'token' ) . length > 0 ) {
93119 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- }
100120 if ( getInput ( 'footer' ) !== '' ) {
101- message += '\n\n' + getInput ( 'footer' )
121+ compareMessage += '\n\n' + getInput ( 'footer' )
102122 }
103- info ( message )
104- const commentUrl = await postCommentIfInPr ( message )
123+ info ( compareMessage )
124+ const commentUrl = await postCommentIfInPr ( compareMessage )
105125 if ( commentUrl !== undefined ) {
106126 setOutput ( 'posted-comment' , commentUrl )
107127 }
0 commit comments