Skip to content

Commit

Permalink
Merge pull request #23 from r7kamura/increment
Browse files Browse the repository at this point in the history
Increment version by `release_type` input option
  • Loading branch information
r7kamura authored May 31, 2024
2 parents 3dd52e7 + d02e0c4 commit fdbd639
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 13 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/bump-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ name: bump-request
on:
workflow_dispatch:
inputs:
version:
description: Version to change to.
required: true
type: string
release_type:
type: choice
description: How to bump the version.
options:
- major
- minor
- patch

jobs:
run:
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ name: bump-request
on:
workflow_dispatch:
inputs:
version:
description: Version to change to.
required: true
type: string
release_type:
type: choice
description: How to bump the version.
options:
- major
- minor
- patch

jobs:
run:
Expand Down Expand Up @@ -75,9 +78,18 @@ command: |
bundle install
```
### `release_type`

How to bump the version.

One of:

- `major`
- `minor`
- `patch`

### `version`

Version to change to.
You can also specify this directly instead of `release_type`.

- required
- e.g. `1.2.3`
16 changes: 13 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ inputs:
github_token:
description: GitHub access token.
required: false
release_type:
description: How to bump the version. One of `major`, `minor`, `patch`.
required: false
version:
description: Version to change to.
required: true
description: You can also specify this directly instead of `release_type`.
required: false
runs:
using: composite
steps:
Expand All @@ -21,11 +24,18 @@ runs:
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: deno run --allow-all src/main.ts
- run: deno run --allow-all src/detectNextVersion.ts
env:
BUMP_REQUEST_INPUTS_RELEASE_TYPE: ${{ inputs.release_type }}
BUMP_REQUEST_INPUTS_VERSION: ${{ inputs.version }}
GITHUB_TOKEN: ${{ inputs.github_token || github.token }}
shell: bash
id: detect_next_version
- run: deno run --allow-all src/createPullRequest.ts
env:
BUMP_REQUEST_VERSION: ${{ steps.detect_next_version.outputs.version }}
GITHUB_TOKEN: ${{ inputs.github_token || github.token }}
shell: bash
branding:
color: blue
icon: git-pull-request
File renamed without changes.
3 changes: 3 additions & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * as core from "https://esm.sh/@actions/[email protected]?dts";
export * as exec from "https://esm.sh/@actions/[email protected]?dts";
export * as github from "https://esm.sh/@actions/[email protected]?dts";
export * as semver from "https://esm.sh/[email protected]?dts";
export * as asserts from "https://deno.land/[email protected]/testing/asserts.ts";
54 changes: 54 additions & 0 deletions src/detectNextVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { core } from "./deps.ts";
import { github } from "./deps.ts";
import { fetchLatestRelease } from "./github.ts";
import { incrementVersion } from "./versioning.ts";

const forcedVersion = Deno.env.get("BUMP_REQUEST_INPUTS_VERSION");
const releaseType = Deno.env.get("BUMP_REQUEST_INPUTS_RELEASE_TYPE");
const githubToken = Deno.env.get("GITHUB_TOKEN")!;

if (!forcedVersion && !releaseType) {
throw new Error(
"Specify either `release_type` or `version`.",
);
}

if (releaseType) {
if (!["major", "minor", "patch"].includes(releaseType)) {
throw new Error(
"`release_type` must be one of `major`, `minor`, or `patch`.",
);
}
}

core.setOutput(
"version",
await detectNextVersion({
githubToken,
githubOwner: github.context.repo.owner,
githubRepo: github.context.repo.repo,
releaseType: releaseType!,
}),
);

async function detectNextVersion({
githubToken,
githubOwner,
githubRepo,
releaseType,
}: {
githubToken: string;
githubOwner: string;
githubRepo: string;
releaseType: string;
}) {
const response = await fetchLatestRelease({
githubToken,
owner: githubOwner,
repo: githubRepo,
});
return incrementVersion({
version: response.data.tag_name,
releaseType,
});
}
18 changes: 18 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ export async function createPullRequest({
);
}

export async function fetchLatestRelease({
githubToken,
owner,
repo,
}: {
githubToken: string;
owner: string;
repo: string;
}) {
return await github.getOctokit(githubToken).request(
"GET /repos/{owner}/{repo}/releases/latest",
{
owner,
repo,
},
);
}

function preventMention(text: string) {
return text.replace(/@/g, "@\u200B");
}
32 changes: 32 additions & 0 deletions src/versioning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { semver } from "./deps.ts";
import { asserts } from "./deps.ts";

export function incrementVersion({
version,
releaseType,
}: {
version: string;
releaseType: string;
}) {
return semver.inc(
version,
releaseType as semver.ReleaseType,
);
}

Deno.test("incrementVersion", () => {
asserts.assertEquals(
incrementVersion({
version: "1.0.0",
releaseType: "major",
}),
"2.0.0",
);
asserts.assertEquals(
incrementVersion({
version: "v1.0.0",
releaseType: "minor",
}),
"1.1.0",
);
});

0 comments on commit fdbd639

Please sign in to comment.