Skip to content

Instantly share code, notes, and snippets.

@yefim
Last active December 6, 2022 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yefim/78549d2d838f89459027ef78f29bca1d to your computer and use it in GitHub Desktop.
Save yefim/78549d2d838f89459027ef78f29bca1d to your computer and use it in GitHub Desktop.
To install into Google Sheets, follow the "Creating a custom function" instructions found here: https://developers.google.com/apps-script/guides/sheets/functions and paste the snippet below into the editor. Follow the instructions to create a personal access token. After you install, use =TOTAL_CONTRIBUTIONS("yefim") in your sheet.
/**
*
* YOUR GITHUB PERSONAL ACCESS TOKEN GOES BELOW!
*
* To create a token, follow these instructions: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
* For Step 7, only select the "read:user" scope.
*/
var PERSONAL_ACCESS_TOKEN = 'yourtokengoeshere';
var getDateRange = function(days) {
days = days || 30;
var today = new Date();
today.setHours(0, 0, 0);
today.setMilliseconds(0);
var thirtyDaysAgo = new Date(today.valueOf());
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - days);
return {from: thirtyDaysAgo, to: today};
}
var getRequestOptions = function(username, range) {
username = username.substring(username.lastIndexOf('/') + 1); // strip everything after last slash
var query = ['query {',
'user(login: "' + username + '") {',
'contributionsCollection(from: "' + range.from.toISOString() + '", to: "' + range.to.toISOString() + '") {',
'startedAt',
'endedAt',
'contributionCalendar {',
'totalContributions',
'weeks {',
'contributionDays {',
'contributionCount',
'date',
'}',
'}',
'}',
'}',
'}',
'}'].join('\n');
var options = {
'method' : 'post',
'contentType': 'application/json',
'headers': {
'Authorization': 'Bearer ' + PERSONAL_ACCESS_TOKEN
},
'payload' : JSON.stringify({query: query})
};
return options;
}
function TOTAL_DAYS_CONTRIBUTED(username, days) {
if (PERSONAL_ACCESS_TOKEN === 'yourtokengoeshere') return 'Please fill in your personal access token by following the instructions inside the script.';
if (!username) return 'Please pass a GitHub username or link as the first argument.';
var range = getDateRange(days);
var requestOptions = getRequestOptions(username, range);
var textResponse = UrlFetchApp.fetch('https://api.github.com/graphql', requestOptions).getContentText();
var res = JSON.parse(textResponse);
var weeks = res.data.user.contributionsCollection.contributionCalendar.weeks;
var totalDays = 0;
// Count the contribution days
weeks.forEach(function(week) {
week.contributionDays.forEach(function(day) {
if (day.contributionCount > 0) {
totalDays += 1;
}
});
});
Logger.log(totalDays);
return totalDays;
}
function TOTAL_CONTRIBUTIONS(username, days) {
if (PERSONAL_ACCESS_TOKEN === 'yourtokengoeshere') return 'Please fill in your personal access token by following the instructions inside the script.';
if (!username) return 'Please pass a GitHub username or link as the first argument.';
var range = getDateRange(days);
var requestOptions = getRequestOptions(username, range);
var textResponse = UrlFetchApp.fetch('https://api.github.com/graphql', requestOptions).getContentText();
var res = JSON.parse(textResponse);
var contribs = res.data.user.contributionsCollection.contributionCalendar.totalContributions;
Logger.log(contribs);
return contribs;
}
@yefim
Copy link
Author

yefim commented Jan 25, 2020

Some example output

Screen Shot 2020-01-25 at 2 02 12 PM

Video demo

https://streamable.com/j9abz

@TravisFox
Copy link

you da man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment