Created
August 10, 2013 10:39
-
-
Save qmacro/6199968 to your computer and use it in GitHub Desktop.
#YRS2013 Hacks On Github - code to retrieve commit counts from Github API and store in a Google spreadsheet
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
// 0-indexed columns | |
COMMITS = 3; | |
GITHUBREPO = 2; | |
TOKEN = ""; | |
function main() { | |
TOKEN = ScriptProperties.getProperty("githubtoken"); | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sh = ss.getSheetByName("Hacks"); | |
var repolist = getHackRepoList(sh); | |
var commits = []; | |
for (var i = 0; i < repolist.length; i++) { | |
var repo = repolist[i]; | |
if (repo[0]) { | |
var count = pullCommitCount(repo[0]); | |
putHackRepoCommitCount(sh, i, count); | |
} | |
} | |
} | |
/* | |
* Returns a list of tuples: user/repo and current commit count | |
* | |
*/ | |
function getHackRepoList(sh) { | |
var data = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues(); | |
return data.map(function(row) { return [row[GITHUBREPO], row[COMMITS]] ; }); | |
} | |
/* | |
* Update a commit counter (for the nth repo retrieved) | |
* | |
*/ | |
function putHackRepoCommitCount(sh, n, count) { | |
sh.getRange(2 + n, COMMITS + 1, 1, 1).setValue(count); | |
} | |
/* | |
* Retrieves the commits for a repo via the Github API; | |
* follows the 'next' paging via the Link header. | |
* Returns a count of commits | |
*/ | |
function pullCommitCount(repo) { | |
var httpoptions = { | |
"headers": { | |
"User-Agent": "qmacro-yrs2013-commits (dj.adams@pobox.com)", | |
"Authorization": "Basic " + Utilities.base64Encode("qmacro" + ":" + TOKEN) | |
}, | |
"muteHttpExceptions": true | |
}; | |
sheetLog("Retrieving commit info for " + repo); | |
var url = "https://api.github.com/repos/" + repo + "/commits?per_page=100"; | |
var commitcount = 0; | |
// return Math.floor(Math.random() * 100); | |
while (url) { | |
var response = UrlFetchApp.fetch(url, httpoptions); | |
var responseheaders = response.getHeaders(); | |
sheetLog("Remaining calls available: " + responseheaders["X-RateLimit-Remaining"]); | |
Utilities.sleep(1000); | |
// Assume no further pages of commit info | |
url = null; | |
if (response.getResponseCode() == 200) { | |
// Add the commits returned to the overall count | |
var commitinfo = Utilities.jsonParse(response.getContentText()); | |
commitcount += commitinfo.length; | |
sheetLog("commitcount is now " + commitcount); | |
// Is there more (via a next-page URL)? | |
var linkinfo = responseheaders["Link"]; | |
if (linkinfo) { | |
sheetLog(linkinfo); | |
var pagelinks = linkinfo.match(/<(.+?)>; rel="next"/); | |
if (pagelinks) { | |
url = pagelinks[1]; | |
sheetLog("Setting next page URL"); | |
} | |
} | |
} | |
} | |
return commitcount; | |
} | |
function sheetLog(message) { | |
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Log"); | |
sh.getRange(sh.getLastRow() + 1, 1, 1, 2).setValues([[new Date(), message]]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment