Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify usage of the bump CLI workflow #3915

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions .github/workflows/bump-cli.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
name: Bump CLI version
on:
workflow_dispatch:
inputs:
option:
description: "Option"
required: true
default: 'replace'
type: choice
options:
- prepend
- replace
version:
description: |
The version to prepend to the supported versions file. This should be in the form: `vA.B.C`.
required: false
type: string
pull_request:
branches: [main]
paths:
- .github/actions/create-pr/action.yml
- .github/workflows/bump-cli.yml
- extensions/ql-vscode/scripts/bump-supported-cli-versions.ts
schedule:
- cron: 0 0 */14 * * # run every 14 days

Expand All @@ -34,28 +21,31 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
fetch-depth: 1
node-version-file: extensions/ql-vscode/.nvmrc
cache: 'npm'
cache-dependency-path: extensions/ql-vscode/package-lock.json
- name: Install dependencies
working-directory: extensions/ql-vscode
run: |
npm ci
shell: bash
- name: Bump CLI
if: ${{ inputs.option == 'replace' }}
working-directory: extensions/ql-vscode
id: bump-cli
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
scripts/replace-cli-version.sh
- name: Prepend another version
if: ${{ inputs.option == 'prepend' }}
run: |
cat extensions/ql-vscode/supported_cli_versions.json | jq '. |= ["${{ inputs.version }}"] + .' > supported_cli_versions_temp.json
mv supported_cli_versions_temp.json extensions/ql-vscode/supported_cli_versions.json
echo "LATEST_VERSION=${{ inputs.version }}" >> $GITHUB_ENV
echo "PREVIOUS_VERSION=`jq -r '.[1]' extensions/ql-vscode/supported_cli_versions.json`" >> $GITHUB_ENV
npx vite-node scripts/bump-supported-cli-versions.ts
shell: bash
- name: Commit, Push and Open a PR
uses: ./.github/actions/create-pr
with:
token: ${{ secrets.GITHUB_TOKEN }}
base-branch: main
head-branch: github-action/bump-cli
commit-message: Bump CLI version from ${{ env.PREVIOUS_VERSION }} to ${{ env.LATEST_VERSION }} for integration tests
title: Bump CLI Version to ${{ env.LATEST_VERSION }} for integration tests
commit-message: Bump CLI version from ${{ steps.bump-cli.outputs.PREVIOUS_VERSION }} to ${{ steps.bump-cli.outputs.LATEST_VERSION }} for integration tests
title: Bump CLI Version to ${{ steps.bump-cli.outputs.LATEST_VERSION }} for integration tests
body: >
Bumps CLI version from ${{ env.PREVIOUS_VERSION }} to ${{ env.LATEST_VERSION }}
Bumps CLI version from ${{ steps.bump-cli.outputs.PREVIOUS_VERSION }} to ${{ steps.bump-cli.outputs.LATEST_VERSION }}
95 changes: 95 additions & 0 deletions extensions/ql-vscode/scripts/bump-supported-cli-versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { spawnSync } from "child_process";
import { resolve } from "path";
import { appendFile, outputJson, readJson } from "fs-extra";
import { SemVer } from "semver";

const supportedCliVersionsPath = resolve(
__dirname,
"..",
"supported_cli_versions.json",
);

async function bumpSupportedCliVersions() {
const existingVersions = (await readJson(
supportedCliVersionsPath,
)) as string[];

const release = runGhJSON<Release>([
"release",
"view",
"--json",
"id,name",
"--repo",
"github/codeql-cli-binaries",
]);

// There are two cases:
// - Replace the version if it's the same major and minor version
// - Prepend the version if it's a new major or minor version

const latestSupportedVersion = new SemVer(existingVersions[0]);
const latestReleaseVersion = new SemVer(release.name);

if (latestSupportedVersion.compare(latestReleaseVersion) === 0) {
console.log("No need to update supported CLI versions");
return;
}

if (process.env.GITHUB_OUTPUT) {
await appendFile(
process.env.GITHUB_OUTPUT,
`PREVIOUS_VERSION=${existingVersions[0]}\n`,
{
encoding: "utf-8",
},
);
}

if (
latestSupportedVersion.major === latestReleaseVersion.major &&
latestSupportedVersion.minor === latestReleaseVersion.minor
) {
existingVersions[0] = release.name;
console.log(`Replaced latest supported CLI version with ${release.name}`);
} else {
existingVersions.unshift(release.name);
console.log(`Added latest supported CLI version ${release.name}`);
}

await outputJson(supportedCliVersionsPath, existingVersions, {
spaces: 2,
finalEOL: true,
});

if (process.env.GITHUB_OUTPUT) {
await appendFile(
process.env.GITHUB_OUTPUT,
`LATEST_VERSION=${existingVersions[0]}\n`,
{
encoding: "utf-8",
},
);
}
}

bumpSupportedCliVersions().catch((e: unknown) => {
console.error(e);
process.exit(2);
});

function runGh(args: readonly string[]): string {
const gh = spawnSync("gh", args);
if (gh.status !== 0) {
throw new Error(`Failed to run gh ${args.join(" ")}: ${gh.stderr}`);
}
return gh.stdout.toString("utf-8");
}

function runGhJSON<T>(args: readonly string[]): T {
return JSON.parse(runGh(args));
}

type Release = {
id: string;
name: string;
};
12 changes: 0 additions & 12 deletions scripts/replace-cli-version.sh

This file was deleted.

Loading