forked from Linaro/jipdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jipdate.py
executable file
·335 lines (266 loc) · 10 KB
/
jipdate.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
#!/usr/bin/env python
from __future__ import print_function
from argparse import ArgumentParser
from jira import JIRA
from subprocess import call
import json
import os
import re
import sys
import sys
import tempfile
TEST_SERVER = 'https://dev-projects.linaro.org'
PRODUCTION_SERVER = 'https://projects.linaro.org'
server = PRODUCTION_SERVER
################################################################################
# Helper functions
################################################################################
def eprint(*args, **kwargs):
""" Helper function that prints on stderr. """
print(*args, file=sys.stderr, **kwargs)
def vprint(*args, **kwargs):
""" Helper function that prints when verbose has been enabled. """
if verbose:
print(*args, file=sys.stdout, **kwargs)
def print_status(status):
""" Helper function printing your status """
print("This is your status:")
print("\n---\n")
print("\n".join(l.strip() for l in status))
def open_editor(filename):
"""
Function that tries to find the best suited editor to use and then
opens the status file in the editor.
"""
if "EDITOR" in os.environ:
editor = os.environ['EDITOR']
elif "VISUAL" in os.environ:
editor = os.environ['VISUAL']
elif os.path.exists("/usr/bin/editor"):
editor = "/usr/bin/editor"
elif os.path.exists("/usr/bin/vim"):
editor = "/usr/bin/vim"
else:
eprint("Could not load an editor. Please define EDITOR or VISUAL")
sys.exit()
call([editor, filename])
def open_file(filename):
"""
This will open the user provided file and if there has not been any file
provided it will create and open a temporary file instead.
"""
vprint("filename: %s\n" % filename)
if filename:
return open(filename, "w")
else:
return tempfile.NamedTemporaryFile(delete=False)
def add_domain(user):
"""
Helper function that appends @linaro.org to the username. It does nothing if
it is already included.
"""
if '@' not in user:
user = user + "@linaro.org"
return user
def email_to_name(email):
""" Converts '[email protected]' to 'First Last'. """
n = email.split("@")[0].title()
return n.replace(".", " ")
################################################################################
# Argument parser
################################################################################
def get_parser():
""" Takes care of script argument parsing. """
parser = ArgumentParser(description='Script used to update comments in Jira')
parser.add_argument('-q', required=False, action="store_true", \
default=False, \
help='Query Jira for issue(s) assigned to you')
parser.add_argument('-e', required=False, action="store_true", \
default=False, \
help='Only include epics (no initiatives or stories). Used in combination \
with "-q"')
parser.add_argument('-f', '--file', required=False, action="store", \
default=None, \
help='Load status update from FILE. NOTE: -q will overwrite the \
content of FILE')
parser.add_argument('-t', required=False, action="store_true", \
default=False, \
help='Use the test server')
parser.add_argument('-u', '--user', required=False, action="store", \
default=None, \
help='Query Jira with another Jira username \
(first.last or [email protected])')
parser.add_argument('-v', required=False, action="store_true", \
default=False, \
help='Output some verbose debugging info')
parser.add_argument('-x', required=False, action="store_true", \
default=False, \
help='EXCLUDE stories from gathered Jira issues. Used in combination \
with "-q"')
parser.add_argument('--all', required=False, action="store_true", \
default=False, \
help='Load all Jira issues, not just the once marked in progress.')
return parser
################################################################################
# Jira functions
################################################################################
def update_jira(jira, i, c):
"""
This is the function that do the actual updates to Jira and in this case it
is adding comments to a certain issue.
"""
vprint("Updating Jira issue: %s with comment:" % i)
vprint("-- 8< --------------------------------------------------------------------------")
vprint("%s" % c)
vprint("-- >8 --------------------------------------------------------------------------\n\n")
jira.add_comment(i, c)
message_header = """Hi,
This is the status update from me for the last week.
Cheers!
"""
def get_jira_issues(jira, exclude_stories, epics_only, all_status, filename,
user):
"""
Query Jira and then creates a status update file (either temporary or named)
containing all information found from the JQL query.
"""
issue_types = ["Epic"]
if not epics_only:
issue_types.append("Initiative")
if not exclude_stories:
issue_types.append("Story")
issue_type = "issuetype in (%s)" % ", ".join(issue_types)
status = "status in (\"In Progress\")"
if all_status:
status = "status not in (Resolved, Closed)"
if user is None:
user = "currentUser()"
else:
user = "\"%s\"" % add_domain(user)
jql = "%s AND assignee = %s AND %s" % (issue_type, user, status)
vprint(jql)
my_issues = jira.search_issues(jql)
msg = message_header + email_to_name(os.environ['JIRA_USERNAME']) + "\n\n"
f = open_file(filename)
filename = f.name
f.write(msg)
vprint("Found issue:")
for issue in my_issues:
vprint("%s : %s" % (issue, issue.fields.summary))
f.write("[%s]\n" % issue)
f.write("# Header: %s\n" % issue.fields.summary)
f.write("# Type: %s\n" % issue.fields.issuetype)
f.write("# Status: %s\n" % issue.fields.status)
f.write("No updates since last week.\n\n")
f.close()
return filename
def should_update():
""" A yes or no dialogue. """
global server
while True:
target = ""
if server == PRODUCTION_SERVER:
target = "OFFICAL!"
elif server == TEST_SERVER:
target = "TEST"
print("Server to update: %s" % target)
print(" %s\n" % server);
answer = raw_input("Sure you want to update Jira with the information " +
"above? [y/n] ").lower().strip()
if answer in set(['y', 'n']):
return answer
else:
print("Incorrect input: %s" % answer)
def parse_status_file(jira, filename):
"""
The main parsing function, which will decide what should go into the actual
Jira call. This for example removes the beginning until it finds a
standalone [ISSUE] tag. It will also remove all comments prefixed with '#'.
"""
# Regexp to match Jira issue on a single line, i.e:
# [SWG-28]
# [LITE-32]
# etc ...
regex = r"^\[([A-Z]+-\d+)\]\n$"
# Contains the status text, it could be a file or a status email
status = ""
with open(filename) as f:
status = f.readlines()
myissue = "";
mycomment = "";
# build list of {issue-key,comment} tuples found in status
issue_comments = []
for line in status:
# New issue?
match = re.search(regex, line)
if match:
myissue = match.group(1)
issue_comments.append((myissue, ""))
else:
# Don't add lines with comments
if (line[0] != "#" and issue_comments):
(i,c) = issue_comments[-1]
issue_comments[-1] = (i, c + line)
print("These JIRA cards will be updated as follows:\n")
for (idx,t) in enumerate(issue_comments):
(issue,comment) = issue_comments[idx]
# Strip beginning and trailing blank lines
comment = comment.strip()
issue_comments[idx] = (issue, comment)
print("[%s]\n %s" % (issue, "\n ".join(comment.splitlines())))
print("")
if should_update() == "n":
print("No change, Jira was not updated!\n")
print_status(status)
sys.exit()
# if we found something, let's update jira
for (issue,comment) in issue_comments:
update_jira(jira, issue, comment)
print("Successfully updated your Jira tickets!\n")
print_status(status)
def get_jira_instance(use_test_server):
"""
Makes a connection to the Jira server and returns the Jira instance to the
caller.
"""
global server
try:
username = os.environ['JIRA_USERNAME']
password = os.environ['JIRA_PASSWORD']
except KeyError:
eprint("Forgot to export JIRA_USERNAME and JIRA_PASSWORD?")
sys.exit()
credentials=(username, password)
if use_test_server:
server = TEST_SERVER
return JIRA(server, basic_auth=credentials)
################################################################################
# Main function
################################################################################
def main(argv):
global verbose
parser = get_parser()
args = parser.parse_args()
verbose=args.v
if not args.file and not args.q:
eprint("No file provided and not in query mode\n")
parser.print_help()
sys.exit()
jira = get_jira_instance(args.t)
exclude_stories = args.x
epics_only = args.e
if args.x or args.e:
if not args.q:
eprint("Arguments '-x' and '-e' can only be used together with '-c'")
sys.exit()
if args.q:
filename = get_jira_issues(jira, exclude_stories, epics_only, \
args.all, args.file, args.user)
elif args.file is not None:
filename = args.file
else:
eprint("This should not happen")
open_editor(filename)
parse_status_file(jira, filename)
if __name__ == "__main__":
main(sys.argv)