-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathgithub_agile_dashboard.py
executable file
·1029 lines (870 loc) · 30.6 KB
/
github_agile_dashboard.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
'''
Agile Dashboard for a GitHub Repository
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This script will output a dashboard for GitHub issues and pull
requests of a repository.
It was the original tool that did lead to the development of
``sgqlc``, while the author was very familiar with GraphQL, using it
from Python was kind of painful.
:license: ISC
'''
__docformat__ = 'reStructuredText en'
import datetime
import json
import logging
import os
import re
import sys
from collections import OrderedDict
from sgqlc.operation import Operation # noqa: I900
from sgqlc.endpoint.http import HTTPEndpoint # noqa: I900
from github_schema import github_schema as schema # noqa: I900
logger = logging.getLogger(__name__)
def json_pretty_print(d, file=None):
args = {'sort_keys': True, 'indent': 2, 'separators': (',', ': ')}
if file:
return json.dump(d, file, **args)
return json.dumps(d, **args)
def compact_fmt(d):
s = []
for k, v in d.items():
if isinstance(v, dict):
v = compact_fmt(v)
elif isinstance(v, (list, tuple)):
lst = []
for e in v:
if isinstance(e, dict):
lst.append(compact_fmt(e))
else:
lst.append(repr(e))
s.append('%s=[%s]' % (k, ', '.join(lst)))
continue
s.append('%s=%r' % (k, v))
return '(' + ', '.join(s) + ')'
def report_download_errors(errors):
logger.error('Document contain %d errors', len(errors))
for i, e in enumerate(errors):
msg = e.pop('message')
extra = ''
if e:
extra = ' %s' % compact_fmt(e)
logger.error('Error #%d: %s%s', i + 1, msg, extra)
raise SystemExit('Failed to download')
def select_issues(
repo, labels=(), states=(), first=100, after=None, last=None, before=None
):
args = {}
if labels:
args['labels'] = labels
if states:
args['states'] = states
if first:
args['first'] = first
if after:
args['after'] = after
if last:
args['last'] = last
if before:
args['before'] = before
conn = repo.issues(**args)
conn.page_info.__fields__(
has_next_page=True,
end_cursor=True,
)
nodes = conn.nodes
nodes.__fields__(
number=True,
state=True,
title=True,
body_text=True,
created_at=True,
closed_at=True,
)
nodes.labels(first=100).nodes.name()
nodes.author.login()
nodes.project_cards(first=100).nodes.project.id()
nodes.milestone.id()
nodes.assignees(first=100).nodes.login()
def select_pull_requests(
repo, labels=(), states=(), first=100, after=None, last=None, before=None
):
args = {}
if labels:
args['labels'] = labels
if states:
args['states'] = states
if first:
args['first'] = first
if after:
args['after'] = after
if last:
args['last'] = last
if before:
args['before'] = before
conn = repo.pull_requests(**args)
conn.page_info.__fields__(
has_next_page=True,
end_cursor=True,
)
nodes = conn.nodes
nodes.__fields__(
number=True,
state=True,
title=True,
body_text=True,
created_at=True,
closed_at=True,
merged_at=True,
additions=True,
deletions=True,
head_ref_name=True,
)
nodes.labels(first=100).nodes.name()
nodes.author.login()
nodes.project_cards(first=100).nodes.project.id()
nodes.milestone.id()
nodes.assignees(first=100).nodes.login()
reviews = nodes.reviews(first=100)
reviews.nodes.author.login()
reviews.nodes.__fields__(
body_text=True,
created_at=True,
state=True,
)
def create_operation(owner, name, labels=(), issue_states=(), pr_states=()):
op = Operation(schema.Query)
repo = op.repository(owner=owner, name=name)
repo.labels(first=100).nodes.__fields__(
name=True,
color=True,
)
repo.milestones(first=100).nodes.__fields__(
id=True,
state=True,
due_on=True,
title=True,
description=True,
)
repo.projects(first=100).nodes.__fields__(
id=True,
number=True,
name=True,
state=True,
)
select_issues(repo, labels, issue_states)
select_pull_requests(repo, labels, pr_states)
return op
def download(endpoint, reponame, labels=(), issue_states=(), pr_states=()):
owner, name = reponame.split('/', 1)
op = create_operation(owner, name, labels, issue_states, pr_states)
logger.info('Downloading base information from %s', endpoint)
logger.debug('Operation:\n%s', op)
d = endpoint(op)
errors = d.get('errors')
if errors:
return report_download_errors(errors)
repodata = (op + d).repository
while (
repodata.issues.page_info.has_next_page
or repodata.pull_requests.page_info.has_next_page
):
op = Operation(schema.Query)
repo = op.repository(owner=owner, name=name)
if repodata.issues.page_info.has_next_page:
select_issues(
repo,
labels,
issue_states,
after=repodata.issues.page_info.end_cursor,
)
if repodata.pull_requests.page_info.has_next_page:
select_pull_requests(
repo,
labels,
pr_states,
after=repodata.pull_requests.page_info.end_cursor,
)
logger.info(
'Downloading extra connection nodes: issues=%s, prs=%s',
repodata.issues.page_info,
repodata.pull_requests.page_info,
)
logger.debug('Operation:\n%s', op)
cont = endpoint(op)
errors = cont.get('errors')
if errors:
return report_download_errors(errors)
contdata = (op + cont).repository
if repodata.issues.page_info.has_next_page:
repodata.issues += contdata.issues
if repodata.pull_requests.page_info.has_next_page:
repodata.pull_requests += contdata.pull_requests
logger.info('Finished downloading repository: %s', reponame)
logger.debug('%s', repodata)
return d
class ComplexObjectFilter:
'''Filter by number of string attribute, including regexp.
Used to filter projects and milestones, this will check if the
filter ``f`` is an integer and filter by property ``obj.number``,
otherwise, if ``f`` is a string, will check if it matches
``attrname`` attribute of object. Last but not least it will call
``f.search()`` with attribute ``attrname`` of object, allowing a
compiled regular expression to be used.
'''
def __init__(self, f, attrname):
if isinstance(f, str):
self.matches = lambda obj: getattr(obj, attrname) == f
elif isinstance(f, int):
self.matches = lambda obj: obj.number == f
else:
self.matches = lambda obj: bool(f.search(getattr(obj, attrname)))
class GitHubAgileDashboard:
'''Base class for Agile Dashboard over GitHub database.
Given a repository data structure ``repo`` which matches GitHub's
GraphQL (API v4) schema, then extra filters are applied and the
following members will be populated:
- ``labels`` dict with keys being name
- ``projects`` dict with keys being ``Node.id``
- ``milestones`` dict with keys being ``Node.id``
- ``issues`` dict with keys being ``Issue.number``
- ``pull_requests`` dict with keys being ``PullRequest.number``
The unfiltered versions are prefixed with ``all_``, ie:
``all_labels`` contains all labels in the repository.
The following lists provide insights to render Agile Dashboards:
- ``todo`` open and unassigned issues
- ``working`` open and assigned issues
- ``reviewing`` open pull requests
- ``done`` closed or merged pull requests or issues
Extra members that may be useful:
- ``max_number`` the maximum ``Issue.number`` and
``PullRequest.number``, can be used to indentation/formatting
purposes.
'''
def __init__(
self,
repo,
filter_labels=(),
filter_issue_states=(),
filter_pr_states=(),
filter_projects=(),
filter_milestones=(),
filter_assignees=(),
):
self.repo = repo
self.labels = {}
self.projects = {}
self.milestones = {}
self.issues = {}
self.pull_requests = {}
self.all_labels = {}
self.all_projects = {}
self.all_milestones = {}
self.all_issues = {}
self.all_pull_requests = {}
self.todo = []
self.working = []
self.reviewing = []
self.done = []
self.max_number = 0
self.any_milestone = not bool(filter_milestones)
self.any_project = not bool(filter_projects)
self.filter_label = self._build_filter(filter_labels)
self.filter_issue_state = self._build_filter(filter_issue_states)
self.filter_pr_state = self._build_filter(filter_pr_states)
self.filter_project = self._build_complex_filter(
filter_projects, 'name'
)
self.filter_milestone = self._build_complex_filter(
filter_milestones, 'title'
)
self.filter_assignee = self._build_filter(filter_assignees)
self.populate()
def populate(self):
self.populate_labels()
self.populate_projects()
self.populate_milestones()
self.populate_issues()
self.populate_requests()
def populate_labels(self):
if self.repo.labels:
for n in self.repo.labels.nodes:
self.all_labels[n.name] = n
if self.filter_label(n.name):
self.labels[n.name] = n
def populate_projects(self):
if self.repo.projects:
for n in self.repo.projects.nodes:
self.all_projects[n.id] = n
if self.filter_project(n):
self.projects[n.id] = n
def populate_milestones(self):
if self.repo.milestones:
for n in self.repo.milestones.nodes:
self.all_milestones[n.id] = n
if self.filter_milestone(n):
self.milestones[n.id] = n
def populate_issues(self):
if self.repo.issues:
for n in self.repo.issues.nodes:
self.all_issues[n.number] = n
if self.filter_issue(n):
self.issues[n.number] = n
self.max_number = max(self.max_number, n.number)
if n.state == 'OPEN':
if not n.assignees or not n.assignees.nodes:
self.todo.append(n)
else:
self.working.append(n)
else:
self.done.append(n)
def populate_requests(self):
if self.repo.pull_requests:
for n in self.repo.pull_requests.nodes:
self.all_pull_requests[n.number] = n
if self.filter_pull_request(n):
self.pull_requests[n.number] = n
self.max_number = max(self.max_number, n.number)
if n.state == 'OPEN':
self.reviewing.append(n)
else:
self.done.append(n)
def filter_connection(self, conn, filter_cb, attrname):
if not conn or not conn.nodes:
return filter_cb(None)
for n in conn.nodes:
if filter_cb(getattr(n, attrname)):
return True
return False
def filter_project_cards(self, obj):
if not hasattr(obj, 'projects'):
obj.projects = []
if not obj.project_cards or not obj.project_cards.nodes:
return self.any_project
for n in obj.project_cards.nodes:
if n.project.id in self.projects:
n.project = self.projects[n.project.id]
if n.project not in obj.projects:
obj.projects.append(n.project)
return True
return False
def filter_milestone_id(self, obj):
if not obj.milestone:
return self.any_milestone
if obj.milestone.id in self.milestones:
obj.milestone = self.milestones[obj.milestone.id]
return True
return False
def filter_common(self, node):
if not self.filter_connection(node.labels, self.filter_label, 'name'):
return False
if not self.filter_project_cards(node):
return False
if not self.filter_milestone_id(node):
return False
if not self.filter_connection(
node.assignees, self.filter_assignee, 'login'
):
return False
return True
def filter_issue(self, issue):
if not self.filter_issue_state(issue.state):
return False
return self.filter_common(issue)
def filter_pull_request(self, pr):
if not self.filter_pr_state(pr.state):
return False
return self.filter_common(pr)
@staticmethod
def _build_filter(lst):
if not lst:
return lambda x: True
return lambda x: x in lst
@staticmethod
def _build_complex_filter(lst, attrname):
if not lst:
return lambda x: True
filters_lst = tuple(ComplexObjectFilter(e, attrname) for e in lst)
return lambda x: any(f.matches(x) for f in filters_lst)
def cmd_save(endpoint, args):
d = download(
endpoint, args.repo, args.label, args.issue_state, args.pr_state
)
if args.pretty:
return json_pretty_print(d, args.file)
return json.dump(d, args.file, sort_keys=True, separators=(',', ':'))
def create_complex_filter_list(lst):
ret = []
for e in lst:
try:
ret.append(int(e))
continue
except ValueError:
pass
if e.startswith('/') and e.endswith('/'):
try:
ret.append(re.compile(e[1:-1]))
continue
except re.error:
pass
ret.append(e)
return tuple(ret)
def create_simple_filter_list(lst):
ret = list(lst)
if '' in ret:
ret.remove('')
ret.append(None)
return tuple(ret)
def create_state_filter_list(lst):
if lst == ['']:
return (None,) # won't match anything
return tuple(lst)
def create_dashboard(endpoint, args):
labels = create_simple_filter_list(args.label)
issue_states = create_state_filter_list(args.issue_state)
pr_states = create_state_filter_list(args.pr_state)
projects = create_complex_filter_list(args.project)
milestones = create_complex_filter_list(args.milestone)
assignees = create_simple_filter_list(args.assignee)
if args.load:
try:
d = json.load(args.load)
except json.decoder.JSONDecodeError as exc:
raise SystemExit('Failed to parse: %s' % (exc,)) from exc
if d is None:
raise SystemExit('Failed to load!')
else:
d = download(
endpoint, args.repo, args.label, args.issue_state, args.pr_state
)
op = create_operation(
args.repo, args.label, args.issue_state, args.pr_state
)
repodata = (op + d).repository
return GitHubAgileDashboard(
repodata,
labels,
issue_states,
pr_states,
projects,
milestones,
assignees,
)
# TODO: refactor this into classes, is too complex (C901)
def cmd_dashboard_text(endpoint, args): # noqa: C901
dashboard = create_dashboard(endpoint, args)
outfile = sys.stdout
colors = {
'Issue': {
'state': {
'OPEN': '\033[1;33m',
'CLOSED': '\033[1;32m',
},
},
'PullRequest': {
'state': {
'OPEN': '\033[1;33m',
'MERGED': '\033[1;32m',
'CLOSED': '\033[1;35m',
},
'unreviewed': '\033[1;33m',
'reviews': {
'APPROVED': '\033[1;32m',
'CHANGES_REQUESTED': '\033[1;31m',
'COMMENTED': '\033[1;37m',
'DISMISSED': '\033[1;34m',
'PENDING': '\033[1;33m',
},
},
'todo': '\033[1;31m',
'working': '\033[1;33m',
'reviewing': '\033[1;36m',
'done': '\033[1;32m',
'clear': '\033[0m',
'number': '\033[1m',
'body': '\033[0m',
'key-value': '\033[36m',
'overtime': '\033[31m',
'deadline': '\033[1;33m',
'oneline': {
'assignees': '\033[1;37m',
},
}
def label_rgb_to_ansi_index(r, g, b, bright):
return 'label-%d-%d-%d-%d' % (int(r), int(g), int(b), int(bright))
colors[label_rgb_to_ansi_index(0, 0, 0, 0)] = '\033[0;97;40m'
colors[label_rgb_to_ansi_index(1, 0, 0, 0)] = '\033[0;97;41m'
colors[label_rgb_to_ansi_index(0, 1, 0, 0)] = '\033[0;97;42m'
colors[label_rgb_to_ansi_index(1, 1, 0, 0)] = '\033[0;90;43m'
colors[label_rgb_to_ansi_index(0, 0, 1, 0)] = '\033[0;97;44m'
colors[label_rgb_to_ansi_index(1, 0, 1, 0)] = '\033[0;90;45m'
colors[label_rgb_to_ansi_index(0, 1, 1, 0)] = '\033[0;90;46m'
colors[label_rgb_to_ansi_index(1, 1, 1, 1)] = '\033[0;30;47m'
colors[label_rgb_to_ansi_index(0, 0, 0, 1)] = '\033[0;97;100m'
colors[label_rgb_to_ansi_index(1, 0, 0, 1)] = '\033[0;97;101m'
colors[label_rgb_to_ansi_index(0, 1, 0, 1)] = '\033[0;97;102m'
colors[label_rgb_to_ansi_index(1, 1, 0, 1)] = '\033[0;30;103m'
colors[label_rgb_to_ansi_index(0, 0, 1, 1)] = '\033[0;97;104m'
colors[label_rgb_to_ansi_index(1, 0, 1, 1)] = '\033[0;30;105m'
colors[label_rgb_to_ansi_index(0, 1, 1, 1)] = '\033[0;30;106m'
colors[label_rgb_to_ansi_index(1, 1, 1, 1)] = '\033[0;30;107m'
no_colors = False
if args.colors != 'yes' and (not outfile.isatty() or args.colors == 'no'):
no_colors = True
def uncolorize_dict(d):
for k, v in tuple(d.items()): # copy to modify d while iterating
if isinstance(v, str):
d[k] = ''
elif isinstance(v, dict):
uncolorize_dict(v)
uncolorize_dict(colors)
def out(color='', text='', end='\n'):
outfile.writelines((color, text, colors['clear'], end))
review_state_symbol = {
'APPROVED': '+',
'CHANGES_REQUESTED': '-',
'COMMENTED': '"',
'DISMISSED': '#',
'PENDING': '?',
}
number_len = len(str(dashboard.max_number))
number_fmt = '%%%dd' % number_len
if args.format == 'oneline':
title_len = os.environ.get('COLUMNS', 80) - number_len - 2
def get_assignees(node):
if not node.assignees or not node.assignees.nodes:
return ''
return '[' + ','.join(a.login for a in node.assignees.nodes) + '] '
def out_common(node, title_len=title_len):
color = colors[node.__class__.__name__]['state'][node.state]
out(colors['number'], number_fmt % node.number, ' ')
assignees = get_assignees(node)
if assignees:
out(colors['oneline']['assignees'], assignees, '')
title_len -= len(assignees)
out(color, node.title[:title_len], ' ')
def out_issue(issue):
out_common(issue)
out()
review_state_len = len(schema.PullRequestReviewState.__choices__)
def get_reviews_summary(pr):
if not pr.reviews or not pr.reviews.nodes:
s = '!' * review_state_len
return '%s%s' % (colors['PullRequest']['unreviewed'], s)
final = set()
for r in pr.reviews.nodes:
final.add(r.state)
def fmt_review_state(state):
color = colors['PullRequest']['reviews'][state]
sym = review_state_symbol[state]
return '%s%s' % (color, sym)
return ''.join(fmt_review_state(state) for state in final)
def out_pr(pr):
out_common(pr, title_len - review_state_len - 3)
out('', ' [%s%s]' % (get_reviews_summary(pr), colors['clear']), '')
out()
def out_header(name, label):
prefix = '-' * number_len
text = ' %s: %d' % (label, len(getattr(dashboard, name)))
out(colors[name], prefix + text)
elif args.format in ('full', 'medium'):
prefix = ' ' * number_len + ' '
def out_key_val(key, value):
if not value:
return
try:
color = colors[key]
except KeyError:
color = colors['key-value']
title = key.replace('_', ' ').title()
out(color, '%s%s: %s' % (prefix, title, value))
def get_labels(node):
if not node.labels or not node.labels.nodes:
return ''
def fmt_label(n):
if no_colors:
return n.name
label = dashboard.all_labels.get(n.name)
if not label:
return n.name
color = label.color
red = int(color[0:2], 16) >> 6
green = int(color[2:4], 16) >> 6
blue = int(color[4:6], 16) >> 6
is_red = red > 0
is_green = green > 0
is_blue = blue > 0
is_high_red = red > 1
is_high_green = green > 1
is_high_blue = blue > 1
is_bright = is_high_red or is_high_green or is_high_blue
if is_bright:
is_red = is_high_red
is_green = is_high_green
is_blue = is_high_blue
color = colors[
label_rgb_to_ansi_index(
is_red, is_green, is_blue, is_bright
)
]
return '%s%s%s' % (color, n.name, colors['clear'])
return ', '.join(fmt_label(n) for n in node.labels.nodes)
today = datetime.datetime.now(datetime.timezone.utc)
def get_milestone(node):
if not node.milestone:
return ''
m = node.milestone
color = ''
if m.due_on < today:
color = colors['overtime']
else:
td = m.due_on - today
if td.days < 7:
color = colors['deadline']
date = m.due_on.date().isoformat()
return '%s %sdue %s' % (m.title, color, date)
def get_projects(node):
if not node.projects:
return ''
return ', '.join(p.name for p in node.projects)
def get_assignees(node):
if not node.assignees or not node.assignees.nodes:
return ''
return ', '.join(a.login for a in node.assignees.nodes)
def out_common(node):
color = colors[node.__class__.__name__]['state'][node.state]
out(colors['number'], number_fmt % node.number, ' ')
out(color, node.title)
out_key_val('author', node.author.login)
out_key_val('created_at', node.created_at)
out_key_val('closed_at', node.closed_at)
out_key_val('labels', get_labels(node))
out_key_val('milestone', get_milestone(node))
out_key_val('projects', get_projects(node))
out_key_val('assigness', get_assignees(node))
def out_body(node):
body_color = colors['body']
out()
body_prefix = prefix + ' '
for ln in node.body_text.splitlines():
out(body_color, body_prefix + ln.rstrip())
def out_issue(issue):
out_common(issue)
if args.format == 'full':
out_body(issue)
out()
def get_reviews_summary(pr):
if not pr.reviews or not pr.reviews.nodes:
return '%sunreviewed' % (colors['PullRequest']['unreviewed'],)
final = OrderedDict()
for r in pr.reviews.nodes:
final[r.author.login] = r
def fmt_review(r):
color = colors['PullRequest']['reviews'][r.state]
sym = review_state_symbol[r.state]
author = r.author.login
return '%s%s%s%s' % (color, sym, author, colors['clear'])
return ', '.join(fmt_review(r) for r in final.values())
def out_pr(pr):
out_common(pr)
out_key_val('reviews', get_reviews_summary(pr))
if args.format == 'full':
out_body(issue)
out_key_val('diff', '-%d/+%d' % (pr.deletions, pr.additions))
out_key_val('ref', pr.head_ref_name)
out_body(issue)
out()
def out_header(name, label):
prefix = '-' * number_len
text = ' %s: %d' % (label, len(getattr(dashboard, name)))
out(colors[name], prefix + text)
def out_node(node):
if isinstance(node, schema.Issue):
out_issue(node)
else:
out_pr(node)
out_header('todo', 'To Do')
for issue in dashboard.todo:
out_issue(issue)
out()
out_header('working', 'Working')
for issue in dashboard.working:
out_issue(issue)
out()
out_header('reviewing', 'Reviewing')
for pr in dashboard.reviewing:
out_pr(pr)
out()
out_header('done', 'Done')
for node in dashboard.done:
out_node(node)
DEFAULT_GRAPHQL_ENDPOINT = 'https://api.github.com/graphql'
if __name__ == '__main__':
import argparse
import configparser
import os.path
cfg_path = os.path.expanduser('~/.github-agile-dashboard.cfg')
cp = configparser.ConfigParser()
cp['github-agile-dashboard'] = {
'token': '',
'graphql-endpoint': DEFAULT_GRAPHQL_ENDPOINT,
}
cp.read(cfg_path)
cfg = cp['github-agile-dashboard']
ap = argparse.ArgumentParser(description='GitHub Agile Dashboard')
def tuple_arg(s):
return s.split('=', 1)
# Generic options to access the GraphQL API
ap.add_argument(
'--graphql-endpoint',
help=(
'GitHub GraphQL endpoint. '
'May be stored in "' + cfg_path + '", section: '
'[github-agile-dashboard], key: '
'graphql-endpoint=URL. '
'Default=%(default)s'
),
default=DEFAULT_GRAPHQL_ENDPOINT,
)
ap.add_argument(
'--token',
'-T',
help=(
'API token to use. '
'May be stored in "' + cfg_path + '", section: '
'[github-agile-dashboard], key: '
'token=STRING'
),
)
ap.add_argument(
'--verbose', '-v', help='Increase verbosity', action='count', default=0
)
ap.add_argument(
'--label',
'-l',
action='append',
help='Filter issues and PR by label.',
default=[],
)
ap.add_argument(
'--issue-state',
action='append',
help=(
'Filter Issues by state. '
'Providing a single empty string disables Issues'
),
choices=schema.IssueState.__choices__ + ('',),
default=[],
)
ap.add_argument(
'--pr-state',
action='append',
help=(
'Filter Pull Requests by state. '
'Providing a single empty string disables '
'Pull Requests'
),
choices=schema.PullRequestState.__choices__ + ('',),
default=[],
)
ap.add_argument('repo', help='Repository name, such as "team/repo".')
subparsers = ap.add_subparsers(dest='command')
# Save GraphQL to JSON
sp = subparsers.add_parser('save', help='Download and save JSON to disk')
sp.set_defaults(func=cmd_save)
sp.add_argument(
'--pretty', '-p', action='store_true', help='Pretty print JSON'
)
sp.add_argument(
'file',
type=argparse.FileType('w'),
nargs='?',
help='Where to save the JSON payload',
default=sys.stdout,
)
# Dashboard for a Milestone
sp = subparsers.add_parser(
'dashboard', help='View Agile Dashboard as text output'
)
sp.set_defaults(func=cmd_dashboard_text)
sp.add_argument(
'--load',
type=argparse.FileType('r'),
help='Where to load saved JSON, otherwise downloads.',
)
sp.add_argument(
'--project',
'-p',
action='append',
help=(
'Filter issues and PR by project title or number. '
'If a string and starts and ends with "/", '
'will be handled as regular expression, '
'ie: "/^prefix-/".'
),
default=[],
)
sp.add_argument(
'--milestone',
'-m',
action='append',
help=(
'Filter issues and PR by milestone title or number. '
'If a string and starts and ends with "/", '
'will be handled as regular expression, '
'ie: "/^prefix-/".'
),
default=[],
)
sp.add_argument(
'--assignee',
'-a',
action='append',
help=(
'Filter issues and PR by assignees. '
'Use empty assignee to state "unassigned"'
),
default=[],
)
sp.add_argument(
'--colors',
'-C',
choices=('auto', 'yes', 'no'),
help=('Select color mode (ansi colors for terminals)'),
default='auto',
)
sp.add_argument(
'--format',
'-f',
choices=('oneline', 'medium', 'full'),
help=('How to format issues and pull requests'),
default='oneline',
)
args = ap.parse_args()
endpoint_loglevel = max(10, 40 - ((args.verbose - 3) * 10))