Skip to content

Commit

Permalink
feat(core): cleanup downloaded artifacts
Browse files Browse the repository at this point in the history
Fixes #639
  • Loading branch information
paambaati committed Dec 23, 2022
1 parent 7b125ca commit f331896
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions src/main.ts
@@ -1,5 +1,6 @@
import { platform } from 'os';
import { chdir } from 'process';
import { platform } from 'node:os';
import { chdir } from 'node:process';
import { unlinkSync } from 'node:fs';
import { debug, error, setFailed, warning, info } from '@actions/core';
import { exec } from '@actions/exec';
import { context } from '@actions/github';
Expand Down Expand Up @@ -31,6 +32,17 @@ const SUPPORTED_GITHUB_EVENTS = [
'pull_request_target',
];

const fileArtifacts = new Set<string>();

async function downloadAndRecord(
url: string,
file: string,
mode?: number
): Promise<void> {
await downloadToFile(url, file, mode);
fileArtifacts.add(file);
}

function prepareEnv() {
const env = process.env as { [key: string]: string };

Expand Down Expand Up @@ -120,7 +132,7 @@ export function run(

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

try {
debug(`ℹ️ Verifying CC Reporter checksum...`);
await downloadToFile(checksumUrl, checksumFilePath);
await downloadAndRecord(checksumUrl, checksumFilePath);
const checksumVerified = await verifyChecksum(
executable,
checksumFilePath,
Expand All @@ -158,8 +170,8 @@ export function run(

try {
debug(`ℹ️ Verifying CC Reporter GPG signature...`);
await downloadToFile(signatureUrl, signatureFilePath);
await downloadToFile(
await downloadAndRecord(signatureUrl, signatureFilePath);
await downloadAndRecord(
CODECLIMATE_GPG_PUBLIC_KEY_URL,
ccPublicKeyFilePath
);
Expand Down Expand Up @@ -353,14 +365,25 @@ if (require.main === module) {
'verifyDownload',
DEFAULT_VERIFY_DOWNLOAD
);
run(
DOWNLOAD_URL,
EXECUTABLE,
coverageCommand,
workingDirectory,
codeClimateDebug,
coverageLocations,
coveragePrefix,
verifyDownload
);
try {
run(
DOWNLOAD_URL,
EXECUTABLE,
coverageCommand,
workingDirectory,
codeClimateDebug,
coverageLocations,
coveragePrefix,
verifyDownload
);
} catch (err) {
throw err;
} finally {
// Finally clean up all artifacts that we downloaded.
for (const artifact of fileArtifacts) {
try {
unlinkSync(artifact);
} catch {}
}
}
}

0 comments on commit f331896

Please sign in to comment.