Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Single commit validation does not factor in merge commits [#108] #131

Merged
merged 3 commits into from
Oct 28, 2021

Conversation

julianpoy
Copy link
Contributor

@julianpoy julianpoy commented Oct 26, 2021

See julianpoy#2 for validation that this works.

Continuation of #127

The breaking problem with the original PR was that GitHub API returns a list of commits each with a property commit inside which contains the message that we care about. When mapping over commits => commit, I forgot to add an additional chain for commits => commit.commit.

Additionally, I realized that not having a comprehensive list of commits means that any user could potentially have a non semantic commit, a merge, and then a semantic commit, still resulting in the denial of the PR. Given that there could be any number of merge commits in between the original real commit and the final real commit, we need to pull all commits for the PR from the GitHub API. I've created a loop to fetch all commits from the GitHub API with a page cap for sanity (say if someone created a diff across two giant branches). This sanity limit could probably be lowered if desired (currently 100 pages).

I also noticed that validatePrTitle should check the nonMergeCommit that we found in the case that there is, in fact, only one commit, rather than any one of the commits in case the first commit returned is a merge commit (for some strange reason).

Lastly, I noticed that the test workflows included in this repository (the one titled current branch) don't use any of the additional options that this repo has to offer, such as the validateSingleCommit option. I would recommend that the current branch workflow test for this repository either include these options, or that there be multiple workflows defined - one for each option.

Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for looking into this again!

Lastly, I noticed that the test workflows included in this repository (the one titled current branch) don't use any of the additional options that this repo has to offer, such as the validateSingleCommit option. I would recommend that the current branch workflow test for this repository either include these options, or that there be multiple workflows defined - one for each option.

That's a fair point. There used to be errors when external contributors use the preview workflow, as environment variables were not available (see event triggers in the README). But I think Github has changed something here, as there's now a button for maintainers to manually trigger CI runs from external contributors.

So potentially this is no longer an issue. If you feel like it, it would be nice if you add a new workflow with the validateSingleCommit option as you've suggested!

src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Show resolved Hide resolved
@julianpoy
Copy link
Contributor Author

julianpoy commented Oct 28, 2021

Added workflow for validateSingleCommit option and used paginate iterator and a break statement to exit early when enough commits have been pulled to make a decision.

See the example PR for validation that the new code works.


if (commits.length === 1) {
for await (const response of client.paginate.iterator(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty cool with the iterator!

// the PR title or a commit message for the squash commit message.
nonMergeCommits = commits.filter(
(commit) => !commit.commit.message.startsWith('Merge branch')
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, another minor nit: I think we don't need commits, but only nonMergeCommits – right? Now we're applying this filter on every iteration step.

Maybe something like this would be a bit simpler?

if (!response.data.commit.message.startsWith('Merge branch')) {
  nonMergeCommits.push(response.data);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally think that's a micro optimization. I also find the existing solution cleaner, and gives us the ability to take actions on the whole list of commits in the future if need be.

If you'd like to switch it though, feel free. It's your repo so I respect the decision. Both should work just fine.

One note is that if you implement your example, you'll need to make sure to spread response.data (nonMergeCommits.push(...response.data).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, fair enough – let's go with your approach and avoid more changes & testing.

Thanks again!

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
validateSingleCommit: true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem!

@amannn amannn merged commit 5265383 into amannn:master Oct 28, 2021
@github-actions
Copy link

🎉 This PR is included in version 3.4.5 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants