-
Notifications
You must be signed in to change notification settings - Fork 2
/
jira_plugin.py
404 lines (315 loc) · 10.1 KB
/
jira_plugin.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
395
396
397
398
399
400
401
402
403
__kupfer_name__ = _('Jira')
__version__ = '0.1.4'
__author__ = _('Hugo Sena Ribeiro <[email protected]>')
__description__ = _('''Kupfer plugin to control Jira''')
__kupfer_sources__ = ("ProjectSource", "IssueSource")
__kupfer_actions__ = ("Issue", "Show", "Comment", "Assign")
import re
from jira import JIRA
from jira import Issue as issue
from jira import Project as project
from kupfer.utils import show_url
from kupfer.objects import Leaf
from kupfer.objects import Source
from kupfer.objects import Action, TextLeaf
from kupfer.plugin_support import \
PluginSettings, check_keyring_support, UserNamePassword
check_keyring_support()
__kupfer_settings__ = PluginSettings(
{
"key" : "jira_login",
"label": "Login",
"type": UserNamePassword,
"value": None
},
{
"key" : "jira_url",
"label": "Jira URL",
"type": str,
"value": ""
},
{
"key" : "issue_jql",
"label": "JQL for source",
"type": str,
"value": "assignee = currentUser()"
}
)
FIELD_WHITE_LIST = (
'labels',
'priority',
'timespent',
'description',
'summary',
'environment'
)
issue_regexp = '^[a-zA-Z0-9]+-[\d]+$'
project_regexp = '^[a-zA-Z0-9]+$'
def initialize_jira():
if Jiraya.resource:
return Jiraya.resource
jira_url = __kupfer_settings__['jira_url']
jira_login = __kupfer_settings__['jira_login']
if jira_url and \
jira_login and \
jira_login.username and \
jira_login.password:
try:
Jiraya.resource = JIRA(jira_url,
basic_auth=(
jira_login.username,
jira_login.password
)
)
except:
Jiraya.resource = None
return Jiraya.resource
def get_issue(item, jira):
if isinstance(item, issue):
return item
if isinstance(item.object, issue):
return item.object
key = None
if re.match(issue_regexp, str(item)):
key = str(item)
elif re.match(issue_regexp, str(item.object)):
key = str(item.object)
if key:
return jira.issue(key)
def get_issue_leaf(item, jira):
if isinstance(item, IssueLeaf):
return item
i = get_issue(item, jira)
return IssueLeaf(i, jira=jira)
def is_issue(item):
if isinstance(item, issue):
return item
if isinstance(item.object, issue):
return item.object
if re.match(issue_regexp, str(item)):
return True
elif re.match(issue_regexp, str(item.object)):
return True
def get_project(item, jira):
if isinstance(item, project):
return item
if isinstance(item.object, project):
return item.object
key = None
if re.match(project_regexp, str(item)):
key = str(item)
elif re.match(project_regexp, str(item.object)):
key = str(item.object)
if key:
return jira.project(key)
def is_project(item):
if isinstance(item, project):
return item
if isinstance(item.object, project):
return item.object
if re.match(project_regexp, str(item)):
return True
elif re.match(project_regexp, str(item.object)):
return True
class IssueLeaf(Leaf):
def __init__(self, obj, fields=None, transition=None, jira=None):
Leaf.__init__(self, obj, IssueLeaf.get_name(obj))
self.fields = fields
self.transition = None
self.jira = jira
@staticmethod
def get_name(obj):
status = ''
if obj.fields and obj.fields.status:
status = obj.fields.status.name
return '{} - {}'.format(obj.key, status)
def get_description(self):
return self.fields or self.object.fields.summary
class ProjectLeaf(Leaf):
def __init__(self, obj, jira):
Leaf.__init__(self, obj, obj.key)
self.jira = jira
def get_description(self):
return self.object.name
class Jiraya(object):
resource = None
def __init__(self, jira=None):
self._jira = jira
@property
def jira(self):
self._jira = self._jira or initialize_jira()
return self._jira
@jira.setter
def jira(self, value):
self._jira = value
class ProjectSource(Source, Jiraya):
def __init__(self):
Source.__init__(self, "Jira Projects")
Jiraya.__init__(self)
def get_items(self):
if self.jira:
for obj in self.jira.projects():
yield ProjectLeaf(obj, self.jira)
class IssueSource(Source, Jiraya):
resource = None
def __init__(self):
Source.__init__(self, "Jira Issues")
Jiraya.__init__(self)
def get_items(self, start_at=0):
jql = __kupfer_settings__['issue_jql']
page_size = 50
if self.jira:
issues = self.jira.search_issues(jql,
startAt=start_at,
maxResults=page_size)
for i in issues:
yield IssueLeaf(i)
if len(issues) == page_size:
for issue_leaf in self.get_items(start_at + page_size):
yield issue_leaf
def is_dynamic(self):
return True
class Issue(Jiraya, Action):
def __init__(self):
Action.__init__(self, name="Jira Issue")
Jiraya.__init__(self)
def activate(self, item):
return get_issue_leaf(item, self.jira)
def item_types(self):
yield TextLeaf
def valid_for_item(self, item):
return is_issue(item)
def has_result(self):
return True
class Show(Jiraya, Action):
def __init__(self):
Jiraya.__init__(self)
Action.__init__(self, name="Show Issue/Project")
def get_description(self):
return "Open issue/project in browser"
def activate(self, item):
url = None
if is_issue(item):
i = get_issue(item, self.jira)
url = i.permalink()
else:
p = get_project(item, self.jira)
url = __kupfer_settings__['jira_url'] + \
'/projects/' + p.key + '/summary'
show_url(url)
def valid_for_item(self, item):
return is_issue(item) or is_project(item)
def item_types(self):
yield IssueLeaf
yield ProjectLeaf
yield TextLeaf
class Comment(Jiraya, Action):
def __init__(self):
Action.__init__(self, name="Add Comment")
Jiraya.__init__(self)
def valid_for_item(self, item):
return is_issue(item)
def item_types(self):
yield IssueLeaf
yield TextLeaf
def activate(self, item, iobj):
i = get_issue(item, self.jira)
comment = self.jira.add_comment(i, iobj.object)
return get_issue_leaf(item, self.jira)
def valid_for_item(self, item):
return is_issue(item)
def requires_object(self):
return True
def object_types(self, for_item=None):
yield TextLeaf
def valid_object(self, iobj, for_item=None):
return type(iobj) is TextLeaf
def has_result(self):
return True
class Assign(Jiraya, Action):
def __init__(self):
Action.__init__(self, name="Assign User")
Jiraya.__init__(self)
def valid_for_item(self, item):
return is_issue(item)
def item_types(self):
yield IssueLeaf
yield TextLeaf
def activate(self, item, iobj):
i = get_issue(item, self.jira)
self.jira.assign_issue(i, iobj.object)
return get_issue_leaf(item, self.jira)
def valid_for_item(self, item):
return is_issue(item)
def requires_object(self):
return True
def object_types(self, for_item=None):
yield TextLeaf
def valid_object(self, iobj, for_item=None):
return type(iobj) is TextLeaf
def has_result(self):
return True
def object_source(self, for_item=None):
if for_item:
item = get_issue_leaf(for_item, self.jira)
return _AssignValues(item)
class _AssignValues(Source, Jiraya):
def __init__(self, issue):
Source.__init__(self, _("Users"))
Jiraya.__init__(self, issue.jira)
self.issue = issue
def get_items(self, start_at=0):
i = get_issue(self.issue, self.jira)
page_size = 50
users = self.jira.search_assignable_users_for_issues('',
issueKey=i,
startAt=start_at,
maxResults=page_size)
for u in users:
yield TextLeaf(u.key, u.name)
if len(users) == page_size:
for leaf in self.get_items(start_at + page_size):
yield leaf
def provides(self):
yield TextLeaf
class ChangeStatus(Jiraya, Action):
def __init__(self):
Action.__init__(self, name="Change Status")
Jiraya.__init__(self)
def valid_for_item(self, item):
return is_issue(item)
def item_types(self):
yield IssueLeaf
yield TextLeaf
def activate(self, item, iobj):
i = get_issue(item, self.jira)
l = get_issue_leaf(item, self.jira)
self.jira.transition_issue(i, iobj.object, fields=l.fields)
l.fields = None
return l
def valid_for_item(self, item):
return is_issue(item)
def requires_object(self):
return True
def object_types(self, for_item=None):
yield TextLeaf
def valid_object(self, iobj, for_item=None):
return type(iobj) is TextLeaf
def has_result(self):
return True
def object_source(self, for_item=None):
if for_item:
item = get_issue_leaf(for_item, self.jira)
return _StatusValues(item)
class _StatusValues(Source, Jiraya):
def __init__(self, issue):
Source.__init__(self, _("Users"))
Jiraya.__init__(self, issue.jira)
self.issue = issue
def get_items(self):
i = get_issue(self.issue, self.jira)
transitions = self.jira.transitions(i)
for t in transitions:
yield TextLeaf(t["id"], t["name"])
def provides(self):
yield TextLeaf