Skip to content

Instantly share code, notes, and snippets.

@EddyVerbruggen
Last active October 3, 2018 19:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EddyVerbruggen/e394b07d1e2817effe551a4943fba0d4 to your computer and use it in GitHub Desktop.
Save EddyVerbruggen/e394b07d1e2817effe551a4943fba0d4 to your computer and use it in GitHub Desktop.
Google Apps Script that adds GitHub repo and PR labels to your Gmail inbox
function processInbox() {
var threads = GmailApp.search("is:unread in:inbox has:nouserlabels from:notifications@github.com newer_than:1h");
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
processMessage(messages[j]);
}
}
}
function processMessage(message) {
var body = message.getRawContent();
var subject = message.getSubject();
var subjectFilter = "[EddyVerbruggen/"; // you may want to adjust this line, to filter only your own repos
if (subject.indexOf(subjectFilter) > -1) {
var repo = subject.substring(subject.indexOf("/") + 1, subject.indexOf("]"));
if (repo) {
// add the 'my-github-repo' label
if (!GmailApp.getUserLabelByName(repo)) {
GmailApp.createLabel(repo);
}
message.getThread().addLabel(GmailApp.getUserLabelByName(repo));
// add the 'PR' label, if applicable
var isPullRequest = body.indexOf("/pull/") > -1;
if (isPullRequest) {
if (!GmailApp.getUserLabelByName("PR")) {
GmailApp.createLabel("PR");
}
message.getThread().addLabel(GmailApp.getUserLabelByName("PR"));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment