Skip to content
Snippets Groups Projects
Commit c61faeeb authored by Florent Gluck's avatar Florent Gluck
Browse files

Dirty hack which fixes the issue in which a user id cannot be retrieved based on an email.

parent 1f977907
No related branches found
No related tags found
No related merge requests found
Pipeline #19536 passed
......@@ -59,15 +59,30 @@ def emails_to_ids(emails: List[str], headers: Dict[str, str]) -> List[int]:
Get students ids from their emails
"""
user_ids = []
for email in emails:
user_requested = requests.get(
BASE_API_URL + '/users', params={'search': email}, headers=headers).json()
if len(user_requested) == 0:
print('No user %s found, operation aborted' % email)
exit(1)
user_ids.append(user_requested[0]['id'])
return user_ids
# Dirty and hackish way that attempts to extract the username from the email.
# It's inefficient, but currently there is now way to reliably obtain
# the username from the email.
username = email.split("@")[0]
#print("Email: ",email)
while len(username) > 1:
#print("Guessed username: ",username)
user_requested = requests.get(BASE_API_URL + '/users', params={'search': username}, headers=headers).json()
if len(user_requested) == 0:
#print('No user %s found, another try...' % email)
lastchar = username[-1]
username = username.rstrip(lastchar)
continue
#print(json.dumps(user_requested, indent=4))
user_ids.append(user_requested[0]['id'])
return user_ids
print('User %s not found, aborting.' % email)
exit(1)
def create_repository(token: str, group_id: str, emails: List[str], name: str, import_url: Optional[str], expires_at: Optional[str]):
"""
......@@ -229,11 +244,35 @@ def clone_all(token: str, id: str, directory: str, until_date: Optional[str], so
print()
def validate_yaml(args):
"""
Verify that the yaml file is valid by checking:
- the yaml syntax
- that a user id can be retrieved from the email (through some hackish inference)
This function stops the program if a check fails.
"""
with open(args.repos_file) as f:
repos = yaml.full_load(f)
for repo in repos:
if 'name' in repo:
name = repo['name']
elif 'emails' in repo:
name = repo['emails'][0].split('@')[0]
else:
print('Syntax error in YAML file.\nAborted.')
exit(1)
headers = {'PRIVATE-TOKEN': args.token}
# check the user id can sucessfully be retrieved from the email address
emails_to_ids(repo['emails'], headers)
def command_create_group_repos(args):
"""
Combine create_group and create_repository. For each repository listed in
given file, create a repo in group.
"""
validate_yaml(args)
if args.visibility:
group_id = create_group(args.token, args.group_name, args.visibility)
else:
......@@ -248,11 +287,11 @@ def command_create_group_repos(args):
elif 'emails' in repo:
name = repo['emails'][0].split('@')[0]
else:
print('YAML file not correct, exit and delete group')
print('Syntax error in YAML file.')
delete_group(args.token, group_id)
print('Deleted group.\nAborted.')
exit(1)
create_repository(
args.token, group_id, repo['emails'], name, args.import_url, args.expires_at)
create_repository(args.token, group_id, repo['emails'], name, args.import_url, args.expires_at)
print()
......@@ -261,6 +300,8 @@ def command_create_repos(args):
Create a set of repositories inside the specified (existing) group. For each repository listed
in the given file, create a repository.
"""
validate_yaml(args)
group_id = args.group_id
with open(args.repos_file) as f:
......@@ -274,8 +315,8 @@ def command_create_repos(args):
print('YAML file not correct, exit and delete group')
delete_group(args.token, group_id)
exit(1)
create_repository(
args.token, group_id, repo['emails'], name, args.import_url, args.expires_at)
create_repository(args.token, group_id, repo['emails'], name, args.import_url, args.expires_at)
print('created repo:',repo['emails'],name)
print()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment