Github GraphQL - 获取存储库的提交列表

时间:2018-01-16 16:36:00

标签: github graphql github-api github-graphql

我使用GraphQL使用Github的GraphQL(v4)API从存储库列表中获取一些数据。我想从存储库中获取最新提交的列表,无论commit的branch / tag / ref是什么。

目前我正在执行以下操作以获取某个存储库的提交列表:

... on Repository{
    refs(refPrefix:"refs/",orderBy:$refOrder,first:1){
        edges{
            node{
                ... on Ref{
                    target{
                        ... on Commit{
                            history(first:10){
                                totalCount
                                edges{
                                    node{
                                        ... on Commit{
                                            committedDate
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

$refOrder是我与请求一起发送的对象,其定义如下:

{
    "refOrder": {
        "direction": "DESC",
        "field": "TAG_COMMIT_DATE"
    }
}

这段代码正在运行,但没有检索到我想要的结果。响应返回一个提交列表,但不一定是来自存储库的最后提交。当我进入存储库页面并单击“提交”时,我通常会看到一个提交列表,这些提交比我从API调用中获得的结果更新。

我错过了什么?我应该尝试不同的refPrefixorderBy参数吗?我已经尝试将“master”作为refPrefix,但遇到了同样的问题。

2 个答案:

答案 0 :(得分:3)

刚才意识到我所寻找的是Repository对象中存在的一个名为defaultBranchRef的字段。使用此字段,我能够检索我正在寻找的数据。

我的查询现在看起来像这样:

... on Repository{
    defaultBranchRef{
        target{
            ... on Commit{
                history(first:10){
                    edges{
                        node{
                            ... on Commit{
                                committedDate
                            }
                        }
                    }
                }
            }
        }
    }
}

答案 1 :(得分:2)

如果您也有兴趣获取所有分支的最新提交(不仅仅是默认分支),您可以请求前缀为refs/heads/的引用:

{
  repository(owner: "bertrandmartel", name: "callflow-workshop") {
    refs(refPrefix: "refs/heads/", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, first: 100) {
      edges {
        node {
          ... on Ref {
            name
            target {
              ... on Commit {
                history(first: 2) {
                  edges {
                    node {
                      ... on Commit {
                        committedDate
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

在你使用refs/的情况下,你也给了你标记参考。

Try it in the explorer