Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently retrieving stats for all GitHub Commits

Tags:

github-api

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.

like image 864
chrisb Avatar asked Nov 19 '16 16:11

chrisb


2 Answers

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
like image 182
VonC Avatar answered Nov 12 '22 05:11

VonC


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

like image 1
Bertrand Martel Avatar answered Nov 12 '22 05:11

Bertrand Martel