Skip to content

Commit

Permalink
Add commit-message-template input
Browse files Browse the repository at this point in the history
This change adds a new input to the action. It can be used to customize
the message of the commit created by this action.

This input is not required and has the default value `Update Gradle
Wrapper from %sourceVersion% to %targetVersion%`, where `%sourceVersion%`
will be replaced by the current/old version of the Gradle Wrapper and
`%targetVersion%` will be replaced by the new version of the Gradle
Wrapper.

The PR title and body remain unaffected by this change.

Implements #246
  • Loading branch information
paulschuberth committed Jul 3, 2023
1 parent 28afc7c commit 58c3265
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 18 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Request](https://user-images.githubusercontent.com/316923/93274006-8922ef80-f7b9
- [`set-distribution-checksum`](#set-distribution-checksum)
- [`release-channel`](#release-channel)
- [`merge-method`](#merge-method)
- [`commit-message-template`](#commit-message-template)
- [Examples](#examples)
- [Scheduling action execution](#scheduling-action-execution)
- [Targeting a custom branch](#targeting-a-custom-branch)
Expand Down Expand Up @@ -424,6 +425,25 @@ with:
merge-method: SQUASH
```

### `commit-message-template`

| Name | Description | Required | Default |
| --- | --- | --- | --- |
| `commit-message-template` | The template to use for the commit message created by this action | No | `Update Gradle Wrapper from %sourceVersion% to %targetVersion%` |

This input is used for the message of the commit created by this action. This allows for better integration into
repositories which make use of commit message patterns like [Conventional Commits](https://www.conventionalcommits.org/).

`%sourceVersion%` and `%targetVersion%` will be replaced by the current/old and the new version of the Gradle Wrapper
respectively.

For example:

```yaml
with:
commit-message-template: 'chore(deps): Bump Gradle Wrapper from %sourceVersion% to %targetVersion%'
```

## Examples

### Scheduling action execution
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ inputs:
merge-method:
description: 'Which merge method to use for auto-merge (either `MERGE`, `REBASE`, or `SQUASH`). If unset, auto-merge will not be enabled on opened PRs.'
required: false
commit-message-template:
description: 'Template used for commit message'
required: false
default: 'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'

runs:
using: 'node16'
Expand Down
25 changes: 21 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.commit = void 0;
const git = __importStar(__nccwpck_require__(8940));
function commit(files, targetVersion, sourceVersion) {
function commit(files, commitMessage) {
return __awaiter(this, void 0, void 0, function* () {
yield git.add(files);
yield git.commit(`Update Gradle Wrapper from ${sourceVersion} to ${targetVersion}.`);
yield git.commit(commitMessage);
});
}
exports.commit = commit;
Expand Down Expand Up @@ -846,6 +846,13 @@ class ActionInputs {
if (!this.mergeMethod) {
this.mergeMethod = undefined;
}
this.commitMessageTemplate = core
.getInput('commit-message-template', { required: false })
.trim();
if (!this.commitMessageTemplate) {
this.commitMessageTemplate =
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%';
}
}
}

Expand All @@ -858,8 +865,16 @@ class ActionInputs {
"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.pullRequestText = void 0;
exports.pullRequestText = exports.commitMessageText = void 0;
const ISSUES_URL = 'https://github.com/gradle-update/update-gradle-wrapper-action/issues';
const TARGET_VERSION_PLACEHOLDER = '%targetVersion%';
const SOURCE_VERSION_PLACEHOLDER = '%sourceVersion%';
function commitMessageText(template, source, target) {
return template
.replace(TARGET_VERSION_PLACEHOLDER, target)
.replace(SOURCE_VERSION_PLACEHOLDER, source);
}
exports.commitMessageText = commitMessageText;
function pullRequestText(distTypes, targetRelease, sourceVersion) {
const targetVersion = targetRelease.version;
const title = sourceVersion
Expand Down Expand Up @@ -1112,6 +1127,7 @@ const git = __importStar(__nccwpck_require__(8940));
const gitAuth = __importStar(__nccwpck_require__(1304));
const store = __importStar(__nccwpck_require__(5826));
const git_commit_1 = __nccwpck_require__(4779);
const messages_1 = __nccwpck_require__(9112);
const wrapperInfo_1 = __nccwpck_require__(6832);
const wrapperUpdater_1 = __nccwpck_require__(7412);
const find_1 = __nccwpck_require__(2758);
Expand Down Expand Up @@ -1190,7 +1206,8 @@ class MainAction {
yield updater.verify();
core.endGroup();
core.startGroup('Committing');
yield (0, git_commit_1.commit)(modifiedFiles, targetRelease.version, wrapper.version);
const commitMessage = (0, messages_1.commitMessageText)(this.inputs.commitMessageTemplate, wrapper.version, targetRelease.version);
yield (0, git_commit_1.commit)(modifiedFiles, commitMessage);
core.endGroup();
commitDataList.push({
files: modifiedFiles,
Expand Down
10 changes: 2 additions & 8 deletions src/git/git-commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@

import * as git from './git-cmds';

export async function commit(
files: string[],
targetVersion: string,
sourceVersion: string
) {
export async function commit(files: string[], commitMessage: string) {
await git.add(files);
await git.commit(
`Update Gradle Wrapper from ${sourceVersion} to ${targetVersion}.`
);
await git.commit(commitMessage);
}
10 changes: 10 additions & 0 deletions src/inputs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Inputs {
pathsIgnore: string[];
releaseChannel: string;
mergeMethod: string | undefined;
commitMessageTemplate: string;
}

export function getInputs(): Inputs {
Expand All @@ -46,6 +47,7 @@ class ActionInputs implements Inputs {
pathsIgnore: string[];
releaseChannel: string;
mergeMethod: string | undefined;
commitMessageTemplate: string;

constructor() {
this.repoToken = core.getInput('repo-token', {required: false});
Expand Down Expand Up @@ -107,5 +109,13 @@ class ActionInputs implements Inputs {
if (!this.mergeMethod) {
this.mergeMethod = undefined;
}

this.commitMessageTemplate = core
.getInput('commit-message-template', {required: false})
.trim();
if (!this.commitMessageTemplate) {
this.commitMessageTemplate =
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%';
}
}
}
13 changes: 13 additions & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ import {Release} from './releases';
const ISSUES_URL =
'https://github.com/gradle-update/update-gradle-wrapper-action/issues';

const TARGET_VERSION_PLACEHOLDER = '%targetVersion%';
const SOURCE_VERSION_PLACEHOLDER = '%sourceVersion%';

export function commitMessageText(
template: string,
source: string,
target: string
): string {
return template
.replace(TARGET_VERSION_PLACEHOLDER, target)
.replace(SOURCE_VERSION_PLACEHOLDER, source);
}

export function pullRequestText(
distTypes: Set<string>,
targetRelease: Release,
Expand Down
9 changes: 8 additions & 1 deletion src/tasks/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as gitAuth from '../git/git-auth';
import * as store from '../store';

import {commit} from '../git/git-commit';
import {commitMessageText} from '../messages';
import {createWrapperInfo} from '../wrapperInfo';
import {createWrapperUpdater} from '../wrapperUpdater';
import {findWrapperPropertiesFiles} from '../wrapper/find';
Expand Down Expand Up @@ -160,7 +161,13 @@ export class MainAction {
core.endGroup();

core.startGroup('Committing');
await commit(modifiedFiles, targetRelease.version, wrapper.version);

const commitMessage = commitMessageText(
this.inputs.commitMessageTemplate,
wrapper.version,
targetRelease.version
);
await commit(modifiedFiles, commitMessage);
core.endGroup();

commitDataList.push({
Expand Down
4 changes: 3 additions & 1 deletion tests/github/gh-ops.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ const defaultMockInputs: Inputs = {
paths: [],
pathsIgnore: [],
releaseChannel: '',
mergeMethod: undefined
mergeMethod: undefined,
commitMessageTemplate:
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'
};

const defaultMockGitHubApi: IGitHubApi = {
Expand Down
26 changes: 26 additions & 0 deletions tests/inputs/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import * as core from '@actions/core';

import {getInputs} from '../../src/inputs';

jest.mock('@actions/core');

describe('getInputs', () => {
Expand Down Expand Up @@ -44,6 +45,7 @@ describe('getInputs', () => {
expect(getInputs()).toMatchInlineSnapshot(`
ActionInputs {
"baseBranch": "",
"commitMessageTemplate": "Update Gradle Wrapper from %sourceVersion% to %targetVersion%",
"labels": Array [],
"mergeMethod": undefined,
"paths": Array [],
Expand Down Expand Up @@ -190,6 +192,30 @@ describe('getInputs', () => {
});
});

describe('commitMessageTemplate', () => {
it('defaults to "Update Gradle Wrapper from %sourceVersion% to %targetVersion%"', () => {
ymlInputs = {
'repo-token': 's3cr3t'
};

expect(getInputs().commitMessageTemplate).toStrictEqual(
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'
);
});

it('is set to the input string value', () => {
ymlInputs = {
'repo-token': 's3cr3t',
'commit-message-template':
'Change wrapper from %sourceVersion% to %targetVersion%'
};

expect(getInputs().commitMessageTemplate).toStrictEqual(
'Change wrapper from %sourceVersion% to %targetVersion%'
);
});
});

describe('releaseChannel', () => {
it('defaults to stable channel', () => {
ymlInputs = {
Expand Down
31 changes: 30 additions & 1 deletion tests/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,37 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {pullRequestText} from '../src/messages';
import {Release} from '../src/releases';
import {commitMessageText, pullRequestText} from '../src/messages';

describe('commitMessageText', () => {
it('replaces %sourceVersion% with the source version parameter', () => {
const message = commitMessageText(
'Update from %sourceVersion%',
'1.0.0',
'1.0.1'
);
expect(message).toEqual('Update from 1.0.0');
});

it('replaces %targetVersion% with the source version parameter', () => {
const message = commitMessageText(
'Update to %targetVersion%',
'1.0.0',
'1.0.1'
);
expect(message).toEqual('Update to 1.0.1');
});

it('replaces both placeholders', () => {
const message = commitMessageText(
'Update from %sourceVersion% to %targetVersion%',
'1.0.0',
'1.0.1'
);
expect(message).toEqual('Update from 1.0.0 to 1.0.1');
});
});

describe('pullRequestText', () => {
const distributionTypes = new Set(['all', 'bin']);
Expand Down
7 changes: 4 additions & 3 deletions tests/tasks/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ const defaultMockInputs: Inputs = {
paths: [],
pathsIgnore: [],
releaseChannel: '',
mergeMethod: undefined
mergeMethod: undefined,
commitMessageTemplate:
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'
};

const defaultMockGitHubApi: IGitHubApi = {
Expand Down Expand Up @@ -152,8 +154,7 @@ describe('run', () => {
'/path/to/gradle/wrapper/gradle-wrapper.properties',
'/path/to/gradle/wrapper/gradle-wrapper.jar'
],
'1.0.1',
'1.0.0'
'Update Gradle Wrapper from 1.0.0 to 1.0.1'
);

expect(git.push).toHaveBeenCalledWith('gradlew-update-1.0.1');
Expand Down

0 comments on commit 58c3265

Please sign in to comment.