Skip to content

Commit c71238c

Browse files
authored
Merge pull request #2052 from github/koesie10/source-map-script-from-release-asset
Download sourcemaps from release asset if available
2 parents 202c93b + b03efa4 commit c71238c

1 file changed

Lines changed: 70 additions & 22 deletions

File tree

extensions/ql-vscode/scripts/source-map.ts

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { spawnSync } from "child_process";
2020
import { basename, resolve } from "path";
2121
import { pathExists, readJSON } from "fs-extra";
2222
import { RawSourceMap, SourceMapConsumer } from "source-map";
23+
import { Open } from "unzipper";
2324

2425
if (process.argv.length !== 4) {
2526
console.error(
@@ -36,6 +37,12 @@ const versionNumber = process.argv[2].startsWith("v")
3637
const stacktrace = process.argv[3];
3738

3839
async function extractSourceMap() {
40+
const releaseAssetsDirectory = resolve(
41+
__dirname,
42+
"..",
43+
"release-assets",
44+
versionNumber,
45+
);
3946
const sourceMapsDirectory = resolve(
4047
__dirname,
4148
"..",
@@ -47,34 +54,64 @@ async function extractSourceMap() {
4754
if (!(await pathExists(sourceMapsDirectory))) {
4855
console.log("Downloading source maps...");
4956

50-
const workflowRuns = runGhJSON<WorkflowRunListItem[]>([
51-
"run",
52-
"list",
53-
"--workflow",
54-
"release.yml",
55-
"--branch",
57+
const release = runGhJSON<Release>([
58+
"release",
59+
"view",
5660
versionNumber,
5761
"--json",
58-
"databaseId,number",
62+
"id,name,assets",
5963
]);
6064

61-
if (workflowRuns.length !== 1) {
62-
throw new Error(
63-
`Expected exactly one workflow run for ${versionNumber}, got ${workflowRuns.length}`,
65+
const sourcemapAsset = release.assets.find(
66+
(asset) => asset.name === `vscode-codeql-sourcemaps-${versionNumber}.zip`,
67+
);
68+
69+
if (sourcemapAsset) {
70+
// This downloads a ZIP file of the source maps
71+
runGh([
72+
"release",
73+
"download",
74+
versionNumber,
75+
"--pattern",
76+
sourcemapAsset.name,
77+
"--dir",
78+
releaseAssetsDirectory,
79+
]);
80+
81+
const file = await Open.file(
82+
resolve(releaseAssetsDirectory, sourcemapAsset.name),
6483
);
84+
await file.extract({ path: sourceMapsDirectory });
85+
} else {
86+
const workflowRuns = runGhJSON<WorkflowRunListItem[]>([
87+
"run",
88+
"list",
89+
"--workflow",
90+
"release.yml",
91+
"--branch",
92+
versionNumber,
93+
"--json",
94+
"databaseId,number",
95+
]);
96+
97+
if (workflowRuns.length !== 1) {
98+
throw new Error(
99+
`Expected exactly one workflow run for ${versionNumber}, got ${workflowRuns.length}`,
100+
);
101+
}
102+
103+
const workflowRun = workflowRuns[0];
104+
105+
runGh([
106+
"run",
107+
"download",
108+
workflowRun.databaseId.toString(),
109+
"--name",
110+
"vscode-codeql-sourcemaps",
111+
"--dir",
112+
sourceMapsDirectory,
113+
]);
65114
}
66-
67-
const workflowRun = workflowRuns[0];
68-
69-
runGh([
70-
"run",
71-
"download",
72-
workflowRun.databaseId.toString(),
73-
"--name",
74-
"vscode-codeql-sourcemaps",
75-
"--dir",
76-
sourceMapsDirectory,
77-
]);
78115
}
79116

80117
if (stacktrace.includes("at")) {
@@ -172,6 +209,17 @@ function runGhJSON<T>(args: readonly string[]): T {
172209
return JSON.parse(runGh(args));
173210
}
174211

212+
type ReleaseAsset = {
213+
id: string;
214+
name: string;
215+
};
216+
217+
type Release = {
218+
id: string;
219+
name: string;
220+
assets: ReleaseAsset[];
221+
};
222+
175223
type WorkflowRunListItem = {
176224
databaseId: number;
177225
number: number;

0 commit comments

Comments
 (0)