How to compare two branches in github with GraphQL?

社会主义新天地 提交于 2021-01-29 05:15:15

问题


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:


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>


来源:https://stackoverflow.com/questions/64202547/how-to-compare-two-branches-in-github-with-graphql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!