|
| 1 | +import { spawnSync } from "child_process"; |
| 2 | +import { resolve } from "path"; |
| 3 | +import { appendFile, outputJson, readJson } from "fs-extra"; |
| 4 | +import { SemVer } from "semver"; |
| 5 | + |
| 6 | +const supportedCliVersionsPath = resolve( |
| 7 | + __dirname, |
| 8 | + "..", |
| 9 | + "supported_cli_versions.json", |
| 10 | +); |
| 11 | + |
| 12 | +async function bumpSupportedCliVersions() { |
| 13 | + const existingVersions = (await readJson( |
| 14 | + supportedCliVersionsPath, |
| 15 | + )) as string[]; |
| 16 | + |
| 17 | + const release = runGhJSON<Release>([ |
| 18 | + "release", |
| 19 | + "view", |
| 20 | + "--json", |
| 21 | + "id,name", |
| 22 | + "--repo", |
| 23 | + "github/codeql-cli-binaries", |
| 24 | + ]); |
| 25 | + |
| 26 | + // There are two cases: |
| 27 | + // - Replace the version if it's the same major and minor version |
| 28 | + // - Prepend the version if it's a new major or minor version |
| 29 | + |
| 30 | + const latestSupportedVersion = new SemVer(existingVersions[0]); |
| 31 | + const latestReleaseVersion = new SemVer(release.name); |
| 32 | + |
| 33 | + if (latestSupportedVersion.compare(latestReleaseVersion) === 0) { |
| 34 | + console.log("No need to update supported CLI versions"); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + if (process.env.GITHUB_OUTPUT) { |
| 39 | + await appendFile( |
| 40 | + process.env.GITHUB_OUTPUT, |
| 41 | + `PREVIOUS_VERSION=${existingVersions[0]}\n`, |
| 42 | + { |
| 43 | + encoding: "utf-8", |
| 44 | + }, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + if ( |
| 49 | + latestSupportedVersion.major === latestReleaseVersion.major && |
| 50 | + latestSupportedVersion.minor === latestReleaseVersion.minor |
| 51 | + ) { |
| 52 | + existingVersions[0] = release.name; |
| 53 | + console.log(`Replaced latest supported CLI version with ${release.name}`); |
| 54 | + } else { |
| 55 | + existingVersions.unshift(release.name); |
| 56 | + console.log(`Added latest supported CLI version ${release.name}`); |
| 57 | + } |
| 58 | + |
| 59 | + await outputJson(supportedCliVersionsPath, existingVersions, { |
| 60 | + spaces: 2, |
| 61 | + finalEOL: true, |
| 62 | + }); |
| 63 | + |
| 64 | + if (process.env.GITHUB_OUTPUT) { |
| 65 | + await appendFile( |
| 66 | + process.env.GITHUB_OUTPUT, |
| 67 | + `LATEST_VERSION=${existingVersions[0]}\n`, |
| 68 | + { |
| 69 | + encoding: "utf-8", |
| 70 | + }, |
| 71 | + ); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +bumpSupportedCliVersions().catch((e: unknown) => { |
| 76 | + console.error(e); |
| 77 | + process.exit(2); |
| 78 | +}); |
| 79 | + |
| 80 | +function runGh(args: readonly string[]): string { |
| 81 | + const gh = spawnSync("gh", args); |
| 82 | + if (gh.status !== 0) { |
| 83 | + throw new Error(`Failed to run gh ${args.join(" ")}: ${gh.stderr}`); |
| 84 | + } |
| 85 | + return gh.stdout.toString("utf-8"); |
| 86 | +} |
| 87 | + |
| 88 | +function runGhJSON<T>(args: readonly string[]): T { |
| 89 | + return JSON.parse(runGh(args)); |
| 90 | +} |
| 91 | + |
| 92 | +type Release = { |
| 93 | + id: string; |
| 94 | + name: string; |
| 95 | +}; |
0 commit comments