4

How to search for Github Repositories using GraphQL, and get its total commits count as well in return?

It looks strange to me that all fields available describing Repositories contains total count of commit comments but not total count of commits.

Hugo y
  • 1,421
  • 10
  • 20
xpt
  • 20,363
  • 37
  • 127
  • 216
  • This Q/A helped me find a [better Github search approach](https://medium.com/@suntong001/query-github-graphql-9a4547d33bad), FYI. – xpt Mar 31 '18 at 20:05

2 Answers2

9

Here's an example of how to get the total number of commits for the master branch in the rails/rails repository:

query {
  repository(owner:"rails", name:"rails") {
    object(expression:"master") {
      ... on Commit {
        history {
          totalCount
        }
      }
    }
  }
}
bswinnerton
  • 4,533
  • 8
  • 41
  • 56
  • OMG bswinnerton, you are better than the Github insiders that support GraphQL -- they said that it is [impossible now and hard to do in future](https://platform.github.community/t/schema-request-repository-total-contributors-and-commits/5405). Amazing! – xpt Mar 30 '18 at 19:01
  • 1
    I may _be_ a GitHub insider – bswinnerton May 27 '18 at 19:27
  • @bswinnerton Please can tell me how I'll make request using C# to call API v4 query for getting commit count, folks count, watch count & Created Date for a repository?? I've list of some repository – dilipkumar1007 Oct 25 '18 at 05:14
  • 1
    what does ... on Commit mean? – jjxtra Feb 23 '20 at 20:40
  • @jjxtra It is an [inline fragment](https://graphql.org/learn/queries/#inline-fragments) – jdprc06 Dec 17 '20 at 17:47
  • This actually helped with GitLab's GraphQL API! Thanks!! – Anwar Hossain Sep 18 '21 at 05:27
3

bswinnerton's answer works well, but not for repositories without a master branch. In this case you can use defaultBranchRef

Here's an example of how to get the total number of commits for the default branch in the rails/rails repository

query {
  repository(owner:"rails", name:"rails") {
    defaultBranchRef {
      target {
        ... on Commit {
          history {
            totalCount
          }
        }
      }
    }
  }
}
tiger
  • 225
  • 1
  • 9