diff --git a/.github/workflows/lint-pr-title-preview-validateSingleCommit.yml b/.github/workflows/lint-pr-title-preview-validateSingleCommit.yml index c02578f40..80ec74eaa 100644 --- a/.github/workflows/lint-pr-title-preview-validateSingleCommit.yml +++ b/.github/workflows/lint-pr-title-preview-validateSingleCommit.yml @@ -21,3 +21,4 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: validateSingleCommit: true + validateSingleCommitMatchesPrTitle: true diff --git a/README.md b/README.md index 124d6905a..897171196 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,9 @@ The action works without configuration, however you can provide options for cust # merge commit, and it's easy to commit this by mistake. Enable this option # to also validate the commit message for one commit PRs. validateSingleCommit: true + # Related to `validateSingleCommit` you can opt-in to validate that the PR + # title matches a single commit to avoid confusion. + validateSingleCommitMatchesPrTitle: true # If you use Github Enterprise, you can set this to the URL of your server githubBaseUrl: https://github.myorg.com/api/v3 ``` diff --git a/action.yml b/action.yml index 824fe0069..0bacf5d70 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,9 @@ inputs: validateSingleCommit: description: "When using \"Squash and merge\" on a PR with only one commit, GitHub will suggest using that commit message instead of the PR title for the merge commit, and it's easy to commit this by mistake. Enable this option to also validate the commit message for one commit PRs." required: false + validateSingleCommitMatchesPrTitle: + description: "Related to `validateSingleCommit` you can opt-in to validate that the PR title matches a single commit to avoid confusion." + required: false githubBaseUrl: description: "If you use Github Enterprise, you can set this to the URL of your server (e.g. https://github.myorg.com/api/v3)" required: false diff --git a/src/index.js b/src/index.js index 0686bda28..545b35e90 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ module.exports = async function run() { subjectPattern, subjectPatternError, validateSingleCommit, + validateSingleCommitMatchesPrTitle, githubBaseUrl } = parseConfig(); @@ -98,12 +99,14 @@ module.exports = async function run() { ); } - const commitTitle = - nonMergeCommits[0].commit.message.split('\n')[0]; - if (commitTitle !== pullRequest.title) { - throw new Error( - `The pull request has only one (non-merge) commit and in this case Github will use it as the default commit message when merging. The pull request title doesn't match the commit though ("${pullRequest.title}" vs. "${commitTitle}"). Please update the pull request title accordingly to avoid surprises.` - ); + if (validateSingleCommitMatchesPrTitle) { + const commitTitle = + nonMergeCommits[0].commit.message.split('\n')[0]; + if (commitTitle !== pullRequest.title) { + throw new Error( + `The pull request has only one (non-merge) commit and in this case Github will use it as the default commit message when merging. The pull request title doesn't match the commit though ("${pullRequest.title}" vs. "${commitTitle}"). Please update the pull request title accordingly to avoid surprises.` + ); + } } } } diff --git a/src/parseConfig.js b/src/parseConfig.js index 48d2a07f3..9ff23731e 100644 --- a/src/parseConfig.js +++ b/src/parseConfig.js @@ -40,6 +40,13 @@ module.exports = function parseConfig() { ); } + let validateSingleCommitMatchesPrTitle; + if (process.env.INPUT_VALIDATESINGLECOMMITMATCHESPRTITLE) { + validateSingleCommitMatchesPrTitle = ConfigParser.parseBoolean( + process.env.INPUT_VALIDATESINGLECOMMITMATCHESPRTITLE + ); + } + let githubBaseUrl; if (process.env.INPUT_GITHUBBASEURL) { githubBaseUrl = ConfigParser.parseString(process.env.INPUT_GITHUBBASEURL); @@ -53,6 +60,7 @@ module.exports = function parseConfig() { subjectPattern, subjectPatternError, validateSingleCommit, + validateSingleCommitMatchesPrTitle, githubBaseUrl }; };