Skip to content

Commit fafdffc

Browse files
committed
reuse the same code in downloadArtifact
1 parent ec90685 commit fafdffc

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

src/actions.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
1-
import create from '@actions/artifact'
1+
import {
2+
DefaultArtifactClient,
3+
DownloadArtifactOptions,
4+
DownloadArtifactResponse,
5+
GetArtifactResponse,
6+
} from '@actions/artifact'
27
import { endGroup, getInput, startGroup } from '@actions/core'
38
import { context, getOctokit } from '@actions/github'
49
import { retry } from '@octokit/plugin-retry'
10+
import path from 'path'
511
import { Md5 } from 'ts-md5'
6-
import { DefaultArtifactClient, UploadArtifactResponse } from '@actions/artifact'
712

813
const artifact = new DefaultArtifactClient()
914

10-
export function uploadArtifact(
11-
artifactName: string,
12-
...files: string[]
13-
): Promise<UploadArtifactResponse> {
15+
export async function uploadArtifact(artifactName: string, ...files: string[]) {
1416
startGroup('Uploading artifact ' + artifactName)
15-
var reponse = artifact.uploadArtifact(artifactName, files, '.')
17+
await artifact.uploadArtifact(artifactName, files, '.')
1618
endGroup()
17-
return reponse
1819
}
1920

20-
export async function downloadArtifact(artifactName: string) {
21+
export async function downloadArtifact(artifactName: string): Promise<string> {
2122
startGroup('Downloading artifact ' + artifactName)
22-
await create.getArtifact(artifactName)
23+
const getReponse: GetArtifactResponse = await artifact.getArtifact(artifactName)
24+
const option: DownloadArtifactOptions = {
25+
path: path.join(process.env['GITHUB_WORKSPACE'] || '.', artifactName),
26+
}
27+
const downloadReponse: DownloadArtifactResponse = await artifact.downloadArtifact(
28+
getReponse.artifact.id,
29+
option
30+
)
31+
if (downloadReponse.downloadPath === undefined) {
32+
throw new Error('Failed to download artifact ' + artifactName)
33+
}
2334
endGroup()
35+
return downloadReponse.downloadPath
2436
}
2537

2638
export async function postCommentIfInPr(message: string): Promise<string | undefined> {

src/index.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
telemetryCollector,
2121
} from './util'
2222

23-
import { exec } from 'child_process'
23+
import path from 'path'
2424

2525
const scaSarifReport = 'scaReport/output.sarif'
2626
const scaReport = 'sca.sarif'
@@ -89,32 +89,22 @@ async function runAnalysis() {
8989
async function displayResults() {
9090
info('Displaying results')
9191
const downloadStart = Date.now()
92-
await downloadArtifact('results-old')
93-
await downloadArtifact('results-new')
92+
const artifactOld = await downloadArtifact('results-old')
93+
const artifactNew = await downloadArtifact('results-new')
9494
telemetryCollector.addField(
9595
'duration.download-artifacts',
9696
(Date.now() - downloadStart).toString()
9797
)
98+
const sarifFileOld = path.join(artifactOld, scaReport)
99+
const sarifFileNew = path.join(artifactNew, scaReport)
100+
console.log('Artifact old: ', sarifFileOld)
101+
console.log('Artifact new: ', sarifFileNew)
102+
98103
const issuesByTool: { [tool: string]: string } = {}
99-
if (existsSync(`results-old/${scaReport}`) && existsSync(`results-new/${scaReport}`)) {
100-
issuesByTool['sca'] = await compareResults(
101-
'sca',
102-
`results-old/${scaReport}`,
103-
`results-new/${scaReport}`
104-
)
104+
if (existsSync(sarifFileOld) && existsSync(sarifFileNew)) {
105+
issuesByTool['sca'] = await compareResults('sca', sarifFileOld, sarifFileNew)
105106
} else {
106-
exec('ls -R -lh', (error, stdout, stderr) => {
107-
if (error) {
108-
console.error(`Error: ${error.message}`)
109-
return
110-
}
111-
if (stderr) {
112-
console.error(`Stderr: ${stderr}`)
113-
return
114-
}
115-
console.log(`Output:\n${stdout}`)
116-
})
117-
throw new Error('No results found for SCA')
107+
throw new Error('SARIF file not found for SCA')
118108
}
119109

120110
const commentStart = Date.now()

0 commit comments

Comments
 (0)