diff --git a/pwm b/pwm index d803f3f6722821f6cf7a51d53a9e9877355a18f2..a9f4556ab5d3d1a87d082d1dca32a3d6fb406020 100755 --- a/pwm +++ b/pwm @@ -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()