-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitdata.py
1199 lines (1039 loc) · 50 KB
/
gitdata.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
"""GitData 2.0 - GitHub query CLI
Entry point:
cli() --------------------> Handle command-line arguments.
"""
import collections
import configparser
import json
import os
import sys
import time
from timeit import default_timer
import click
from dougerino import dicts2csv, dicts2json, setting, time_stamp, logcalls
from githuberino import github_allpages
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.group(context_settings=CONTEXT_SETTINGS, options_metavar='[options]',
invoke_without_command=True)
@click.option('-a', '--auth', default='',
help='GitHub username (for configuring access)', metavar='<str>')
@click.option('-t', '--token', default='',
help='store access token for specified username', metavar='<str>')
@click.option('-d', '--delete', default=False,
help='delete specified username', is_flag=True, metavar='')
@click.version_option(version='1.0', prog_name='Gitdata')
@click.pass_context
def cli(ctx, auth, token, delete): #-----------------------------------------<<<
"""\b
------------------------------------
Get information from GitHub REST API
------------------------------------
syntax help: gitdata <subcommand> -h"""
if auth:
auth_status(auth.lower(), token, delete)
return
# note that all subcommands are invoked by the Click framework decorators,
# so nothing to do here.
class _settings: #-----------------------------------------------------------<<<
"""This class exists to provide a namespace used for global settings.
Use auth_config() or log_config() to change these settings.
"""
# authentication settings used by auth_*() functions
username = '' # default = no GitHub authentication
accesstoken = '' # auth_config() may set this from '../_private' folder
datasource = 'p' # a=API, c=cache, p=prompt user to select
# current session object from requests library
requests_session = None
verbose = False # whether to display status information on console
display_data = True # whether to display retrieved data on console
# initialize gitdata session settings
start_time = time.time() # session start time (seconds)
tot_api_calls = 0 # number of API calls made through gitdata
tot_api_bytes = 0 # total bytes returned by these API calls
last_ratelimit = 0 # API rate limit for the most recent API call
last_remaining = 0 # remaining portion of rate limit after last API call
unknownfieldname = set() # list of unknown field names encountered
def auth_config(settings=None): #--------------------------------------------<<<
"""Configure authentication settings.
1st parameter = dictionary of configuration settings; see config_settings
below for settings managed by this function.
Returns dictionary of current settings - call auth_config() with no
parameters to get status.
"""
config_settings = ['username', 'accesstoken']
# if username is specified but no accesstoken specified, look up this
# user's PAT setting()
if settings and 'username' in settings and not 'accesstoken' in settings:
if not settings['username']:
settings['accesstoken'] = None
else:
settings['accesstoken'] = setting('github', settings['username'], 'pat')
if not settings['accesstoken']:
click.echo('Unknown authentication username: ' +
settings['username'])
if settings:
for settings_opt in config_settings:
if settings_opt in settings:
setattr(_settings, settings_opt, settings[settings_opt])
retval = dict()
for settings_opt in config_settings:
retval[settings_opt] = getattr(_settings, settings_opt)
return retval
def auth_status(auth, token, delete): #--------------------------------------<<<
"""Display status for a GitHub username.
auth = username
token = optional GitHub access token; if provided, the existing token in
the INI file is replaced with this value.
delete = flag for whether to delete the username from INI file
"""
if token or delete:
# both of these options write to the file, so initialize parser
configfile = inifile_name()
config = configparser.ConfigParser()
config.read(configfile)
if delete:
config.remove_section(auth)
else:
# save this token; may need to create a new section
if not auth in config.sections():
config[auth] = {}
config[auth]['PAT'] = token
with open(configfile, 'w') as fhandle:
config.write(fhandle)
# display username and access token
click.echo(' Username: ' + auth)
click.echo(' Token: ' + token_abbr(setting('github', auth, 'pat')))
def auth_user(): #-----------------------------------------------------------<<<
"""Credentials for basic authentication.
Returns the tuple used for API calls, based on current settings.
Returns None if no GitHub username/PAT is currently set.
<internal>
"""
if _settings.username:
return (_settings.username, _settings.accesstoken)
return None
def cache_exists(endpoint, auth=None): #-------------------------------------<<<
"""Check whether cached data exists for an endpoint.
endpoint = GitHub REST API endpoint
auth = GitHub authentication username
Returns True if local cached data exists, False if not.
"""
return os.path.isfile(cache_filename(endpoint, auth))
def cache_filename(endpoint, auth=None): #-----------------------------------<<<
"""Get cache filename for specified user/endpoint.
endpoint = the endpoint at https://api.github.com (starts with /)
auth = authentication username
Returns the filename for caching data returned from this API call.
"""
if not auth:
auth = _settings.username if _settings.username else '_anon'
source_folder = os.path.dirname(os.path.realpath(__file__))
filename = auth + '_' + endpoint.replace('/', '-').strip('-')
if '?' in filename:
# remove parameters from the endpoint
filename = filename[:filename.find('?')]
return os.path.join(source_folder, 'gh_cache/' + filename + '.json')
def cache_update(endpoint, payload, constants): #----------------------------<<<
"""Update cached data.
endpoint = the API endpoint (e.g., '/repos/org')
payload = the list of dictionaries returned from API endpoint
constants = dictionary of fieldnames/values to be included in the
cached data (e.g., criteria used in the API call)
Writes the cache file for this endpoint. Overwrites existing cached data.
"""
if constants:
# add the constants to the API payload
cached_data = []
for data_item in payload:
for fldname in constants:
data_item[fldname] = constants[fldname]
cached_data.append(data_item)
else:
cached_data = payload # no constants to be added
filename = cache_filename(endpoint)
dicts2json(source=payload, filename=filename) # write cached data
if _settings.verbose:
nameonly = os.path.basename(filename)
click.echo('Cache update: ', nl=False)
click.echo(click.style(nameonly, fg='cyan'))
@cli.command(help='Get collaborator information for a repo')
@click.option('-o', '--owner', default='',
help='owner (org or user)', metavar='<str>')
@click.option('-r', '--repo', default='',
help='repo name', metavar='<str>')
@click.option('--audit2fa', is_flag=True,
help='include only 2FA-not-enabled collaborators')
@click.option('-a', '--authuser', default='',
help='authentication username', metavar='<str>')
@click.option('-s', '--source', default='p',
help='data source - a/API, c/cache, or p/prompt', metavar='<str>')
@click.option('-n', '--filename', default='',
help='output filename (.CSV or .JSON)', metavar='<str>')
@click.option('-f', '--fields', default='',
help='fields to include', metavar='<str>')
@click.option('-d', '--display', is_flag=True, default=True,
help="Don't display retrieved data")
@click.option('-v', '--verbose', is_flag=True, default=False,
help="Display verbose status info")
@click.option('-l', '--listfields', is_flag=True,
help='list available fields and exit.')
def collabs(owner, repo, audit2fa, authuser, source, #-----------------------<<<
filename, fields, display, verbose, listfields):
"""Get collaborator information for a repo.
"""
if listfields:
list_fields('collab') # display online help
return
# validate inputs/options
if not owner or not repo:
click.echo('ERROR: must specify owner and repo')
return
if not filename_valid(filename):
return
start_time = default_timer()
# store settings in _settings
_settings.display_data = display
_settings.verbose = verbose
source = source if source else 'p'
_settings.datasource = source.lower()[0]
# retrieve requested data
auth_config({'username': authuser})
fldnames = fields.split('/') if fields else None
endpoint = '/repos/' + owner + '/' + repo + '/collaborators?per_page=100' + \
('&filter=2fa_disabled' if audit2fa else '')
templist = github_data(
endpoint=endpoint, entity='collab',
fields=fldnames, constants={"owner": owner, "repo": repo}, headers={})
# handle returned data
sorted_data = sorted(templist, key=data_sort)
data_display(sorted_data)
data_write(filename, sorted_data)
elapsed_time(start_time)
@cli.command(help='Get commits for a repo')
@click.option('-o', '--owner', default='',
help='owner (org or user)', metavar='<str>')
@click.option('-r', '--repo', default='',
help='repo name', metavar='<str>')
@click.option('-a', '--authuser', default='',
help='authentication username', metavar='<str>')
@click.option('-s', '--source', default='p',
help='data source - a/API, c/cache, or p/prompt', metavar='<str>')
@click.option('-n', '--filename', default='',
help='output filename (.CSV or .JSON)', metavar='<str>')
@click.option('-f', '--fields', default='',
help='fields to include', metavar='<str>')
@click.option('-d', '--display', is_flag=True, default=True,
help="Don't display retrieved data")
@click.option('-v', '--verbose', is_flag=True, default=False,
help="Display verbose status info")
@click.option('-l', '--listfields', is_flag=True,
help='list available fields and exit.')
def commits(owner, repo, authuser, source, filename, fields, #---------------<<<
display, verbose, listfields):
"""Get commits for a repo.
"""
if listfields:
list_fields('commit') # display online help
return
# validate inputs/options
if not owner or not repo:
click.echo('ERROR: must specify owner and repo')
return
if not filename_valid(filename):
return
start_time = default_timer()
# store settings in _settings
_settings.display_data = display
_settings.verbose = verbose
source = source if source else 'p'
_settings.datasource = source.lower()[0]
# retrieve requested data
auth_config({'username': authuser})
fldnames = fields.split('/') if fields else None
endpoint = '/repos/' + owner + '/' + repo + '/commits?per_page=100'
templist = github_data(
endpoint=endpoint, entity='commit',
fields=fldnames, constants={"owner": owner, "repo": repo}, headers={})
# handle returned data
sorted_data = sorted(templist, key=data_sort)
data_display(sorted_data)
data_write(filename, sorted_data)
elapsed_time(start_time)
def data_fields(*, entity=None, jsondata=None, #-----------------------------<<<
fields=None, constants=None):
"""Get dictionary of desired values from GitHub API JSON payload.
entity = entity type ('repo', 'member')
jsondata = a JSON payload returned by the GitHub API
fields = list of names of fields (entries) to include from the JSON data,
or one of these shorthand values:
'*' -------> return all fields returned by GitHub API
'nourls' --> return all non-URL fields (not *_url or url)
'urls' ----> return all URL fields (*_url and url)
constants = dictionary of fieldnames/values that can be included in the
specified fields but aren't returned by GitHub API (these are
typically criteria used in the API call)
Returns a dictionary of fieldnames/values.
"""
if not fields:
fields = default_fields(entity)
values = collections.OrderedDict()
if fields[0] in ['*', 'urls', 'nourls']:
# special cases to return all fields or all url/non-url fields
if constants and fields[0] in ['*', 'nourls']:
values.update(constants)
for fldname in jsondata:
if fields[0] == '*' or \
(fields[0] == 'urls' and fldname.endswith('url')) or \
(fields[0] == 'nourls' and not fldname.endswith('url')):
this_item = jsondata[fldname]
if str(this_item.__class__) == "<class 'dict'>" and \
fields[0] == 'nourls':
# this is an embedded dictionary, so for the 'nourls' case
# remove *url fields ...
values[fldname] = {key:value for
(key, value) in this_item.items()
if not key.endswith('url')}
else:
values[fldname] = this_item
else:
# fields == an actual list of fieldnames, not a special case
for fldname in fields:
if constants and fldname in constants:
values[fldname] = constants[fldname]
else:
values[fldname.replace('.', '_')] = \
nested_json_value(jsondata, fldname)
if fldname.lower() == 'private':
values[fldname] = \
'private' if jsondata[fldname] else 'public'
return values
def data_display(datasource=None): #-----------------------------------------<<<
"""Display data on console.
datasource = list of dictionaries
If _settings.display_data, displays the data in console output
"""
if not _settings.display_data:
return
for data_item in datasource:
values = [str(value) for _, value in data_item.items()]
click.echo(click.style(','.join(values), fg='cyan'))
# List unknown field names encountered in this session (if any)
try:
if _settings.unknownfieldname:
click.echo('Unknown field name(s): ' + \
','.join(_settings.unknownfieldname))
except AttributeError:
# no unknown fields have been logged
pass
def data_sort(datadict): #---------------------------------------------------<<<
"""Sort function for output lists.
takes an OrderedDict object as input, returns lower-case version of the
first value in the OrderedDict, for use as a sort key.
"""
sortkey = list(datadict.keys())[0]
sortvalue = str(datadict[sortkey]).lower()
return sortvalue
def data_write(filename=None, datasource=None): #----------------------------<<<
"""Write output file.
filename = output filename
datasource = list of dictionaries
"""
if not filename:
return
_, file_ext = os.path.splitext(filename)
if file_ext.lower() == '.json':
dicts2json(source=datasource, filename=filename) # write JSON file
else:
dicts2csv(datasource, filename) # write CSV file
click.echo('Output file written: ' + filename)
def default_fields(entity=None): #-------------------------------------------<<<
"""Get default field names for an entity.
entity = the entity/data type (e.g., "team" or "repo")
Returns a list of the default field names for this entity.
"""
if entity == 'member':
return ['login', 'id', 'type']
elif entity == 'repo':
return ['name', 'owner.login']
elif entity == 'team':
return ['name', 'id', 'privacy', 'permission']
elif entity == 'org':
return ['login', 'user']
elif entity == 'collab':
return ['login', 'owner', 'repo', 'id']
elif entity == 'commit':
return ['commit.committer.date', 'committer.login', 'commit.message']
return ['name'] # if unknown entity type, use name
def elapsed_time(starttime): #-----------------------------------------------<<<
"""Display elapsed time.
starttime = time to measure from, as returned by default_timer()
If _settings.verbose, displays elapsed time in seconds.
"""
if _settings.verbose:
click.echo('Elapsed time: ', nl=False)
elapsed = default_timer() - starttime
click.echo(click.style("{0:.2f}".format(elapsed) + ' seconds', fg='cyan'))
def filename_valid(filename=None): #-----------------------------------------<<<
"""Check filename for valid file type.
filename = output filename passed on command line
Returns True if valid, False if not.
"""
if not filename:
return True # filename is optional
_, file_ext = os.path.splitext(filename)
if file_ext.lower() not in ['.csv', '.json']:
click.echo('ERROR: output file must be .CSV or .JSON')
return False
return True
def github_data(*, endpoint=None, entity=None, fields=None, #----------------<<<
constants=None, headers=None):
"""Get data for specified GitHub API endpoint.
endpoint = HTTP endpoint for GitHub API call
entity = entity type ('repo', 'member')
fields = list of fields to be returned
constants = dictionary of fieldnames/values that can be included in the
specified fields but aren't returned by GitHub API (these are
typically criteria used in the API call)
headers = HTTP headers to be included with API call
Returns a list of dictionaries containing the specified fields.
Returns a complete data set - if this endpoint does pagination, all pages
are retrieved and aggregated.
"""
# _settings.datasource contains one of these three values:
# 'a' = call the GitHub REST API to get the data
# 'c' = get data from the locally cached data for this endpoint/username
# 'p' (or None) = prompt the user for which data to use
if _settings.datasource == 'c' and not cache_exists(endpoint):
click.echo('ERROR: cached data requested, but none found.')
return []
if _settings.datasource == 'a':
read_from = 'a'
elif _settings.datasource == 'c':
read_from = 'c'
else:
# prompt user for which data source to use
click.echo(' Endpoint: ', nl=False)
click.echo(click.style(endpoint, fg='cyan'))
if cache_exists(endpoint):
filetime = time_stamp(cache_filename(endpoint))
click.echo(' Cached data: ', nl=False)
click.echo(click.style(filetime, fg='cyan'))
read_from = \
click.prompt('Read from API (a), cache (c) or exit (x)?').lower()
else:
click.echo('Cached data not available.')
read_from = click.prompt('Read from API (a) or exit (x)?').lower()
if read_from == 'x':
sys.exit(0)
if read_from == 'a':
all_fields = github_allpages(endpoint=endpoint, auth=auth_user(),
headers=headers, state=_settings)
cache_update(endpoint, all_fields, constants)
elif read_from == 'c' and cache_exists(endpoint):
all_fields = github_data_from_cache(endpoint=endpoint)
if _settings.verbose:
nameonly = os.path.basename(cache_filename(endpoint))
click.echo(' Data source: ', nl=False)
click.echo(click.style(nameonly, fg='cyan'))
else:
all_fields = []
# extract the requested fields and return them
retval = []
for json_item in all_fields:
retval.append(data_fields(entity=entity, jsondata=json_item,
fields=fields, constants=constants))
return retval
def github_data_from_cache(endpoint=None): #---------------------------------<<<
"""Get data from local cache file.
endpoint = GitHub API endpoint
"""
filename = cache_filename(endpoint)
return read_json(filename)
def inifile_name(): #--------------------------------------------------------<<<
"""Return full name of INI file where GitHub tokens are stored.
Note that this file is stored in a 'private' subfolder under the parent
folder of the gitdata module.
"""
source_folder = os.path.dirname(os.path.realpath(__file__))
return os.path.join(source_folder, '../_private/github.ini')
def list_fields(entity=None): #----------------------------------------------<<<
"""Display available field names for an entity.
entity = the entity type (e.g., 'org' or 'team')
Displays to the console a list of available field names for this entity.
"""
click.echo('\nDefault fields for ' + entity.upper() + 'S: ', nl=False)
click.echo(click.style('/'.join(default_fields(entity)), fg='cyan'))
click.echo(click.style(60*'-', fg='blue'))
wildcard_fields()
if entity == 'collab':
click.echo(click.style('avatar_url'.ljust(27) + 'organizations_url', fg='cyan'))
click.echo(click.style('events_url'.ljust(27) + 'received_events_url', fg='cyan'))
click.echo(click.style('followers_url'.ljust(27) + 'repos_url', fg='cyan'))
click.echo(click.style('following_url'.ljust(27) + 'site_admin', fg='cyan'))
click.echo(click.style('gists_url'.ljust(27) + 'starred_url', fg='cyan'))
click.echo(click.style('gravatar_id'.ljust(27) + 'subscriptions_url', fg='cyan'))
click.echo(click.style('html_url'.ljust(27) + 'type', fg='cyan'))
click.echo(click.style('id'.ljust(27) + 'url', fg='cyan'))
click.echo(click.style('login', fg='cyan'))
elif entity == 'commit':
click.echo(click.style('comments_url'.ljust(27) +
'commit.message', fg='cyan'))
click.echo(click.style('html_url'.ljust(27) +
'commit.tree.sha', fg='cyan'))
click.echo(click.style('sha'.ljust(27) +
'commit.tree.url', fg='cyan'))
click.echo(click.style('url'.ljust(27) +
'commit.url', fg='cyan'))
click.echo(click.style('author.avatar_url'.ljust(27) +
'commit.url', fg='cyan'))
click.echo(click.style('author.events_url'.ljust(27) +
'commit.verification.payload', fg='cyan'))
click.echo(click.style('author.followers_url'.ljust(27) +
'commit.verification.reason', fg='cyan'))
click.echo(click.style('author.following_url'.ljust(27) +
'commit.verification.signature', fg='cyan'))
click.echo(click.style('author.gists_url'.ljust(27) +
'commit.verification.verified', fg='cyan'))
click.echo(click.style('author.gravatar_id'.ljust(27) +
'committer.avatar_url', fg='cyan'))
click.echo(click.style('author.html_url'.ljust(27) +
'committer.events_url', fg='cyan'))
click.echo(click.style('author.id'.ljust(27) +
'committer.followers_url', fg='cyan'))
click.echo(click.style('author.login'.ljust(27) +
'committer.following_url', fg='cyan'))
click.echo(click.style('author.organizations_url'.ljust(27) +
'committer.gists_url', fg='cyan'))
click.echo(click.style('author.received_events_url'.ljust(27) +
'committer.gravatar_id', fg='cyan'))
click.echo(click.style('author.repos_url'.ljust(27) +
'committer.html_url', fg='cyan'))
click.echo(click.style('author.site_admin'.ljust(27) +
'committer.id', fg='cyan'))
click.echo(click.style('author.starred_url'.ljust(27) +
'committer.login', fg='cyan'))
click.echo(click.style('author.subscriptions_url'.ljust(27) +
'committer.organizations_url', fg='cyan'))
click.echo(click.style('author.type'.ljust(27) +
'committer.received_events_url', fg='cyan'))
click.echo(click.style('author.url'.ljust(27) +
'committer.repos_url', fg='cyan'))
click.echo(click.style('commit.author.date'.ljust(27) +
'committer.site_admin', fg='cyan'))
click.echo(click.style('commit.author.email'.ljust(27) +
'committer.starred_url', fg='cyan'))
click.echo(click.style('commit.author.name'.ljust(27) +
'committer.subscriptions_url', fg='cyan'))
click.echo(click.style('commit.comment_count'.ljust(27) +
'committer.type', fg='cyan'))
click.echo(click.style('commit.committer.date'.ljust(27) +
'committer.url', fg='cyan'))
click.echo(click.style('commit.committer.email'.ljust(27) +
'parents.sha', fg='cyan'))
click.echo(click.style('commit.committer.name'.ljust(27) +
'parents.url', fg='cyan'))
elif entity == 'member':
click.echo(click.style('id avatar_url ' +
'html_url', fg='cyan'))
click.echo(click.style('login events_url ' +
'organizations_url', fg='cyan'))
click.echo(click.style('org followers_url ' +
'received_events_url', fg='cyan'))
click.echo(click.style('site_admin following_url ' +
'repos_url', fg='cyan'))
click.echo(click.style('type gists_url ' +
'starred_url', fg='cyan'))
click.echo(click.style('url gravatar_id ' +
'subscriptions_url', fg='cyan'))
elif entity == 'org':
click.echo(click.style('avatar_url', fg='cyan'))
click.echo(click.style('description', fg='cyan'))
click.echo(click.style('events_url', fg='cyan'))
click.echo(click.style('hooks_url', fg='cyan'))
click.echo(click.style('id', fg='cyan'))
click.echo(click.style('issues_url', fg='cyan'))
click.echo(click.style('login', fg='cyan'))
click.echo(click.style('members_url', fg='cyan'))
click.echo(click.style('public_members_url', fg='cyan'))
click.echo(click.style('repos_url', fg='cyan'))
click.echo(click.style('url', fg='cyan'))
click.echo(click.style('user', fg='cyan'))
elif entity == 'repo':
click.echo(click.style('archive_url git_tags_url ' +
'open_issues', fg='cyan'))
click.echo(click.style('assignees_url git_url ' +
'open_issues_count', fg='cyan'))
click.echo(click.style('blobs_url has_downloads ' +
'private', fg='cyan'))
click.echo(click.style('branches_url has_issues ' +
'pulls_url', fg='cyan'))
click.echo(click.style('clone_url has_pages ' +
'pushed_at', fg='cyan'))
click.echo(click.style('collaborators_url has_wiki ' +
'releases_url', fg='cyan'))
click.echo(click.style('commits_url homepage ' +
'size', fg='cyan'))
click.echo(click.style('compare_url hooks_url ' +
'ssh_url', fg='cyan'))
click.echo(click.style('contents_url html_url ' +
'stargazers_count', fg='cyan'))
click.echo(click.style('contributors_url id ' +
'stargazers_url', fg='cyan'))
click.echo(click.style('created_at issue_comment_url ' +
'statuses_url', fg='cyan'))
click.echo(click.style('default_branch issue_events_url ' +
'subscribers_url', fg='cyan'))
click.echo(click.style('deployments_url issues_url ' +
'subscription_url', fg='cyan'))
click.echo(click.style('description keys_url ' +
'svn_url', fg='cyan'))
click.echo(click.style('downloads_url labels_url ' +
'tags_url', fg='cyan'))
click.echo(click.style('events_url language ' +
'teams_url', fg='cyan'))
click.echo(click.style('fork languages_url ' +
'trees_url', fg='cyan'))
click.echo(click.style('forks master_branch ' +
'updated_at', fg='cyan'))
click.echo(click.style('forks_count merges_url ' +
'url', fg='cyan'))
click.echo(click.style('forks_url milestones_url ' +
'watchers', fg='cyan'))
click.echo(click.style('full_name mirror_url ' +
'watchers_count', fg='cyan'))
click.echo(click.style('git_commits_url name', fg='cyan'))
click.echo(click.style('git_refs_url notifications_url', fg='cyan'))
click.echo(click.style(60*'-', fg='blue'))
click.echo(click.style('license.featured ' +
'owner.login', fg='cyan'))
click.echo(click.style('license.key ' +
'owner.organizations_url', fg='cyan'))
click.echo(click.style('license.name ' +
'owner.received_events_url', fg='cyan'))
click.echo(click.style('license.url ' +
'owner.repos_url', fg='cyan'))
click.echo(click.style('owner.avatar_url ' +
'owner.site_admin', fg='cyan'))
click.echo(click.style('owner.events_url ' +
'owner.starred_url', fg='cyan'))
click.echo(click.style('owner.followers_url ' +
'owner.subscriptions_url', fg='cyan'))
click.echo(click.style('owner.following_url ' +
'owner.type', fg='cyan'))
click.echo(click.style('owner.gists_url ' +
'owner.url', fg='cyan'))
click.echo(click.style('owner.gravatar_id ' +
'permissions.admin', fg='cyan'))
click.echo(click.style('owner.html_url ' +
'permissions.pull', fg='cyan'))
click.echo(click.style('owner.id ' +
'permissions.push', fg='cyan'))
elif entity == 'team':
click.echo(click.style('description', fg='cyan'))
click.echo(click.style('id', fg='cyan'))
click.echo(click.style('members_url', fg='cyan'))
click.echo(click.style('name', fg='cyan'))
click.echo(click.style('org', fg='cyan'))
click.echo(click.style('permission', fg='cyan'))
click.echo(click.style('privacy', fg='cyan'))
click.echo(click.style('repositories_url', fg='cyan'))
click.echo(click.style('slug', fg='cyan'))
click.echo(click.style('url', fg='cyan'))
@cli.command(help='Get member information by org or team ID')
@click.option('-o', '--org', default='',
help='GitHub org (* = all orgs authuser is a member of)', metavar='<str>')
@click.option('-t', '--team', default='',
help='team ID', metavar='<str>')
@click.option('--audit2fa', is_flag=True,
help='include only 2FA-not-enabled members')
@click.option('--adminonly', is_flag=True,
help='include only members with role=admin')
@click.option('-a', '--authuser', default='',
help='authentication username', metavar='<str>')
@click.option('-s', '--source', default='p',
help='data source - a/API, c/cache, or p/prompt', metavar='<str>')
@click.option('-n', '--filename', default='',
help='output filename (.CSV or .JSON)', metavar='<str>')
@click.option('-f', '--fields', default='',
help='fields to include', metavar='<str>')
@click.option('-d', '--display', is_flag=True, default=True,
help="Don't display retrieved data")
@click.option('-v', '--verbose', is_flag=True, default=False,
help="Display verbose status info")
@click.option('-l', '--listfields', is_flag=True,
help='list available fields and exit.')
def members(org, team, audit2fa, adminonly, authuser, #----------------------<<<
source, filename, fields, display, verbose, listfields):
"""Get member info for an organization or team.
"""
if listfields:
list_fields('member')
return
# validate inputs/options
if not org and not team:
click.echo('ERROR: must specify an org or team ID')
return
if not filename_valid(filename=filename):
return
start_time = default_timer()
# store settings in _settings
_settings.display_data = display
_settings.verbose = verbose
source = source if source else 'p'
_settings.datasource = source.lower()[0]
# retrieve requested data
auth_config({'username': authuser})
fldnames = fields.split('/') if fields else None
templist = membersdata(org=org, team=team, audit2fa=audit2fa,
authname=authuser, adminonly=adminonly, fields=fldnames)
# handle returned data
sorted_data = sorted(templist, key=data_sort)
data_display(sorted_data)
data_write(filename, sorted_data)
elapsed_time(start_time)
def membersdata(*, org=None, team=None, fields=None, authname=None, #--------<<<
audit2fa=False, adminonly=False):
"""Get members for one or more teams or organizations.
org = organization name
team = team ID; if provided, org is ignored
fields = list of field names to be returned; names must be the same as
returned by the GitHub API (see list_fields()).
authname = GitHub authentication username; required for org=* syntax
You must be authenticated via auth_config() as an admin of the org(s) to
use the audit2fa or adminonly options ...
audit2fa = whether to only return members with 2FA disabled.
adminonly = whether to only return members with role=admin.
Returns a list of dictionary objects, one per member.
"""
memberlist = [] # the list of members that will be returned
if team:
# get members by team
memberlist.extend(membersget(team=team, fields=fields))
else:
# get members by organization
if org == '*':
# handle special org=* syntax: all orgs for this user
if not authname:
click.echo('ERROR: -a option required for org=* syntax.')
return []
user_orgs = orglist(authname)
for orgid in user_orgs:
memberlist.extend( \
membersget(org=orgid, fields=fields,
audit2fa=audit2fa, adminonly=adminonly))
else:
# get members for a single specified organization
memberlist.extend( \
membersget(org=org, fields=fields,
audit2fa=audit2fa, adminonly=adminonly))
return memberlist
def membersget(*, org=None, team=None, fields=None, #------------------------<<<
audit2fa=False, adminonly=False):
"""Get member info for a specified organization. Called by members() to
aggregate member info for multiple organizations.
org = organization ID (ignored if a team is specified)
team = team ID
fields = list of fields to be returned
You must be authenticated via auth_config() as an admin of the org(s) to
use the audit2fa or adminonly options ...
audit2fa = whether to only return members with 2FA disabled.
adminonly = whether to only return members with role=admin.
Returns a list of dictionaries containing the specified fields.
<internal>
"""
if team:
endpoint = '/teams/' + team + '/members?per_page=100'
else:
endpoint = '/orgs/' + org + '/members?per_page=100' + \
('&filter=2fa_disabled' if audit2fa else '') + \
('&role=admin' if adminonly else '')
return github_data(endpoint=endpoint, entity='member', fields=fields,
constants={"org": org}, headers={})
def nested_json_value(nested_dict, dot_fldname): #---------------------------<<<
"""Return a nested value from a JSON data structure.
nested_dict = a JSON object, which contains nested dictionaries (nested up
to 4 levels deep)
dot_fldname = a dot-notation reference to a value nested inside the JSON
for example, 'commit.committer.date' would return the value
nested_dict['commit']['committer']['date']
"""
depth = dot_fldname.count('.') + 1
keys = dot_fldname.split('.')
if depth == 1:
try:
retval = nested_dict[dot_fldname]
except (TypeError, KeyError):
_settings.unknownfieldname.add(dot_fldname)
retval = None
elif depth == 2:
try:
retval = nested_dict[keys[0]][keys[1]]
except (TypeError, KeyError):
_settings.unknownfieldname.add(dot_fldname)
retval = None
elif depth == 3:
try:
retval = nested_dict[keys[0]][keys[1]][keys[2]]
except (TypeError, KeyError):
_settings.unknownfieldname.add(dot_fldname)
retval = None
elif depth == 4:
try:
retval = nested_dict[keys[0]][keys[1]][keys[2]][keys[3]]
except (TypeError, KeyError):
_settings.unknownfieldname.add(dot_fldname)
retval = None
else:
try:
retval = nested_dict[keys[0]][keys[1]][keys[2]][keys[3]][keys[4]]
except (TypeError, KeyError):
_settings.unknownfieldname.add(dot_fldname)
retval = None
return retval
def orglist(authname=None, contoso=False): #---------------------------------<<<
"""Get all orgs for a GitHub user.
authname = GitHub user name
contoso = whether to include orgs named contoso* (to deal with a Microsoft
specific problem)
Returns a list of all GitHub organizations that this user is a member of.
"""
auth_config({'username': authname})
templist = github_data(endpoint='/user/orgs', entity='org', fields=['login'],
constants={"user": authname}, headers={})
sortedlist = sorted([_['login'].lower() for _ in templist])
if contoso:
return sortedlist
else:
return [orgname for orgname in sortedlist if not orgname.startswith('contoso')]
@cli.command(help='Get org memberships for a user')
@click.option('-a', '--authuser', default='',
help='authentication username', metavar='<str>')
@click.option('-s', '--source', default='p',
help='data source - a/API, c/cache, or p/prompt', metavar='<str>')
@click.option('-n', '--filename', default='',
help='output filename (.CSV or .JSON)', metavar='<str>')
@click.option('-f', '--fields', default='',
help='fields to include', metavar='<str>')
@click.option('-d', '--display', is_flag=True, default=True,
help="Don't display retrieved data")
@click.option('-v', '--verbose', is_flag=True, default=False,
help="Display verbose status info")
@click.option('-l', '--listfields', is_flag=True,
help='list available fields and exit.')
def orgs(authuser, source, filename, fields, #-------------------------------<<<
display, verbose, listfields):
"""Get organization information.
"""
if listfields:
list_fields('org')
return
# validate inputs/options
if not authuser:
click.echo('ERROR: authentication username is required')
return
if not filename_valid(filename):
return
start_time = default_timer()
# store settings in _settings
_settings.display_data = display
_settings.verbose = verbose
source = source if source else 'p'
_settings.datasource = source.lower()[0]
# retrieve requested data
auth_config({'username': authuser})
fldnames = fields.split('/') if fields else None
templist = github_data(
endpoint='/user/orgs', entity='org', fields=fldnames,
constants={"user": authuser}, headers={})
# handle returned data
sorted_data = sorted(templist, key=data_sort)
data_display(sorted_data)
data_write(filename, sorted_data)
elapsed_time(start_time)
def read_json(filename=None): #----------------------------------------------<<<
"""Read .json file into a Python object.
filename = the filename
Returns the object that has been serialized to the .json file (list, etc).
<internal>
"""
with open(filename, 'r') as datafile:
retval = json.loads(datafile.read())
return retval
@cli.command(help='Get repo information by org or user/owner')
@click.option('-o', '--org', default='',
help='GitHub org (* = all orgs authuser is a member of)', metavar='<str>')
@click.option('-u', '--user', default='',
help='GitHub user', metavar='<str>')
@click.option('-a', '--authuser', default='',
help='authentication username', metavar='<str>')