Skip to content

Commit 4f82979

Browse files
flevi29Strift
authored andcommitted
Add all the necessary modifications and additions
1 parent d3509fb commit 4f82979

File tree

5 files changed

+138
-55
lines changed

5 files changed

+138
-55
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: "Bump version"
2+
inputs:
3+
github-token:
4+
description: GitHub token for authentication
5+
required: true
6+
version:
7+
description: Optional version so action can skip checking separately
8+
runs:
9+
using: "composite"
10+
steps:
11+
- name: Get release draft version and set environment variable
12+
if: "${{ inputs.version == '' }}"
13+
run: |
14+
VERSION_DETAILS=gh release -R ${{ github.repository }} ls -L 1 --json isDraft,tagName
15+
echo "PKG_VERSION=$VERSION_DETAILS" >> "$GITHUB_ENV"
16+
env:
17+
GH_TOKEN: ${{ inputs.github-token }}
18+
- name: Set environment variable to version
19+
if: "${{ inputs.version != '' }}"
20+
run: echo "PKG_VERSION=${{ inputs.version }}" >> "$GITHUB_ENV"
21+
- name: Bump version of package
22+
id: bump-version
23+
run: |
24+
PARSED_VERSION=node .\scripts\bump-version.js $PKG_VERSION
25+
npm version $PARSED_VERSION --commit-hooks false --git-tag-version false
26+
echo "PKG_VERSION=$PARSED_VERSION" >> "$GITHUB_OUTPUT"
27+
- name: Create pull request with the changes
28+
uses: peter-evans/create-pull-request@v7
29+
with:
30+
token: ${{ inputs.github-token }}
31+
commit-message: Update files containing the version of the package
32+
branch: bump-version
33+
title: Update version for the next release (${{ steps.bump-version.outputs.PKG_VERSION }})
34+
body: |
35+
_This PR is auto-generated._
36+
37+
- updates package version to `"${{ steps.bump-version.outputs.PKG_VERSION }}"`
38+
39+
> [!IMPORTANT]
40+
>
41+
> Every time the main branch is updated and release drafter bumps the version of the new release draft, this PR is auto-updated!
42+
labels: skip-changelog

.github/workflows/release-drafter.yml

+19-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,27 @@ on:
88
jobs:
99
update_release_draft:
1010
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
pull-requests: write
1114
steps:
12-
- uses: release-drafter/release-drafter@v6
15+
- name: Draft release
16+
id: draft-release
17+
uses: release-drafter/release-drafter@v6
1318
with:
1419
config-name: release-draft-template.yml
1520
env:
1621
GITHUB_TOKEN: ${{ secrets.RELEASE_DRAFTER_TOKEN }}
22+
- name: Get status of version bump PR
23+
id: pr-status
24+
run: |
25+
PR_NOT_OPEN=gh pr -R ${{ github.repository }} view bump-version --json closed --jq '.closed' || echo true
26+
echo "PR_NOT_OPEN=$PR_NOT_OPEN" >> "$GITHUB_OUTPUT"
27+
- uses: actions/checkout@v4
28+
if: steps.pr-status.outputs.PR_NOT_OPEN == 'false'
29+
- name: Update PR with version bump
30+
if: steps.pr-status.outputs.PR_NOT_OPEN == 'false'
31+
uses: ./.github/actions/bump-version
32+
with:
33+
version: ${{ steps.draft-release.outputs.resolved_version }}
34+
github-token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/version.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: TODO Name Me Properly
2+
# TODO: Maybe we want to do this every time release drafter runs, read more: https://github.com/marketplace/actions/create-pull-request#action-behaviour
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
update_version:
8+
runs-on: ubuntu-latest
9+
permissions:
10+
contents: write
11+
pull-requests: write
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Bump version in package files and create PR
15+
uses: ./.github/actions/bump-version
16+
with:
17+
github-token: ${{ secrets.GITHUB_TOKEN }}

scripts/bump-version.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { argv, stdout } from "node:process";
2+
import { writeFileSync } from "node:fs";
3+
4+
if (argv.length !== 3) {
5+
throw new Error("expected one command line argument", {
6+
cause: argv.slice(2),
7+
});
8+
}
9+
10+
/** @type {string | { isDraft: boolean; tagName: string }[]} */
11+
const arrayWithLastReleaseOrVersion = JSON.parse(argv[2]);
12+
13+
const pkgVersion = (function () {
14+
if (typeof arrayWithLastReleaseOrVersion === "string") {
15+
return arrayWithLastReleaseOrVersion;
16+
}
17+
18+
if (
19+
!Array.isArray(arrayWithLastReleaseOrVersion) ||
20+
arrayWithLastReleaseOrVersion.length !== 1
21+
) {
22+
throw new Error("expected an array with one element", {
23+
cause: arrayWithLastReleaseOrVersion,
24+
});
25+
}
26+
27+
const lastRelease = arrayWithLastReleaseOrVersion[0];
28+
const { isDraft, tagName } = lastRelease;
29+
30+
if (
31+
Object.keys(lastRelease).length !== 2 ||
32+
typeof isDraft !== "boolean" ||
33+
typeof tagName !== "string"
34+
) {
35+
throw new Error(
36+
'expected an object with keys "isDraft" as boolean and "tagName" as string',
37+
{ cause: lastRelease },
38+
);
39+
}
40+
41+
if (!isDraft) {
42+
throw new Error(
43+
"expected a draft release to be present as the first element in the releases list",
44+
);
45+
}
46+
47+
// remove the "v" from "vX.X.X"
48+
return tagName.substring(1);
49+
})();
50+
51+
const pkgVersionFileContents = `export const PACKAGE_VERSION = "${pkgVersion}";\n`;
52+
53+
const pkgVersionFilePath = new URL(
54+
"../src/package-version.ts",
55+
import.meta.url,
56+
);
57+
58+
writeFileSync(pkgVersionFilePath, pkgVersionFileContents);
59+
60+
stdout.write(pkgVersion);

scripts/update-version.js

-54
This file was deleted.

0 commit comments

Comments
 (0)