Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding total contributions of a user from Github API

Tags:

I am trying to extract the below info for any user from github.

enter image description here

Is there a way/api exposed in github-api where we can get this information directly ?

like image 577
AnkitG Avatar asked Aug 15 '13 21:08

AnkitG


2 Answers

Answers for 2019, Use GitHub API V4.

First go to GitHub to apply for a token: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line. step 7, scopes select only read:user

cUrl

curl -H "Authorization: bearer token" -X POST -d '{"query":"query {\n  user(login: \"MeiK2333\") {\n    name\n    contributionsCollection {\n      contributionCalendar {\n        colors\n        totalContributions\n        weeks {\n          contributionDays {\n            color\n            contributionCount\n            date\n            weekday\n          }\n          firstDay\n        }\n      }\n    }\n  }\n}"}' https://api.github.com/graphql 

JavaScript

async function getContributions(token, username) {     const headers = {         'Authorization': `bearer ${token}`,     }     const body = {         "query": `query {             user(login: "${username}") {               name               contributionsCollection {                 contributionCalendar {                   colors                   totalContributions                   weeks {                     contributionDays {                       color                       contributionCount                       date                       weekday                     }                     firstDay                   }                 }               }             }           }`     }     const response = await fetch('https://api.github.com/graphql', { method: 'POST', body: JSON.stringify(body), headers: headers })     const data = await response.json()     return data }  const data = await getContributions('token', 'MeiK2333') console.log(data) 
like image 72
MeiK Avatar answered Oct 18 '22 23:10

MeiK


Yes, You can do this easily with the new graphql API

Check out the explorer: https://developer.github.com/v4/explorer/

There you can see the contributions collection which is an edge of the user. You can get all of the information necessary to rebuild the calendar.

I've included a full example, and the explorer documentation can guide you even further.

Specifically to answer your question, the query.user.contributionsCollection.contributionsCalendar.totalContributions is what you are looking for

Go ahead and copy/paste the following into the explorer and you will see my contribution history for the last year

query {    user(login: "qhenkart") {     email     createdAt     contributionsCollection(from: "2019-09-28T23:05:23Z", to: "2020-09-28T23:05:23Z") {       contributionCalendar {         totalContributions         weeks {           contributionDays {             weekday             date              contributionCount              color           }         }         months  {           name             year             firstDay            totalWeeks                     }       }     }   }    } 
like image 41
Quest Avatar answered Oct 18 '22 21:10

Quest