-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcodeql-setup.ts
More file actions
167 lines (145 loc) · 4.44 KB
/
codeql-setup.ts
File metadata and controls
167 lines (145 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { OutgoingHttpHeaders } from "http";
import { join } from "path";
import { performance } from "perf_hooks";
import { debug, info } from "@actions/core";
import { rmRF } from "@actions/io";
import {
cacheDir,
downloadTool,
extractTar,
find as findInToolCache,
} from "@actions/tool-cache";
import { v4 as uuidV4 } from "uuid";
import { assertNever } from "./util";
type CodeQLToolsSource =
| {
codeqlFolder: string;
sourceType: "toolcache";
}
| {
codeqlURL: string;
sourceType: "download";
};
function getCodeQLBundleName(): string {
let platform: string;
if (process.platform === "win32") {
platform = "win64";
} else if (process.platform === "linux") {
platform = "linux64";
} else if (process.platform === "darwin") {
platform = "osx64";
} else {
return "codeql-bundle.tar.gz";
}
return `codeql-bundle-${platform}.tar.gz`;
}
/**
* Returns the path to the CodeQL bundle after finding or downloading it.
*
* @param tempDir A temporary directory to download the bundle to.
* @param cliVersion The version of the CLI to use.
*/
export async function setupCodeQLBundle(
tempDir: string,
cliVersion: string,
): Promise<string> {
const source = getCodeQLSource(cliVersion);
let codeqlFolder: string;
switch (source.sourceType) {
case "toolcache":
codeqlFolder = source.codeqlFolder;
debug(`CodeQL found in cache ${codeqlFolder}`);
break;
case "download": {
codeqlFolder = await downloadCodeQL(
cliVersion,
source.codeqlURL,
tempDir,
);
break;
}
default:
assertNever(source);
}
return codeqlFolder;
}
/**
* Determine where to find the CodeQL tools. This will check the tool cache
* first, and if the tools are not found there, it will provide a download
* URL for the tools.
*
* @param cliVersion The CLI version of the CodeQL bundle to find
*/
function getCodeQLSource(cliVersion: string): CodeQLToolsSource {
// If we find the specified CLI version, we always use that.
const codeqlFolder = findInToolCache("CodeQL", cliVersion);
if (codeqlFolder) {
info(`Found CodeQL tools version ${cliVersion} in the toolcache.`);
return {
codeqlFolder,
sourceType: "toolcache",
};
}
info(`Did not find CodeQL tools version ${cliVersion} in the toolcache.`);
/** Tag name of the CodeQL bundle, for example `codeql-bundle-v2.17.1`. */
const tagName = `codeql-bundle-v${cliVersion}`;
const url = `https://github.com/github/codeql-action/releases/download/${tagName}/${getCodeQLBundleName()}`;
return {
codeqlURL: url,
sourceType: "download",
};
}
/**
* @param cliVersion The CLI version of the CodeQL bundle to download
* @param codeqlURL The URL to download the CodeQL bundle from
* @param tempDir The temporary directory to download the CodeQL bundle to
* @return the path to the downloaded CodeQL tools folder
*/
async function downloadCodeQL(
cliVersion: string,
codeqlURL: string,
tempDir: string,
): Promise<string> {
const headers: OutgoingHttpHeaders = {
accept: "application/octet-stream",
};
info(`Downloading CodeQL tools from ${codeqlURL} . This may take a while.`);
const dest = join(tempDir, uuidV4());
const finalHeaders = Object.assign(
// eslint-disable-next-line @typescript-eslint/naming-convention
{ "User-Agent": "CodeQL Variant Analysis Action" },
headers,
);
const toolsDownloadStart = performance.now();
const archivedBundlePath = await downloadTool(
codeqlURL,
dest,
undefined,
finalHeaders,
);
const toolsDownloadDurationMs = Math.round(
performance.now() - toolsDownloadStart,
);
debug(
`Finished downloading CodeQL bundle to ${archivedBundlePath} (${toolsDownloadDurationMs} ms).`,
);
debug("Extracting CodeQL bundle.");
const extractionStart = performance.now();
const extractedBundlePath = await extractTar(archivedBundlePath);
const extractionMs = Math.round(performance.now() - extractionStart);
debug(
`Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionMs} ms).`,
);
await rmRF(archivedBundlePath);
debug("Caching CodeQL bundle.");
const toolcachedBundlePath = await cacheDir(
extractedBundlePath,
"CodeQL",
cliVersion,
);
// Defensive check: we expect `cacheDir` to copy the bundle to a new location.
if (toolcachedBundlePath !== extractedBundlePath) {
await rmRF(extractedBundlePath);
}
return toolcachedBundlePath;
}