Skip to content

Commit c05e358

Browse files
authored
feat: Add opt-in validation that PR titles match a single commit (#160)
1 parent 04cc956 commit c05e358

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

.github/workflows/lint-pr-title-preview-validateSingleCommit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ jobs:
2121
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2222
with:
2323
validateSingleCommit: true
24+
validateSingleCommitMatchesPrTitle: true

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ The action works without configuration, however you can provide options for cust
7777
# merge commit, and it's easy to commit this by mistake. Enable this option
7878
# to also validate the commit message for one commit PRs.
7979
validateSingleCommit: true
80+
# Related to `validateSingleCommit` you can opt-in to validate that the PR
81+
# title matches a single commit to avoid confusion.
82+
validateSingleCommitMatchesPrTitle: true
8083
# If you use Github Enterprise, you can set this to the URL of your server
8184
githubBaseUrl: https://github.myorg.com/api/v3
8285
```

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ inputs:
2929
validateSingleCommit:
3030
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."
3131
required: false
32+
validateSingleCommitMatchesPrTitle:
33+
description: "Related to `validateSingleCommit` you can opt-in to validate that the PR title matches a single commit to avoid confusion."
34+
required: false
3235
githubBaseUrl:
3336
description: "If you use Github Enterprise, you can set this to the URL of your server (e.g. https://github.myorg.com/api/v3)"
3437
required: false

src/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = async function run() {
1313
subjectPattern,
1414
subjectPatternError,
1515
validateSingleCommit,
16+
validateSingleCommitMatchesPrTitle,
1617
githubBaseUrl
1718
} = parseConfig();
1819

@@ -98,12 +99,14 @@ module.exports = async function run() {
9899
);
99100
}
100101

101-
const commitTitle =
102-
nonMergeCommits[0].commit.message.split('\n')[0];
103-
if (commitTitle !== pullRequest.title) {
104-
throw new Error(
105-
`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.`
106-
);
102+
if (validateSingleCommitMatchesPrTitle) {
103+
const commitTitle =
104+
nonMergeCommits[0].commit.message.split('\n')[0];
105+
if (commitTitle !== pullRequest.title) {
106+
throw new Error(
107+
`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.`
108+
);
109+
}
107110
}
108111
}
109112
}

src/parseConfig.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ module.exports = function parseConfig() {
4040
);
4141
}
4242

43+
let validateSingleCommitMatchesPrTitle;
44+
if (process.env.INPUT_VALIDATESINGLECOMMITMATCHESPRTITLE) {
45+
validateSingleCommitMatchesPrTitle = ConfigParser.parseBoolean(
46+
process.env.INPUT_VALIDATESINGLECOMMITMATCHESPRTITLE
47+
);
48+
}
49+
4350
let githubBaseUrl;
4451
if (process.env.INPUT_GITHUBBASEURL) {
4552
githubBaseUrl = ConfigParser.parseString(process.env.INPUT_GITHUBBASEURL);
@@ -53,6 +60,7 @@ module.exports = function parseConfig() {
5360
subjectPattern,
5461
subjectPatternError,
5562
validateSingleCommit,
63+
validateSingleCommitMatchesPrTitle,
5664
githubBaseUrl
5765
};
5866
};

0 commit comments

Comments
 (0)