-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBZStatusProcessor.gs
96 lines (86 loc) · 2.87 KB
/
BZStatusProcessor.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function processLastMonth() {
startProcess("from:(bugzilla@redhat.com) newer_than:30d");
}
function processInbox() {
startProcess("from:(bugzilla@redhat.com) newer_than:1d");
}
function startProcess(search) {
// process all recent threads in the Inbox (see comment to this answer)
var threads = GmailApp.search(search);
GmailApp.createLabel('Bugzilla');
GmailApp.createLabel('Bugzilla/Component');
GmailApp.createLabel('Bugzilla/Status');
var bzLabels = getBzLabels();
Logger.log("Got threads: " + threads.length);
for (var i = 0; i < threads.length; i++) {
processThread(threads[i], bzLabels);
}
}
function processThread(thread, bzLabels) {
// get all messages in a given thread
var messages = thread.getMessages();
var latestStatus = null;
var latestComponent = null;
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
Logger.log("Processing message: " + j);
var messageInfo = processMessage(message);
if (messageInfo.status != null) latestStatus = messageInfo.status;
if (messageInfo.component != null) latestComponent = messageInfo.component;
}
if (latestStatus != null || latestComponent != null) {
var statusLabel = getLabel("Status/" + latestStatus);
var componentLabel = getLabel("Component/" + latestComponent);
for (var i = 0; i < bzLabels.length; i++) {
if (bzLabels[i] != statusLabel && bzLabels[i] != componentLabel) {
thread.removeLabel(bzLabels[i]);
}
}
if (statusLabel != null) thread.addLabel(statusLabel);
if (componentLabel != null) thread.addLabel(componentLabel);
}
}
function processMessage(message) {
var body = message.getRawContent();
var bzStatus = extractBZStatus(body);
var bzComponent = extractBZComponent(body);
if (bzStatus != null) {
Logger.log("Got BZ status: " + bzStatus);
}
if (bzComponent != null) {
Logger.log("Got BZ component: " + bzComponent);
}
return {status: bzStatus, component: bzComponent};
}
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 extractBZComponent(messageBody) {
var regex = new RegExp('X-Bugzilla-Component: (.*)$', 'm');
var result = regex.exec(messageBody);
if (result == null) return null;
return result[1];
}
function getLabel(bzStatus) {
if (bzStatus == null) return null;
var labelName = "Bugzilla/" + bzStatus;
var label = GmailApp.getUserLabelByName(labelName);
if (label == undefined) {
label = GmailApp.createLabel(labelName);
}
return label;
}
function getBzLabels(){
var allLabels = GmailApp.getUserLabels();
var bzLabels = [];
for (var i = 0; i < allLabels.length; i++) {
name = allLabels[i].getName();
if (name.indexOf("Bugzilla/") == 0) {
bzLabels.push(allLabels[i])
}
}
return bzLabels;
}