Skip to content

Commit eab32b3

Browse files
committed
ADDED: continuation on developing the model and first tests to github api
1 parent 97203c0 commit eab32b3

File tree

1 file changed

+73
-35
lines changed

1 file changed

+73
-35
lines changed

src/main.py

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from github import Github
88
from github import Auth
99

10+
# Generate same date string for all entities
11+
datetime_string = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") # ISO 8601-like format
12+
1013

1114
def is_file_directory_writable(file_path):
1215
if os.access(os.path.dirname(file_path), os.W_OK):
@@ -33,9 +36,14 @@ def parse_arguments():
3336
nargs="+", # Allow one or more values
3437
help="Adds custom GitLab providers",
3538
dest="custom_providers")
39+
parser.add_argument("-f", "--force",
40+
action="store_true",
41+
help="Does not ask confirmation for destructive actions.",
42+
default=False,
43+
dest="is_forced")
3644
parser.add_argument("-r", "--remove", "--remove-folder",
3745
action="store_true",
38-
help="Removes the backup folder after the backup.",
46+
help="Removes the backup content after the backup.",
3947
default=False,
4048
dest="remove_backup_folder_afterwards")
4149
parser.add_argument("-s", "--scratch", "--from-scratch",
@@ -84,16 +92,11 @@ def parse_arguments():
8492
metavar="PATH",
8593
help="Custom path where the JSON report will be stored. Implies -j.",
8694
dest="json_path")
87-
parser.add_argument("-b", "--backup", "--backup-path",
95+
parser.add_argument("-b", "--backup", "--backup-directory", "--backup-folder",
8896
type=str,
8997
metavar="PATH",
9098
help="Custom folder where all the clones will be done. Requires a value.",
91-
dest="backup_path")
92-
parser.add_argument("-f", "--force",
93-
action="store_true",
94-
help="Does not ask confirmation for destructive actions.",
95-
default=False,
96-
dest="is_forced")
99+
dest="backup_folder")
97100
parser.add_argument("-v", "--verbose",
98101
action="store_true",
99102
help="Enable verbose output during backup.",
@@ -112,26 +115,23 @@ def parse_arguments():
112115
parser.add_argument("usernames",
113116
nargs="+",
114117
metavar="USERNAME1 USERNAME2 USERNAME3 ...",
115-
help="List of usernames to back up from GitHub and GitLab.")
118+
help="List of usernames to back up.")
116119

117120
args = parser.parse_args()
118121

119-
# Generate same date string for all entities
120-
datetime_string = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") # ISO 8601-like format
121-
122122
# Supply default backup directory
123-
if not args.backup_path:
124-
args.backup_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "backup" + datetime_string)
123+
if not args.backup_folder:
124+
args.backup_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "backup")
125125

126126
# Check existence and access of backup directory
127-
if not os.path.exists(args.backup_path):
127+
if not os.path.exists(args.backup_folder):
128128
try:
129-
os.makedirs(args.backup_path)
129+
os.makedirs(args.backup_folder)
130130
except OSError as e:
131-
parser.error(f"The folder for the backup {args.backup_path} does not exist and cannot be created.")
131+
parser.error(f"The folder for the backup {args.backup_folder} does not exist and cannot be created.")
132132
else:
133-
if not os.access(args.backup_path, os.W_OK):
134-
parser.error(f"The folder for the backup {args.backup_path} cannot be written or there is some problem "
133+
if not os.access(args.backup_folder, os.W_OK):
134+
parser.error(f"The folder for the backup {args.backup_folder} cannot be written or there is some problem "
135135
f"with it: ")
136136

137137
# Flattening all directory levels makes hierarchy disappears, so it makes no sense to select both configurations
@@ -220,8 +220,8 @@ def print_summary(args):
220220
if args.exclude_github:
221221
print(" - GitLab (gitlab.com)")
222222

223-
if args.backup_path:
224-
print("* Backup folder: " + args.backup_path)
223+
if args.backup_folder:
224+
print("* Backup folder: " + args.backup_folder)
225225

226226
if args.produce_compressed:
227227
print("* Compressed backup path: " + args.compressed_path)
@@ -269,36 +269,74 @@ def print_summary(args):
269269

270270
def build_model(args):
271271

272-
model = {args.backup_path: {}}
272+
model = {datetime_string: {}}
273273

274-
275-
model[args.backup_path]
276-
'''
274+
# TODO: use two auxiliar functions to create the dictionaries DRY code
275+
for username in args.usernames:
276+
model[datetime_string][username] = {}
277+
if not args.exclude_enterprise:
278+
if args.exclude_github:
279+
model[datetime_string][username]["GitLab"] = {'url': 'https://gitlab.com/', 'type': 'GitLab',
280+
'orgs': {}}
281+
if args.exclude_gitlab:
282+
model[datetime_string][username]["GitHub"] = {'url': 'https://github.com/', 'type': 'GitHub',
283+
'orgs': {}}
284+
if not args.exclude_gitlab and not args.exclude_github:
285+
model[datetime_string][username]["GitLab"] = {'url': 'https://gitlab.com/', 'type': 'GitLab',
286+
'orgs': {}}
287+
model[datetime_string][username]["GitHub"] = {'url': 'https://github.com/', 'type': 'GitHub',
288+
'orgs': {}}
289+
if args.custom_providers:
290+
for custom_provider in args.custom_providers:
291+
if args.exclude_github:
292+
model[datetime_string][username][custom_provider] = {'url': custom_provider, 'type': 'GitLab',
293+
'orgs': {}}
294+
if args.exclude_gitlab:
295+
model[datetime_string][username][custom_provider] = {'url': custom_provider, 'type': 'GitHub',
296+
'orgs': {}}
297+
if not args.exclude_gitlab and not args.exclude_github:
298+
model[datetime_string][username][custom_provider] = {'url': custom_provider, 'type': 'GitLab',
299+
'orgs': {}}
300+
model[datetime_string][username][custom_provider] = {'url': custom_provider, 'type': 'GitHub',
301+
'orgs': {}}
302+
303+
304+
305+
def get_organizations(hostname):
277306
# using an access token
278-
auth = Auth.Token("access_token")
279-
280-
# First create a Github instance:
281-
282-
# Public Web Github
283-
g = Github(auth=auth)
307+
auth = Auth.Token("".join(open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "secrets", "GH_TOKEN.txt")).readlines()))
284308

285309
# Github Enterprise with custom hostname
286-
g = Github(base_url="https://{hostname}/api/v3", auth=auth)
310+
#g = Github(base_url="https://" + hostname + "/api/v3", auth=auth)
311+
g = Github(auth=auth)
312+
USER = "AleixMT"
313+
user = g.get_user(USER)
287314

288-
# Then play with your Github objects:
289-
for repo in g.get_user().get_repos():
315+
user_repos = user.get_repos()
316+
owned_repos = [repo for repo in user_repos if repo.owner.login == USER]
317+
for repo in owned_repos:
290318
print(repo.name)
319+
#for orgs in g.get_user().get_repos():
320+
#print(orgs)
291321

322+
'''
323+
for orgs in g.get_user().get_orgs():
324+
print()
325+
print(orgs.login)
326+
for repo in orgs.get_repos():
327+
print(repo.name)
328+
'''
292329
# To close connections after use
293330
g.close()
294-
'''
295331

296332
def main():
297333
args = parse_arguments()
298334
if args.is_verbose:
299335
print_summary(args)
300336
model = build_model(args)
301337

338+
get_organizations("github.com")
339+
302340

303341
if __name__ == "__main__":
304342
main()

0 commit comments

Comments
 (0)