代码之家  ›  专栏  ›  技术社区  ›  yershalom

我如何从Github API获取提交数,直到某个分支

  •  6
  • yershalom  · 技术社区  · 7 年前

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

    例如,在git cli中,我正在执行以下操作: git log --no-merges --oneline ${branchHash} | wc -l 我可以看到号码。

    Github API有100个限制,因此如果提交次数超过100次,则无法全部提交。

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

    2 回复  |  直到 7 年前
        1
  •  1
  •   VonC    7 年前

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

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

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

    compare_url = '{}/repos/{}/{}/compare/{}...{}'.format(base_url, owner, repo, first_commit, sha)
    
    commit_count = commit_req.json()['total_commits'] + 1
    
        2
  •  0
  •   VonC    7 年前

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

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