-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetGHorgtree.py
More file actions
99 lines (82 loc) · 3.14 KB
/
getGHorgtree.py
File metadata and controls
99 lines (82 loc) · 3.14 KB
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
import requests
import os
import getopt
import sys
# Sometimes you need to know what types of files are in a github repo along
# with their layout in order to prepare for a risk-reasonable static analysis.
# This script is a model for extracting a list of files in tree format
# This version is for GH enterprise environments where there are organizations
# and repositories.
# INPUTS:
# **Have your github.com PAT in a user environment variable and fetch it from there - or
# use the practice appropriate for your situation.
# If you handle your PAT differently, adjust the code.
# -u <user> -- where this is your github.com username - the name associated with the PAT
# -o <organization> -- the target organization name (authorize your PAT for the target orgs)
# -r <repo> -- the target repository name
# -b <branch> -- the target branch in the targeted reposity
####
def main(argv):
if sys.version_info < (3, 7):
raise Exception("Use only with Python 3.7 or higher")
username = ''
orgname = ''
repo = ''
branchname = ''
if not (sys.argv[1:]):
print('Get list of repo tree file names from github.com.')
print('Inputs required: getGHorgtree.py -u <user> -o <organization> -r <repo> -b <branch>')
sys.exit(2)
try:
opts, args = getopt.getopt(argv, 'hu:o:r:b:', ['user=','repo=','org=','branch='])
except getopt.GetoptError as err:
print(err)
print('Get list of repo tree file names from github.com.')
print('getGHorgtree.py -u <user> -o <organization> -r <repo> -b <branch>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('Get list of repo tree file names from github.com.')
print('getGHorgtree.py -u <user> -o <organization> -r <repo> -b <branch>')
sys.exit()
elif opt in ("-u", "--user"):
username = arg
elif opt in ("-o", "--org"):
orgname = arg
elif opt in ("-r", "--repo"):
repo = arg
elif opt in ("-b", "--branch"):
branchname = arg
"""
username = '<UserNameHere>'
orgname = '<OrganizationNameHere>'
repo = '<RepositoryNameHere>'
branchname = '<branchNameHere>'
token = '<PersonalTokenFromEnvironmentorSecretStoreHere>'
# or get the token from the environment:
"""
token = os.environ['GITHUB_TOKEN']
url = "https://api.github.com/repos/{}/{}/git/trees/{}?recursive=1".format(orgname, repo, branchname)
reposeparator = "="*80
# Authenticate & create session
####
gh_session = requests.Session()
gh_session.auth = (username, token)
# Fetch repo data
####
r = gh_session.get(url)
response = r.json()
# Report
####
print(reposeparator)
numberoffiles = len(response["tree"])
print(f"Github user: {username}")
print(f"Github org: {orgname}")
print(f"Github repo: {repo}")
print(f"Github repo branch: {branchname}")
print(f"{str(numberoffiles)} files in {orgname}/{repo}/{branchname} include: ")
for file in response["tree"]:
print(file["path"])
print(reposeparator)
if __name__ == '__main__':
main(sys.argv[1:])