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

How to compare two branches in github with GraphQL?

我们可以用 Github GraphQL 比较两个分支吗?

从他们的 v3 rest API,你可以:

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

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

这适用于 SHA、分支、标签等。

但是,我无法在文档中找到它的等效 GraphQL 查询。

这是我目前的尝试:

我能够分别获取每个分支的提交列表,但是,整个历史都已加载,我只想要金丝雀分支和夜间分支之间的区别。

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

不幸的是,在 github 社区页面上滚动浏览了几个小时之后,似乎截至目前,API 尚未在 v4 上迁移,必须通过 v3 完成本身。

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

触发此查询以获取起点和终点:

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

这将为您提供查询的开始和结束日期。

将这些值插入到:

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个)

最后,你可以调用v3 API,同样的:

/repos/samridh/release-generator/compare/<commit1>...<commit100>
/repos/samridh/release-generator/compare/<commit101>...<commit200>
/repos/samridh/release-generator/compare/<commit201>...<commit300>