Skip to content

Commit

Permalink
fix: Consider merge commits for single commit validation (#131)
Browse files Browse the repository at this point in the history
* fix: Single commit validation does not factor in merge commits [#108]

* fix: Use octokit paginate iterator for perf

* feat: Add validateSingleCommit job for validation
  • Loading branch information
julianpoy authored Oct 28, 2021
1 parent 64af21f commit 5265383
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/lint-pr-title-preview-validateSingleCommit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: 'Lint PR title preview (current branch, validateSingleCommit enabled)'
on:
pull_request:
types:
- opened
- edited
- synchronize

jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- run: yarn install
- run: yarn build
- uses: ./
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
validateSingleCommit: true
37 changes: 29 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,37 @@ module.exports = async function run() {
});

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

if (commits.length === 1) {
for await (const response of client.paginate.iterator(
client.pulls.listCommits,
{
owner,
repo,
pull_number: contextPullRequest.number
}
)) {
commits.push(...response.data);

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

// We only need two non-merge commits to know that the PR
// title won't be used.
if (nonMergeCommits.length >= 2) break;
}

// 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 5265383

Please sign in to comment.