Skip to content

Commit 3f20459

Browse files
author
simlrh
authored
feat: Add validateSingleCommit flag to validate the commit message 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
1 parent 3301e0e commit 3f20459

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ The action works without configuration, however you can provide options for cust
7171
# validation of the PR title and the pull request checks remain pending.
7272
# Note that a second check will be reported if this is enabled.
7373
wip: true
74+
# When using "Squash and merge" on a PR with only one commit, GitHub
75+
# will suggest using that commit message instead of the PR title for the
76+
# merge commit, and it's easy to commit this by mistake. Enable this option
77+
# to also validate the commit message for one commit PRs.
78+
validateSingleCommit: true
7479
```
7580
7681
## Event triggers

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ inputs:
2626
wip:
2727
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."
2828
required: false
29+
validateSingleCommit:
30+
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."
31+
required: false

src/index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = async function run() {
1212
requireScope,
1313
wip,
1414
subjectPattern,
15-
subjectPatternError
15+
subjectPatternError,
16+
validateSingleCommit
1617
} = parseConfig();
1718

1819
const contextPullRequest = github.context.payload.pull_request;
@@ -48,6 +49,31 @@ module.exports = async function run() {
4849
subjectPattern,
4950
subjectPatternError
5051
});
52+
53+
if (validateSingleCommit) {
54+
const {data: commits} = await client.pulls.listCommits({
55+
owner,
56+
repo,
57+
pull_number: contextPullRequest.number,
58+
per_page: 2
59+
});
60+
61+
if (commits.length === 1) {
62+
try {
63+
await validatePrTitle(commits[0].commit.message, {
64+
types,
65+
scopes,
66+
requireScope,
67+
subjectPattern,
68+
subjectPatternError
69+
});
70+
} catch (error) {
71+
throw new Error(
72+
`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.`
73+
);
74+
}
75+
}
76+
}
5177
} catch (error) {
5278
validationError = error;
5379
}

src/parseConfig.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ module.exports = function parseConfig() {
3333
wip = ConfigParser.parseBoolean(process.env.INPUT_WIP);
3434
}
3535

36+
let validateSingleCommit;
37+
if (process.env.INPUT_VALIDATESINGLECOMMIT) {
38+
validateSingleCommit = ConfigParser.parseBoolean(
39+
process.env.INPUT_VALIDATESINGLECOMMIT
40+
);
41+
}
42+
3643
return {
3744
types,
3845
scopes,
3946
requireScope,
4047
wip,
4148
subjectPattern,
42-
subjectPatternError
49+
subjectPatternError,
50+
validateSingleCommit
4351
};
4452
};

0 commit comments

Comments
 (0)