Last active
December 6, 2022 18:33
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you da man