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 PR validity check in create_release_backmerge_pr #607

Merged
merged 6 commits into from
Oct 31, 2024

Conversation

AliSoftware
Copy link
Contributor

@AliSoftware AliSoftware commented Oct 29, 2024

Why?

Our logic for testing if the backmerge PR would be valid or rejected by GitHub was incorrect.

  • Indeed, so far we checked if the head and base of the PR about to be created returned true for point_to_same_commit?. Which would never be true, as even if there were no new commit on the head branch since the last backmerge PR into the base branch, those would still be two different commits (due to the merge commit in base being created from the last backmerge PR).
  • Also worth noting that even if the diff between head and base were empty, it would technically still be valid to create a PR between the two, because one could have added a commit and its revert on the head branch since the last backmerge of head into base, resulting in an empty diff in the PR, but still a valid PR as some commits from head would not be on base branch yet and would still be ok to merge to reconcile the git graph / history.

GitHub uses the three-dots diff to create Pull Requests, meaning it uses git diff base...head (aka git diff $(git merge-base base head) head) to only include the diff since the common ancestor (merge-base) of the base branch and the head branch.

We thus need to use a similar logic in our action, to detect if GitHub would consider that there was nothing to merge and would reject the PR creation before even attempting to do the API call to create the PR.

How?

This PR fixes the logic to check if PR would be invalid (nothing to merge), by first finding the merge-base between the two branches and checking if the merge base is the same as the head commit we are trying to merge.
If so, that means that there were no new commits on the head branch since it was last cut or last merged into the base branch. In other words, all the commits in the head branch are already in the base branch too (are already part of the common ancestor tree). And in such case there is nothing new to merge, so we should skip the PR creation

This PR also fixes the call to ensure_git_branch after having called the callback optionally provided by the user, as it had a typo in the regex used.

Checklist before requesting a review

  • Run bundle exec rubocop to test for code style violations and recommendations
  • Add Unit Tests (aka specs/*_spec.rb) if applicable
  • Run bundle exec rspec to run the whole test suite and ensure all your tests pass
  • Make sure you added an entry in the CHANGELOG.md file to describe your changes under the appropriate existing ### subsection of the existing ## Trunk section.
  • If applicable, add an entry in the MIGRATION.md file to describe how the changes will affect the migration from the previous major version and what the clients will need to change and consider.

@AliSoftware AliSoftware added the bug Something isn't working label Oct 29, 2024
@AliSoftware AliSoftware self-assigned this Oct 29, 2024
@AliSoftware AliSoftware marked this pull request as ready for review October 29, 2024 12:53
@AliSoftware AliSoftware requested review from iangmaia and a team October 29, 2024 12:53
@iangmaia
Copy link
Contributor

I've tried to run a simple test:

The result:

[19:16:35]: Creating new pull request from 'merge/iangmaia-test-fix-backmerge-pr-validity-check-into-iangmaia-test-fix-backmerge-pr-validity-check-target' to branch 'iangmaia/test-fix-backmerge-pr-validity-check-target' of 'bloom/DayOne-Android'
[19:16:35]: GitHub responded with 422: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"No commits between iangmaia/test-fix-backmerge-pr-validity-check-target and merge/iangmaia-test-fix-backmerge-pr-validity-check-into-iangmaia-test-fix-backmerge-pr-validity-check-target"}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"}

Also, the lane didn't fail, as it usually doesn't when the creation of PRs fails like this at the GitHub API level.

@iangmaia
Copy link
Contributor

I've tried to run a simple test:

Another very similar test, but with an empty commit: it works fine.

@AliSoftware
Copy link
Contributor Author

iangmaia/test-fix-backmerge-pr-validity-check-target' of 'bloom/DayOne-Android

Strange, because checking the results of merge-base by running the git command in a Terminal, this returns the same sha1 for everyone, so I'd have expected the action to correctly detect that both branches pointed to the same commit and thus skip creating the PR… 🤔

 19:36:08  📂 ~/Documents/Dev/apps/DayOne-Android/  (trunk) 
✓ $ git merge-base origin/merge/iangmaia-test-fix-backmerge-pr-validity-check-into-iangmaia-test-fix-backmerge-pr-validity-check-target origin/iangmaia/test-fix-backmerge-pr-validity-check-target
fc9356c15cba2d03b25112c6397b1c27949b4213

 19:36:20  📂 ~/Documents/Dev/apps/DayOne-Android/  (trunk) 
✓ $ git rev-parse origin/merge/iangmaia-test-fix-backmerge-pr-validity-check-into-iangmaia-test-fix-backmerge-pr-validity-check-target origin/iangmaia/test-fix-backmerge-pr-validity-check-target
fc9356c15cba2d03b25112c6397b1c27949b4213
fc9356c15cba2d03b25112c6397b1c27949b4213

So this means the reasoning we intended to apply is correct, but there's a bug in the current implementation I guess…

@AliSoftware
Copy link
Contributor Author

@iangmaia I've pushed some commits that I think should fix the issue. Can you redo your tests after pointing to the latest commit of this branch? 🙇

Copy link
Contributor

@iangmaia iangmaia left a comment

Choose a reason for hiding this comment

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

I ran a few checks with the latest changes and I correctly had the Backmerge PR created or a Nothing to merge from <source> into <target>. Skipping PR creation. message.

:shipit:

@AliSoftware AliSoftware merged commit 532eec7 into trunk Oct 31, 2024
5 checks passed
@AliSoftware AliSoftware deleted the fix-backmerge-pr-validity-check branch October 31, 2024 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants