Skip to content

Commit f331896

Browse files
committed
feat(core): cleanup downloaded artifacts
Fixes #639
1 parent 7b125ca commit f331896

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/main.ts

+39-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { platform } from 'os';
2-
import { chdir } from 'process';
1+
import { platform } from 'node:os';
2+
import { chdir } from 'node:process';
3+
import { unlinkSync } from 'node:fs';
34
import { debug, error, setFailed, warning, info } from '@actions/core';
45
import { exec } from '@actions/exec';
56
import { context } from '@actions/github';
@@ -31,6 +32,17 @@ const SUPPORTED_GITHUB_EVENTS = [
3132
'pull_request_target',
3233
];
3334

35+
const fileArtifacts = new Set<string>();
36+
37+
async function downloadAndRecord(
38+
url: string,
39+
file: string,
40+
mode?: number
41+
): Promise<void> {
42+
await downloadToFile(url, file, mode);
43+
fileArtifacts.add(file);
44+
}
45+
3446
function prepareEnv() {
3547
const env = process.env as { [key: string]: string };
3648

@@ -120,7 +132,7 @@ export function run(
120132

121133
try {
122134
debug(`ℹ️ Downloading CC Reporter from ${downloadUrl} ...`);
123-
await downloadToFile(downloadUrl, executable);
135+
await downloadAndRecord(downloadUrl, executable);
124136
debug('✅ CC Reporter downloaded...');
125137
} catch (err) {
126138
error((err as Error).message);
@@ -141,7 +153,7 @@ export function run(
141153

142154
try {
143155
debug(`ℹ️ Verifying CC Reporter checksum...`);
144-
await downloadToFile(checksumUrl, checksumFilePath);
156+
await downloadAndRecord(checksumUrl, checksumFilePath);
145157
const checksumVerified = await verifyChecksum(
146158
executable,
147159
checksumFilePath,
@@ -158,8 +170,8 @@ export function run(
158170

159171
try {
160172
debug(`ℹ️ Verifying CC Reporter GPG signature...`);
161-
await downloadToFile(signatureUrl, signatureFilePath);
162-
await downloadToFile(
173+
await downloadAndRecord(signatureUrl, signatureFilePath);
174+
await downloadAndRecord(
163175
CODECLIMATE_GPG_PUBLIC_KEY_URL,
164176
ccPublicKeyFilePath
165177
);
@@ -353,14 +365,25 @@ if (require.main === module) {
353365
'verifyDownload',
354366
DEFAULT_VERIFY_DOWNLOAD
355367
);
356-
run(
357-
DOWNLOAD_URL,
358-
EXECUTABLE,
359-
coverageCommand,
360-
workingDirectory,
361-
codeClimateDebug,
362-
coverageLocations,
363-
coveragePrefix,
364-
verifyDownload
365-
);
368+
try {
369+
run(
370+
DOWNLOAD_URL,
371+
EXECUTABLE,
372+
coverageCommand,
373+
workingDirectory,
374+
codeClimateDebug,
375+
coverageLocations,
376+
coveragePrefix,
377+
verifyDownload
378+
);
379+
} catch (err) {
380+
throw err;
381+
} finally {
382+
// Finally clean up all artifacts that we downloaded.
383+
for (const artifact of fileArtifacts) {
384+
try {
385+
unlinkSync(artifact);
386+
} catch {}
387+
}
388+
}
366389
}

0 commit comments

Comments
 (0)