-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredmine-cli.py
executable file
·457 lines (371 loc) · 13 KB
/
redmine-cli.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/env python
# coding=UTF-8
__author__ = u"Mickaël Zhu"
__license__ = u"MIT"
__version__ = u"0.1"
__status__ = u"Prototype"
# core libs
import sys
import os
import shlex
# 3rd party libs
from redmine import Redmine
import redmine.exceptions as exceptions
# libs
from redminecli.server.config import Config as ServerConfig
from redminecli.context import Context
import redminecli.printer as printer
import redminecli.history as history
from redminecli.task import *
# print banner
printer.banner(u"===========")
printer.banner(u"Redmine cli")
printer.banner(u"===========")
# create config directory
config_dir = os.path.expanduser("~/.redmine-cli")
if not os.path.exists(config_dir):
os.mkdir(config_dir)
# register cli persistent history
history.register(config_dir + '/history')
# init server config
config = ServerConfig(config_dir + '/server.json')
if config.exists():
try:
config.load()
except ValueError as e:
printer.fatal(u"Invalid json config file")
printer.fatal(e.message)
printer.fatal(u"Please delete .redmine-cli/server.json in your home")
sys.exit(0)
else:
printer.warning("Redmine config not found. Please configure")
config.install()
# init tasks
tasks = Tasks(config_dir + '/tasks.json')
tasks.load()
# init context
context = Context()
# init redmine. Use getter to initialize it
redmine = None
def get_redmine():
"""
:return: Redmine instance
:rtype: Redmine
"""
global redmine
if redmine is None:
redmine = Redmine(config.url, key=config.api_key)
return redmine
def _input_int():
response = raw_input('> ').strip()
try:
return int(response, 10)
except ValueError:
return -1
def create_issue():
project = context.project
# Select the issue category
printer.title(u"Which tracker?")
tracker_list = project.trackers
for n, tracker in enumerate(tracker_list):
printer.info(" %d: %s" % (n + 1, tracker.name))
while True:
tracker_i = _input_int()
if tracker_i <= 0 or tracker_i > len(tracker_list):
printer.error("Invalid number")
continue
tracker = tracker_list[tracker_i - 1]
break
printer.info("You selected %s" % (tracker.name))
# Type subject
printer.title(u"Subject?")
while True:
subject = raw_input("> ").strip()
if subject:
break
printer.error("Please define a subject")
# Type description
printer.title("Description?")
descriptionList = []
while True:
line = raw_input("> ").strip()
if line:
descriptionList.append(line)
continue
break
description = "\n".join(descriptionList)
# Select the assigned user
printer.title(u"Which assigned user?")
user_list = get_redmine().user.all()
for n, user in enumerate(user_list):
printer.info(" %d: %s" % (n + 1, user.login))
while True:
user_i = _input_int()
if user_i <= 0 or user_i > len(user_list):
printer.error("Invalid number")
continue
user = user_list[user_i - 1]
break
printer.info("You selected %s" % (user.login))
# Send issue
printer.title("Create this issue (y/n)")
printer.info("Project : %s" % (project.name))
printer.info("Tracker : %s" % (tracker.name))
printer.info("Subject : %s" % (subject))
printer.info("Description : %s" % (description))
printer.info("Assigned to : %s" % (user.login))
while True:
confirm = raw_input("> ").strip().lower()
if confirm == "n":
return
if confirm == "y":
break
printer.error("Invalid response")
issue = get_redmine().issue.new()
issue.project_id = project.id
issue.tracker_id = tracker.id
issue.subject = subject
issue.description = description
issue.assigned_to_id = user.id
issue.status_id = 1
issue.priority_id = 1
if issue.save():
printer.success("Done")
else:
printer.error("Error")
def issue_task_upload():
task = tasks.find_from_issue(context.issue.id)
if not task:
printer.warning("No task")
return
# Select the issue category
printer.title(u"Which activity?")
activity_list = redmine.enumeration.filter(resource='time_entry_activities')
for n, activity in enumerate(activity_list):
printer.info(" %d: %s" % (n + 1, activity.name))
while True:
activity_i = _input_int()
if activity_i <= 0 or activity_i > len(activity_list):
printer.error("Invalid number")
continue
activity = activity_list[activity_i - 1]
break
printer.info("You selected %s" % (activity.name))
# Type subject
printer.title(u"Commentaire?")
comments = raw_input("> ").strip()
time_entry = get_redmine().time_entry.new()
time_entry.issue_id = context.issue.id
time_entry.hours = task.total // 3600
time_entry.activity_id = activity.id
time_entry.comments = comments
time_entry.save()
issue_task_delete()
printer.title(u"Update done ratio? [%d]" % (context.issue.done_ratio))
while True:
response = raw_input('> ').strip()
if not response:
return
try:
done_ratio = int(response, 10)
if done_ratio >= 0 and done_ratio <= 100:
break
printer.error("Invalid number")
except ValueError:
printer.error("Invalid number")
get_redmine().issue.update(context.issue.id, done_ratio=done_ratio)
def select_project(id=None):
if not id:
printer.title(u"Select by typing the id or identifier")
project_list = get_redmine().project.all()
for n, project in enumerate(project_list):
printer.info("%s [%d] (%s)" % (project.name, project.id, project.identifier))
id = raw_input('> ')
try:
context.project = get_redmine().project.get(id)
context.issue = None
except exceptions.ResourceNotFoundError:
printer.error("No project found with id %s" % (id))
return
def unselect_project():
context.project = None
context.issue = None
def select_issue(id=None):
if not id:
if context.project:
issue_list = get_redmine().issue.filter(assigned_to_id="me", project_id=context.project.id)
else:
issue_list = get_redmine().issue.filter(assigned_to_id="me")
if len(issue_list) > 0:
printer.title(u"Select by typing the id")
else:
printer.warning(u"No issues found")
return
if context.project:
issue_list = get_redmine().issue.filter(assigned_to_id="me", status_id=1, project_id=context.project.id)
for n, issue in enumerate(issue_list):
printer.info("#%d %s" % (issue.id, issue.subject))
else:
issue_list = get_redmine().issue.filter(assigned_to_id="me", status_id=1)
for n, issue in enumerate(issue_list):
printer.info("#%d %s [%s]" % (issue.id, issue.subject, issue.project.name))
id = raw_input('> ')
try:
context.issue = get_redmine().issue.get(id)
except exceptions.ResourceNotFoundError:
printer.error("No id found with id %s" % (id))
return
if not context.project or context.project.id != context.issue.project.id:
context.project = get_redmine().project.get(context.issue.project.id)
def unselect_issue():
context.issue = None
def show_issue():
issue = context.issue
printer.title(issue.subject)
printer.info("ID : %d" % (issue.id))
printer.info("Project : %s" % (issue.project.name))
printer.info("Tracker : %s" % (issue.tracker.name))
printer.info("Status : %s" % (issue.status.name))
printer.info("Priority : %s" % (issue.priority.name))
printer.info("Author : %s" % (issue.author.name))
printer.info("Assigned to : %s" % (issue.assigned_to.name))
printer.info("Description : %s" % (issue.description))
printer.info("Start date : %s" % (issue.start_date))
printer.info("Done ratio : %d" % (issue.done_ratio))
printer.info("Spent hours : %f" % (issue.spent_hours))
def _format_duration(seconds):
value = ''
if seconds % 60 > 0:
value = str((seconds % 60)) + 's'
minutes = seconds / 60
if minutes % 60 > 0:
value = str((minutes % 60)) + 'm' + value
return str(minutes / 60) + 'h' + value
def show_all_tasks():
for task in tasks.list:
printer.info("%s : %s %s" % (task.description, _format_duration(task.total), "*" if task.start else ""))
def issue_task_start():
tasks.start_task(context.issue.id, " [%s] %s" % (context.project.name, context.issue.subject))
def issue_task_stop():
tasks.stop_task(context.issue.id)
def issue_task_delete():
tasks.delete_task(context.issue.id)
def issue_task_info():
task = tasks.find_from_issue(context.issue.id)
if task:
printer.info("Total time %s" % (_format_duration(task.total)))
else:
printer.info("No task defined")
def interpret_from_project(args):
if args[0] == 'help':
printer.info(" - createissue : create issue")
printer.info(" - tasks : show all tasks")
printer.info(" - project [id|identifier] : select another project")
printer.info(" - unselect : unselect the project")
printer.info(" - issue [id] : select an issue")
printer.info(" - exit : exit the program")
elif args[0] == 'tasks':
show_all_tasks()
elif args[0] == 'createissue':
create_issue()
elif args[0] == 'project':
select_project(args[1] if len(args) > 1 else None)
elif args[0] == 'unselect':
unselect_project()
elif args[0] == 'issue':
select_issue(args[1] if len(args) > 1 else None)
else:
printer.error("Invalid command. Type help")
def issue_task_edit():
task = tasks.find_from_issue(context.issue.id)
if not task:
printer.warning("No task")
return
printer.title(u"Set second elapsed [%d]" % (task.total))
while True:
response = raw_input('> ').strip()
if not response:
return
try:
total = int(response, 10)
break
except ValueError:
printer.error("Invalid number")
task.total = total
tasks.save()
def interpret_from_issue_context(args):
if args[0] == 'help':
printer.info(" - show_all_tasks : show all task")
printer.info(" - tasks : show all tasks")
printer.info(" - taskstart : start task counter")
printer.info(" - taskstop : stop task counter")
printer.info(" - taskinfo : show task info")
printer.info(" - taskedit : edit task")
printer.info(" - taskdelete : delete task")
printer.info(" - taskupload : upload task")
printer.info(" - project [id|identifier] : select another project")
printer.info(" - unselect : unselect the issue")
printer.info(" - issue [id] : select another issue")
printer.info(" - info : info about this issue")
printer.info(" - exit : exit the program")
elif args[0] == 'tasks':
show_all_tasks()
elif args[0] == 'taskstart':
issue_task_start()
elif args[0] == 'taskstop':
issue_task_stop()
elif args[0] == 'taskedit':
issue_task_edit()
elif args[0] == 'taskdelete':
issue_task_delete()
elif args[0] == 'taskinfo':
issue_task_info()
elif args[0] == 'taskupload':
issue_task_upload()
elif args[0] == 'project':
select_project(args[1] if len(args) > 1 else None)
elif args[0] == 'unselect':
unselect_issue()
elif args[0] == 'issue':
select_issue(args[1] if len(args) > 1 else None)
elif args[0] == 'info':
show_issue()
elif args[0] == 'cd':
if len(args) < 2:
unselect_project()
elif args[1] == '..':
unselect_issue()
else:
printer.error("Invalid command. Type help")
def interpret(args):
"""
:rtype : list
"""
if len(args) == 0:
return
if args[0] == 'exit':
sys.exit(0)
if context.issue and context.project:
return interpret_from_issue_context(args)
elif context.project:
return interpret_from_project(args)
if args[0] == 'help':
printer.info(" - project [id|identifier] : select a project")
printer.info(" - exit : exit the program")
elif args[0] == 'project':
select_project(args[1] if len(args) > 1 else None)
elif args[0] == 'issue':
select_issue(args[1] if len(args) > 1 else None)
else:
printer.error("Invalid command. Type help")
def prompt():
if context.issue and context.project:
user_input = raw_input("[%s] #%d > " % (context.project.identifier, context.issue.id))
elif context.project:
user_input = raw_input('[%s] > ' % (context.project.identifier))
else:
user_input = raw_input('[] > ')
user_input_splitted = shlex.split(user_input)
interpret(user_input_splitted)
while True:
prompt()