Counting the number of Commits in Repositories


#1

Hi All! I’m new to this github api and specially new to GraphQL API. I’ve been trying it out and wrote a query to find the number of repositories and commits in the Mozilla github. https://github.com/mozilla/activity-stream

Comparing the number of commits in the repository activity-stream ( 2,872). However in my query it lists a couple of number that when added together don’t add up to 2,872.

Also is there a way to list the highest number of commits or only return the highest number by using orderBy or some other type of code.

My Query
{
organization(login:“mozilla”) {
name,
repositories(first:100, orderBy:{field:NAME,direction:ASC}){
totalCount
edges{
node{
name,
commitComments(first:10){
totalCount,

  nodes{ 
    commit {
      history{
        totalCount
      }
    }
  }
  
}
    }
  }
  
}

}
}

Results
{
“data”: {
“organization”: {
“name”: “Mozilla”,
“repositories”: {
“totalCount”: 1708,
“edges”: [

I omitted the other repositories due to length

{
“node”: {
“name”: “activity-stream”,
“commitComments”: {
“totalCount”: 17,
“nodes”: [
{
“commit”: {
“history”: {
“totalCount”: 55
}
}
},
{
“commit”: {
“history”: {
“totalCount”: 78
}
}
},
{
“commit”: {
“history”: {
“totalCount”: 78
}
}
},
{
“commit”: {
“history”: {
“totalCount”: 107
}
}
},
{
“commit”: {
“history”: {
“totalCount”: 116
}
}
},
{
“commit”: {
“history”: {
“totalCount”: 228
}
}
},
{
“commit”: {
“history”: {
“totalCount”: 866
}
}
},
{
“commit”: {
“history”: {
“totalCount”: 866
}
}
},
{
“commit”: {
“history”: {
“totalCount”: 1600
}
}
},
{
“commit”: {
“history”: {
“totalCount”: 1600
}
}
}
]
}
}
},


#2

Hi @Jezzjezz ,

I don’t think commitComments is the connection you’re looking for here.

There is no way to calculate the total number of commit objects in a repository across all branches. However, if you want the total number of commits on a specific branch, master for example, this query will work:

query {
  repository(name: "name", owner: "owner"){
    ref(qualifiedName: "master"){
      target{
        ... on Commit{
          history{
            totalCount
          }
        }
      }
    }
  }
}

Will this work for you?


#3

Hi @d12 I am looking for a similar type of query,
I would like to compute the total number of commits on an organization, per year. Is that something doable with the new graphql API?

Thanks!


#4

Hi @vvo!

I think something like this is possible with a GraphQL query and some light addition!

I just came up with the following query:

query {
  organization(login: "YOUR_ORG"){
    repositories(first: 10) {
      nodes {
        nameWithOwner
        defaultBranchRef {
          target {
            ...on Commit {
              history(since: "2018-01-01T00:00:00+00:00") {
                totalCount
              }
            }
          }
        }
      }
    }
  }
}

You can also do something like history(since: "2017-01-01T00:00:00+00:00", before:"2018-01-01T00:00:00+00:00") to get all of the commits for 2017, for example.

Depending on the number (and size) of the repositories, you may need to paginate with less than 100 repositories at a time to avoid having the query take too long.

Let me know!


#5

Thanks exactly what I discovered by doing try and catch on the explorer (awesome tool).

About:

I could not find documentation on this notation “…on …”, do you have any link documenting it? Thanks


#6

Hey @vvo!

Definitely! It’s called the ‘spread operator’ - I don’t think there’s a lot of official documentation about it. Here are a couple of resources for more reading:


#7

Hi @nickvanw it all went pretty well and I built https://github.com/vvo/zorgs! :smiley: