Skip to content

Commit 9d0e8d8

Browse files
author
Pavel Makhov
committed
Added check for updates button and project name
1 parent c344a91 commit 9d0e8d8

File tree

4 files changed

+72
-57
lines changed

4 files changed

+72
-57
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Each item in the list is showing following information:
2626
- install [Hammerspoon](http://www.hammerspoon.org/) - a powerfull automation tool for OS X
2727
- Manually:
2828

29-
Download the [latest release], and drag Hammerspoon.app from your Downloads folder to Applications.
29+
Download the [latest release](https://github.com/Hammerspoon/hammerspoon/releases/latest), and drag Hammerspoon.app from your Downloads folder to Applications.
3030
- Homebrew:
3131

3232
```brew install hammerspoon --cask```

init.lua

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ local obj = {}
22
obj.__index = obj
33

44
-- Metadata
5-
obj.name = "gitlab-merge-requests"
6-
obj.version = "1.0"
5+
obj.name = "gitlab-merge-requests.spoon"
6+
obj.version = "1.1"
77
obj.author = "Pavel Makhov"
88
obj.homepage = "https://github.com/fork-my-spoons/gitlab-merge-requests.spoon"
99
obj.license = "MIT - https://opensource.org/licenses/MIT"
@@ -26,6 +26,7 @@ local calendar_icon = hs.styledtext.new(' ', { font = {name = 'feather', size
2626
local warning_icon = hs.styledtext.new('', { font = {name = 'feather', size = 12 }, color = {hex = '#ffd60a'}})
2727
local checkbox_icon = hs.styledtext.new('', { font = {name = 'feather', size = 12 }, color = {hex = '#8e8e8e'}})
2828
local checkbox_icon_green = hs.styledtext.new('', { font = {name = 'feather', size = 12 }, color = {hex = '#7CB342'}})
29+
local project_icon = hs.styledtext.new('', { font = {name = 'feather', size = 12 }, color = {hex = '#8e8e8e'}})
2930

3031
local function subtitle(text)
3132
return hs.styledtext.new(text, {color = {hex = '#8e8e8e'}})
@@ -60,6 +61,61 @@ local function to_time_ago(seconds)
6061
end
6162
end
6263

64+
local function check_for_updates(spoon_name, spoon_version)
65+
local release_url = 'https://api.github.com/repos/fork-my-spoons/' .. spoon_name .. '/releases/latest'
66+
hs.http.asyncGet(release_url, {}, function(status, body)
67+
local latest_release = hs.json.decode(body)
68+
latest = latest_release.tag_name:sub(2)
69+
70+
if latest == spoon_version then
71+
hs.notify.new(function() end, {
72+
autoWithdraw = false,
73+
title = 'Gitlab Merge Requests Spoon',
74+
informativeText = "You have the latest version installed!"
75+
}):send()
76+
else
77+
hs.notify.new(function()
78+
os.execute('open ' .. latest_release.assets[1].browser_download_url)
79+
end,
80+
{
81+
title = 'Gitlab Merge Requests Spoon',
82+
informativeText = "New version is available",
83+
actionButtonTitle = "Download",
84+
hasActionButton = true
85+
}):send()
86+
end
87+
end)
88+
end
89+
90+
local function build_menu_item(merge_request, approvals, current_time)
91+
local isApprovedByMe = false
92+
93+
for _, approver in ipairs(approvals.rules[1].approved_by) do
94+
if approver.username == obj.username then
95+
isApprovedByMe = true
96+
end
97+
end
98+
99+
local _,_,project = string.find(merge_request.references.full, "/([%w-]+)!")
100+
101+
local title = hs.styledtext.new(merge_request.title .. '\n')
102+
.. project_icon .. subtitle(project .. ' ') .. user_icon .. subtitle(merge_request.author.name .. '\n')
103+
.. comment_icon .. subtitle(tostring(merge_request.user_notes_count) .. ' ')
104+
.. (isApprovedByMe and checkbox_icon_green or checkbox_icon) .. subtitle(#approvals.rules[1].approved_by .. '/' .. approvals.rules[1].approvals_required .. ' ')
105+
.. calendar_icon .. subtitle(to_time_ago(os.difftime(current_time, parse_date(merge_request.created_at))))
106+
107+
if merge_request.merge_status == 'cannot_be_merged' then
108+
title = warning_icon .. title
109+
end
110+
111+
return {
112+
created = parse_date(merge_request.created_at),
113+
title = title,
114+
image = hs.image.imageFromURL(merge_request.author.avatar_url):setSize({w=36,h=36}),
115+
checked = #approvals.rules[1].approved_by >= approvals.rules[1].approvals_required,
116+
fn = function() os.execute('open ' .. merge_request.web_url) end
117+
}
118+
end
63119

64120
local function updateMenu()
65121
local toReviewUrl = string.format(obj.mrUrl, obj.gitlab_host, 'reviewer_username=' .. hs.http.convertHtmlEntities(obj.username))
@@ -75,32 +131,8 @@ local function updateMenu()
75131
for _, merge_request in ipairs(merge_requests) do
76132
hs.http.asyncGet(string.format(obj.approvalsUrl, obj.gitlab_host, merge_request.project_id, merge_request.iid), auth_header, function(code, body)
77133
local approvals = hs.json.decode(body)
78-
79-
local isApprovedByMe = false
80-
81-
for _, approver in ipairs(approvals.rules[1].approved_by) do
82-
if approver.username == obj.username then
83-
isApprovedByMe = true
84-
end
85-
end
86-
87-
local title = hs.styledtext.new(merge_request.title .. '\n')
88-
.. comment_icon .. subtitle(tostring(merge_request.user_notes_count) .. ' ')
89-
.. (isApprovedByMe and checkbox_icon_green or checkbox_icon) .. subtitle(#approvals.rules[1].approved_by .. '/' .. approvals.rules[1].approvals_required .. ' ')
90-
.. calendar_icon .. subtitle(to_time_ago(os.difftime(current_time, parse_date(merge_request.created_at))) .. ' ')
91-
.. user_icon .. subtitle(merge_request.author.name)
92-
93-
if merge_request.merge_status == 'cannot_be_merged' then
94-
title = warning_icon .. title
95-
end
96-
97-
local menu_item = {
98-
created = parse_date(merge_request.created_at),
99-
title = title,
100-
image = hs.image.imageFromURL(merge_request.author.avatar_url):setSize({w=32,h=32}),
101-
checked = #approvals.rules[1].approved_by >= approvals.rules[1].approvals_required,
102-
fn = function() os.execute('open ' .. merge_request.web_url) end
103-
}
134+
local menu_item = build_menu_item(merge_request, approvals, current_time)
135+
104136
table.insert(obj.toReview, menu_item)
105137
end)
106138
end
@@ -114,32 +146,7 @@ local function updateMenu()
114146
for _, merge_request in ipairs(merge_requests) do
115147
hs.http.asyncGet(string.format(obj.approvalsUrl, obj.gitlab_host, merge_request.project_id, merge_request.iid), auth_header, function(code, body)
116148
local approvals = hs.json.decode(body)
117-
118-
local isApprovedByMe = false
119-
120-
for _, approver in ipairs(approvals.rules[1].approved_by) do
121-
if approver.username == obj.username then
122-
isApprovedByMe = true
123-
end
124-
end
125-
126-
local title = hs.styledtext.new(merge_request.title .. '\n')
127-
.. comment_icon .. subtitle(tostring(merge_request.user_notes_count) .. ' ')
128-
.. (isApprovedByMe and checkbox_icon_green or checkbox_icon) .. subtitle(#approvals.rules[1].approved_by .. '/' .. approvals.rules[1].approvals_required .. ' ')
129-
.. calendar_icon .. subtitle(to_time_ago(os.difftime(current_time, parse_date(merge_request.created_at))) .. ' ')
130-
.. user_icon .. subtitle(merge_request.author.name)
131-
132-
if merge_request.merge_status == 'cannot_be_merged' then
133-
title = warning_icon .. title
134-
end
135-
136-
local menu_item = {
137-
created = parse_date(merge_request.created_at),
138-
title = title,
139-
image = hs.image.imageFromURL(merge_request.author.avatar_url):setSize({w=32,h=32}),
140-
checked = #approvals.rules[1].approved_by >= approvals.rules[1].approvals_required,
141-
fn = function() os.execute('open ' .. merge_request.web_url) end
142-
}
149+
local menu_item = build_menu_item(merge_request, approvals, current_time)
143150

144151
table.insert(obj.assignedToYou, menu_item)
145152
end)
@@ -174,11 +181,20 @@ function obj:buildMenu()
174181
table.insert(gitlab_menu, { title = '-'})
175182
end
176183

177-
table.insert(gitlab_menu, { title = 'Refresh', fn = function() updateMenu() end})
184+
table.insert(gitlab_menu, {
185+
image = hs.image.imageFromName('NSRefreshTemplate'),
186+
title = 'Refresh', fn = function() updateMenu() end
187+
})
188+
189+
table.insert(gitlab_menu, {
190+
image = hs.image.imageFromName('NSTouchBarDownloadTemplate'),
191+
title = 'Check for updates',
192+
fn = function() check_for_updates(obj.name, obj.version) end})
178193

179194
return gitlab_menu
180195
end
181196

197+
182198
function obj:init()
183199
self.indicator = hs.menubar.new()
184200
self.indicator:setIcon(hs.image.imageFromPath(obj.iconPath .. '/gitlab-icon-rgb.png'):setSize({w=16,h=16}), true)
@@ -194,7 +210,6 @@ function obj:setup(args)
194210
end
195211

196212
function obj:start()
197-
print('started')
198213
self.timer:fire()
199214
self.timer:start()
200215
end

screenshots/details.png

94.2 KB
Loading

screenshots/screenshot.png

32.1 KB
Loading

0 commit comments

Comments
 (0)