Skip to content

Instantly share code, notes, and snippets.

@cmsj
Created August 4, 2020 20:24
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 cmsj/23935c42a10cf10c9ea0d544b8c31f65 to your computer and use it in GitHub Desktop.
Save cmsj/23935c42a10cf10c9ea0d544b8c31f65 to your computer and use it in GitHub Desktop.
Google Script mail header example
function processInbox() {
// process all recent threads in the Inbox (see comment to this answer)
var threads = GmailApp.search("from:(bugzilla@redhat.com) newer_than:1d");
GmailApp.createLabel('Bugzilla');
Logger.log("Got threads: " + threads.length);
for (var i = 0; i < threads.length; i++) {
processThread(threads[i]);
}
}
function processThread(thread) {
// get all messages in a given thread
var messages = thread.getMessages();
var latestStatus = null;
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
Logger.log("Processing message: " + j);
var messageStatus = processMessage(message);
if (messageStatus != null) latestStatus = messageStatus;
}
if (latestStatus != null) {
var label = getLabel(latestStatus);
thread.addLabel(label);
if (latestStatus == "CLOSED") {
thread.markRead();
}
}
}
function processMessage(message) {
var body = message.getRawContent();
var bzStatus = extractBZStatus(body);
if (bzStatus != null) {
Logger.log("Got BZ status: " + bzStatus);
}
return bzStatus;
}
function extractBZStatus(messageBody) {
var regex = new RegExp('X-Bugzilla-Status: (.*)$', 'm');
var result = regex.exec(messageBody);
if (result == null) return null;
return result[1];
}
function getLabel(bzStatus) {
var labelName = "Bugzilla/" + bzStatus;
var label = GmailApp.getUserLabelByName(labelName);
if (label == undefined) {
label = GmailApp.createLabel(labelName);
}
return label;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment