diff --git a/README.md b/README.md index 235d60ab..0b34d539 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Request](https://user-images.githubusercontent.com/316923/93274006-8922ef80-f7b9 - [`paths-ignore`](#paths-ignore) - [`set-distribution-checksum`](#set-distribution-checksum) - [`release-channel`](#release-channel) + - [`commit-message-template`](#commit-message-template) - [Examples](#examples) - [Scheduling action execution](#scheduling-action-execution) - [Targeting a custom branch](#targeting-a-custom-branch) @@ -408,6 +409,25 @@ with: release-channel: release-candidate ``` +### `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 diff --git a/action.yml b/action.yml index cb42ae8c..abf2b5cd 100644 --- a/action.yml +++ b/action.yml @@ -45,6 +45,10 @@ inputs: description: 'Gradle release channel to be used (either `stable` or `release-candidate`).' required: false default: stable + commit-message-template: + description: 'Template used for commit message and PR title' + required: false + default: 'Update Gradle Wrapper from %sourceVersion% to %targetVersion%' runs: using: 'node16' diff --git a/dist/index.js b/dist/index.js index 4e648967..fe0663ba 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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; @@ -789,6 +789,13 @@ class ActionInputs { if (!acceptedReleaseChannels.includes(this.releaseChannel)) { throw new Error('release-channel has unexpected value'); } + this.commitMessageTemplate = core + .getInput('commit-message-template', { required: false }) + .trim(); + if (!this.commitMessageTemplate) { + this.commitMessageTemplate = + 'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'; + } } } @@ -801,8 +808,17 @@ 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) { + let message = template; + message = message.replace(TARGET_VERSION_PLACEHOLDER, target); + message = message.replace(SOURCE_VERSION_PLACEHOLDER, source ? source : 'undefined'); + return message; +} +exports.commitMessageText = commitMessageText; function pullRequestText(distTypes, targetRelease, sourceVersion) { const targetVersion = targetRelease.version; const title = sourceVersion @@ -1055,6 +1071,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); @@ -1133,7 +1150,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, diff --git a/src/git/git-commit.ts b/src/git/git-commit.ts index 2d2b0d36..01306cbc 100644 --- a/src/git/git-commit.ts +++ b/src/git/git-commit.ts @@ -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); } diff --git a/src/inputs/index.ts b/src/inputs/index.ts index f6fc48ed..01e9ade9 100644 --- a/src/inputs/index.ts +++ b/src/inputs/index.ts @@ -25,6 +25,7 @@ export interface Inputs { paths: string[]; pathsIgnore: string[]; releaseChannel: string; + commitMessageTemplate: string; } export function getInputs(): Inputs { @@ -44,6 +45,7 @@ class ActionInputs implements Inputs { paths: string[]; pathsIgnore: string[]; releaseChannel: string; + commitMessageTemplate: string; constructor() { this.repoToken = core.getInput('repo-token', {required: false}); @@ -100,5 +102,14 @@ class ActionInputs implements Inputs { if (!acceptedReleaseChannels.includes(this.releaseChannel)) { throw new Error('release-channel has unexpected value'); } + + this.commitMessageTemplate = core + .getInput('commit-message-template', {required: false}) + .trim(); + + if (!this.commitMessageTemplate) { + this.commitMessageTemplate = + 'Update Gradle Wrapper from %sourceVersion% to %targetVersion%'; + } } } diff --git a/src/messages.ts b/src/messages.ts index 7d10fd5f..bdcec6f4 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -17,6 +17,23 @@ 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 | undefined, + target: string +): string { + let message = template; + message = message.replace(TARGET_VERSION_PLACEHOLDER, target); + message = message.replace( + SOURCE_VERSION_PLACEHOLDER, + source ? source : 'undefined' + ); + return message; +} + export function pullRequestText( distTypes: Set, targetRelease: Release, diff --git a/src/tasks/main.ts b/src/tasks/main.ts index e4ceea86..8e5cd759 100644 --- a/src/tasks/main.ts +++ b/src/tasks/main.ts @@ -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'; @@ -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({ diff --git a/tests/github/gh-ops.test.ts b/tests/github/gh-ops.test.ts index d8bb600f..98d2eee1 100644 --- a/tests/github/gh-ops.test.ts +++ b/tests/github/gh-ops.test.ts @@ -35,7 +35,9 @@ const defaultMockInputs: Inputs = { setDistributionChecksum: true, paths: [], pathsIgnore: [], - releaseChannel: '' + releaseChannel: '', + commitMessageTemplate: + 'Update Gradle Wrapper from %sourceVersion% to %targetVersion%' }; const defaultMockGitHubApi: IGitHubApi = { diff --git a/tests/inputs/inputs.test.ts b/tests/inputs/inputs.test.ts index a0e787e8..5bcb703e 100644 --- a/tests/inputs/inputs.test.ts +++ b/tests/inputs/inputs.test.ts @@ -15,6 +15,7 @@ import * as core from '@actions/core'; import {getInputs} from '../../src/inputs'; + jest.mock('@actions/core'); describe('getInputs', () => { @@ -44,6 +45,7 @@ describe('getInputs', () => { expect(getInputs()).toMatchInlineSnapshot(` ActionInputs { "baseBranch": "", + "commitMessageTemplate": "Update Gradle Wrapper from %sourceVersion% to %targetVersion%", "labels": Array [], "paths": Array [], "pathsIgnore": Array [], @@ -189,6 +191,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 = { diff --git a/tests/messages.test.ts b/tests/messages.test.ts index 007200b7..906670ac 100644 --- a/tests/messages.test.ts +++ b/tests/messages.test.ts @@ -12,8 +12,46 @@ // 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('uses `undefined` for %sourceVersion% as fallback', () => { + const message = commitMessageText( + 'Update from %sourceVersion% to %targetVersion%', + undefined, + '1.0.1' + ); + expect(message).toEqual('Update from undefined 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']); diff --git a/tests/tasks/main.test.ts b/tests/tasks/main.test.ts index b8307708..c7e17b34 100644 --- a/tests/tasks/main.test.ts +++ b/tests/tasks/main.test.ts @@ -42,7 +42,9 @@ const defaultMockInputs: Inputs = { setDistributionChecksum: true, paths: [], pathsIgnore: [], - releaseChannel: '' + releaseChannel: '', + commitMessageTemplate: + 'Update Gradle Wrapper from %sourceVersion% to %targetVersion%' }; const defaultMockGitHubApi: IGitHubApi = { @@ -150,8 +152,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');