Skip to content

Commit

Permalink
feat: Add opt-in validation that PR titles match a single commit (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenji-miyake committed Feb 8, 2022
1 parent 04cc956 commit c05e358
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
validateSingleCommit: true
validateSingleCommitMatchesPrTitle: true
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = async function run() {
subjectPattern,
subjectPatternError,
validateSingleCommit,
validateSingleCommitMatchesPrTitle,
githubBaseUrl
} = parseConfig();

Expand Down Expand Up @@ -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.`
);
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/parseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -53,6 +60,7 @@ module.exports = function parseConfig() {
subjectPattern,
subjectPatternError,
validateSingleCommit,
validateSingleCommitMatchesPrTitle,
githubBaseUrl
};
};

0 comments on commit c05e358

Please sign in to comment.