-
Notifications
You must be signed in to change notification settings - Fork 0
/
taiga-query.py
executable file
·283 lines (206 loc) · 7.37 KB
/
taiga-query.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
"""taiga-query metrics tool"""
import sys
import os
import datetime
import logging
import argparse
from tabulate import tabulate
from taiga import TaigaAPI
# TripleO CI (production) 289231
PROJECT_SLUG = 'tripleo-ci-board'
def get_project(project_slug):
"""
Retrieve project id for a given slug (board name)
@param project_slug: Project board name
@return: Returns a project id (integer)
"""
project_id=api.projects.get_by_slug(project_slug).id
message='project_id = %d, project" ' % project_id
log.debug(message)
message='https://tree.taiga.io/project/%s' % PROJECT_SLUG
log.info(message)
return project_id
def auth_taiga():
"""
Authenticates user/password on taiga instance using pre-loaded
env variables
@param <none>
@return: Returns a token id (string)
"""
try:
taiga_username=os.environ['TAIGA_USERNAME']
taiga_password=os.environ['TAIGA_PASSWORD']
except KeyError:
log.error("Environment variables TAIGA_USERNAME and "
"TAIGA_PASSWORD must be set.")
sys.exit(1)
api.auth(username=taiga_username, password=taiga_password)
message='User %s successfully authenticated.' % taiga_username
log.debug(message)
return api.token
def get_user_stories(project_id):
"""
Retrieves user stories from Taiga
@param project_id: Integer that uniquely identifies a project
@return: Returns a list of user stories
"""
return api.search(project_id).user_stories
def filter_user_stories(user_stories, **filters):
"""
Retrieves a filtered list of user stories based on keyword args
@param user_stories: List of user stories to filter from
@param filters: keypair values to filter e.g. k1=v1, k2=v2 ...
@return: Returns a list of filtered user stories
"""
filtered_stories = []
for us in user_stories:
for key, value in filters.items():
story_property = getattr(api.user_stories.get(us.id), key)
message = 'User Story #%d : ref=%d key=%s value=%s' % (us.id,
us.ref, key, story_property)
log.debug(message)
if story_property is value:
message = 'Adding US #%d to query result' % us.ref
log.debug(message)
filtered_stories.append(us)
logging.debug('Filtered User Stories:')
logging.debug(filtered_stories)
return filtered_stories
def get_stories_data(user_stories, project_id, *fields):
"""
Retrieves an user story array like:
[["value1", "value2"], ["value3", "value4"]]
\------ row 1 -----/ \----- row 2 ------/
\----------------- array ------------------/
@param user_stories: List of user stories to get data from
@param project_id: Project id to get users and status info
@param fields: comma separated set of fields to query
@return: Returns an array of user story rows
"""
users_dict = get_project_users(project_id)
statuses_dict = get_story_statuses(project_id)
us_array=[]
for us in user_stories:
us_row=[]
for f in fields:
us_property = getattr(api.user_stories.get(us.id), f)
# Get status/user name from id
if f == 'status':
us_property = statuses_dict.get(us_property)
if f == 'assigned_to':
us_property = users_dict.get(us_property)
us_row.append(us_property)
us_array.append(us_row)
return us_array
def print_user_stories(us_array, columns):
"""
Prints an user story array like:
+------------+------------+
| column 1 | column 2 |
+============+============+
| value1 | value2 |
+------------+------------+
| value3 | value4 |
+------------+------------+
@param us_array: Array of user stories
@param columns: List of columns (fields)
@return: <none>
"""
print tabulate(us_array, columns, tablefmt="grid")
def get_current_sprint(project_id):
"""
Retrieves the current milestone
@param project_id: Project identifier (integer)
@return: Returns the current milestone
"""
now = datetime.datetime.now()
date = now.strftime("%Y-%m-%d")
for ms in api.milestones.list(project=project_id):
if date > ms.estimated_start and date < ms.estimated_finish:
sprint_url = get_sprint_board(ms)
message='Sprint: [ %s ] | %s' % (ms.name, sprint_url)
log.info(message)
return ms
return milestone
def get_sprint_stories(milestone):
"""
Retrieves user stories belonging to a milestone
@param milestone: milestone to get the user stories from
@return: Returns the list of user stories from a milestone
"""
return milestone.user_stories
def get_sprint_board(milestone):
"""
Retrieves the taskboard link url for a milestone
@param milestone: milestone to get the board url from
@return: Returns the taskboard link url for a milestone
"""
baseurl='https://tree.taiga.io/project/%s/taskboard/' % PROJECT_SLUG
sprint_url = baseurl + milestone.slug
return sprint_url
def get_project_users(project_id):
"""
Retrieves all users from a project
@param project_id: project id to get users from
@return: Returns a dict with user full names and ids
"""
users_dict={}
users_list = api.users.list(project=project_id)
for u in users_list:
users_dict[u.id] = u.full_name
return users_dict
def get_story_statuses(project_id):
"""
Retrieves all user story statuses from a project
@param project_id: project id to get users from
@return: Returns a dict with statuses and ids
"""
statuses_dict={}
statuses_list = api.user_story_statuses.list(project=project_id)
for s in statuses_list:
statuses_dict[s.id] = s.name
return statuses_dict
def main(argv):
token = auth_taiga()
project_id = get_project(PROJECT_SLUG)
parser = argparse.ArgumentParser(
description="Query user stories from Taiga project"
)
parser.add_argument(
'-o', '--orphan',
action='store_true',
default=False,
help='Search for orphan user stories (without owner/assigned_to)',
)
parser.add_argument(
'-s', '--sprint',
default='current',
help='Specify the sprint to search'
)
parser.add_argument(
'-d', '--debug',
action='store_true',
help='Enable DEBUG mode.'
)
args = parser.parse_args()
logformat = '%(asctime)s %(levelname)s %(name)s: %(message)s'
if args.debug:
logging.basicConfig(level=logging.DEBUG, format=logformat)
else:
logging.basicConfig(level=logging.INFO, format=logformat)
if args.sprint == "current":
sprint = get_current_sprint(project_id)
user_stories = get_sprint_stories(sprint)
else:
user_stories = get_user_stories(project_id)
if args.orphan:
message = 'Getting orphan user stories...'
log.info(message)
user_stories = filter_user_stories(user_stories, assigned_to=None)
us_array = get_stories_data(user_stories, project_id, 'ref', 'subject',
'status', 'assigned_to')
print_user_stories(us_array, ['ref', 'subject', 'status', 'assigned_to'])
if __name__ == "__main__":
log = logging.getLogger(__name__)
api = TaigaAPI()
main(sys.argv)