Skip to content

Instantly share code, notes, and snippets.

@mapsam
Created January 14, 2016 04:51
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 mapsam/8b39321e7f2abebc14a8 to your computer and use it in GitHub Desktop.
Save mapsam/8b39321e7f2abebc14a8 to your computer and use it in GitHub Desktop.
Gmail Label Runner
// append old log to the new log
Logger.log(Logger.getLog());
function processInbox() {
var threads = GmailApp.getInboxThreads(0, 25);
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var messages = thread.getMessages();
// GITHUB
if (messages && ((messages[0].getFrom()).indexOf("notifications@github.com") > -1)) {
// run through each message since there are long threads and might get mentioned in a
// message down the road, not necessarily the first
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
githubSort(message, thread);
}
thread.moveToArchive(); // move out of inbox, but don't mark as read
}
// CALENDAR & MEETINGS
if (messages[0].getFrom().has("uberconference.com") ||
messages[0].getRawContent().has("calendar-notification@google.com") ||
// insideicloud.icloud.com might be too generic, but will roll with it until
// something gets borked
messages[0].getFrom().has("insideicloud.icloud.com")) {
label("cal", thread);
thread.moveToArchive();
}
}
}
function githubSort(message, thread) {
var body = message.getRawContent();
var subject = message.getSubject();
// github notifications
if (body.has("X-GitHub-Reason: author") || body.has("X-GitHub-Reason: comment")) {
label("participating", thread);
}
if (body.has("X-GitHub-Reason: mention")) {
label("mention-name", thread);
}
if (body.has("X-GitHub-Reason: team_mention")) {
label("mention-team", thread);
}
if (body.has("X-GitHub-Reason: assign")) {
label("assigned", thread);
}
// mapnik
if (subject.has("[mapnik-internal]") ||
subject.has("[mapnik-vector-tile]") ||
subject.has("[mapnik]")) {
label("mapnik", thread);
}
// hey
if (subject.has("[hey]")) {
label("hey", thread);
}
}
// abstracted labelling
function label(l, t) {
t.addLabel(GmailApp.getUserLabelByName(l));
}
// boolean shorthand for indexOf, returns true/false
String.prototype.has = function(check) {
return this.indexOf(check) > -1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment