bitbucket.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. -------------------------------------------------
  2. -- Bitbucket Widget for Awesome Window Manager
  3. -- Shows the number of currently assigned pull requests
  4. -- More details could be found here:
  5. -- https://github.com/streetturtle/awesome-wm-widgets/tree/master/bitbucket-widget
  6. -- @author Pavel Makhov
  7. -- @copyright 2020 Pavel Makhov
  8. -------------------------------------------------
  9. local awful = require("awful")
  10. local wibox = require("wibox")
  11. local watch = require("awful.widget.watch")
  12. local json = require("json")
  13. local spawn = require("awful.spawn")
  14. local naughty = require("naughty")
  15. local gears = require("gears")
  16. local beautiful = require("beautiful")
  17. local gfs = require("gears.filesystem")
  18. local HOME_DIR = os.getenv("HOME")
  19. local WIDGET_DIR = HOME_DIR .. '/.config/awesome/awesome-wm-widgets/bitbucket-widget/'
  20. local GET_PRS_CMD= [[bash -c "curl -s --show-error -n ]]
  21. .. [['%s/2.0/repositories/%s/%s/pullrequests]]
  22. .. [[?fields=values.participants.approved,values.title,values.links.html,values.author.display_name,]]
  23. .. [[values.author.uuid,values.author.links.avatar,values.source.branch,values.destination.branch,]]
  24. .. [[values.comment_count,values.created_on&q=reviewers.uuid+%%3D+%%22%s%%22+AND+state+%%3D+%%22OPEN%%22']]
  25. .. [[ | jq '.[] '"]]
  26. local DOWNLOAD_AVATAR_CMD = [[bash -c "curl -L -n --create-dirs -o %s/.cache/awmw/bitbucket-widget/avatars/%s %s"]]
  27. local bitbucket_widget = wibox.widget {
  28. {
  29. {
  30. id = 'icon',
  31. widget = wibox.widget.imagebox
  32. },
  33. margins = 4,
  34. layout = wibox.container.margin
  35. },
  36. {
  37. id = "txt",
  38. widget = wibox.widget.textbox
  39. },
  40. {
  41. id = "new_pr",
  42. widget = wibox.widget.textbox
  43. },
  44. layout = wibox.layout.fixed.horizontal,
  45. set_text = function(self, new_value)
  46. self.txt.text = new_value
  47. end,
  48. set_icon = function(self, new_value)
  49. self:get_children_by_id('icon')[1]:set_image(new_value)
  50. end
  51. }
  52. local function show_warning(message)
  53. naughty.notify{
  54. preset = naughty.config.presets.critical,
  55. title = 'Bitbucket Widget',
  56. text = message}
  57. end
  58. local popup = awful.popup{
  59. ontop = true,
  60. visible = false,
  61. shape = gears.shape.rounded_rect,
  62. border_width = 1,
  63. border_color = beautiful.bg_focus,
  64. maximum_width = 400,
  65. offset = { y = 5 },
  66. widget = {}
  67. }
  68. --- Converts string representation of date (2020-06-02T11:25:27Z) to date
  69. local function parse_date(date_str)
  70. local pattern = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)%Z"
  71. local y, m, d, h, min, sec, _ = date_str:match(pattern)
  72. return os.time{year = y, month = m, day = d, hour = h, min = min, sec = sec}
  73. end
  74. --- Converts seconds to "time ago" represenation, like '1 hour ago'
  75. local function to_time_ago(seconds)
  76. local days = seconds / 86400
  77. if days > 1 then
  78. days = math.floor(days + 0.5)
  79. return days .. (days == 1 and ' day' or ' days') .. ' ago'
  80. end
  81. local hours = (seconds % 86400) / 3600
  82. if hours > 1 then
  83. hours = math.floor(hours + 0.5)
  84. return hours .. (hours == 1 and ' hour' or ' hours') .. ' ago'
  85. end
  86. local minutes = ((seconds % 86400) % 3600) / 60
  87. if minutes > 1 then
  88. minutes = math.floor(minutes + 0.5)
  89. return minutes .. (minutes == 1 and ' minute' or ' minutes') .. ' ago'
  90. end
  91. end
  92. local function ellipsize(text, length)
  93. return (text:len() > length and length > 0)
  94. and text:sub(0, length - 3) .. '...'
  95. or text
  96. end
  97. local function count_approves(participants)
  98. local res = 0
  99. for i = 1, #participants do
  100. if participants[i]['approved'] then res = res + 1 end
  101. end
  102. return res
  103. end
  104. local function worker(user_args)
  105. local args = user_args or {}
  106. local icon = args.icon or WIDGET_DIR .. '/bitbucket-icon-gradient-blue.svg'
  107. local host = args.host or show_warning('Bitbucket host is not set')
  108. local uuid = args.uuid or show_warning('UUID is not set')
  109. local workspace = args.workspace or show_warning('Workspace is not set')
  110. local repo_slug = args.repo_slug or show_warning('Repo slug is not set')
  111. local timeout = args.timeout or 60
  112. local current_number_of_prs
  113. local to_review_rows = {layout = wibox.layout.fixed.vertical}
  114. local my_review_rows = {layout = wibox.layout.fixed.vertical}
  115. local rows = {layout = wibox.layout.fixed.vertical}
  116. bitbucket_widget:set_icon(icon)
  117. local update_widget = function(widget, stdout, stderr, _, _)
  118. if stderr ~= '' then
  119. show_warning(stderr)
  120. return
  121. end
  122. local result = json.decode(stdout)
  123. current_number_of_prs = rawlen(result)
  124. if current_number_of_prs == 0 then
  125. widget:set_visible(false)
  126. return
  127. end
  128. widget:set_visible(true)
  129. widget:set_text(current_number_of_prs)
  130. for i = 0, #rows do rows[i]=nil end
  131. for i = 0, #to_review_rows do to_review_rows[i]=nil end
  132. table.insert(to_review_rows, {
  133. {
  134. markup = '<span size="large" color="#ffffff">PRs to review</span>',
  135. align = 'center',
  136. forced_height = 20,
  137. widget = wibox.widget.textbox
  138. },
  139. bg = beautiful.bg_normal,
  140. widget = wibox.container.background
  141. })
  142. for i = 0, #my_review_rows do my_review_rows[i]=nil end
  143. table.insert(my_review_rows, {
  144. {
  145. markup = '<span size="large" color="#ffffff">My PRs</span>',
  146. align = 'center',
  147. forced_height = 20,
  148. widget = wibox.widget.textbox
  149. },
  150. bg = beautiful.bg_normal,
  151. widget = wibox.container.background
  152. })
  153. local current_time = os.time(os.date("!*t"))
  154. for _, pr in ipairs(result) do
  155. local path_to_avatar = os.getenv("HOME") ..'/.cache/awmw/bitbucket-widget/avatars/' .. pr.author.uuid
  156. local number_of_approves = count_approves(pr.participants)
  157. local row = wibox.widget {
  158. {
  159. {
  160. {
  161. {
  162. resize = true,
  163. image = path_to_avatar,
  164. forced_width = 40,
  165. forced_height = 40,
  166. widget = wibox.widget.imagebox
  167. },
  168. id = 'avatar',
  169. margins = 8,
  170. layout = wibox.container.margin
  171. },
  172. {
  173. {
  174. id = 'title',
  175. markup = '<b>' .. ellipsize(pr.title, 50) .. '</b>',
  176. widget = wibox.widget.textbox,
  177. forced_width = 400
  178. },
  179. {
  180. {
  181. {
  182. {
  183. text = ellipsize(pr.source.branch.name, 30),
  184. widget = wibox.widget.textbox
  185. },
  186. {
  187. text = '->',
  188. widget = wibox.widget.textbox
  189. },
  190. {
  191. text = pr.destination.branch.name,
  192. widget = wibox.widget.textbox
  193. },
  194. spacing = 8,
  195. layout = wibox.layout.fixed.horizontal
  196. },
  197. {
  198. {
  199. text = pr.author.display_name,
  200. widget = wibox.widget.textbox
  201. },
  202. {
  203. text = to_time_ago(os.difftime(current_time, parse_date(pr.created_on))),
  204. widget = wibox.widget.textbox
  205. },
  206. spacing = 8,
  207. expand = 'none',
  208. layout = wibox.layout.fixed.horizontal
  209. },
  210. forced_width = 285,
  211. layout = wibox.layout.fixed.vertical
  212. },
  213. {
  214. {
  215. {
  216. image = WIDGET_DIR .. '/check.svg',
  217. resize = false,
  218. widget = wibox.widget.imagebox
  219. },
  220. {
  221. text = number_of_approves,
  222. widget = wibox.widget.textbox
  223. },
  224. layout = wibox.layout.fixed.horizontal
  225. },
  226. {
  227. {
  228. image = WIDGET_DIR .. '/message-circle.svg',
  229. resize = false,
  230. widget = wibox.widget.imagebox
  231. },
  232. {
  233. text = pr.comment_count,
  234. widget = wibox.widget.textbox
  235. },
  236. layout = wibox.layout.fixed.horizontal
  237. },
  238. layout = wibox.layout.fixed.vertical
  239. },
  240. layout = wibox.layout.fixed.horizontal
  241. },
  242. spacing = 8,
  243. layout = wibox.layout.fixed.vertical
  244. },
  245. spacing = 8,
  246. layout = wibox.layout.fixed.horizontal
  247. },
  248. margins = 8,
  249. layout = wibox.container.margin
  250. },
  251. bg = beautiful.bg_normal,
  252. widget = wibox.container.background
  253. }
  254. if not gfs.file_readable(path_to_avatar) then
  255. local cmd = string.format(DOWNLOAD_AVATAR_CMD, HOME_DIR, pr.author.uuid, pr.author.links.avatar.href)
  256. spawn.easy_async(cmd, function() row:get_children_by_id('avatar')[1]:set_image(path_to_avatar) end)
  257. end
  258. row:connect_signal("mouse::enter", function(c) c:set_bg(beautiful.bg_focus) end)
  259. row:connect_signal("mouse::leave", function(c) c:set_bg(beautiful.bg_normal) end)
  260. row:get_children_by_id('title')[1]:buttons(
  261. awful.util.table.join(
  262. awful.button({}, 1, function()
  263. spawn.with_shell("xdg-open " .. pr.links.html.href)
  264. popup.visible = false
  265. end)
  266. )
  267. )
  268. row:get_children_by_id('avatar')[1]:buttons(
  269. awful.util.table.join(
  270. awful.button({}, 1, function()
  271. spawn.with_shell(
  272. string.format('xdg-open "https://bitbucket.org/%s/%s/pull-requests?state=OPEN&author=%s"',
  273. workspace, repo_slug, pr.author.uuid)
  274. )
  275. popup.visible = false
  276. end)
  277. )
  278. )
  279. local old_cursor, old_wibox
  280. row:get_children_by_id('title')[1]:connect_signal("mouse::enter", function()
  281. local wb = mouse.current_wibox
  282. old_cursor, old_wibox = wb.cursor, wb
  283. wb.cursor = "hand1"
  284. end)
  285. row:get_children_by_id('title')[1]:connect_signal("mouse::leave", function()
  286. if old_wibox then
  287. old_wibox.cursor = old_cursor
  288. old_wibox = nil
  289. end
  290. end)
  291. row:get_children_by_id('avatar')[1]:connect_signal("mouse::enter", function()
  292. local wb = mouse.current_wibox
  293. old_cursor, old_wibox = wb.cursor, wb
  294. wb.cursor = "hand1"
  295. end)
  296. row:get_children_by_id('avatar')[1]:connect_signal("mouse::leave", function()
  297. if old_wibox then
  298. old_wibox.cursor = old_cursor
  299. old_wibox = nil
  300. end
  301. end)
  302. if (pr.author.uuid == '{' .. uuid .. '}') then
  303. table.insert(my_review_rows, row)
  304. else
  305. table.insert(to_review_rows, row)
  306. end
  307. end
  308. table.insert(rows, to_review_rows)
  309. if (#my_review_rows > 1) then
  310. table.insert(rows, my_review_rows)
  311. end
  312. popup:setup(rows)
  313. end
  314. bitbucket_widget:buttons(
  315. awful.util.table.join(
  316. awful.button({}, 1, function()
  317. if popup.visible then
  318. popup.visible = not popup.visible
  319. else
  320. popup:move_next_to(mouse.current_widget_geometry)
  321. end
  322. end)
  323. )
  324. )
  325. watch(string.format(GET_PRS_CMD, host, workspace, repo_slug, uuid, uuid),
  326. timeout, update_widget, bitbucket_widget)
  327. return bitbucket_widget
  328. end
  329. return setmetatable(bitbucket_widget, { __call = function(_, ...) return worker(...) end })