forked from Linaro/jipdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jipstatus.py
executable file
·283 lines (235 loc) · 9.43 KB
/
jipstatus.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
#!/usr/bin/env python3
from argparse import ArgumentParser
from subprocess import call
from time import gmtime, strftime
from jinja2 import Template
import datetime
import json
import logging as log
import os
import re
import sys
import tempfile
import yaml
# Local files
import cfg
import jiralogin
import pprint
pprint = pprint.PrettyPrinter().pprint
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 default_jql():
user = cfg.args.user
project = cfg.args.project
team = cfg.args.team
if team and project:
jql = "(project = %s or assignee in membersOf('%s')) " % (project, team)
elif team:
jql = "assignee in membersOf('%s') " % team
elif project:
jql = "project = '%s' " % project
else:
jql = "assignee = '%s' " % add_domain(user)
return jql
def enumerate_updates(jira):
since = datetime.datetime.now() - datetime.timedelta(days=int(cfg.args.days))
jql = default_jql()
jql += "AND updatedDate > -%sd" % cfg.args.days
log.debug(jql)
my_issues = jira.search_issues(jql, expand="changelog", fields="summary,comment,components,assignee,created")
if my_issues.total > my_issues.maxResults:
my_issues = jira.search_issues(jql, expand="changelog", fields="summary,comment,components,assignee,created",
maxResults=my_issues.total)
for issue in my_issues:
changelog = issue.changelog
comments = issue.fields.comment.comments
status = {}
status['issue'] = str(issue)
if issue.fields.assignee:
status['assignee'] = issue.fields.assignee.displayName
else:
status['assignee'] = 'Unassigned'
status['summary'] = issue.fields.summary
status['comments'] = []
status['resolution'] = None
status['components'] = issue.fields.components
created = datetime.datetime.strptime(issue.fields.created, '%Y-%m-%dT%H:%M:%S.%f%z')
if created.replace(tzinfo=None) > since:
status['resolution'] = 'Created'
for comment in comments:
when = datetime.datetime.strptime(comment.created, '%Y-%m-%dT%H:%M:%S.%f%z')
if when.replace(tzinfo=None) < since:
continue
status['comments'].append(comment.body)
for history in changelog.histories:
when = datetime.datetime.strptime(history.created, '%Y-%m-%dT%H:%M:%S.%f%z')
if when.replace(tzinfo=None) < since:
continue
for item in history.items:
if item.field == 'resolution':
status['resolution'] = item.toString
if len(status['comments']) != 0 or status['resolution']:
yield(status)
def enumerate_pending(jira):
since = datetime.datetime.now() - datetime.timedelta(days=7)
jql = default_jql()
jql += "AND status = 'In Progress' \
AND issuetype != Initiative \
AND issuetype != Epic"
log.debug(jql)
my_issues = jira.search_issues(jql, expand="changelog", fields="summary,assignee,created,components")
if my_issues.total > my_issues.maxResults:
my_issues = jira.search_issues(jql, expand="changelog", fields="summary,assignee,created,components",
maxResults=my_issues.total)
for issue in my_issues:
status = {}
status['issue'] = str(issue)
if issue.fields.assignee:
status['assignee'] = issue.fields.assignee.displayName
else:
status['assignee'] = 'Unassigned'
status['summary'] = issue.fields.summary
status['components'] = issue.fields.components
created = datetime.datetime.strptime(issue.fields.created, '%Y-%m-%dT%H:%M:%S.%f%z')
status['new'] = created.replace(tzinfo=None) > since
yield(status)
################################################################################
# Argument parser
################################################################################
def get_parser():
""" Takes care of script argument parsing. """
parser = ArgumentParser(description='Script used to update comments in Jira')
parser.add_argument('--test', 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 first.last@linaro.org)')
parser.add_argument('-p', '--project', required=False, action="store", \
default=None, \
type = str.upper, \
help='Query Jira for only a specifc project')
parser.add_argument('-t', '--team', required=False, action="store", \
default=None, \
type = str.lower, \
help='Query Jira for only issues assigned to members of a specific tema (eg. linaro-landing-team-qualcomm)')
parser.add_argument('--days', required=False, action="store", \
default=7, \
help='Period of the report in days')
parser.add_argument('--html', required=False, nargs='?', action="store", \
const='status.html', \
help='Store output to HTML file')
parser.add_argument('-v', required=False, action="store_true", \
default=False, \
help='Output some verbose debugging info')
return parser
def initialize_logger(args):
LOG_FMT = ("[%(levelname)s] %(funcName)s():%(lineno)d %(message)s")
lvl = log.ERROR
if args.v:
lvl = log.DEBUG
log.basicConfig(
# filename="core.log",
level=lvl,
format=LOG_FMT,
filemode='w')
################################################################################
# Template for outout
################################################################################
output = """
{%- for assignee in assignees %}
{{assignee}}:
{%- for issue in updates | selectattr('assignee', 'equalto', assignee) | list %}
{%- if loop.index == 1 %}
* Past
{%- endif %}
* [{{issue['issue']}}]{% if issue['components'] |length > 0 %} [{{issue['components']|join(',')}}]{% endif %} {{issue['summary']}} {% if issue['resolution'] %}- was {{issue['resolution']|lower}}{% endif %}
{%- for c in issue['comments'] %}
{%- for cc in c.splitlines() %}
{% if loop.index == 1 %} *{% else %} {% endif %} {{cc}}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- for issue in pendings | selectattr('assignee', 'equalto', assignee) | list %}
{%- if loop.index == 1 %}
* Ongoing
{%- endif %}
* [{{issue['issue']}}]{% if issue['components'] |length > 0 %} [{{issue['components']|join(',')}}]{% endif %} {{issue['summary']}}
{%- endfor %}
{% endfor %}
"""
output_html = """
<html>
<body>
{%- for assignee in assignees %}
{{assignee}}:
<ul>
{%- for issue in updates | selectattr('assignee', 'equalto', assignee) | list %}
{%- if loop.index == 1 %}
<li>Past</li>
<ul>
{%- endif %}
<li>[<a href="https://linaro.atlassian.net/browse/{{issue['issue']}}">{{issue['issue']}}</a>]{% if issue['components'] |length > 0 %} [{{issue['components']|join(',')}}]{% endif %} {{issue['summary']}} {% if issue['resolution'] %} - was {{issue['resolution']|lower}}{% endif %}</li>
{%- for c in issue['comments'] %}
{%- if loop.index == 1 %}
<ul>
{%- endif %}
<li>{{'<br>'.join(c.splitlines())}}</li>
{%- if loop.index == loop.length %}
</ul>
{%- endif %}
{%- endfor %}
{%- if loop.index == loop.length %}
</ul>
{%- endif %}
{%- endfor %}
{%- for issue in pendings | selectattr('assignee', 'equalto', assignee) | list %}
{%- if loop.index == 1 %}
<li>Ongoing</li>
<ul>
{%- endif %}
<li>[<a href="https://linaro.atlassian.net/browse/{{issue['issue']}}">{{issue['issue']}}</a>]{% if issue['components'] |length > 0 %} [{{issue['components']|join(',')}}]{% endif %} {{issue['summary']}}</li>
{%- if loop.index == loop.length %}
</ul>
{%- endif %}
{%- endfor %}
</ul>
{% endfor %}
</body>
</html>
"""
################################################################################
# Main function
################################################################################
def main(argv):
parser = get_parser()
# The parser arguments (cfg.args) are accessible everywhere after this call.
cfg.args = parser.parse_args()
initialize_logger(cfg.args)
# This initiates the global yml configuration instance so it will be
# accessible everywhere after this call.
cfg.initiate_config()
jira, username = jiralogin.get_jira_instance(cfg.args.test)
if cfg.args.user is None:
cfg.args.user = username
updates = list(enumerate_updates(jira))
pendings = list(enumerate_pending(jira))
assignees = sorted(set([u['assignee'] for u in updates]) | set([p['assignee'] for p in pendings]))
# Move "Unassigned" issues to the end
assignees.sort(key='Unassigned'.__eq__)
template = Template(output)
print(template.render(assignees=assignees, updates=updates, pendings=pendings))
if cfg.args.html:
f = open(cfg.args.html, 'w')
template = Template(output_html)
f.write(template.render(assignees=assignees, updates=updates, pendings=pendings))
f.close()
if __name__ == "__main__":
main(sys.argv)