|
| 1 | +import * as github from "@actions/github"; |
| 2 | +import test from "ava"; |
| 3 | + |
| 4 | +import { |
| 5 | + BundleSelectionOptions, |
| 6 | + getPublicRelease, |
| 7 | + getRelease, |
| 8 | + selectBundle, |
| 9 | +} from "./codeql-release"; |
| 10 | +import { ActionsEnvVars } from "./environment"; |
| 11 | +import { Feature } from "./feature-flags"; |
| 12 | +import { BuiltInLanguage } from "./languages"; |
| 13 | +import { BundlePlatform } from "./platform"; |
| 14 | +import { |
| 15 | + createFeatures, |
| 16 | + getRecordingLogger, |
| 17 | + getTestEnv, |
| 18 | + initAllState, |
| 19 | + LoggedMessage, |
| 20 | +} from "./testing-utils"; |
| 21 | +import { ConfigurationError, GitHubVariant } from "./util"; |
| 22 | + |
| 23 | +const TAG = "codeql-bundle-v2.27.1"; |
| 24 | +const REFERENCE = { |
| 25 | + serverURL: "https://github.com", |
| 26 | + owner: "octo", |
| 27 | + repo: "tools", |
| 28 | + tagName: TAG, |
| 29 | +}; |
| 30 | +const RELEASE_PAGE = `https://github.com/octo/tools/releases/tag/${TAG}`; |
| 31 | +const API_URL = "https://api.github.com"; |
| 32 | +const COMBINED = "codeql-bundle-linux64.tar.zst"; |
| 33 | +const JAVA = "codeql-bundle-java-linux64.tar.zst"; |
| 34 | + |
| 35 | +const OPTIONS: BundleSelectionOptions = { |
| 36 | + rawLanguages: ["java-kotlin"], |
| 37 | + cliVersion: "2.27.1", |
| 38 | + platform: BundlePlatform.Linux64, |
| 39 | + variant: GitHubVariant.DOTCOM, |
| 40 | + tarSupportsZstd: true, |
| 41 | +}; |
| 42 | + |
| 43 | +/** Serves the release tagged `TAG` with the given assets from a stubbed API. */ |
| 44 | +function releaseFixture({ assetNames = [COMBINED, JAVA], status = 200 } = {}) { |
| 45 | + const apiBase = `${API_URL}/repos/octo/tools/releases`; |
| 46 | + const releaseAPIURL = `${apiBase}/tags/${TAG}`; |
| 47 | + const assets = assetNames.map((name, index) => ({ |
| 48 | + name, |
| 49 | + url: `${apiBase}/assets/${1000 + index}`, |
| 50 | + })); |
| 51 | + const requests: string[] = []; |
| 52 | + const messages: LoggedMessage[] = []; |
| 53 | + const state = initAllState({ |
| 54 | + env: getTestEnv({ [ActionsEnvVars.RUNNER_ENVIRONMENT]: "github-hosted" }), |
| 55 | + logger: getRecordingLogger(messages, { logToConsole: false }), |
| 56 | + features: createFeatures([Feature.PerLanguageBundles]), |
| 57 | + apiClient: github.getOctokit("123", { |
| 58 | + baseUrl: API_URL, |
| 59 | + request: { |
| 60 | + fetch: async (url) => { |
| 61 | + requests.push(String(url)); |
| 62 | + if (String(url) !== releaseAPIURL) { |
| 63 | + throw new Error(`Unexpected API request: ${url}`); |
| 64 | + } |
| 65 | + return new Response(JSON.stringify({ tag_name: TAG, assets }), { |
| 66 | + status, |
| 67 | + headers: { "content-type": "application/json" }, |
| 68 | + }); |
| 69 | + }, |
| 70 | + }, |
| 71 | + }), |
| 72 | + }); |
| 73 | + return { |
| 74 | + assets, |
| 75 | + messages, |
| 76 | + releaseAPIURL, |
| 77 | + requests, |
| 78 | + state, |
| 79 | + select: async (options: Partial<BundleSelectionOptions> = {}) => |
| 80 | + selectBundle(state, await getRelease(state, REFERENCE), { |
| 81 | + ...OPTIONS, |
| 82 | + ...options, |
| 83 | + }), |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +test("selectBundle selects an eligible per-language bundle with a single release lookup", async (t) => { |
| 88 | + const fixture = releaseFixture(); |
| 89 | + t.deepEqual(await fixture.select(), { |
| 90 | + bundle: { |
| 91 | + kind: "per-language", |
| 92 | + url: fixture.assets[1].url, |
| 93 | + language: BuiltInLanguage.java, |
| 94 | + combinedBundleURL: fixture.assets[0].url, |
| 95 | + }, |
| 96 | + compressionMethod: "zstd", |
| 97 | + }); |
| 98 | + t.deepEqual(fixture.requests, [fixture.releaseAPIURL]); |
| 99 | +}); |
| 100 | + |
| 101 | +test("selectBundle falls back to the combined bundle from the same release", async (t) => { |
| 102 | + const fixture = releaseFixture({ assetNames: [COMBINED] }); |
| 103 | + t.deepEqual(await fixture.select(), { |
| 104 | + bundle: { kind: "combined", url: fixture.assets[0].url }, |
| 105 | + compressionMethod: "zstd", |
| 106 | + perLanguageBundleFallback: true, |
| 107 | + }); |
| 108 | + t.true( |
| 109 | + fixture.messages.some( |
| 110 | + (message) => |
| 111 | + message.type === "warning" && |
| 112 | + typeof message.message === "string" && |
| 113 | + message.message.includes(`'java' at ${RELEASE_PAGE}`), |
| 114 | + ), |
| 115 | + ); |
| 116 | +}); |
| 117 | + |
| 118 | +test("selectBundle selects the combined bundle for jobs that aren't eligible for a per-language bundle", async (t) => { |
| 119 | + const fixture = releaseFixture(); |
| 120 | + t.deepEqual(await fixture.select({ rawLanguages: ["java", "python"] }), { |
| 121 | + bundle: { kind: "combined", url: fixture.assets[0].url }, |
| 122 | + compressionMethod: "zstd", |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +test("selectBundle uses the other compression method when the preferred one is missing", async (t) => { |
| 127 | + const gzipOnly = releaseFixture({ |
| 128 | + assetNames: ["codeql-bundle-linux64.tar.gz", JAVA], |
| 129 | + }); |
| 130 | + t.deepEqual(await gzipOnly.select(), { |
| 131 | + bundle: { kind: "combined", url: gzipOnly.assets[0].url }, |
| 132 | + compressionMethod: "gzip", |
| 133 | + }); |
| 134 | + |
| 135 | + for (const [platform, options] of [ |
| 136 | + [BundlePlatform.Win64, {}], |
| 137 | + [BundlePlatform.Linux64, { cliVersion: "2.18.4" }], |
| 138 | + [BundlePlatform.Linux64, { cliVersion: undefined }], |
| 139 | + [BundlePlatform.Linux64, { tarSupportsZstd: false }], |
| 140 | + ] as const) { |
| 141 | + const selection = await releaseFixture({ |
| 142 | + assetNames: [ |
| 143 | + `codeql-bundle-${platform}.tar.zst`, |
| 144 | + `codeql-bundle-${platform}.tar.gz`, |
| 145 | + ], |
| 146 | + }).select({ platform, ...options }); |
| 147 | + t.is( |
| 148 | + selection.compressionMethod, |
| 149 | + "gzip", |
| 150 | + `${platform} ${JSON.stringify(options)}`, |
| 151 | + ); |
| 152 | + } |
| 153 | +}); |
| 154 | + |
| 155 | +test("selectBundle requires a combined bundle that we can extract", async (t) => { |
| 156 | + await t.throwsAsync(releaseFixture({ assetNames: [JAVA] }).select(), { |
| 157 | + instanceOf: ConfigurationError, |
| 158 | + message: `No compatible CodeQL bundle was found in release ${RELEASE_PAGE}. Expected codeql-bundle-linux64.tar.zst or codeql-bundle-linux64.tar.gz.`, |
| 159 | + }); |
| 160 | + await t.throwsAsync( |
| 161 | + releaseFixture({ assetNames: [COMBINED] }).select({ |
| 162 | + tarSupportsZstd: false, |
| 163 | + }), |
| 164 | + { instanceOf: ConfigurationError, message: /Expected [^ ]+\.tar\.gz\.$/ }, |
| 165 | + ); |
| 166 | +}); |
| 167 | + |
| 168 | +test("getRelease propagates API errors", async (t) => { |
| 169 | + const fixture = releaseFixture({ status: 404 }); |
| 170 | + t.like(await t.throwsAsync(fixture.select()), { status: 404 }); |
| 171 | + t.deepEqual(fixture.requests, [fixture.releaseAPIURL]); |
| 172 | +}); |
| 173 | + |
| 174 | +test("getPublicRelease constructs download URLs without looking up the release", async (t) => { |
| 175 | + const fixture = releaseFixture(); |
| 176 | + const release = getPublicRelease({ ...REFERENCE, tagName: "nightly/v1+2" }); |
| 177 | + const baseURL = |
| 178 | + "https://github.com/octo/tools/releases/download/nightly/v1%2B2"; |
| 179 | + t.is( |
| 180 | + release.url, |
| 181 | + "https://github.com/octo/tools/releases/tag/nightly/v1%2B2", |
| 182 | + ); |
| 183 | + t.deepEqual( |
| 184 | + await selectBundle(fixture.state, release, { |
| 185 | + ...OPTIONS, |
| 186 | + cliVersion: undefined, |
| 187 | + isLatestNightly: true, |
| 188 | + }), |
| 189 | + { |
| 190 | + bundle: { |
| 191 | + kind: "per-language", |
| 192 | + url: `${baseURL}/${JAVA}`, |
| 193 | + language: BuiltInLanguage.java, |
| 194 | + combinedBundleURL: `${baseURL}/${COMBINED}`, |
| 195 | + }, |
| 196 | + compressionMethod: "zstd", |
| 197 | + }, |
| 198 | + ); |
| 199 | + t.deepEqual(fixture.requests, []); |
| 200 | +}); |
0 commit comments