-
Notifications
You must be signed in to change notification settings - Fork 3
/
jira.py
157 lines (126 loc) · 4.36 KB
/
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
from errbot import BotPlugin, botcmd
import logging
log = logging.getLogger(name='errbot.plugins.Jira')
try:
from jira import JIRA, JIRAError
except ImportError:
log.error("Please install 'jira' python package")
class Jira(BotPlugin):
"""Plugin for Jira"""
def activate(self):
if not self.config:
#Don't allow activation until we are configured
message = 'Jira is not configured, please do so.'
self.log.info(message)
self.warn_admins(message)
return
self.jira_connect = self._login()
if self.jira_connect:
super().activate()
def get_configuration_template(self):
""" configuration entries """
config = {
'api_url': None,
'api_user': None,
'api_pass': None,
}
return config
def _login(self):
username = self.config['api_user']
password = self.config['api_pass']
api_url = self.config['api_url']
try:
login = JIRA(server=api_url, basic_auth=(username, password))
self.log.info('logging into {}'.format(api_url))
return login
except JIRAError:
message = 'Unable to login to {}'.format(api_url)
self.log.error(message)
return False
def _check_ticket_passed(self, msg, ticket):
if ticket == '':
self.send(msg.frm,
'Ticket must be passed',
message_type=msg.type,
in_reply_to=msg,
groupchat_nick_reply=True)
return False
return True
@botcmd(split_args_with=' ')
def jira(self, msg, args):
"""
Returns the subject of the ticket along with a link to it.
"""
ticket = args.pop(0).upper()
if not self._check_ticket_passed(msg, ticket):
return
jira = self.jira_connect
try:
issue = jira.issue(ticket)
response = '{0} created on {1} by {2} ({4}) - {3}'.format(
issue.fields.summary,
issue.fields.created,
issue.fields.reporter.displayName,
issue.permalink(),
issue.fields.status.name
)
except JIRAError:
response = 'Ticket {0} not found.'.format(ticket)
self.send(msg.frm,
response,
message_type=msg.type,
in_reply_to=msg,
groupchat_nick_reply=True)
@botcmd(split_args_with=' ')
def jira_comment(self, msg, args):
"""
Adds a comment to a ticket
Options:
ticket: jira ticket
comment: text to add to ticket
Example
!jira comment PROJECT-123 I need to revisit this.
"""
ticket = args.pop(0).upper()
raw_comment = ' '.join(args)
if not self._check_ticket_passed(msg, ticket):
return
jira = self.jira_connect
try:
issue = jira.issue(ticket)
user = msg.frm
comment = '{} added: {}'.format(user, raw_comment)
jira.add_comment(issue, comment)
response = 'Added comment to {}'.format(ticket)
except JIRAError:
response = 'Unable to add comment to {0}.'.format(ticket)
self.send(msg.frm,
response,
message_type=msg.type,
in_reply_to=msg,
groupchat_nick_reply=True)
@botcmd(split_args_with=' ')
def jira_reassign(self, msg, args):
"""
Reassign a ticket to someone else
Options:
ticket: jira ticket
user: user to reassign ticket
Example
!jira reassign PROJECT-123 sijis
"""
ticket = args.pop(0).upper()
user = args.pop(0)
if not self._check_ticket_passed(msg, ticket):
return
jira = self.jira_connect
try:
jira.assign_issue(assignee=user, issue=ticket)
response = 'Reassigned {} to {}'.format(ticket, user)
except JIRAError:
response = 'Unable to reassign {} to {}'.format(ticket, user)
self.send(msg.frm,
response,
message_type=msg.type,
in_reply_to=msg,
groupchat_nick_reply=True)