如何使用GraphQL比较github中的两个分支?

问题描述:

我们可以将两个分支与Github GraphQL进行比较吗?

Can we compare two branches with the Github GraphQL?

通过他们的v3 rest API,您可以执行以下操作:

From their v3 rest API, you can do:

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

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

这适用于SHA,分支,​​标签等.

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

但是,我在文档中找不到等效的GraphQL查询.

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
  }
}

这本来可以做为:

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

在v3 REST API中

in the v3 REST API

不幸的是,滚动浏览了github社区页面的各个小时之后,看来,截至该日期,该API尚未在v4上迁移,并且必须通过v3本身完成.

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.

但是,v3 API仅支持250次提交,超出此范围的任何提交都将被忽略并且不会显示.不过,这可以通过使用graphQL来解决.

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.

将这些值插入到:

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
  }
}

并一次提取所有oid,一次提取100个(Github GraphQL一次仅支持100个)

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

最后,您也可以调用v3 API:

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>