How to compare two branches in github with GraphQL?

1.6k views Asked by At

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

1

There are 1 answers

4
tsamridh86 On BEST ANSWER

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>