2

Can we compare two branches with the Github GraphQL?

From their v3 rest API, you can do:

/repos/:owner/:repo/compare/:base...:head

(docs: https://developer.github.com/v3/repos/commits/#compare-two-commits)

and this works with SHA's, branches, tags, etc.

However, I'm unable to find it's equivalent GraphQL query in the docs.

This is my attempt so far :

I'm able to get the list of commits for each branch seperately, however, the entire history is loaded and I would only like the difference between canary branch and nightly branch.

query{
  repository(owner:"samridh",name:"release-generator"){
    name
    branch0: ref(qualifiedName: "canary"){
      target{
        ... on Commit {
         history(first:100){
           ...CommitFragment
         }
       }
      }
    }
    branch1: ref(qualifiedName: "nightly"){
      target{
        ... on Commit {
         history(first:100){
           ...CommitFragment
         }
       }
      }
    }
  }
}
             
fragment CommitFragment on CommitHistoryConnection {
  totalCount
  nodes {
    oid
    message
    committedDate
    author {
      name
      email
    }
  }
  pageInfo {
    hasNextPage
    endCursor
  }
}

This would have been done as :

/repos/samridh/release-generator/compare/nightly...canary

in the v3 REST API

tsamridh86
  • 1,480
  • 11
  • 23

1 Answers1

4

Unfortunately, after scrolling through hours and hours of the github community page, it seems that as of this date, the API is not migrated on the v4, and must be done via v3 itself.

However, the v3 API only supports 250 commits, any commits beyond that will be ignored and not shown. This can be worked around using graphQL though.

Fire this query to get the starting and ending points:

query getStartAndEndPoints {
  repository(owner: "samridh", name: "release-generator") {
    endPoint: ref(qualifiedName: "canary") {
      ...internalBranchContent
    }
    startPoint: ref(qualifiedName: "nightly") {
      ...internalBranchContent
    }
  }
}

fragment internalBranchContent on Ref {
  target {
    ... on Commit {
      history(first: 1) {
        edges {
          node {
            committedDate
          }
        }
      }
    }
  }
}

This will give you the start and end date of the query.

Plugin these values to :

query findDifference{
  repository(owner:"samridh",name:"release-generator"){
    ref(qualifiedName: "canary"){
      target{
        ... on Commit {
         history(
                  first : 100,
                  after: $(value of previous end cursor) #keep it empty first time
                  until : $(endDate),
                  since: $(startDate),
                  ){
           ...CommitFragment
         }
       }
      }
    }
  }
}

fragment CommitFragment on CommitHistoryConnection {
  totalCount
  nodes {
    oid
  }
  pageInfo {
    startCursor
    hasNextPage
    endCursor
  }
}

and extract all the oid, 100 at a time ( Github GraphQL only supports 100 at a time )

Finally, you can call the v3 API, likewise :

/repos/samridh/release-generator/compare/<commit1>...<commit100>
/repos/samridh/release-generator/compare/<commit101>...<commit200>
/repos/samridh/release-generator/compare/<commit201>...<commit300>
tsamridh86
  • 1,480
  • 11
  • 23
  • Crazy how difficult the API is! `Ref` object has a `compare` field that you can use to compare against another head ref. Computationally, that wouldn't be any cheaper than directly comparing commits if anyone can create a branch from any commit. – Chuanqi Sun Oct 28 '22 at 06:35
  • Have you found any feature requests that I can help you vote up for? I really need this feature too. – Chuanqi Sun Oct 28 '22 at 06:38
  • sorry, i haven't tracked this for quite some time now and have kept this work around running till now. If you do find a feature request, let me know, i'll upvote it too! – tsamridh86 Oct 29 '22 at 07:12
  • Found a feature request for this in the community discussion board: https://github.com/orgs/community/discussions/11358 – ento Apr 22 '23 at 18:02