Skip to content

Commit

Permalink
Customize commit messages (#679)
Browse files Browse the repository at this point in the history
* ignore .idea directory

* Add `commit-message-template` input

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

* Align behaviour for `undefined` sourceVersion with #695

For cases where the source version of the Gradle wrapper cannot
be determined the string `undefined` is used as a fallback.

This allows to use the same custom commit message template in
all cases.

* Update action.yml

---------

Co-authored-by: Cristian Greco <cristian@regolo.cc>
  • Loading branch information
paulschuberth and cristiangreco authored Sep 16, 2024
1 parent aee8d70 commit 13be621
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 21 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Request](https://user-images.githubusercontent.com/316923/93274006-8922ef80-f7b9
- [`release-channel`](#release-channel)
- [`merge-method`](#merge-method)
- [`pr-title-template`](#pr-title-template)
- [`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 @@ -444,6 +445,27 @@ with:
pr-title-template: 'chore(deps): Bump Gradle Wrapper from %sourceVersion% to %targetVersion%'
```

### `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.

There are cases in which the source version of the Gradle Wrapper can not be determined successfully. In such cases, the string 'undefined' will be used to replace the source version placeholder.

For example:

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

## Examples

### Scheduling action execution
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ inputs:
used to replace the source version placeholder.
required: false
default: 'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'
commit-message-template:
description: |
Template used for commit message.
There are cases in which the source version of the Gradle Wrapper can not
be determined successfully. In such cases, the string 'undefined' will be
used to replace the source version placeholder.
required: false
default: 'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'

runs:
using: 'node16'
Expand Down
23 changes: 19 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 @@ -853,6 +853,13 @@ class ActionInputs {
this.prTitleTemplate =
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%';
}
this.commitMessageTemplate = core
.getInput('commit-message-template', { required: false })
.trim();
if (!this.commitMessageTemplate) {
this.commitMessageTemplate =
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%';
}
}
}

Expand All @@ -865,7 +872,7 @@ class ActionInputs {
"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.pullRequestText = exports.pullRequestTitle = void 0;
exports.pullRequestText = exports.commitMessageText = exports.pullRequestTitle = 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%';
Expand All @@ -875,6 +882,12 @@ function pullRequestTitle(template, sourceVersion, targetVersion) {
.replace(SOURCE_VERSION_PLACEHOLDER, sourceVersion !== null && sourceVersion !== void 0 ? sourceVersion : 'undefined');
}
exports.pullRequestTitle = pullRequestTitle;
function commitMessageText(template, source, target) {
return template
.replace(TARGET_VERSION_PLACEHOLDER, target)
.replace(SOURCE_VERSION_PLACEHOLDER, source ? source : 'undefined');
}
exports.commitMessageText = commitMessageText;
function pullRequestText(prTitleTemplate, distTypes, targetRelease, sourceVersion) {
const targetVersion = targetRelease.version;
const title = pullRequestTitle(prTitleTemplate, sourceVersion, targetVersion);
Expand Down Expand Up @@ -1125,6 +1138,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 @@ -1203,7 +1217,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 @@ -27,6 +27,7 @@ export interface Inputs {
releaseChannel: string;
mergeMethod: string | undefined;
prTitleTemplate: string;
commitMessageTemplate: string;
}

export function getInputs(): Inputs {
Expand All @@ -48,6 +49,7 @@ class ActionInputs implements Inputs {
releaseChannel: string;
mergeMethod: string | undefined;
prTitleTemplate: string;
commitMessageTemplate: string;

constructor() {
this.repoToken = core.getInput('repo-token', {required: false});
Expand Down Expand Up @@ -117,5 +119,13 @@ class ActionInputs implements Inputs {
this.prTitleTemplate =
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%';
}

this.commitMessageTemplate = core
.getInput('commit-message-template', {required: false})
.trim();
if (!this.commitMessageTemplate) {
this.commitMessageTemplate =
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%';
}
}
}
11 changes: 10 additions & 1 deletion src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export function pullRequestTitle(
.replace(SOURCE_VERSION_PLACEHOLDER, sourceVersion ?? 'undefined');
}

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

export function pullRequestText(
prTitleTemplate: string,
distTypes: Set<string>,
Expand All @@ -38,7 +48,6 @@ export function pullRequestText(
): {title: string; body: string} {
const targetVersion = targetRelease.version;
const title = pullRequestTitle(prTitleTemplate, sourceVersion, targetVersion);

const bodyHeader = `${title}.
Read the release notes: https://docs.gradle.org/${targetVersion}/release-notes.html`;
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 @@ -159,7 +160,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
2 changes: 2 additions & 0 deletions tests/github/gh-ops.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const defaultMockInputs: Inputs = {
releaseChannel: '',
mergeMethod: undefined,
prTitleTemplate:
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%',
commitMessageTemplate:
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'
};

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": [],
"mergeMethod": undefined,
"paths": [],
Expand Down Expand Up @@ -191,6 +193,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
50 changes: 46 additions & 4 deletions tests/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
// limitations under the License.

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

describe('pullRequestTitle', () => {
it('replaces %sourceVersion% with sourceVersion parameter', () => {
Expand Down Expand Up @@ -53,6 +57,44 @@ describe('pullRequestTitle', () => {
});
});

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 %sourceVersion% with "undefined" if the parameters is `undefined`', () => {
const message = commitMessageText(
'Update from %sourceVersion%',
undefined,
'1.0.1'
);
expect(message).toEqual('Update from undefined');
});

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 Expand Up @@ -105,14 +147,14 @@ If something doesn't look right with this PR please file an issue [here](https:/
describe('when source version is unspecified', () => {
it('returns title and body text with only the target version', () => {
const {title, body} = pullRequestText(
'Update Gradle Wrapper to %targetVersion%',
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%',
distributionTypes,
targetRelease
);

expect(title).toEqual('Update Gradle Wrapper to 1.0.1');
expect(title).toEqual('Update Gradle Wrapper from undefined to 1.0.1');

expect(body).toEqual(`Update Gradle Wrapper to 1.0.1.
expect(body).toEqual(`Update Gradle Wrapper from undefined to 1.0.1.
Read the release notes: https://docs.gradle.org/1.0.1/release-notes.html
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 @@ -44,7 +44,9 @@ const defaultMockInputs: Inputs = {
pathsIgnore: [],
releaseChannel: '',
mergeMethod: undefined,
prTitleTemplate: 'Bump wrapper from %sourceVersion% to %targetVersion%'
prTitleTemplate: 'Bump wrapper from %sourceVersion% to %targetVersion%',
commitMessageTemplate:
'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'
};

const defaultMockGitHubApi: IGitHubApi = {
Expand Down Expand Up @@ -153,8 +155,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 13be621

Please sign in to comment.