【问题标题】:How can I get number of commits from Github API until certain branch在某个分支之前,如何从 Github API 获取提交次数
【发布时间】:2017-12-10 12:08:33
【问题描述】:

我想知道在从 Github API 创建某个分支之前完成了多少次提交。

例如在 git cli 中我正在做:git log --no-merges --oneline ${branchHash} | wc -l,我可以看到数字。

Github API 有 100 个限制,所以如果我有超过 100 个提交,我就无法全部获得。

这种情况有什么解决办法吗?

【问题讨论】:

    标签: git github github-api


    【解决方案1】:

    我写了一个小东西来解决这个问题:

    Gist "Easy way to calculate commits count from the GitHub API"。

    它基于使用GitHub Commit APIcompare URL,并使用total_commits字段:

    compare_url = '{}/repos/{}/{}/compare/{}...{}'.format(base_url, owner, repo, first_commit, sha)
    
    commit_count = commit_req.json()['total_commits'] + 1
    

    【讨论】:

    • +1。不鼓励仅提供链接的答案 (meta.stackexchange.com/q/8231/6309),因此我冒昧地添加了一些参考资料、链接和详细信息:如果您认为我遗漏了什么,请进行更多编辑。
    【解决方案2】:

    为了避免执行多个查询,您可以使用GraphQL one,类似于this onethis one:它将获取给定分支的所有提交,以便您计算它们。

    {
      repository(name: "sickvim", owner: "jonathansick") {
        ref(qualifiedName: "master") {
          target {
            ... on Commit {
              id
              history(first: 5) {
                pageInfo {
                  hasNextPage
                }
                edges {
                  node {
                    oid
                  }...
    

    【讨论】:

    • 谢谢伙计,但我尝试了这样的事情并得到了下一个答案:{ "data": null, "errors": [ { "message": "Requesting 101 records on the connection exceeds the `first` limit of 100 records." } ] }
    • 嗨,用过这个:{ repository(name: "sickvim", owner: "jonathansick") { ref(qualifiedName: "master") { target { ... on Commit { id history(first: 101) { pageInfo { hasNextPage } edges { node { messageHeadline oid message author { name email date } } } } } } } } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 2018-01-05
    相关资源
    最近更新 更多