Skip to content

Commit e8f5d72

Browse files
henrymercerCopilot
andcommitted
Read the CLI version of a release from its assets
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 4a4922e commit e8f5d72

3 files changed

Lines changed: 116 additions & 36 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 37 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/codeql-release.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
BundleSelectionOptions,
66
getPublicRelease,
77
getRelease,
8+
getReleaseCliVersion,
89
parseCodeQLReleaseUrl,
910
selectBundle,
1011
} from "./codeql-release";
@@ -290,3 +291,32 @@ test("parseCodeQLReleaseUrl excludes archives, REST references and untrusted URL
290291
);
291292
}
292293
});
294+
295+
test("getRelease returns the names of the release's assets", async (t) => {
296+
const assetNames = [COMBINED, "cli-version-2.27.2.txt"];
297+
const fixture = releaseFixture({ assetNames });
298+
const release = await getRelease(fixture.state, REFERENCE);
299+
t.deepEqual(release.assetNames, assetNames);
300+
});
301+
302+
test("getReleaseCliVersion prefers an unambiguous marker asset to the tag", (t) => {
303+
for (const [tagName, markers, cliVersion] of [
304+
[TAG, [], "2.27.1"],
305+
[TAG, ["invalid"], "2.27.1"],
306+
[TAG, ["2.27.2"], "2.27.2"],
307+
[TAG, ["2.27.1", "2.28.0"], undefined],
308+
["codeql-bundle-20260101", [], undefined],
309+
["run-123", [], undefined],
310+
["run-123", ["2.27.2+202601011200"], "2.27.2+202601011200"],
311+
] as const) {
312+
t.is(
313+
getReleaseCliVersion(
314+
tagName,
315+
[COMBINED, ...markers.map((version) => `cli-version-${version}.txt`)],
316+
getRecordingLogger([], { logToConsole: false }),
317+
),
318+
cliVersion,
319+
`${tagName} ${markers.join(",")}`,
320+
);
321+
}
322+
});

‎src/codeql-release.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ActionState } from "./action-common";
44
import type { GitHubApiDetails } from "./api-client";
55
import { CodeQLBundle, getCodeQLBundleName } from "./codeql-bundle";
66
import { CODEQL_VERSION_ZSTD_BUNDLE } from "./feature-flags";
7+
import { Logger } from "./logging";
78
import {
89
getPerLanguageBundleLanguage,
910
logMissingPerLanguageBundle,
@@ -26,6 +27,8 @@ export interface CodeQLRelease {
2627
url: string;
2728
/** Returns the download URL for an asset, or `undefined` if the release doesn't have it. */
2829
getAssetURL(name: string): string | undefined;
30+
/** Names of the release's assets, if we looked up the release. */
31+
assetNames?: string[];
2932
}
3033

3134
/** A release requested by URL, on this GitHub instance or on GitHub.com. */
@@ -86,6 +89,51 @@ export function parseCodeQLReleaseUrl(
8689
};
8790
}
8891

92+
function parseCliVersion(value: string): string | undefined {
93+
const parsed = semver.parse(value);
94+
if (parsed === null) {
95+
return undefined;
96+
}
97+
// semver's normalized version omits build metadata, which distinguishes nightly builds.
98+
return (
99+
parsed.version + (parsed.build.length ? `+${parsed.build.join(".")}` : "")
100+
);
101+
}
102+
103+
/**
104+
* Returns the CLI version from the release's `cli-version-<version>.txt` asset if it has one, and
105+
* otherwise the version in the tag. Returns `undefined` if the release has conflicting markers.
106+
*/
107+
export function getReleaseCliVersion(
108+
tagName: string,
109+
assetNames: string[],
110+
logger: Logger,
111+
): string | undefined {
112+
const versions = new Set<string>();
113+
for (const name of assetNames) {
114+
const match = name.match(/^cli-version-(.+)\.txt$/);
115+
if (match === null) {
116+
continue;
117+
}
118+
const version = parseCliVersion(match[1]);
119+
if (version !== undefined) {
120+
versions.add(version);
121+
} else {
122+
logger.debug(`Ignoring invalid CLI version marker ${name}.`);
123+
}
124+
}
125+
if (versions.size > 1) {
126+
logger.warning(
127+
`Release ${tagName} has conflicting CLI version markers. Using a combined CodeQL bundle.`,
128+
);
129+
return undefined;
130+
}
131+
if (versions.size === 1) {
132+
return versions.values().next().value;
133+
}
134+
return parseCliVersion(tagName.replace(/^codeql-bundle-/, ""));
135+
}
136+
89137
/**
90138
* Encodes a tag for use in a URL path. Slashes stay as path separators, as in GitHub's release URLs
91139
* for tags like `build/123`.
@@ -121,6 +169,7 @@ export async function getRelease(
121169
url: getReleasePageURL(reference),
122170
getAssetURL: (name) =>
123171
release.assets.find((asset) => asset.name === name)?.url,
172+
assetNames: release.assets.map((asset) => asset.name),
124173
};
125174
}
126175

0 commit comments

Comments
 (0)