Efficiently retrieving stats for all GitHub Commits

那年仲夏 提交于 2019-12-21 17:50:10

问题


Is there a more efficient way of getting the count of additions/deletions related to a commit than looping through every single commit and calling the:

GET /repos/:owner/:repo/commits/:sha

(https://developer.github.com/v3/repos/commits/)

Just to get the:

"stats": {
   "additions": 104,
   "deletions": 4,
   "total": 108
},

Data?

Unfortunately the commits endpoint:

GET /repos/:owner/:repo/commits

Contains a lot of data about each commit but not this detail which means a huge number of additional API calls to get it.


回答1:


Whenever you need multiple GitHub API query, check if GraphQL (introduced by GitHub last Sept. 2016) could allow you to get all those commits in one query.

You can see examples here and apply to GitHub GraphQL early access, but that sees to be the only way to get:

  • all stats from all commits (and only the stats)
  • in one query



回答2:


It is now possible to get commit stats (additions, deletions & changedFiles count) using the GraphQL API :

To get commit stats for the 100 first commit on the default branch :

{
  repository(owner: "google", name: "gson") {
    defaultBranchRef {
      name
      target {
        ... on Commit {
          id
          history(first: 100) {
            nodes {
              oid
              message
              additions
              deletions
              changedFiles
            }
          }
        }
      }
    }
  }
}

Try it in the explorer

To get commit stats for the first 10 branches, for the 100 first commits of each one of these branches:

{
  repository(owner: "google", name: "gson") {
    refs(first: 10, refPrefix: "refs/heads/") {
      edges {
        node {
          name
          target {
            ... on Commit {
              id
              history(first: 100) {
                nodes {
                  oid
                  message
                  additions
                  deletions
                  changedFiles
                }
              }
            }
          }
        }
      }
    }
  }
}

Try it in the explorer



来源:https://stackoverflow.com/questions/40695403/efficiently-retrieving-stats-for-all-github-commits

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!