Some PRs are missing Head Refs


#1

Some GraphQL PullRequest objects are missing HeadRefs, while the V3 API has them. As part of a CI merge bot, we use the GraphQL API to search for open PRs and retrieve their data.

This GraphQL query:

{
 repository(owner:"kubernetes", name:"charts"){
   pullRequest(number:3219) { 
     headRef { target { oid } }
   }
 }
}

Returns "pullRequest": { "headRef": null}, but the equivalent v3 API endpoint (https://api.github.com/repos/kubernetes/charts/pulls/3219) has the head ref as expected.


Github commits returned in the incorrect order, how to get the HEAD commit for statuses
#2

Hi @rmmh :wave:

I’ve created an internal issue to investigate what’s occurring here. We’ll update as soon as we know more.


#3

Hey @rmmh – I learned more about your report.

In the example you provided, the head repo does not exist which is why headRef is null. There is a headRefName field that provides the name of the head Ref even if the ref has been deleted:

{
 repository(owner:"kubernetes", name:"charts"){
   pullRequest(number:3219) { 
     headRefName
   }
 }
}

I hope that’s helpful and please let me know if you have any questions.


#4

Odd. The headRefName doesn’t help in this case, since we want the SHA.

As a workaround we can emulate the desired headRef behavior using commits:

{
  repository(owner: "kubernetes", name: "charts") {
    pullRequest(number: 3219) {
      commits(last: 1) {
        nodes {
          commit {
            oid
          }
        }
      }
    }
  }
}

Which doesn’t seem to cost any more.


#5

Thanks for the update, @rmmh. I’m glad that you found a workaround. Based on your use case, a field on PullRequest like headCommitOid might be useful. We’ve logged this internally as a schema request and will reply here if/when it makes it in.


#6

The workaround isn’t quite correct: commit nodes are ordered by author date, not the commit hierarchy, so it’s possible to get the wrong head in PRs with multiple commits and git rebase.

Our new workaround is requesting headRef { target { oid } } and commits(last:1){nodes{commit{oid}}}, using headRef if present, and hoping that we don’t run into the out-of-order commits case and the deleted base repo edge case simultaneously.


#7

@rmmh Hi!

We’ve just shipped headRefOid and baseRefOid fields on the PullRequest object! This should achieve exactly what you’re looking for :slight_smile: