2

I'm creating a GitHub Action which runs a check to see how a pull request affects the codebase. The check needs two inputs:

  1. The repository state before the PR changes
  2. The repository state after the PR changes

The 2nd input is easy: its the newest commit on the PR branch

The 1st one is a bit tricky and I'm having trouble figuring it out. It can't simply be the master of the main (upstream) repository, because of the potential rebase issues (new changes are already on upstream/master which are not in the forks' master). So I'm using the merge-base between the master branch and the HEAD.

git merge-base master HEAD

This works as expected, but the problem arises if the PR is made from the forks' master branch. The merge-base is the same as HEAD.

How to get the correct SHA in order to checkout the repo just before the changes made on PR. GitHub handles this situation correctly and shows only the changed files.

prosoitos
  • 6,679
  • 5
  • 27
  • 41
Marcin Kunert
  • 5,596
  • 5
  • 26
  • 52
  • 1
    Do you really need both inputs? Would the diff work? If the diff of the PR could work, [that's easy to get](https://stackoverflow.com/q/6188591/9210961) – prosoitos Oct 24 '20 at 03:34
  • 1
    Wow, thanks for the tip! I'm not sure yet if this is applicable to my usecase, but it definitely opens up some possibilities in the future. – Marcin Kunert Oct 24 '20 at 06:43

2 Answers2

1

If you run this action after the PR has been merged, you should look for the merge base of the parents of the PR commit :

git merge-base HEAD^ HEAD^2
LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

The solution that seems to work is to find the merge base between pull request HEAD and upstream master.

git remote add upstream https://github.com/${{github.repository}}
git fetch upstream
merge_base=$(git merge-base refs/remotes/upstream/master HEAD)
git checkout $merge_base

This assumes that the pull request is pointed to the master branch.

Marcin Kunert
  • 5,596
  • 5
  • 26
  • 52