From d3509fb9f74822a38aa8dc033a482db47f32eddc Mon Sep 17 00:00:00 2001 From: "F. Levi" <55688616+flevi29@users.noreply.github.com> Date: Thu, 6 Feb 2025 16:18:14 +0200 Subject: [PATCH 1/3] Added initial version of script --- scripts/update-version.js | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 scripts/update-version.js diff --git a/scripts/update-version.js b/scripts/update-version.js new file mode 100644 index 000000000..6b435b5a4 --- /dev/null +++ b/scripts/update-version.js @@ -0,0 +1,54 @@ +import { argv, stdout } from "node:process"; +import { writeFileSync } from "node:fs"; + +if (argv.length !== 3) { + throw new Error("expected one command line argument", { + cause: argv.slice(2), + }); +} + +/** @type {{ isDraft: boolean; tagName: string }[]} */ +const arrayWithLastRelease = JSON.parse(argv[2]); + +if (!Array.isArray(arrayWithLastRelease) || arrayWithLastRelease.length !== 1) { + throw new Error("expected an array with one element", { + cause: arrayWithLastRelease, + }); +} + +const lastRelease = arrayWithLastRelease[0]; +const { isDraft, tagName } = lastRelease; + +if ( + Object.keys(lastRelease).length !== 2 || + typeof isDraft !== "boolean" || + typeof tagName !== "string" +) { + throw new Error( + 'expected an object with keys "isDraft" as boolean and "tagName" as string', + { cause: lastRelease }, + ); +} + +if (!isDraft) { + throw new Error( + "expected a draft release to be present as the first element in the releases list", + ); +} + +// TODO: tagName might be prefixed with a "v" or god knows what else + +const packageVersionFileContents = `export const PACKAGE_VERSION = "${tagName.replace('"', '\\"')}";\n`; + +const packageVersionFilePath = new URL( + "../src/package-version.ts", + import.meta.url, +); + +writeFileSync(packageVersionFilePath, packageVersionFileContents); + +stdout.write(tagName); + +// TODO: check https://docs.npmjs.com/cli/v10/commands/npm-version#sign-git-tag +// TODO: Call npm version for the output of this here script https://docs.npmjs.com/cli/v10/commands/npm-version +// `npm version --commit-hooks false`; From 4f82979fb04b5e5d117b019a87ce33f557c92cee Mon Sep 17 00:00:00 2001 From: "F. Levi" <55688616+flevi29@users.noreply.github.com> Date: Fri, 7 Feb 2025 12:15:33 +0200 Subject: [PATCH 2/3] Add all the necessary modifications and additions --- .github/actions/bump-version/action.yml | 42 +++++++++++++++++ .github/workflows/release-drafter.yml | 20 ++++++++- .github/workflows/version.yml | 17 +++++++ scripts/bump-version.js | 60 +++++++++++++++++++++++++ scripts/update-version.js | 54 ---------------------- 5 files changed, 138 insertions(+), 55 deletions(-) create mode 100644 .github/actions/bump-version/action.yml create mode 100644 .github/workflows/version.yml create mode 100644 scripts/bump-version.js delete mode 100644 scripts/update-version.js diff --git a/.github/actions/bump-version/action.yml b/.github/actions/bump-version/action.yml new file mode 100644 index 000000000..03cb236cd --- /dev/null +++ b/.github/actions/bump-version/action.yml @@ -0,0 +1,42 @@ +name: "Bump version" +inputs: + github-token: + description: GitHub token for authentication + required: true + version: + description: Optional version so action can skip checking separately +runs: + using: "composite" + steps: + - name: Get release draft version and set environment variable + if: "${{ inputs.version == '' }}" + run: | + VERSION_DETAILS=gh release -R ${{ github.repository }} ls -L 1 --json isDraft,tagName + echo "PKG_VERSION=$VERSION_DETAILS" >> "$GITHUB_ENV" + env: + GH_TOKEN: ${{ inputs.github-token }} + - name: Set environment variable to version + if: "${{ inputs.version != '' }}" + run: echo "PKG_VERSION=${{ inputs.version }}" >> "$GITHUB_ENV" + - name: Bump version of package + id: bump-version + run: | + PARSED_VERSION=node .\scripts\bump-version.js $PKG_VERSION + npm version $PARSED_VERSION --commit-hooks false --git-tag-version false + echo "PKG_VERSION=$PARSED_VERSION" >> "$GITHUB_OUTPUT" + - name: Create pull request with the changes + uses: peter-evans/create-pull-request@v7 + with: + token: ${{ inputs.github-token }} + commit-message: Update files containing the version of the package + branch: bump-version + title: Update version for the next release (${{ steps.bump-version.outputs.PKG_VERSION }}) + body: | + _This PR is auto-generated._ + + - updates package version to `"${{ steps.bump-version.outputs.PKG_VERSION }}"` + + > [!IMPORTANT] + > + > Every time the main branch is updated and release drafter bumps the version of the new release draft, this PR is auto-updated! + labels: skip-changelog diff --git a/.github/workflows/release-drafter.yml b/.github/workflows/release-drafter.yml index 20f2d83f4..26c8bd43c 100644 --- a/.github/workflows/release-drafter.yml +++ b/.github/workflows/release-drafter.yml @@ -8,9 +8,27 @@ on: jobs: update_release_draft: runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write steps: - - uses: release-drafter/release-drafter@v6 + - name: Draft release + id: draft-release + uses: release-drafter/release-drafter@v6 with: config-name: release-draft-template.yml env: GITHUB_TOKEN: ${{ secrets.RELEASE_DRAFTER_TOKEN }} + - name: Get status of version bump PR + id: pr-status + run: | + PR_NOT_OPEN=gh pr -R ${{ github.repository }} view bump-version --json closed --jq '.closed' || echo true + echo "PR_NOT_OPEN=$PR_NOT_OPEN" >> "$GITHUB_OUTPUT" + - uses: actions/checkout@v4 + if: steps.pr-status.outputs.PR_NOT_OPEN == 'false' + - name: Update PR with version bump + if: steps.pr-status.outputs.PR_NOT_OPEN == 'false' + uses: ./.github/actions/bump-version + with: + version: ${{ steps.draft-release.outputs.resolved_version }} + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/version.yml b/.github/workflows/version.yml new file mode 100644 index 000000000..6886990d6 --- /dev/null +++ b/.github/workflows/version.yml @@ -0,0 +1,17 @@ +name: TODO Name Me Properly +# TODO: Maybe we want to do this every time release drafter runs, read more: https://github.com/marketplace/actions/create-pull-request#action-behaviour +on: + workflow_dispatch: + +jobs: + update_version: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@v4 + - name: Bump version in package files and create PR + uses: ./.github/actions/bump-version + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/scripts/bump-version.js b/scripts/bump-version.js new file mode 100644 index 000000000..e84230f04 --- /dev/null +++ b/scripts/bump-version.js @@ -0,0 +1,60 @@ +import { argv, stdout } from "node:process"; +import { writeFileSync } from "node:fs"; + +if (argv.length !== 3) { + throw new Error("expected one command line argument", { + cause: argv.slice(2), + }); +} + +/** @type {string | { isDraft: boolean; tagName: string }[]} */ +const arrayWithLastReleaseOrVersion = JSON.parse(argv[2]); + +const pkgVersion = (function () { + if (typeof arrayWithLastReleaseOrVersion === "string") { + return arrayWithLastReleaseOrVersion; + } + + if ( + !Array.isArray(arrayWithLastReleaseOrVersion) || + arrayWithLastReleaseOrVersion.length !== 1 + ) { + throw new Error("expected an array with one element", { + cause: arrayWithLastReleaseOrVersion, + }); + } + + const lastRelease = arrayWithLastReleaseOrVersion[0]; + const { isDraft, tagName } = lastRelease; + + if ( + Object.keys(lastRelease).length !== 2 || + typeof isDraft !== "boolean" || + typeof tagName !== "string" + ) { + throw new Error( + 'expected an object with keys "isDraft" as boolean and "tagName" as string', + { cause: lastRelease }, + ); + } + + if (!isDraft) { + throw new Error( + "expected a draft release to be present as the first element in the releases list", + ); + } + + // remove the "v" from "vX.X.X" + return tagName.substring(1); +})(); + +const pkgVersionFileContents = `export const PACKAGE_VERSION = "${pkgVersion}";\n`; + +const pkgVersionFilePath = new URL( + "../src/package-version.ts", + import.meta.url, +); + +writeFileSync(pkgVersionFilePath, pkgVersionFileContents); + +stdout.write(pkgVersion); diff --git a/scripts/update-version.js b/scripts/update-version.js deleted file mode 100644 index 6b435b5a4..000000000 --- a/scripts/update-version.js +++ /dev/null @@ -1,54 +0,0 @@ -import { argv, stdout } from "node:process"; -import { writeFileSync } from "node:fs"; - -if (argv.length !== 3) { - throw new Error("expected one command line argument", { - cause: argv.slice(2), - }); -} - -/** @type {{ isDraft: boolean; tagName: string }[]} */ -const arrayWithLastRelease = JSON.parse(argv[2]); - -if (!Array.isArray(arrayWithLastRelease) || arrayWithLastRelease.length !== 1) { - throw new Error("expected an array with one element", { - cause: arrayWithLastRelease, - }); -} - -const lastRelease = arrayWithLastRelease[0]; -const { isDraft, tagName } = lastRelease; - -if ( - Object.keys(lastRelease).length !== 2 || - typeof isDraft !== "boolean" || - typeof tagName !== "string" -) { - throw new Error( - 'expected an object with keys "isDraft" as boolean and "tagName" as string', - { cause: lastRelease }, - ); -} - -if (!isDraft) { - throw new Error( - "expected a draft release to be present as the first element in the releases list", - ); -} - -// TODO: tagName might be prefixed with a "v" or god knows what else - -const packageVersionFileContents = `export const PACKAGE_VERSION = "${tagName.replace('"', '\\"')}";\n`; - -const packageVersionFilePath = new URL( - "../src/package-version.ts", - import.meta.url, -); - -writeFileSync(packageVersionFilePath, packageVersionFileContents); - -stdout.write(tagName); - -// TODO: check https://docs.npmjs.com/cli/v10/commands/npm-version#sign-git-tag -// TODO: Call npm version for the output of this here script https://docs.npmjs.com/cli/v10/commands/npm-version -// `npm version --commit-hooks false`; From 37ba30f95b0ac9a1ebe91ccc59c4becf91f57c8c Mon Sep 17 00:00:00 2001 From: "F. Levi" <55688616+flevi29@users.noreply.github.com> Date: Fri, 7 Feb 2025 12:18:44 +0200 Subject: [PATCH 3/3] Rename, misc --- .github/workflows/{version.yml => create-version-bump-pr.yml} | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) rename .github/workflows/{version.yml => create-version-bump-pr.yml} (66%) diff --git a/.github/workflows/version.yml b/.github/workflows/create-version-bump-pr.yml similarity index 66% rename from .github/workflows/version.yml rename to .github/workflows/create-version-bump-pr.yml index 6886990d6..b589f3c0c 100644 --- a/.github/workflows/version.yml +++ b/.github/workflows/create-version-bump-pr.yml @@ -1,5 +1,4 @@ -name: TODO Name Me Properly -# TODO: Maybe we want to do this every time release drafter runs, read more: https://github.com/marketplace/actions/create-pull-request#action-behaviour +name: Create Version Bump PR on: workflow_dispatch: