-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.py
394 lines (344 loc) · 14.4 KB
/
main.py
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
"""Ulauncher GitLab."""
import logging
import re
import gitlab
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.client.Extension import Extension
from ulauncher.api.shared.action.CopyToClipboardAction import \
CopyToClipboardAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.OpenUrlAction import OpenUrlAction
from ulauncher.api.shared.action.RenderResultListAction import \
RenderResultListAction
from ulauncher.api.shared.action.SetUserQueryAction import SetUserQueryAction
from ulauncher.api.shared.event import (KeywordQueryEvent, PreferencesEvent,
PreferencesUpdateEvent)
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
LOGGER = logging.getLogger(__name__)
PROJECTS_SEARCH_TYPE_PUBLIC = "PUBLIC"
PROJECTS_SEARCH_TYPE_MEMBER = "MEMBER"
PROJECTS_SEARCH_TYPE_STARRED = "STARRED"
class GitLabExtension(Extension):
"""Main extension class."""
def __init__(self):
"""init method."""
LOGGER.info("Initializing GitLab Extension")
super(GitLabExtension, self).__init__()
# initializes GitLab Client
self.gitlab = None
self.current_user = None
# Event listeners
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(PreferencesEvent, PreferencesEventListener())
self.subscribe(PreferencesUpdateEvent,
PreferencesUpdateEventListener())
def show_menu(self):
"""Show the main extension menu when the user types the extension
keyword without arguments."""
keyword = self.preferences["kw"]
menu = [
ExtensionResultItem(
icon="images/icon.png",
name="My",
description=
"Your personal menu with shortcuts for your Issues, Merge Requests and more",
highlightable=False,
on_enter=SetUserQueryAction("%s my" % keyword),
),
ExtensionResultItem(
icon="images/icon.png",
name="Project Search",
description=
"Search public projects in the entire GitLab platform",
highlightable=False,
on_enter=SetUserQueryAction("%s search " % keyword),
),
ExtensionResultItem(
icon="images/icon.png",
name="My Projects",
description="List the projects you are a member of",
highlightable=False,
on_enter=SetUserQueryAction("%s projects " % keyword),
),
ExtensionResultItem(
icon="images/icon.png",
name="My Projects (Starred)",
description="List your starred projects",
highlightable=False,
on_enter=SetUserQueryAction("%s starred " % keyword),
),
ExtensionResultItem(
icon="images/icon.png",
name="My Groups",
description="List the groups you belong",
highlightable=False,
on_enter=SetUserQueryAction("%s groups " % keyword),
),
ExtensionResultItem(
icon="images/icon.png",
name="GitLab Website",
description="Opens the GitLab website",
highlightable=False,
on_enter=OpenUrlAction(self.gitlab.url),
on_alt_enter=CopyToClipboardAction(self.gitlab.url),
),
ExtensionResultItem(
icon="images/icon.png",
name="GitLab Status",
description="Opens the GitLab status page",
highlightable=False,
on_enter=OpenUrlAction("https://status.gitlab.com"),
on_alt_enter=CopyToClipboardAction(
"https://status.gitlab.com"),
),
]
return RenderResultListAction(menu)
def show_my_menu(self, query):
"""Show "My" Menu with links for Profile.
Todos, PRs etc
"""
gitlab_url = self.preferences["url"]
# Authenticate the user, if its not already authenticated.
if self.current_user is None:
self.gitlab.auth()
self.current_user = self.gitlab.user
items = [
ExtensionResultItem(
icon="images/icon.png",
name="Logged in as %s" % self.current_user.username,
description='Open "Profile" page in Gitlab',
highlightable=False,
on_enter=OpenUrlAction("%s/profile" % gitlab_url),
on_alt_enter=CopyToClipboardAction("%s/profile" % gitlab_url),
),
ExtensionResultItem(
icon="images/icon.png",
name="My Projects",
description='Open "Projects" page in Gitlab',
highlightable=False,
on_enter=OpenUrlAction("%s/dashboard/projects" % gitlab_url),
on_alt_enter=CopyToClipboardAction("%s/dashboard/projects" %
gitlab_url),
),
ExtensionResultItem(
icon="images/icon.png",
name="My Groups",
description='Open "Groups" page on GitLab',
highlightable=False,
on_enter=OpenUrlAction("%s/dashboard/groups" % gitlab_url),
on_alt_enter=CopyToClipboardAction("%s/dashboard/groups" %
gitlab_url),
),
ExtensionResultItem(
icon="images/icon.png",
name="My Snippets",
description='Open "Snippets" page on GitLab',
highlightable=False,
on_enter=OpenUrlAction("%s/dashboard/snippets" % gitlab_url),
on_alt_enter=OpenUrlAction("%s/dashboard/snippets" %
gitlab_url),
),
ExtensionResultItem(
icon="images/icon.png",
name="My Todos",
description='Open "Todos" page on GitLab',
highlightable=False,
on_enter=OpenUrlAction("%s/dashboard/todos" % gitlab_url),
on_alt_enter=CopyToClipboardAction("%s/dashboard/todos" %
gitlab_url),
),
ExtensionResultItem(
icon="images/icon.png",
name="My Issues",
description='Open "Issues" page on GitLab',
highlightable=False,
on_enter=OpenUrlAction("%s/dashboard/issues?assignee_id=%s" %
(gitlab_url, self.current_user.id)),
on_alt_enter=CopyToClipboardAction(
"%s/dashboard/issues?assignee_id=%s" %
(gitlab_url, self.current_user.id)),
),
ExtensionResultItem(
icon="images/icon.png",
name="My Merge Requests",
description='Open "Merge Requests" page on GitLab',
highlightable=False,
on_enter=OpenUrlAction(
"%s/dashboard/merge_requests?assignee_id=%s" %
(gitlab_url, self.current_user.id)),
on_alt_enter=CopyToClipboardAction(
"%s/dashboard/merge_requests?assignee_id=%s" %
(gitlab_url, self.current_user.id)),
),
]
if query:
items = [p for p in items if query.lower() in p.get_name().lower()]
return RenderResultListAction(items)
def search_projects(self, query, search_type):
"""Search projects in GitLab."""
if search_type == PROJECTS_SEARCH_TYPE_MEMBER:
projects = self.gitlab.projects.list(
search=query,
membership=1,
order_by="name",
sort="asc",
simple=1,
page=1,
per_page=10,
)
elif search_type == PROJECTS_SEARCH_TYPE_STARRED:
projects = self.gitlab.projects.list(
search=query,
order_by="last_activity_at",
sort="desc",
starred=1,
simple=1,
page=1,
per_page=10,
)
else:
projects = self.gitlab.projects.list(
search=query,
visibility="public",
order_by="last_activity_at",
sort="desc",
simple=1,
page=1,
per_page=10,
)
if not projects:
return RenderResultListAction([
ExtensionResultItem(
icon="images/icon.png",
name="No projects found matching your search criteria",
highlightable=False,
on_enter=HideWindowAction(),
)
])
items = []
for project in projects:
if project.description is not None:
description = project.description
else:
description = ""
items.append(
ExtensionResultItem(
icon="images/icon.png",
name=project.name,
description=description,
highlightable=False,
on_enter=OpenUrlAction(project.web_url),
on_alt_enter=CopyToClipboardAction(project.web_url),
))
return RenderResultListAction(items)
def list_groups(self, query):
"""Lists the groups the user belongs to."""
items = []
groups = self.gitlab.groups.list(archived=0,
search=query,
order_by="name",
sort="asc",
page=1,
per_page=10)
if not groups:
return RenderResultListAction([
ExtensionResultItem(
icon="images/icon.png",
name="No groups found matching your search criteria",
highlightable=False,
on_enter=HideWindowAction(),
)
])
for group in groups:
if group.description is not None:
description = group.description
else:
description = ""
items.append(
ExtensionResultItem(
icon="images/icon.png",
name=group.name,
description=description,
highlightable=False,
on_enter=OpenUrlAction(group.web_url),
on_alt_enter=CopyToClipboardAction(group.web_url),
))
return RenderResultListAction(items)
# # pylint: disable=too-many-return-statements
class KeywordQueryEventListener(EventListener):
"""Handles Keyboard input."""
def on_event(self, event, extension):
"""Handles the event."""
query = event.get_argument() or ""
if not query:
return extension.show_menu()
# Get the action based on the search terms
search = re.findall(r"^search(.*)?$", query, re.IGNORECASE)
repos = re.findall(r"^projects(.*)?$", query, re.IGNORECASE)
groups = re.findall(r"^groups(.*)?$", query, re.IGNORECASE)
starred = re.findall(r"^starred(.*)?$", query, re.IGNORECASE)
account = re.findall(r"^my(.*)?$", query, re.IGNORECASE)
try:
if account:
return extension.show_my_menu(account[0])
if search:
return extension.search_projects(search[0],
PROJECTS_SEARCH_TYPE_PUBLIC)
if repos:
return extension.search_projects(repos[0],
PROJECTS_SEARCH_TYPE_MEMBER)
if starred:
return extension.search_projects(starred[0],
PROJECTS_SEARCH_TYPE_STARRED)
if groups:
return extension.list_groups(groups[0])
return extension.search_projects(query,
PROJECTS_SEARCH_TYPE_MEMBER)
except gitlab.GitlabError as exc:
LOGGER.error(exc)
return RenderResultListAction([
ExtensionResultItem(
icon="images/icon.png",
name="An error ocurred when connecting to GitLab",
description=str(exc),
highlightable=False,
on_enter=HideWindowAction(),
)
])
class PreferencesEventListener(EventListener):
"""Listener for prefrences event.
It is triggered on the extension start with the configured
preferences
"""
def on_event(self, event, extension):
"""Initializes the GitLab client."""
extension.gitlab = gitlab.Gitlab(
event.preferences["url"],
private_token=event.preferences["access_token"])
# save the logged in user.
try:
extension.gitlab.auth()
extension.current_user = extension.gitlab.user
except Exception as exc:
LOGGER.error(exc)
extension.current_user = None
class PreferencesUpdateEventListener(EventListener):
"""Listener for "Preferences Update" event.
It is triggered when the user changes any setting in preferences
window
"""
def on_event(self, event, extension):
if event.id == "url":
extension.gitlab.url = event.new_value
elif event.id == "access_token":
extension.gitlab = gitlab.Gitlab(extension.preferences["url"],
private_token=event.new_value)
# save the logged in user.
try:
extension.gitlab.auth()
extension.current_user = extension.gitlab.user
except Exception as exc:
LOGGER.error(exc)
extension.current_user = None
if __name__ == "__main__":
GitLabExtension().run()