Skip to content

Commit

Permalink
feat: Add validateSingleCommit flag to validate the commit message …
Browse files Browse the repository at this point in the history
…if there's only a single commit in the PR (opt-in). This is intended to be used as a workaround for an issue with Github as in this case, the PR title won't be used as the default commit message when merging a PR. (#87)

* Validate commit message for 1 commit PRs

* Use catch-all error message for single-commit errors

* Add input description to action.yml
  • Loading branch information
simlrh committed Feb 15, 2021
1 parent 3301e0e commit 3f20459
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ The action works without configuration, however you can provide options for cust
# validation of the PR title and the pull request checks remain pending.
# Note that a second check will be reported if this is enabled.
wip: true
# 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.
validateSingleCommit: true
```

## Event triggers
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ inputs:
wip:
description: "For work-in-progress PRs you can typically use draft pull requests from Github. However, private repositories on the free plan don't have this option and therefore this action allows you to opt-in to using the special '[WIP]' prefix to indicate this state. This will avoid the validation of the PR title and the pull request checks remain pending. Note that a second check will be reported if this is enabled."
required: false
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
28 changes: 27 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = async function run() {
requireScope,
wip,
subjectPattern,
subjectPatternError
subjectPatternError,
validateSingleCommit
} = parseConfig();

const contextPullRequest = github.context.payload.pull_request;
Expand Down Expand Up @@ -48,6 +49,31 @@ module.exports = async function run() {
subjectPattern,
subjectPatternError
});

if (validateSingleCommit) {
const {data: commits} = await client.pulls.listCommits({
owner,
repo,
pull_number: contextPullRequest.number,
per_page: 2
});

if (commits.length === 1) {
try {
await validatePrTitle(commits[0].commit.message, {
types,
scopes,
requireScope,
subjectPattern,
subjectPatternError
});
} catch (error) {
throw new Error(
`Pull request has only one commit and it's not semantic; this may lead to a non-semantic commit in the base branch (see https://github.community/t/how-to-change-the-default-squash-merge-commit-message/1155). Amend the commit message to match the pull request title, or add another commit.`
);
}
}
}
} catch (error) {
validationError = error;
}
Expand Down
10 changes: 9 additions & 1 deletion src/parseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ module.exports = function parseConfig() {
wip = ConfigParser.parseBoolean(process.env.INPUT_WIP);
}

let validateSingleCommit;
if (process.env.INPUT_VALIDATESINGLECOMMIT) {
validateSingleCommit = ConfigParser.parseBoolean(
process.env.INPUT_VALIDATESINGLECOMMIT
);
}

return {
types,
scopes,
requireScope,
wip,
subjectPattern,
subjectPatternError
subjectPatternError,
validateSingleCommit
};
};

0 comments on commit 3f20459

Please sign in to comment.