/
ghtix.py
executable file
·157 lines (137 loc) · 5.16 KB
/
ghtix.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
#!/usr/bin/env python
import os
import sys
import optparse
import json
import dateutil.parser
import datetime
import pytz
from github_apiv3 import client
from operator import itemgetter
import re
def msg(txt, opts):
if not opts.quiet:
sys.stderr.write(txt + "\n")
def main():
utc = pytz.UTC
farfaraway = datetime.datetime(3000,1,15,8,15,12,0, utc)
parser = optparse.OptionParser(
usage="ghtix.py [options]")
parser.add_option("-p", help="Sort by project name (default)",
action="store_true", dest="byproject", default=True)
parser.add_option("-t", help="Sort by time / due date",
action="store_false", dest="byproject", default=True)
parser.add_option("-d", help="Output remaining days instead of due date",
action="store_true", dest="usedays", default=False)
parser.add_option("-a", help="Include ALL issues even without milestone or due date",
action="store_true", dest="allissues", default=False)
parser.add_option("-e", help="Show empty projects without any issues assigned",
action="store_true", dest="showempty", default=False)
parser.add_option("-q", help="Quiet - no stderr messages, only issues list",
action="store_true", dest="quiet", default=False)
(opts, args) = parser.parse_args()
p = re.compile("(Time: )([\d]+)( hr)")
try:
jsoncfg = os.path.join(os.path.expanduser("~"), '.ghtix.json')
jsontxt = open(jsoncfg,'r').read()
cfg = json.loads(jsontxt)
username = cfg['username']
password = cfg['password']
projects = [ (x['owner'], x['name']) for x in cfg['projects'] ]
except (IOError,):
print """
# Configure your account and projects and
# save in your HOME directory as `~/.ghtix.json`
{
"username": "myusername",
"password": "mysupersecretpassword",
"projects": [
{"owner": "ecotrust", "name": "bioregion-discovery"},
{"owner": "perrygeo", "name": "ghtix"}
]
}
"""
sys.exit()
c = client.Client(username=username, password=password)
issues = []
proj_width = 0
for owner, project in projects:
msg("fetching %s tickets..." % project, opts)
if len(project) > proj_width:
proj_width = len(project)
repo = client.Repo(c, owner, project)
project_issues = repo.issues()
empty = True
for i in project_issues:
if i['assignee'] and \
i['assignee']['login'] == username and \
i['state'] != 'closed':
empty = False
i['project'] = project
i['empty'] = False
try:
due = i['milestone']['due_on']
if due:
dt = dateutil.parser.parse(due)
# d.tzinfo is not None but d.tzinfo.utcoffset(d) returns None
if dt.tzinfo is None:
dt = utc.localize(dt)
i['due_sortable'] = dt
else:
i['due_sortable'] = farfaraway
except TypeError:
i['due_sortable'] = farfaraway
issues.append(i)
if empty:
issues.append( { 'project': project,
'empty': True,
'due_sortable': farfaraway
}
)
if opts.byproject:
# sort by project name
issues = sorted(issues, key=itemgetter('project'))
else:
# sort by due date
issues = sorted(issues, key=itemgetter('due_sortable'))
for i in issues:
if i['empty']:
if opts.showempty:
print i['project'].ljust(proj_width), "---------- None"
continue
title = i['title']
number = i['number']
project = i['project']
if len(project) > 17:
project = project[:15] + ".."
project = "%s " % project
# find estimated time from tags like "Time: 120 hr"
est_time = None
est_time_str = ""
for lbl in i['labels']:
lname = lbl['name']
match = p.findall(lname)
if len(match) > 0 and len(match[0]) == 3:
est_time = match[0][1]
est_time_str = "%s hrs" % est_time
if i['milestone'] or opts.allissues:
try:
mtitle = i['milestone']['title']
if len(mtitle) > 17:
mtitle = mtitle[:15] + ".."
mtitle = "(%s) " % mtitle
except:
mtitle = ""
try:
mdue = dateutil.parser.parse(i['milestone']['due_on'])
if opts.usedays:
togo = mdue - datetime.datetime.now(utc)
duedate = "%d days" % togo.days
else:
duedate = "%d-%d-%d" % (mdue.month, mdue.day, mdue.year)
except:
duedate = ""
print "%s %s %s%s #%d %s" % (project.ljust(18),
duedate.rjust(10), mtitle.ljust(20), est_time_str.rjust(8), number, title)
if __name__ == '__main__':
main()