forked from shaunduncan/helga-jira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helga_jira.py
309 lines (248 loc) · 9.74 KB
/
helga_jira.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
import random
import re
import requests
import smokesignal
from jira import JIRA
from requests.auth import HTTPBasicAuth
from twisted.internet import reactor
from helga import log, settings
from helga.db import db
from helga.plugins import command, match, ACKS, ResponseNotReady
from helga.util.encodings import to_unicode
logger = log.getLogger(__name__)
# These are initialized on client signon
JIRA_PATTERNS = set()
_jira = None
jira_server = None
@smokesignal.on('signon')
def init_jira_patterns(*args, **kwargs):
"""
Signal callback for IRC signon. This pulls down and caches all the stored
JIRA ticket patterns so we don't have to do it on every message received
"""
global JIRA_PATTERNS
if db is None: # pragma: no cover
logger.warning('Cannot initialize JIRA patterns. No database connection')
return
JIRA_PATTERNS = set(item['re'] for item in db.jira.find())
def find_jira_numbers(message):
"""
Finds all jira ticket numbers in a message. This will ignore any that already
appear in a URL
"""
global JIRA_PATTERNS
if not JIRA_PATTERNS:
return []
ticket_patterns = r'({0})-[\d]+'.format('|'.join(JIRA_PATTERNS))
# Remove URLs
message = re.sub(r'https?://.*?{0}'.format(ticket_patterns), '', message)
# Get the tickets, but don't be too greedy. Only allow preceeding spaces or commas
pat = r'(^|[\s,])({0})'.format(ticket_patterns)
print pat
print re.findall(pat, message, re.IGNORECASE)
tickets = [m[1] for m in re.findall(pat, message, re.IGNORECASE)]
return tickets
def add_re(pattern):
"""
Adds a ticket pattern from the database and local cache
"""
global JIRA_PATTERNS
if pattern not in JIRA_PATTERNS:
logger.info('Adding new JIRA ticket RE: %s', pattern)
JIRA_PATTERNS.add(pattern)
re_doc = {'re': pattern}
# Store in DB
if not db.jira.find(re_doc).count():
db.jira.insert(re_doc)
else: # pragma: no cover
logger.info('JIRA ticket RE already exists: %s', pattern)
return random.choice(ACKS)
def remove_re(pattern):
"""
Removes a ticket pattern from the database and local cache
"""
global JIRA_PATTERNS
logger.info('Removing JIRA ticket RE: %s', pattern)
JIRA_PATTERNS.discard(pattern)
db.jira.remove({'re': pattern})
return random.choice(ACKS)
def jira_command(client, channel, nick, message, cmd, args):
"""
Command handler for the jira regex command
"""
try:
subcmd, pattern = args[:2]
except ValueError:
return None
if pattern == "help":
# this means that we cannot have a project in Jira called 'help'
return "Add or remove jira ticket patterns, excluding numbers. " \
"Usage: !jira (add_re|remove_re) <pattern>"
if subcmd == 'add_re':
return add_re(pattern)
elif subcmd == 'remove_re':
return remove_re(pattern)
else:
return None
def _rest_desc(ticket, url, auth=None):
api_url = to_unicode(getattr(settings, 'JIRA_REST_API', 'http://localhost/api/{ticket}'))
resp = requests.get(api_url.format(ticket=ticket), auth=auth, verify=getattr(settings, 'JIRA_SSL_VERIFY', True))
try:
resp.raise_for_status()
except:
logger.error('Error getting JIRA ticket %s. Status %s', ticket, resp.status_code)
return
try:
return u'[{0}] {1} ({2})'.format(ticket.upper(), resp.json()['fields']['summary'], url)
except:
return u'[{0}] {1}'.format(ticket.upper(), url)
def jira_full_descriptions(client, channel, urls):
"""
Meant to be run asynchronously because it uses the network
"""
descriptions = []
user_pass = getattr(settings, 'JIRA_AUTH', ('', ''))
if all(user_pass):
auth = HTTPBasicAuth(*user_pass)
else:
auth = None
for ticket, url in urls.iteritems():
desc = _rest_desc(ticket, url, auth)
if desc is not None:
descriptions.append(desc)
if descriptions:
client.msg(channel, '\n'.join(descriptions))
def jira_match(client, channel, nick, message, matches):
jira_url = to_unicode(getattr(settings, 'JIRA_URL', 'http://localhost/{ticket}'))
full_urls = dict((s, jira_url.format(ticket=s)) for s in matches)
if not getattr(settings, 'JIRA_SHOW_FULL_DESCRIPTION', True):
return u'{0} might be talking about JIRA ticket: {1}'.format(nick, ', '.join(full_urls.values()))
# Otherwise, do the fetching with a deferred
reactor.callLater(0, jira_full_descriptions, client, channel, full_urls)
raise ResponseNotReady
def create_ticket(args):
try:
subargs = args[1]
if subargs == "help":
return "Create a new ticket -- Usage: !jira create <project_id> <issue_type (Bug, Task)> <summary>"
new_issue = get_jira().create_issue(
project=subargs[1],
summary=" ".join(subargs[3:]),
issuetype={'name': subargs[2]}
)
return "Ticket created : {url}/browse/{ticket_id}".format(url=jira_server, ticket_id=new_issue.key)
except Exception as e:
error = "{e}".format(e=e)
read_data = error
try:
file = error.split()[len(error.split()) - 1]
with open(file, 'r') as f:
for line in f:
if "response text" in line:
read_data = line
except:
pass
return "Failed to create ticket: {data}".format(data=read_data)
def assign_ticket(args):
try:
subargs = args[1]
ticket_id = subargs[1]
if ticket_id == "help":
return "Assign ticket to a user -- Usage: !jira assign <ticket_id> <username>"
jira_username = subargs[2]
get_jira().assign_issue(ticket_id, jira_username)
return "Successfully assigned issue {ticket_id} to {jira_username}".format(
ticket_id=ticket_id, jira_username=jira_username)
except Exception as e:
error = "{e}".format(e=e)
read_data = error
try:
file = error.split()[len(error.split()) - 1]
with open(file, 'r') as f:
for line in f:
if "response text" in line:
read_data = line
except:
pass
return "Failed to assign ticket: {data}".format(data=read_data)
def add_comment(args):
try:
subargs = args[1]
ticket_id = subargs[1]
if ticket_id == "help":
return "Add comment to a ticket -- Usage: !jira comment <ticket_id> <comment>"
comment = " ".join(subargs[2:])
get_jira().add_comment(ticket_id, comment)
return "Successfully added comment to {ticket_id}".format(ticket_id=ticket_id)
except Exception as e:
error = "{e}".format(e=e)
read_data = error
try:
file = error.split()[len(error.split()) - 1]
with open(file, 'r') as f:
for line in f:
if "response text" in line:
read_data = line
except:
pass
return "Failed to comment ticket: {data}".format(data=read_data)
@match(find_jira_numbers)
@command('jira', help="Various jira commands: show, create, comment, assign, add_re, delete_re")
def jira(client, channel, nick, message, *args):
"""
A plugin for showing URLs to JIRA ticket numbers. This is both a command to add or remove
patterns, and a match to automatically show them. The match requires a setting JIRA_URL
which must contain a ``{ticket}`` substring. For example, ``http://localhost/{ticket}``.
The command takes a pattern as an argument, minus any numbers. For example, if there are JIRA
tickets like FOOBAR-1, FOOBAR-2, and FOOBAR-3. Then you could manage the pattern via::
helga jira add_re FOOBAR
helga jira remove_re FOOBAR
Ticket numbers are automatically detected.
"""
if len(args) > 1 and len(args[1]) > 0:
subcommand = args[1][0]
if subcommand == 'show':
return show_ticket(args)
elif subcommand == "create":
return create_ticket(args)
elif subcommand == "assign":
return assign_ticket(args)
elif subcommand == "comment":
return add_comment(args)
elif subcommand == "help":
return "To get help, specify a subcommand and then help: (show, create, assign, comment, add_re, remove_re) help"
if len(args) == 2:
fn = jira_command
else:
fn = jira_match
return fn(client, channel, nick, message, *args)
def show_ticket(args):
ticket_id = args[1][1]
if ticket_id == "help":
return "Show jira ticket details (Assignee, Status, Summary, Priority) -- Usage: !jira show <ticket_id>"
issue = get_jira().issue(ticket_id)
return [
"{url}/browse/{ticket_id}".format(url=jira_server, ticket_id=ticket_id),
"`Summary: {0}`".format(issue.fields.summary),
"`Assignee: {0}`".format(issue.fields.assignee),
"`Status: {0}`".format(issue.fields.status),
"`Priority: {0}`".format(issue.fields.priority.name),
]
def get_jira():
global _jira, jira_server
if _jira:
try:
# search for an old existing issue as a cheap health check
issue = _jira.issue('FDS-1620', fields='summary,comment')
if issue:
return _jira
except:
# pass here and assume that things are broken and recreate the _jira object
pass
user_pass = getattr(settings, 'JIRA_AUTH', ('', ''))
jira_server = getattr(settings, 'JIRA_URL', 'http://localhost')
options = {
"verify": getattr(settings, 'JIRA_SSL_VERIFY', True)
}
_jira = JIRA(server=jira_server, basic_auth=user_pass, options=options)
return _jira