1
- import * as fs from "fs" ;
2
-
3
1
import * as core from "@actions/core" ;
4
2
5
3
import * as actionsUtil from "./actions-util" ;
@@ -18,6 +16,7 @@ import {
18
16
isThirdPartyAnalysis ,
19
17
} from "./status-report" ;
20
18
import * as upload_lib from "./upload-lib" ;
19
+ import { uploadSarif } from "./upload-sarif" ;
21
20
import {
22
21
ConfigurationError ,
23
22
checkActionVersion ,
@@ -32,60 +31,6 @@ interface UploadSarifStatusReport
32
31
extends StatusReportBase ,
33
32
upload_lib . UploadStatusReport { }
34
33
35
- /**
36
- * Searches for SARIF files for the given `analysis` in the given `sarifPath`.
37
- * If any are found, then they are uploaded to the appropriate endpoint for the given `analysis`.
38
- *
39
- * @param logger The logger to use.
40
- * @param features Information about FFs.
41
- * @param sarifPath The path to a SARIF file or directory containing SARIF files.
42
- * @param pathStats Information about `sarifPath`.
43
- * @param checkoutPath The checkout path.
44
- * @param analysis The configuration of the analysis we should upload SARIF files for.
45
- * @param category The SARIF category to use for the upload.
46
- * @returns The result of uploading the SARIF file(s) or `undefined` if there are none.
47
- */
48
- async function findAndUpload (
49
- logger : Logger ,
50
- features : Features ,
51
- sarifPath : string ,
52
- pathStats : fs . Stats ,
53
- checkoutPath : string ,
54
- analysis : analyses . AnalysisConfig ,
55
- category ?: string ,
56
- ) : Promise < upload_lib . UploadResult | undefined > {
57
- let sarifFiles : string [ ] | undefined ;
58
-
59
- if ( pathStats . isDirectory ( ) ) {
60
- sarifFiles = upload_lib . findSarifFilesInDir (
61
- sarifPath ,
62
- analysis . sarifPredicate ,
63
- ) ;
64
- } else if (
65
- pathStats . isFile ( ) &&
66
- ( analysis . sarifPredicate ( sarifPath ) ||
67
- ( analysis . kind === analyses . AnalysisKind . CodeScanning &&
68
- ! analyses . CodeQuality . sarifPredicate ( sarifPath ) ) )
69
- ) {
70
- sarifFiles = [ sarifPath ] ;
71
- } else {
72
- return undefined ;
73
- }
74
-
75
- if ( sarifFiles . length !== 0 ) {
76
- return await upload_lib . uploadSpecifiedFiles (
77
- sarifFiles ,
78
- checkoutPath ,
79
- category ,
80
- features ,
81
- logger ,
82
- analysis ,
83
- ) ;
84
- }
85
-
86
- return undefined ;
87
- }
88
-
89
34
async function sendSuccessStatusReport (
90
35
startedAt : Date ,
91
36
uploadStats : upload_lib . UploadStatusReport ,
@@ -144,56 +89,37 @@ async function run() {
144
89
const sarifPath = actionsUtil . getRequiredInput ( "sarif_file" ) ;
145
90
const checkoutPath = actionsUtil . getRequiredInput ( "checkout_path" ) ;
146
91
const category = actionsUtil . getOptionalInput ( "category" ) ;
147
- const pathStats = fs . lstatSync ( sarifPath , { throwIfNoEntry : false } ) ;
148
-
149
- if ( pathStats === undefined ) {
150
- throw new ConfigurationError ( `Path does not exist: ${ sarifPath } .` ) ;
151
- }
152
92
153
- const sarifIds : Array < { analysis : string ; id : string } > = [ ] ;
154
- const uploadResult = await findAndUpload (
93
+ const uploadResults = await uploadSarif (
155
94
logger ,
156
95
features ,
157
- sarifPath ,
158
- pathStats ,
159
96
checkoutPath ,
160
- analyses . CodeScanning ,
97
+ sarifPath ,
161
98
category ,
162
99
) ;
163
- if ( uploadResult !== undefined ) {
164
- core . setOutput ( "sarif-id" , uploadResult . sarifID ) ;
165
- sarifIds . push ( {
166
- analysis : analyses . AnalysisKind . CodeScanning ,
167
- id : uploadResult . sarifID ,
168
- } ) ;
100
+
101
+ // Fail if we didn't upload anything.
102
+ if ( Object . keys ( uploadResults ) . length === 0 ) {
103
+ throw new ConfigurationError (
104
+ `No SARIF files found to upload in " ${ sarifPath } ".` ,
105
+ ) ;
169
106
}
170
107
171
- // If there are `.quality.sarif` files in `sarifPath`, then upload those to the code quality service.
172
- const qualityUploadResult = await findAndUpload (
173
- logger ,
174
- features ,
175
- sarifPath ,
176
- pathStats ,
177
- checkoutPath ,
178
- analyses . CodeQuality ,
179
- actionsUtil . fixCodeQualityCategory ( logger , category ) ,
180
- ) ;
181
- if ( qualityUploadResult !== undefined ) {
182
- sarifIds . push ( {
183
- analysis : analyses . AnalysisKind . CodeQuality ,
184
- id : qualityUploadResult . sarifID ,
185
- } ) ;
108
+ const codeScanningResult =
109
+ uploadResults [ analyses . AnalysisKind . CodeScanning ] ;
110
+ if ( codeScanningResult !== undefined ) {
111
+ core . setOutput ( "sarif-id" , codeScanningResult . sarifID ) ;
186
112
}
187
- core . setOutput ( "sarif-ids" , JSON . stringify ( sarifIds ) ) ;
113
+ core . setOutput ( "sarif-ids" , JSON . stringify ( uploadResults ) ) ;
188
114
189
115
// We don't upload results in test mode, so don't wait for processing
190
116
if ( isInTestMode ( ) ) {
191
117
core . debug ( "In test mode. Waiting for processing is disabled." ) ;
192
118
} else if ( actionsUtil . getRequiredInput ( "wait-for-processing" ) === "true" ) {
193
- if ( uploadResult !== undefined ) {
119
+ if ( codeScanningResult !== undefined ) {
194
120
await upload_lib . waitForProcessing (
195
121
getRepositoryNwo ( ) ,
196
- uploadResult . sarifID ,
122
+ codeScanningResult . sarifID ,
197
123
logger ,
198
124
) ;
199
125
}
@@ -202,7 +128,7 @@ async function run() {
202
128
}
203
129
await sendSuccessStatusReport (
204
130
startedAt ,
205
- uploadResult ?. statusReport || { } ,
131
+ codeScanningResult ?. statusReport || { } ,
206
132
logger ,
207
133
) ;
208
134
} catch ( unwrappedError ) {
0 commit comments