Skip to content

Commit 4027b1d

Browse files
authored
Merge pull request #3915 from github/koesie10/simplify-bump-cli
Simplify usage of the bump CLI workflow
2 parents 588441d + 7f35a4b commit 4027b1d

File tree

3 files changed

+112
-39
lines changed

3 files changed

+112
-39
lines changed

.github/workflows/bump-cli.yml

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
name: Bump CLI version
22
on:
33
workflow_dispatch:
4-
inputs:
5-
option:
6-
description: "Option"
7-
required: true
8-
default: 'replace'
9-
type: choice
10-
options:
11-
- prepend
12-
- replace
13-
version:
14-
description: |
15-
The version to prepend to the supported versions file. This should be in the form: `vA.B.C`.
16-
required: false
17-
type: string
184
pull_request:
195
branches: [main]
206
paths:
217
- .github/actions/create-pr/action.yml
228
- .github/workflows/bump-cli.yml
9+
- extensions/ql-vscode/scripts/bump-supported-cli-versions.ts
2310
schedule:
2411
- cron: 0 0 */14 * * # run every 14 days
2512

@@ -34,28 +21,31 @@ jobs:
3421
steps:
3522
- name: Checkout
3623
uses: actions/checkout@v4
24+
- uses: actions/setup-node@v4
3725
with:
38-
fetch-depth: 1
26+
node-version-file: extensions/ql-vscode/.nvmrc
27+
cache: 'npm'
28+
cache-dependency-path: extensions/ql-vscode/package-lock.json
29+
- name: Install dependencies
30+
working-directory: extensions/ql-vscode
31+
run: |
32+
npm ci
33+
shell: bash
3934
- name: Bump CLI
40-
if: ${{ inputs.option == 'replace' }}
35+
working-directory: extensions/ql-vscode
36+
id: bump-cli
4137
env:
4238
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4339
run: |
44-
scripts/replace-cli-version.sh
45-
- name: Prepend another version
46-
if: ${{ inputs.option == 'prepend' }}
47-
run: |
48-
cat extensions/ql-vscode/supported_cli_versions.json | jq '. |= ["${{ inputs.version }}"] + .' > supported_cli_versions_temp.json
49-
mv supported_cli_versions_temp.json extensions/ql-vscode/supported_cli_versions.json
50-
echo "LATEST_VERSION=${{ inputs.version }}" >> $GITHUB_ENV
51-
echo "PREVIOUS_VERSION=`jq -r '.[1]' extensions/ql-vscode/supported_cli_versions.json`" >> $GITHUB_ENV
40+
npx vite-node scripts/bump-supported-cli-versions.ts
41+
shell: bash
5242
- name: Commit, Push and Open a PR
5343
uses: ./.github/actions/create-pr
5444
with:
5545
token: ${{ secrets.GITHUB_TOKEN }}
5646
base-branch: main
5747
head-branch: github-action/bump-cli
58-
commit-message: Bump CLI version from ${{ env.PREVIOUS_VERSION }} to ${{ env.LATEST_VERSION }} for integration tests
59-
title: Bump CLI Version to ${{ env.LATEST_VERSION }} for integration tests
48+
commit-message: Bump CLI version from ${{ steps.bump-cli.outputs.PREVIOUS_VERSION }} to ${{ steps.bump-cli.outputs.LATEST_VERSION }} for integration tests
49+
title: Bump CLI Version to ${{ steps.bump-cli.outputs.LATEST_VERSION }} for integration tests
6050
body: >
61-
Bumps CLI version from ${{ env.PREVIOUS_VERSION }} to ${{ env.LATEST_VERSION }}
51+
Bumps CLI version from ${{ steps.bump-cli.outputs.PREVIOUS_VERSION }} to ${{ steps.bump-cli.outputs.LATEST_VERSION }}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
};

scripts/replace-cli-version.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)