-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpivitool.py
executable file
·86 lines (66 loc) · 2.17 KB
/
pivitool.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
#!/usr/bin/env python
import requests
import yaml
import os
import sys
default_pivotal_context = {
'api_base': 'https://www.pivotaltracker.com/services/v5',
}
default_config_path = os.path.expanduser("~/.pivitool.yml")
def config(path):
with open(path, 'r') as f:
config_string_content = f.read()
return yaml.load(config_string_content)
default_config = config(default_config_path)
def backlog(pivotal_context, config, project_id):
headers = {
'X-TrackerToken': config['api_token'],
}
params = {
'filter': 'state:unstarted,started,finished,delivered',
}
uri = "".join([
pivotal_context['api_base'],
'/projects',
'/',
str(project_id),
'/stories',
])
return requests.get(uri,params=params,headers=headers).json()
def story_pretty(story):
if story['story_type'] == 'release':
estimate = ' '
elif 'estimate' in story:
estimate = ''.join(["(", str(story['estimate']), ") "])
else:
estimate = ''.join(["(", story['story_type'], ") "])
output = ''.join([estimate, story['name']])
if story['story_type'] == 'release':
output = ''.join(["=== ", output, " ==="])
return output
def backlog_pretty(backlog_items):
return [story_pretty(story) for story in backlog_items]
class Pivitool:
def __init__(self, config, pivotal_context):
self.config = config
self.pivotal_context = pivotal_context
def backlog(self, project_id):
return backlog(self.pivotal_context, self.config, project_id)
def backlog_pretty(self, project_id):
return backlog_pretty(self.backlog(project_id))
def backlog_pprint(self, project_id):
print("=== BACKLOG ===")
for story in self.backlog_pretty(project_id):
print(story)
class Executor:
def __init__(self, pivitool):
self.pivitool = pivitool
def run(self, funcname, *args):
func = {
'backlog': self.pivitool.backlog_pprint,
}[funcname]
func(*args)
if __name__ == '__main__':
e = Executor(Pivitool(default_config, default_pivotal_context))
argv = sys.argv
e.run(argv[1], *argv[2:])