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

16
我正在使用GraphQL通过Github的GraphQL (v4) API从一系列存储库中获取数据。我想要获取一个存储库中的最新提交列表,无论提交的分支/标记/引用是什么。
目前,我正在执行以下操作来获取某个存储库的提交列表:
... 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"
    }
}

这段代码能够运行,但是返回的结果不是我想要的。响应会返回一份提交记录列表,但不一定是仓库中最新的提交。当我去仓库页面并点击"Commits"时,通常会看到比我的API调用结果更新的提交列表。
我错过了什么吗?我应该尝试不同的 refPrefixorderBy 参数吗?我已经尝试了 "master" 作为 refPrefix,但问题依旧。

1
在过去的几天里,我学到了很多关于GraphQL的知识,并在Medium上总结了我的经验:https://medium.com/@fabiomolinar/using-githubs-graphql-to-retrieve-a-list-of-repositories-their-commits-and-some-other-stuff-ccbbb4e96d78 - FTM
2个回答

22

刚刚意识到我在寻找的是存储库对象中称为 defaultBranchRef 的字段。使用该字段,我成功检索到了我想要的数据。

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

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

13

如果你也对获取所有分支(而不只是默认分支)的最新提交感兴趣,你可以使用前缀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/也给出了标签引用。
请在浏览器中试一试

请问这个的基本URL是什么? - anju jo
1
@anjujo 是关于 Github GQL API 的吗?如果是,这里是它的链接:https://api.github.com/graphql - FernandoH-G
请求在链接中被阻止。 - Timo
仓库(Repository)没有 refs 字段...? - detly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接