-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathukko.js
188 lines (176 loc) · 5.6 KB
/
ukko.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2021, Tomas Dabašinskas and the Ukko contributors
// SPDX-License-Identifier: MIT
import { GmailApp, Logger } from '../app.js'
// import line used used for testing ukko locally
// when running on google script engine paste
// LINES BELOW
// gmail filter for google script!
// Ukko is a code name for a google script tool accessing gmail
// inbox threads, looping over them and assigning labels based on
// the email metadata. It's intended be setup via google drive
// and have time driven trigger.
//
// https://github.com/T0MASD/ukko#readme
// loop over inboxThreads and process
function runUkko () {
const result = {}
const inboxThreads = GmailApp.getInboxThreads()
for (const inboxThread of inboxThreads) {
const lastMessage = getLastMessage(inboxThread)
const labels = assignLabels(lastMessage)
// uncomment below if you want threads with assigned labels to be archived automatically
// if (labels.length) { inboxThread.moveToArchive() }
// prep result
const from = lastMessage.getFrom()
result[from] = labels
Logger.log(`from:${lastMessage.getFrom()} labels:${labels}`)
}
return result
}
// process inboxThread
function getLastMessage (inboxThread) {
// load inboxThread messages
const messages = inboxThread.getMessages()
// get last message from the hread
return messages[messages.length - 1]
}
// assign labels to message thread
function assignLabels (message) {
// load labels for message
const labels = getLabels(message)
// load message inboxThread to add labels to
const inboxThread = message.getThread()
// loop over ["list/list-id","jira/proj/123"]
for (const label of labels) {
let labelName = ''
// loop over ["jira", "proj", "123"]
for (const subLabel of label.split('/')) {
// make labels jira jira/proj jira/proj/123
labelName = labelName + (labelName === '' ? '' : '/') + subLabel
// create label
const gmailLabel = GmailApp.getUserLabelByName(labelName)
? GmailApp.getUserLabelByName(labelName)
: GmailApp.createLabel(labelName)
gmailLabel.addToThread(inboxThread)
}
}
return labels
}
function getLabels (message) {
const labels = []
const teamArr = ['flast']
const messageSubject = message.getSubject() ? message.getSubject() : ''
const messageFrom = message.getFrom() ? message.getFrom() : ''
// run regex match
const messageEmail = getReMatch('email', messageFrom.trim())
let messageFromFQDN = ''; let messageFromDomain = ''; let messageFromUsername = ''
if (messageEmail && messageEmail.includes('@') && messageEmail.includes('.')) {
messageFromFQDN = messageEmail.split('@')[1]
messageFromDomain = messageFromFQDN.split('.').reverse()[1]
messageFromUsername = messageEmail.split('@')[0]
}
// process @github.com
if (messageFrom.includes('@github.com')) {
let label = 'github'
const toValue = message.getHeader('To')
if (toValue) {
// extract 'PROJ' from '"PROJ" <proj@gh.com>'
const ghProj = getReMatch('to', toValue)
if (ghProj) {
label += `/${ghProj}`
}
}
labels.push(label)
}
if (messageFrom.includes('@docs.google.com')) {
labels.push('gdrive')
}
// calendar
if (message.getHeader('Sender') && message.getHeader('Sender').includes('calendar-notification@google.com')) {
labels.push('calendar')
}
// process errata@domain.com
if (messageFrom.includes('errata@')) {
labels.push('errata')
}
// process team
if (teamArr.includes(messageFromUsername)) {
labels.push(`team/${messageFromUsername}`)
}
// process jira
if (messageFrom.includes('issues@')) {
let label = 'jira'
// extract 'PROJ' from '...(PROJ-123)...'
const jiraProj = getReMatch('jiraproj', messageSubject)
if (jiraProj) {
label += `/${jiraProj}`
}
labels.push(label)
}
// process bugzilla
if (messageFrom.includes('bugzilla@')) {
let label = 'bz'
if (message.getHeader('X-Bugzilla-Product')) {
const bzProdHeader = message.getHeader('X-Bugzilla-Product')
let bzProd
if (bzProdHeader.split(' ').length > 1) {
// create acronym
bzProd = getReMatch('acronym', bzProdHeader)
} else {
bzProd = bzProdHeader
}
label += `/${bzProd}`
if (message.getHeader('X-Bugzilla-Component')) {
const bzComponent = message.getHeader('X-Bugzilla-Component')
label += `/${bzComponent}`
}
}
labels.push(label)
}
// process gitlab notifications
if (message.getHeader('X-GitLab-Project')) {
labels.push(`gitlab/${message.getHeader('X-GitLab-Project')}`)
// don't want process mailing lists after gitlab
return labels
}
// process mailing lists
if (message.getHeader('List-Id')) {
// extract my-list from 'My List <my-list.example.com>'
const listIDshort = getReMatch('listid', message.getHeader('List-Id'))
let listLabel = 'lists/' + listIDshort
if (messageFromDomain !== 'mydomain') {
listLabel += `/${messageFromDomain}`
}
labels.push(listLabel)
}
// end
return labels
}
function getReMatch (kind, myStr) {
let re
switch (kind) {
case 'jiraproj':
re = /\((\w+[^-])-\d+\)/i
break
case 'email':
re = /[^@<\s]+@[^@\s>]+/gi
break
case 'listid':
re = /<([^.]+).+>/i
break
case 'to':
re = /"(.*?)"/i
break
case 'acronym':
re = /\b(\w)/g
return myStr.match(re).join('')
}
if (myStr.match(re)) {
return myStr.match(re).pop()
}
}
// END
// line below used for testing ukko locally
// when running on google script engine
// EXCLUDE LINE BELOW
export { runUkko, getLabels, getReMatch, assignLabels }