Skip to content

Commit

Permalink
fix: Single commit validation does not factor in merge commits [#108]
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpoy committed Oct 26, 2021
1 parent 64af21f commit cb3667e
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,41 @@ module.exports = async function run() {
});

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

if (commits.length === 1) {
const commits = [];

// Pull all of the commits for the given PR from the GitHub API
const COMMITS_PER_PAGE = 50;
const MAX_PAGES = 100; // A sanity limit
let page = 1;
while (page < MAX_PAGES) {
const {data} = await client.pulls.listCommits({
owner,
repo,
pull_number: contextPullRequest.number,
per_page: COMMITS_PER_PAGE,
page
});

commits.push(...data);

if (data.length < COMMITS_PER_PAGE) break;

page++;
}

// GitHub does not count merge commits when deciding whether to use
// the PR title or a commit message for the squash commit message.
const nonMergeCommits = commits.filter(
(commit) => !commit.commit.message.startsWith('Merge branch')
);

// If there is only one (non merge) commit present, GitHub will use
// that commit rather than the PR title for the title of a squash
// commit. To make sure a semantic title is used for the squash
// commit, we need to validate the commit title.
if (nonMergeCommits.length === 1) {
try {
await validatePrTitle(commits[0].commit.message, {
await validatePrTitle(nonMergeCommits[0].commit.message, {
types,
scopes,
requireScope,
Expand Down

0 comments on commit cb3667e

Please sign in to comment.