-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbucketPRs.lua
646 lines (558 loc) · 19.1 KB
/
bitbucketPRs.lua
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
-- Bitbucket Pull Requests monitor module for Hammerspoon
-- v0.5
local inspect = require 'inspect' -- This isn't *required* but helps with debugging. Remove this line if you don't have this file.
-- TODO:
-- Updated x time ago (currently displays 'updated at time'). Not sure if this is really possible...
-- Don't show change indicators for my activity - might not be worthwhile due to extra API calls required
-- More filtering options?
-- Bug: my items get mixed in with the others when sorting
--[[
Note:
Build state currently not functioning; I'm not sure why the BB API isn't returning the status value
Build state isn't too important and requires a API call, so disabled for now
]]
local config = require 'config'
local gmail = require 'gmail'
local credFile = require 'gmail_creds'
local url =
'https://bitbucket.org/api/2.0/repositories/' ..
config.bitbucket.repo_owner ..
'/' .. config.bitbucket.repo_slug .. '/pullrequests/?pagelen=' .. config.bitbucket.max_num_prs
hs.hotkey.bind(
{'cmd', 'alt', 'ctrl'},
'Down',
function()
getPRs(config.bitbucket.username, config.bitbucket.password)
refresh(config.bitbucket.username, config.bitbucket.password)
end
)
hs.hotkey.bind(
{'cmd', 'alt', 'ctrl'},
'x',
function()
hs.alert.show('Reloading PRs...')
getPRs(config.bitbucket.username, config.bitbucket.password)
end
)
local timerValue
local stopped = false
function refresh(username, password)
print('refresh()')
timerValue = 0
refreshTimer =
hs.timer.doEvery(
config.bitbucket.refresh_freq,
function()
local time = hs.timer.localTime() / 36
if
config.bitbucket.auto_refresh and
(time < config.bitbucket.work_start or time > config.bitbucket.work_end)
then
refreshTimer:stop()
stopped = true
print(time)
elseif timerValue > config.bitbucket.refresh_num then
refreshTimer:stop()
stopped = true
end
if config.bitbucket.check_emails then
if newPrEmail() then
hs.alert.show('Reloading PRs!!!!...')
getPRs(username, password)
end
else
getPRs(username, password)
end
timerValue = timerValue + 1
end
)
end
function newPrEmail()
print('newPrEmail()')
local numPrEmails = hs.settings.get('numPrEmails') or 0
local url = 'https://mail.google.com/mail/feed/atom/' .. config.bitbucket.gmail_prs_label
print(numPrEmails)
for index, creds in ipairs(credFile) do
gmail.mailCount(
creds.username,
creds.password,
url,
function(count)
if count > numPrEmails then
hs.settings.set('numPrEmails', count)
hs.settings.set('newEmail', true)
else
hs.settings.set('numPrEmails', count)
hs.settings.set('newEmail', false)
end
end
)
end
return hs.settings.get('newEmail')
end
local lastUpdatedAt = {}
function getURL(url, username, password)
print('getURL()')
hs.http.asyncGet(
url,
{Authorization = 'Basic ' .. hs.base64.encode(username .. ':' .. password)},
function(status1, body1, headers1)
status = status1
body = body1
headers = headers1
end
)
end
function getPRs(username, password)
print('getPRs()')
author_name = ''
title = ''
num_approvals = 0
approved_by_me = false
otherPRs = {}
myPRs = {}
hs.settings.set('filteringBy', nil)
hs.settings.set('sortingBy', nil)
filteringBy = hs.settings.get('filteringBy')
sortingBy = hs.settings.get('sortingBy')
-- Change menu colour to indicate loading
menuColour = {red = 0, blue = 1, green = 0}
doMenu()
getURL(url, username, password)
hs.timer.waitUntil(
function()
return status ~= nil
end,
function()
parseBBJson(username, password)
local allPRs = getAllPRs()
-- if sortingBy then
-- sort(allPRs, sortingBy, true)
-- end
-- if filteringBy then
-- filterBranches(allPRs, filteringBy)
-- end
if filteringBy == nil and sortingBy == nil then
createMenuTable(allPRs)
doMenu()
end
end
)
end
function getAllPRs()
print('getAllPRs()')
-- Get current time for 'last updated at' value
local hour = os.date('%I')
if string.sub(hour, 1, 1) == '0' then
hour = string.sub(hour, 2)
end
lastUpdatedAt.time = os.date(hour .. ':%M:%S %p')
lastUpdatedAt.date = os.date('%x')
-- Add my PRs to other PRs
local allPRs = otherPRs
for i = 1, #myPRs do
allPRs[#allPRs + 1] = myPRs[i]
end
return allPRs
end
function parseBBJson(username, password)
print('parseBBJson()')
if status == 200 then
bodyTable = hs.json.decode(body)
values = bodyTable.values[0] or bodyTable.values[1]
for key, value in pairs(bodyTable.values) do
author_name = value.author.display_name
title = value.title
num_comments = value.comment_count
link = value.links.html.href
remote_branch = config.bitbucket.remote_branches[value.destination.branch.name]
if remote_branch == nil then
remote_branch = config.bitbucket.remote_branches['default']
end
last_updated = value.updated_on
created_on = value.created_on
local url2 = value.links.self.href
status2, body2, headers2 =
hs.http.get(url2, {Authorization = 'Basic ' .. hs.base64.encode(username .. ':' .. password)})
if status2 == 200 then
body2 = hs.json.decode(body2)
participants = body2.participants
approved_by_me = false
num_approvals = 0
for key2, value2 in pairs(body2.participants) do
num_approvals = num_approvals + (value2.approved and 1 or 0)
participant_name = value2.user.display_name
if participant_name == config.bitbucket.my_name and value2.approved then
approved_by_me = true
end
end
build_state = 'SUCCESSFUL' --values3.state
line = {
approved = approved_by_me,
author = author_name,
title = title,
approvals = num_approvals,
comments = num_comments,
state = build_state,
url = link,
branch = remote_branch,
updated = last_updated,
created = created_on
}
if author_name == config.bitbucket.my_name then
table.insert(myPRs, line)
else
table.insert(otherPRs, line)
end
else
print('bb call2 failed with status code: ' .. status)
end
end
else
print('bb call failed with status code: ' .. status)
end
end
menuItem = hs.menubar.new()
function createMenuTable(allPRs)
print('createMenuTable()')
prIcon = hs.image.imageFromPath('180px-Octicons-git-pull-request.png')
size = {h = 23, w = 20}
prIcon = prIcon:setSize(size)
menu = {
{
title = 'View all open pull requests',
fn = function()
hs.urlevent.openURL(
'https://bitbucket.org/' ..
config.bitbucket.repo_owner .. '/' .. config.bitbucket.repo_slug .. '/pull-requests/'
)
end
},
{
title = 'Create new pull request',
fn = function()
hs.urlevent.openURL(
'https://bitbucket.org/' ..
config.bitbucket.repo_owner .. '/' .. config.bitbucket.repo_slug .. '/pull-requests/new'
)
end
},
{
title = 'Filter branches',
menu = {
{
title = 'Beta',
fn = function()
filterBranches(allPRs, 'B')
end,
checked = filteringBy == 'B',
tooltip = 'Show only beta branches'
},
{
title = 'Production',
fn = function()
filterBranches(allPRs, 'P')
end,
checked = filteringBy == 'P',
tooltip = 'Show only production branches'
},
{
title = 'X',
fn = function()
filterBranches(allPRs, 'X')
end,
checked = filteringBy == 'X',
tooltip = 'Show only default branches (i.e. not beta, production or development)'
},
{
title = 'All',
fn = function()
filterBranches(allPRs, 'All')
end,
checked = filteringBy == 'All',
tooltip = 'Show all branches'
},
{
title = 'New changes (experimental)',
fn = function()
onlyShowChanged = true
filterBranches(allPRs, 'changed')
end,
checked = filteringBy == 'changed',
tooltip = 'Show only branches with unseen changes'
}
}
},
{
title = 'Sort by',
menu = {
{
title = 'Number of comments',
fn = function(keyPressed)
sort(allPRs, 'comments', keyPressed.cmd)
end,
checked = sortingBy == 'comments',
tooltip = 'Cmd-click to sort in reverse order'
},
{
title = 'Most recently updated',
fn = function(keyPressed)
sort(allPRs, 'updated', keyPressed.cmd)
end,
checked = sortingBy == 'updated',
tooltip = 'Cmd-click to sort in reverse order'
},
{
title = 'Most recently created',
fn = function(keyPressed)
sort(allPRs, 'created', keyPressed.cmd)
end,
checked = sortingBy == 'created',
tooltip = 'Cmd-click to sort in reverse order'
}
}
}
}
table.insert(menu, {title = '-'})
addPRsToMenu(allPRs)
table.insert(menu, {title = '-'})
if lastUpdatedAt.date ~= os.date('%x') then
table.insert(menu, {title = 'Last updated on ' .. lastUpdatedAt.date .. ' at ' .. lastUpdatedAt.time})
else
table.insert(
menu,
{
title = 'Last updated today at ' .. lastUpdatedAt.time,
fn = function(keyPressed)
if keyPressed.cmd then
clearUpdates(allPRs)
end
end
}
)
end
num_unapproved = num_prs - num_approved - num_my_prs
-- Automatic update mode on
menuColour = {red = 0, blue = 0, green = 0}
if stopped then
-- Automatic update mode off
menuColour = {red = 0.7, blue = 0, green = 0}
end
end
function addPRsToMenu(allPRs)
print('addPRsToMenu()')
num_prs = 0
num_my_prs = 0
num_approved = 0
added_mine = false
for key, value in pairs(allPRs) do
if string.gsub(value.author, '%s+', '') == string.gsub(config.bitbucket.my_name, '%s+', '') then
if not added_mine then
table.insert(menu, {title = '-'})
added_mine = true
end
num_my_prs = num_my_prs + 1
end
BBprev = hs.settings.get('BBprev')
bbkey = value.url:match('([^/]+)$')
if BBprev[bbkey] ~= nil then
value.prev = {
approvals = BBprev[bbkey].approvals,
comments = BBprev[bbkey].comments,
updated = BBprev[bbkey].updated
}
else
value.prev = {approvals = 0, comments = 0, updated = ' - '}
end
-- Add '*' to indicate a change in number of approvals
value.approvals2 = value.approvals .. ' '
if value.approvals ~= value.prev.approvals then
value.approvals2 = value.approvals .. '*'
changed = true
end
-- Add superscript number to show number of unseen comments
value.comments2 = value.comments
value.commentsDiff = ' '
if value.comments ~= value.prev.comments then
value.commentsDiff = value.commentsDiff .. (value.comments - value.prev.comments)
changed = true
end
-- Add '*' to indicate that this PR has been updated
value.updated2 = ' - '
if value.updated ~= value.prev.updated then
value.updated2 = ' * '
changed = true
end
-- If author name is too long, get first initial and all of last name
if string.len(value.author) > 15 then
value.author = string.sub(value.author, 1, 1) .. string.match(value.author, '( .*)')
end
-- Make all author name 'columns' the same length
while string.len(value.author) < 15 do
value.author = value.author .. ' '
end
-- Make all title 'columns' the same length
if string.len(value.title) > 35 then
value.title = string.sub(value.title, 0, 32) .. '...'
else
while string.len(value.title) < 35 do
value.title = value.title .. ' '
end
end
text =
value.author ..
' ' ..
value.branch ..
value.updated2 .. value.title .. ' | 👍 ' .. value.approvals2 .. ' | 💬 ' .. value.comments2
color = {red = 0, blue = 0, green = 0}
if value.approvals > 1 and value.state == 'SUCCESSFUL' then
color = {red = 0, blue = 0.7, green = 1}
elseif value.state ~= 'SUCCESSFUL' then
color = {red = 1, blue = 0, green = 0}
end
line = {
title = hs.styledtext.new(text, {color = color, font = 'Monaco'}) ..
hs.styledtext.new(
value.commentsDiff,
{color = color, baselineOffset = 3.0, font = {name = 'Monaco', size = 10}}
),
checked = value.approved,
fn = function(keyPressed)
local openURL = true
if keyPressed.cmd then
openURL = false
end
if openURL then
hs.urlevent.openURL(value.url)
end
bbkey = value.url:match('([^/]+)$')
BBprev[bbkey] = {approvals = value.approvals, comments = value.comments, updated = value.updated}
hs.settings.set('BBprev', BBprev)
added_mine = false
createMenuTable(allPRs)
doMenu()
end,
tooltip = 'Created: ' .. parseDate(value.created) .. '.\nUpdated: ' .. parseDate(value.updated)
}
if onlyShowChanged == nil or (onlyShowChanged ~= nil and changed == true) then
table.insert(menu, line)
num_prs = num_prs + 1
end
changed = false
if value.approved then
num_approved = num_approved + 1
end
end
end
function doMenu()
print('doMenu()')
if num_unapproved == nil then
menuItem:setTitle('...')
else
menuItem:setTitle(
hs.styledtext.new(
num_unapproved,
{color = menuColour, superscript = 1, baselineOffset = -7, paragraphStyle = {alignment = 'right'}}
) ..
hs.styledtext.new('/', {color = menuColour, superscript = 0, baselineOffset = 0}) ..
hs.styledtext.new(
num_prs,
{color = menuColour, superscript = -1, baselineOffset = 5, paragraphStyle = {alignment = 'left'}}
)
)
end
menuItem:setIcon(prIcon)
menuItem:setMenu(menu)
end
function clearUpdates(allPRs)
print('clearUpdates()')
BBprev = hs.settings.get('BBprev')
for key, value in pairs(allPRs) do
bbkey = value.url:match('([^/]+)$')
BBprev[bbkey] = {approvals = value.approvals, comments = value.comments, updated = value.updated}
hs.settings.set('BBprev', BBprev)
added_mine = false
end
createMenuTable(allPRs)
doMenu()
end
function filterBranches(allPRs, branch)
print('filterBranches()')
if allPRsCopy == nil then
allPRsCopy = table.copy(allPRs)
else
allPRs = table.copy(allPRsCopy)
end
if branch ~= 'All' and branch ~= 'changed' then
for key, value in pairs(allPRs) do
if value.branch ~= branch then
allPRs[key] = nil
end
end
end
filteringBy = branch
hs.settings.set('filteringBy', branch)
createMenuTable(allPRs)
doMenu()
onlyShowChanged = nil
end
function sort(allPRs2, sortBy, reverse)
print('sort()')
local allPRs = allPRs2
if reverse then
table.sort(
allPRs,
function(left, right)
return left[sortBy] > right[sortBy]
end
)
else
table.sort(
allPRs,
function(left, right)
return left[sortBy] < right[sortBy]
end
)
end
sortingBy = sortBy
hs.settings.set('sortingBy', sortBy)
createMenuTable(allPRs)
doMenu()
end
function tableConcat(t1, t2)
print('tableConcat()')
for i = 1, #t2 do
t1[#t1 + 1] = t2[i]
end
return t1
end
function tablelength(T)
print('tablelength()')
local count = 0
for _ in pairs(T) do
count = count + 1
end
return count
end
function table.copy(t)
print('table.copy()')
local t2 = {}
for k, v in pairs(t) do
if type(v) == 'table' then
t2[k] = table.copy(v)
else
t2[k] = v
end
end
return t2
end
function parseDate(date)
print('parseDate()')
-- Assume format is RFC3339 (YYYY-MM-DD[T]HH:MM:SS[Z])
day = date:sub(9, 10)
month = date:sub(6, 7)
year = date:sub(1, 4)
hour = date:sub(12, 13)
minute = date:sub(15, 16)
return day .. '/' .. month .. '/' .. year .. ' at ' .. hour .. ':' .. minute
end