1

I'm trying to build a query with the GitHub API v4 (GraphQL) to get the number of contributors.

At the moment I have something of the likes of

query ($owner: String!, $name: String!) {
  repository(owner: $owner, name: $name) {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          history(first: 100) {
            nodes {
              author {
                name
              }
            }
            pageInfo {
              hasNextPage
              endCursor
            }
          }
        }
      }
    }
  }
}

where I'm going through all the commits and get the name of the Authors (at the time I was trying to get the number of commits for contributor), but for repositories with a large amount of commits, this takes a lot of time.

So back to my question, is the a way to get only the number of contributors in a repository?

ElasticCode
  • 7,311
  • 2
  • 34
  • 45
  • Possible duplicate of [How do you get total contributions with Githubs API v4](https://stackoverflow.com/questions/44579877/how-do-you-get-total-contributions-with-githubs-api-v4) – Daniel Rearden Apr 15 '18 at 13:37

2 Answers2

1

As far as I know, this is only possible with the REST API (v3), by requesting only one item per page, and extracting the total number of pages in the Response headers.

machour
  • 704
  • 1
  • 7
  • 16
1

With this graphql query you can get the: - total releases count - total branches count - total commits count

query{
  repository(owner:"kasadawa", name:"vmware-task") {

    Releases:refs(first: 0, refPrefix: "refs/tags/") {
      totalCount
    }
    Branches:refs(first: 0, refPrefix: "refs/heads/") {
      totalCount

    }

    object(expression:"master") {

      ... on Commit {

        history {
          totalCount
        }
      }
    }
  }
}

But if you want to get the contributors, you should do it with the REST API, because currently there is no simple way to implement it with GRAPHQL API.

Here is a solutions with the REST API.


const options = {
    url: 'https://api.github.com/repos/vmware/contributors' ,
    'json': true ,
    headers: { 
        'User-Agent':'request',
         'Authorization': 'token API_KEY_GENERATED_FROM_GITHUB'
        }
    };


var lastPageNum = 0 ; // global variable 

request.getAsync(options).then((res) =>{

        this.checkHeaders(res.headers.status) // just check the header status 

        // check if there are more pages 
        if(!!res.headers.link){

            const [ , lastURL] = res.headers.link.split(','); // getting the last page link from the header 
            lastPageNum = +lastURL.match(/page=(\d+)/)[1]; // get the number from the url string
            options.url = licenseUrl + lastPageNum; 
            return request.getAsync(options) // make Request with the last page, in order to get the last page results, they could be less than 30
        }else{
            // if its single page just resolve on to the chain 

            return Promise.resolve(res.body.length);
        }

    })
    .then((lastPageRes)=>{

        return (lastPageNum !== 0 
            ? ( (lastPageNum-1)*30 + lastPageRes.body.length ) 
            : lastPageRes)

        })
    .catch() // handle errors 


Checkout for updates: https://github.community/t5/GitHub-API-Development-and/Get-contributor-count-with-the-graphql-api/td-p/18593