Skip to content

Commit

Permalink
minor: Infer bump from commit message
Browse files Browse the repository at this point in the history
Inferring the bump from commit message adds the possibility for the action to use the first word of the commit message in order to determine the bump, or default to patch if it can't make sense of the message.
  • Loading branch information
amiiit authored May 19, 2022
1 parent e7e046c commit e48e518
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Opened and updated PR
name: Closed and merged Pull Request

on:
pull_request:
Expand All @@ -7,6 +7,7 @@ on:

jobs:
release:
if: github.event.pull_request.merged == true
name: PR
runs-on: ubuntu-latest
steps:
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# merge-bump-release

GitHub action to be applied on merged pull-request, to bump a semver and create a new release
GitHub action to be applied on merged pull-request, to bump a semver and create a new release.

## Setting up in your workflow:

The best explanation is by example. Please have a look at this project's [`pr.yml` file](./.github/workflows/pr.yml)
to see how this project is dogfooding the action in order to bump-release itself. Your example
may be a bit different, depending on your circumstances.

## Features

Out of the box, this action will always bump your release with a patch, unless configured
to do otherwise. This action can decide what part of the SemVer so increment depending on
the way it's configured.

**If provided with `bump: <patch | minor | major>`** as input it will use this input to bump and ignore any other configuration.

**If provided with `infer_bump_from_commit: true`** as input it will try to guess the right one depending on the commit message. Right now the logic is a commit header that starts with the words _'patch', 'minor' or 'major'_.

## Contributing

If you need more features, please submit an issue or a pull request.
36 changes: 32 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8791,8 +8791,33 @@ const bump = (version, bump) => {
}
return parts.join('.');
};
const inferBumpFromCommit = (commit) => {
const firstWordMatch = /(\w+)\b.*/.exec(commit.messageHeadline);
if (firstWordMatch) {
const firstWord = firstWordMatch[1].toLowerCase();
if (BUMPS.includes(firstWord)) {
return firstWord;
}
}
return 'patch';
};
const BUMPS = ['patch', 'minor', 'major'];
const determineBumpType = (commit, options) => {
if (options.inputBump) {
if (!BUMPS.includes(options.inputBump.toLowerCase())) {
throw `provided input to bump: "${options.inputBump}", must be one of patch, minor, major.`;
}
else {
return options.inputBump.toLowerCase();
}
}
if (options.inferBumpFromCommit) {
return inferBumpFromCommit(commit);
}
return 'patch';
};

var commitMessageQuery = "query GetCommitMessageFromRepository($repoName: String!, $repoOwner: String!, $prNumber: Int!) {\n repository(name: $repoName, owner: $repoOwner) {\n pullRequest(number: $prNumber) {\n mergeCommit {\n message\n messageBody\n messageHeadline\n }\n }\n }\n}";
var commitMessageQuery = "query GetCommitMessageFromRepository($repoName: String!, $repoOwner: String!, $prNumber: Int!) {\n repository(name: $repoName, owner: $repoOwner) {\n pullRequest(number: $prNumber) {\n mergeCommit {\n messageBody\n messageHeadline\n }\n }\n }\n}";

var lastReleaseQuery = "query GetLastReleaseQuery($repoName: String!, $repoOwner: String!) {\n repository(name: $repoName, owner: $repoOwner) {\n latestRelease {\n tag {\n id\n name\n prefix\n }\n }\n }\n}";

Expand All @@ -8809,14 +8834,17 @@ const start = async () => {
...repoDetails,
prNumber: github.context.payload.pull_request?.number
});
console.log('commit message from gql', JSON.stringify(commitMessage, null, '\t'));
const latestRelease = await octokit.graphql(lastReleaseQuery, {
...repoDetails
});
console.log('LatestReleaseQueryResponse', JSON.stringify(latestRelease, null, '\t'));
const latestVersion = latestRelease.repository.latestRelease.tag.name;
const nextVersion = bump((latestVersion || 'v0'), 'patch');
const bumpType = determineBumpType(commitMessage.repository.pullRequest.mergeCommit, {
inputBump: core.getInput('bump'),
inferBumpFromCommit: core.getInput('infer_bump_from_commit')
});
const nextVersion = bump((latestVersion || 'v0'), bumpType);
const nextReleaseTag = core.getInput('tag_prefix') + nextVersion;
core.setOutput('next_version', nextReleaseTag);
const releaseResult = await octokit.request('POST /repos/{owner}/{repo}/releases', {
repo: repoDetails.repoName,
owner: repoDetails.repoOwner,
Expand Down
2 changes: 1 addition & 1 deletion src/bump.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('bump', () => {
expect(bump('v1.2.3', 'major')).toEqual('2.0.0')
expect(bump('v1.2.3', 'minor')).toEqual('1.3.0')
expect(bump('v1.2.3--alpha3', 'minor')).toEqual('1.3.0')
expect(bump('v1.2', 'patch')).toThrow('invalid semver: v1.2')
expect(() => bump('v1.2', 'patch')).toThrow('invalid semver: v1.2')
})

})
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const start = async () => {

const latestVersion = latestRelease.repository.latestRelease.tag.name

const bumpType: Bump = determineBumpType(commitMessage, {
const bumpType: Bump = determineBumpType(commitMessage.repository.pullRequest.mergeCommit, {
inputBump: core.getInput('bump'),
inferBumpFromCommit: core.getInput('infer_bump_from_commit')
})
Expand Down

0 comments on commit e48e518

Please sign in to comment.