From 5b9c09c9585288c9c703548e628e5cbb74be3ba1 Mon Sep 17 00:00:00 2001 From: Koen Vlaswinkel Date: Tue, 7 Feb 2023 15:16:10 +0000 Subject: [PATCH] Download sourcemaps from release asset if available This download download the sourcemaps from the release asset if it is available. Unfortunately, I'm not able to test this yet because we don't yet have any releases with sourcemaps attached. As a fallback, it will still try to download from the workflow run. --- extensions/ql-vscode/scripts/source-map.ts | 92 ++++++++++++++++------ 1 file changed, 70 insertions(+), 22 deletions(-) diff --git a/extensions/ql-vscode/scripts/source-map.ts b/extensions/ql-vscode/scripts/source-map.ts index 5012a166b66..5e12b0d7e9f 100644 --- a/extensions/ql-vscode/scripts/source-map.ts +++ b/extensions/ql-vscode/scripts/source-map.ts @@ -11,6 +11,7 @@ import { spawnSync } from "child_process"; import { basename, resolve } from "path"; import { pathExists, readJSON } from "fs-extra"; import { SourceMapConsumer } from "source-map"; +import { Open } from "unzipper"; if (process.argv.length !== 4) { console.error( @@ -24,6 +25,12 @@ const versionNumber = process.argv[2].startsWith("v") const filenameAndLine = process.argv[3]; async function extractSourceMap() { + const releaseAssetsDirectory = resolve( + __dirname, + "..", + "release-assets", + versionNumber, + ); const sourceMapsDirectory = resolve( __dirname, "..", @@ -35,34 +42,64 @@ async function extractSourceMap() { if (!(await pathExists(sourceMapsDirectory))) { console.log("Downloading source maps..."); - const workflowRuns = runGhJSON([ - "run", - "list", - "--workflow", - "release.yml", - "--branch", + const release = runGhJSON([ + "release", + "view", versionNumber, "--json", - "databaseId,number", + "id,name,assets", ]); - if (workflowRuns.length !== 1) { - throw new Error( - `Expected exactly one workflow run for ${versionNumber}, got ${workflowRuns.length}`, + const sourcemapAsset = release.assets.find( + (asset) => asset.name === `vscode-codeql-sourcemaps-${versionNumber}.zip`, + ); + + if (sourcemapAsset) { + // This downloads a ZIP file of the source maps + runGh([ + "release", + "download", + versionNumber, + "--pattern", + sourcemapAsset.name, + "--dir", + releaseAssetsDirectory, + ]); + + const file = await Open.file( + resolve(releaseAssetsDirectory, sourcemapAsset.name), ); + await file.extract({ path: sourceMapsDirectory }); + } else { + const workflowRuns = runGhJSON([ + "run", + "list", + "--workflow", + "release.yml", + "--branch", + versionNumber, + "--json", + "databaseId,number", + ]); + + if (workflowRuns.length !== 1) { + throw new Error( + `Expected exactly one workflow run for ${versionNumber}, got ${workflowRuns.length}`, + ); + } + + const workflowRun = workflowRuns[0]; + + runGh([ + "run", + "download", + workflowRun.databaseId.toString(), + "--name", + "vscode-codeql-sourcemaps", + "--dir", + sourceMapsDirectory, + ]); } - - const workflowRun = workflowRuns[0]; - - runGh([ - "run", - "download", - workflowRun.databaseId.toString(), - "--name", - "vscode-codeql-sourcemaps", - "--dir", - sourceMapsDirectory, - ]); } const [filename, line, column] = filenameAndLine.split(":", 3); @@ -119,6 +156,17 @@ function runGhJSON(args: readonly string[]): T { return JSON.parse(runGh(args)); } +type ReleaseAsset = { + id: string; + name: string; +}; + +type Release = { + id: string; + name: string; + assets: ReleaseAsset[]; +}; + type WorkflowRunListItem = { databaseId: number; number: number;