Skip to content

Instantly share code, notes, and snippets.

@SourcingDenis
Created March 13, 2023 19:50
Show Gist options
  • Save SourcingDenis/b4a3fb85a50d8db09a2b42e9c42d9659 to your computer and use it in GitHub Desktop.
Save SourcingDenis/b4a3fb85a50d8db09a2b42e9c42d9659 to your computer and use it in GitHub Desktop.
function getGitHubEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveRange();
var username = range.getValue();
var adjacentCell = range.offset(0, 1); // adjust this if the email is in a different column
var email = "";
if (username.indexOf("github.com") >= 0) {
username = username.replace(/^.*com[/]([^/]*).*$/,'$1');
}
var apiUrl = "https://api.github.com/users/" + username + "/events/public";
var response = UrlFetchApp.fetch(apiUrl);
var data = JSON.parse(response.getContentText());
var commits = [];
data.forEach(function(event) {
if (event.type === "PushEvent") {
commits.push.apply(commits, event.payload.commits);
}
});
var emails = [];
commits.forEach(function(commit) {
if (commit.author && commit.author.email) {
emails.push(commit.author.email);
}
});
if (emails.length > 0) {
email = emails[0]; // change this if you want to use a different email from the list
}
adjacentCell.setValue(email);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment