Srijan Guchhait

Retargeting a stacked PR does not run its CI

Why changing the base branch of a pull request leaves its checks missing, what makes GitHub run them again, and how to change the base through the API and check that it worked.

  • github
  • ci
  • git

I work with stacked pull requests. The second PR is based on the branch of the first one, and the third is based on the second. When the first one merges, I retarget the second to main. This post is about a trap in that step. The PR looks ready, but its checks never ran against the new base.

The trap

You change the base of a pull request, and the Checks tab stays as it was. The old results sit there, and they were computed against the old base. The new base has no results at all. A required check can then show as pending forever. Or an old green mark can hide the fact that nothing tested the combination you are about to merge.

Why it happens

The reason is in the workflow trigger. GitHub's documentation says that by default a workflow only runs for a pull_request event when the activity type is opened, synchronize or reopened. A new commit on the head branch is synchronize. Changing the base is none of those three.

The event list does include an edited activity type. If your workflow names it in types, an edit can start a run. I did not test that, so I will not promise it covers a base change. Check it on a throwaway PR before you rely on it.

on:
  pull_request:
    types: [opened, synchronize, reopened]

That is the default set written out. A workflow that lists only these three will not wake up when you retarget.

What does run the checks

Closing the pull request and opening it again produces a reopened event. That is in the default set, so the workflow runs against the current base. A new commit on the head branch also works, because it produces synchronize. An empty commit is a cheap way to get one, but it adds noise to the history, so I prefer close and reopen.

gh pr close 123
gh pr reopen 123

Replace 123 with your number. After the reopen, look at the Checks tab and confirm that the runs carry a fresh timestamp.

Changing the base

The command line has a flag for it.

gh pr edit 123 --base main

I do not trust any change I cannot read back. After an edit, I ask for the base directly.

gh pr view 123 --json baseRefName --jq .baseRefName

If that prints the branch you wanted, the edit worked. If it prints the old branch, the edit did not apply, whatever the command said. The check costs one line, so I run it every time.

When I want a second way to do the edit, I call the REST endpoint. The API documentation for updating a pull request lists the title, the body, the state and the base as fields that you can change. gh api sends the request with your login.

gh api --method PATCH repos/OWNER/REPO/pulls/123 -f base=main

Replace OWNER, REPO and the number. The response is the updated pull request, so you can read the base in it too.

The routine

This is what I do now when a stack moves.

  1. Merge the bottom pull request.
  2. Retarget the next one to main, with gh pr edit or the API.
  3. Read the base back with gh pr view.
  4. Close and reopen the pull request so that the checks run on the new base.
  5. Wait for the new runs, and merge only when they are green.

Step 4 is the one I used to skip. Skipping it means the merge button can turn green on results that describe the old base. A stack makes that likely, because the old base is a branch that no longer exists in the form the checks saw.

A note on tokens

One more detail from the same documentation. When a workflow changes or opens a pull request with the built-in GITHUB_TOKEN, the resulting pull_request events make runs that need approval. Most other events from that token make no runs at all. If an automated job retargets your pull requests, expect to start the checks yourself.

← All posts