From 13be621f342f736ee1822772d6822406d0952904 Mon Sep 17 00:00:00 2001 From: Paul Schuberth Date: Mon, 16 Sep 2024 17:06:09 +0200 Subject: [PATCH] Customize commit messages (#679) * 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 gradle-update/update-gradle-wrapper-action#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 --- README.md | 22 ++++++++++++++++ action.yml | 8 ++++++ dist/index.js | 23 ++++++++++++++--- src/git/git-commit.ts | 10 ++------ src/inputs/index.ts | 10 ++++++++ src/messages.ts | 11 +++++++- src/tasks/main.ts | 9 ++++++- tests/github/gh-ops.test.ts | 2 ++ tests/inputs/inputs.test.ts | 26 +++++++++++++++++++ tests/messages.test.ts | 50 ++++++++++++++++++++++++++++++++++--- tests/tasks/main.test.ts | 7 +++--- 11 files changed, 157 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 9af35f5c..003e7cb1 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/action.yml b/action.yml index fd0d4707..92504e4e 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/dist/index.js b/dist/index.js index b2ab7d06..26fbda52 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; @@ -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%'; + } } } @@ -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%'; @@ -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); @@ -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); @@ -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, 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 f036e2c8..1d437059 100644 --- a/src/inputs/index.ts +++ b/src/inputs/index.ts @@ -27,6 +27,7 @@ export interface Inputs { releaseChannel: string; mergeMethod: string | undefined; prTitleTemplate: string; + commitMessageTemplate: string; } export function getInputs(): Inputs { @@ -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}); @@ -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%'; + } } } diff --git a/src/messages.ts b/src/messages.ts index 2f522779..50bf3d5b 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -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, @@ -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`; diff --git a/src/tasks/main.ts b/src/tasks/main.ts index 2dde50ba..f035d466 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'; @@ -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({ diff --git a/tests/github/gh-ops.test.ts b/tests/github/gh-ops.test.ts index 11cc0fe8..35630e7e 100644 --- a/tests/github/gh-ops.test.ts +++ b/tests/github/gh-ops.test.ts @@ -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%' }; diff --git a/tests/inputs/inputs.test.ts b/tests/inputs/inputs.test.ts index 0759d3f2..a7a4500a 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": [], "mergeMethod": undefined, "paths": [], @@ -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 = { diff --git a/tests/messages.test.ts b/tests/messages.test.ts index 3d0d8025..1ed0348f 100644 --- a/tests/messages.test.ts +++ b/tests/messages.test.ts @@ -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', () => { @@ -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']); @@ -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 diff --git a/tests/tasks/main.test.ts b/tests/tasks/main.test.ts index 99df352b..c78bec58 100644 --- a/tests/tasks/main.test.ts +++ b/tests/tasks/main.test.ts @@ -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 = { @@ -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');