From 7f35a4b7ed751bb3efff1c526cb72bf9fff4e94d Mon Sep 17 00:00:00 2001 From: Koen Vlaswinkel Date: Thu, 23 Jan 2025 16:41:52 +0100 Subject: [PATCH] Simplify usage of the bump CLI workflow --- .github/workflows/bump-cli.yml | 44 ++++----- .../scripts/bump-supported-cli-versions.ts | 95 +++++++++++++++++++ scripts/replace-cli-version.sh | 12 --- 3 files changed, 112 insertions(+), 39 deletions(-) create mode 100644 extensions/ql-vscode/scripts/bump-supported-cli-versions.ts delete mode 100755 scripts/replace-cli-version.sh diff --git a/.github/workflows/bump-cli.yml b/.github/workflows/bump-cli.yml index e5bc038de1b..4d74fd59cf1 100644 --- a/.github/workflows/bump-cli.yml +++ b/.github/workflows/bump-cli.yml @@ -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 @@ -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 }} diff --git a/extensions/ql-vscode/scripts/bump-supported-cli-versions.ts b/extensions/ql-vscode/scripts/bump-supported-cli-versions.ts new file mode 100644 index 00000000000..12985cf4bdb --- /dev/null +++ b/extensions/ql-vscode/scripts/bump-supported-cli-versions.ts @@ -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", + "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(args: readonly string[]): T { + return JSON.parse(runGh(args)); +} + +type Release = { + id: string; + name: string; +}; diff --git a/scripts/replace-cli-version.sh b/scripts/replace-cli-version.sh deleted file mode 100755 index 03db7fec03c..00000000000 --- a/scripts/replace-cli-version.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -VERSIONS=$(gh api -H "Accept: application/vnd.github+json" /repos/github/codeql-cli-binaries/releases | jq -r '.[].tag_name' | head -2) - -# we are exporting these variables so that we can access these variables in the workflow -LATEST_VERSION=$(echo $VERSIONS | awk '{ print $1 }') -PREVIOUS_VERSION=$(echo $VERSIONS | awk '{ print $2 }') - -echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV -echo "PREVIOUS_VERSION=$PREVIOUS_VERSION" >> $GITHUB_ENV - -sed -i "s/$PREVIOUS_VERSION/$LATEST_VERSION/g" extensions/ql-vscode/supported_cli_versions.json