-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
264 lines (239 loc) · 8.08 KB
/
index.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const config = require('config')
const fs = require('fs')
const debug = require('debug')('gerrit-monitor')
const cron = require('node-cron')
const DEFAULT_NO_CHANGE_MSG = `No new patches have been merged`
const FULL_TSV_EXTENSION = "-full.tsv"
const FILTERED_TSV_EXTENSION = "-filtered.tsv"
const slackConfigured =
config.has('slack') &&
config.slack.has('enabled') &&
config.slack.enabled &&
config.slack.has('webhookUrl')
? config.slack.webhookUrl
: false
let slackNotification =
config.has('spotlight') &&
config.spotlight.has('enabled') &&
config.spotlight.enabled &&
config.spotlight.has('notifications') &&
config.spotlight.notifications.has('slack') &&
config.spotlight.notifications.slack.has('enabled') &&
config.spotlight.notifications.slack.enabled &&
config.spotlight.notifications.slack.has('webhookUrl')
? config.spotlight.notifications.slack.webhookUrl
: false
let fileNotification =
config.has('spotlight') &&
config.spotlight.has('enabled') &&
config.spotlight.enabled &&
config.spotlight.has('notifications') &&
config.spotlight.notifications.has('file') &&
config.spotlight.notifications.file.has('enabled') &&
config.spotlight.notifications.file.enabled &&
config.spotlight.notifications.file.has('outputFilename')
? config.spotlight.notifications.file.outputFilename
: false
debug(`slackNotification: `, slackNotification)
debug(`fileNotification: `, fileNotification)
let fileNotificationFiltered = false
let fileNotificationFull = false
if (fileNotification) {
fileNotificationFull = fileNotification + FULL_TSV_EXTENSION
fileNotificationFiltered = fileNotification + FILTERED_TSV_EXTENSION
if (!fs.existsSync(fileNotificationFull) || !fs.existsSync(fileNotificationFiltered)) {
debug(`Couldn't find Full or Filtered files; creating new files...`)
fs.writeFileSync(
fileNotificationFull,
`${[
`Timestamp`,
`Add Count`,
`Add List`,
`Drop Count`,
`Drop List`,
`Merge Count`,
`Merge List`,
].join(`\t`)}\n`
)
fs.writeFileSync(
fileNotificationFiltered,
`${[
`Timestamp`,
`Add Count`,
`Add List`,
`Drop Count`,
`Drop List`,
`Merge Count`,
`Merge List`,
].join(`\t`)}\n`
)
}
debug(`fileNotification enabled: ${fileNotification}
Full: ${fileNotificationFull}
Filtered: ${fileNotificationFiltered}`)
} else {
debug(`fileNotification not enabled`)
}
if (slackConfigured) {
const { IncomingWebhook } = require('@slack/webhook')
let webhook = new IncomingWebhook(slackConfigured)
const reportEmpty =
config.has('cron') && config.cron.has('reportEmpty')
? config.cron.reportEmpty
: false
const DEFAULT_FREQUENCY = 30 // Number of minutes between checks
const Gerrit = require('./gerrit-lib')
const gLib = new Gerrit()
const freq =
config.has('cron') &&
config.has('cron.frequency') &&
typeof config.cron.frequency === 'number'
? config.cron.frequency
: DEFAULT_FREQUENCY
debug(`freq: ${freq}`)
cron.schedule(`*/${freq} * * * *`, () => {
gLib.fetchAndLog().then(async (x) => {
debug(`done with `, x)
let result = gLib.delta()
let d = new Date().toLocaleString('en', { timeZoneName: 'short' })
debug(`${d} delta result: `, result)
if (result.count || reportEmpty) {
if (result.count) {
if (slackNotification) {
let msg = `*${result.type} Patch Delta Report* @ ${d}: ${
reportEmpty ? `\nReporting Empty: yes` : ''
}${
(result && result.add && result.add.length) || reportEmpty
? `\n:fire: Added: ${result.add.join(',')}`
: ''
} ${
(result && result.drop && result.drop.length) || reportEmpty
? `\n:checkered_flag: Dropped: ${result.drop.join(',')}`
: ''
}`
let headerBlock = {
type: 'section',
text: {
type: 'mrkdwn',
text: `*${result.type} Patch Delta Report* @ ${d}`,
},
}
let mergeBlock =
result.merge && result.merge.length
? {
type: 'section',
text: {
type: 'mrkdwn',
text: `*Merged*\n${`:checkered_flag:` + result.merge.join('\n:checkered_flag:')}\n`,
},
}
: false
let addBlock =
result.add && result.add.length
? {
type: 'section',
text: {
type: 'mrkdwn',
text: `*Added*\n${`:fire:` + result.add.join('\n:fire:')}\n`,
},
}
: false
let dropBlock =
result.drop && result.drop.length
? {
type: 'section',
text: {
type: 'mrkdwn',
text: `*Dropped*\n${
`:arrow_down:` + result.drop.join('\n:arrow_down:')
}\n`,
},
}
: false
if (addBlock || dropBlock || mergeBlock || reportEmpty) {
blocks = [headerBlock]
if (addBlock || dropBlock || mergeBlock) {
if (addBlock) blocks.push(addBlock)
if (dropBlock) blocks.push(dropBlock)
if (mergeBlock) blocks.push(mergeBlock)
} else {
blocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: `Nothing added, dropped, or merged\n(Sending empty notification per reportEmpty config value)`,
},
})
}
await webhook.send({
blocks: blocks,
})
} // addBlock...reportEmpty
} // slackNotification
} else { // result.count is empty, so go ahead and reportEmpty with no results
if (slackNotification) {
await webhook.send({ type: `mrkdwn`, text: DEFAULT_NO_CHANGE_MSG })
}
debug(`No items added or dropped, so no notification/update`)
} // result.count
} // result.count || reportEmpty
if (fileNotification) {
updateReportFiles(fileNotification, gLib.latestTimestamp, result)
}
})
})
} else {
console.error(
`Either Slack or File notification must be configured and enabled.
e.g. 'config.slack.enabled = true' and 'config.slack.webhookUrl' must be set`
)
}
function updateReportFiles(filenameBase, timestamp, result) {
debug(`updateReportFiles(${filenameBase}, ${timestamp}, result) called...`)
debug(`...appending ${filenameBase + FULL_TSV_EXTENSION}`)
fs.appendFileSync(
filenameBase + FULL_TSV_EXTENSION,
`${[
timestamp,
result.add.length,
result.add.join(','),
result.drop.length,
result.drop.join(','),
result.merge.length,
result.merge.join(','),
].join('\t')}\n`
)
// Print non-zero elements to FILTERED file
if (result.add.length + result.drop.length + result.merge.length > 0) {
debug(`...appending ${filenameBase + FILTERED_TSV_EXTENSION}`)
fs.appendFileSync(
filenameBase + FILTERED_TSV_EXTENSION,
`${[
timestamp,
result.add.length,
result.add.join(','),
result.drop.length,
result.drop.join(','),
result.merge.length,
result.merge.join(','),
].join('\t')}\n`
)
}
}
function formatUrgent(data, icon = false) {
const results = ['\n']
data.forEach((d) => {
results.push(
`${icon ? `:${icon}:` : ''}<${config.gerritUrlBase}/${d.id}|${d.id}>: ${
d.subject
} (${
config.has('slack') &&
config.slack.has('users') &&
Object.keys(config.slack.users).includes(d.email)
? `<@${config.slack.users[d.email]}>`
: `${d.owner} ${d.email}`
}; ${d.vScore})`
)
})
return results.join('\n')
}