def main(args): print('\n\n' + 'Changing branch protection'.center(80, '=')) args = parser.parse_args(args) print('Args:\n' + ''.join(f'\t{k}: {v}\n' for k, v in vars(args).items())) github_utils.verify_token(args.token) repositories = github_utils.get_students_repositories( token=args.token, org_name=args.org_name, repo_filter=args.repo_filter ) num_fail = 0 for repo in repositories: try: branch = repo.get_branch(args.branch) if args.protect: branch.edit_protection( user_push_restrictions=[''] ) elif branch.protected: branch.remove_protection() else: pass print(f'{Fore.GREEN}Repo: {repo.full_name}') except Exception as e: print(f'{Fore.RED}Repo: {repo.full_name}') pprint.pprint(vars(repo)) print(f'{Fore.RED}{e}') num_fail += 1 print('\nSummary:') print(f'\tTotal number of repositories: {len(repositories)}') print(f'\tTotal number failed: {num_fail}') if num_fail > 0: raise Exception(f'{Fore.RED}Couldn\'t change branch protections')
def main(args): print('\n\n' + 'Triggering workflows'.center(80, '=')) args = parser.parse_args(args) print('Args:\n' + ''.join(f'\t{k}: {v}\n' for k, v in vars(args).items())) github_utils.verify_token(args.token) num_success = 0 num_fail = 0 repositories = github_utils.get_students_repositories( token=args.token, org_name=args.org_name, repo_filter=args.repo_filter ) print('Triggering workflows in repos:') for repo in repositories: success = repo.create_repository_dispatch(event_type=args.event_type) if success: num_success += 1 print(f'{Fore.GREEN}\t{repo.name}') else: num_fail += 1 print(f'{Fore.RED}\tFAILED {repo.name}') print('\nSummary:') print(f'\tNumber of successful repository_dispatch events: {num_success}') print(f'\tNumber of failed: {num_fail}') if num_fail != 0: raise Exception(f'{Fore.RED}Couldn\'t trigger all workflows')
def main(args): print('\n\n' + 'Changing access permissions'.center(80, '=')) args = parser.parse_args(args) print('Args:\n' + ''.join(f'\t{k}: {v}\n' for k, v in vars(args).items())) github_utils.verify_token(args.token) repositories = github_utils.get_students_repositories( token=args.token, org_name=args.org_name, repo_filter=args.repo_filter) apply_changes(repositories=repositories, new_permission=args.new_permission_level) confirm_changes(repositories=repositories, new_permission=args.new_permission_level)
def main(args): print('\n\n' + 'Deleting workflows'.center(80, '=')) args = parser.parse_args(args) print('Args:\n' + ''.join(f'\t{k}: {v}\n' for k, v in vars(args).items())) github_utils.verify_token(args.token) repositories = github_utils.get_students_repositories( token=args.token, org_name=args.org_name, repo_filter=args.repo_filter) for repo in repositories: print(f'Deleting workflows from repo: {repo.full_name}') deleted = github_utils.delete_all_workflows(repo, branch=args.branch) for path in deleted: print(f'\tRemoved: {path}') print('\nSummary:') print(f'\tTotal number of repositories updated: {len(repositories)}')
def update_with_github_api(files_to_update, template_repo_fullname, token, org_name, repo_filter, branch): github_utils.verify_token(token) template_repo = github_utils.get_repo(fullname=template_repo_fullname, token=token) template_files = get_relevant_template_files( files_to_update=files_to_update, template_repo=template_repo) repositories = github_utils.get_students_repositories( token=token, org_name=org_name, repo_filter=repo_filter) print('Syncing repositories:') for repo in repositories: print(f'{Fore.GREEN}\t{repo.full_name}') for file in template_files: github_utils.copy_file_to_repo(file=file, repo=repo, branch=branch) print('\nSummary:') print(f'\tTotal number of repositories updated: {len(repositories)}')
def main(args): print('\n\n' + 'Creating protected branches'.center(80, '=')) args = parser.parse_args(args) print('Args:\n' + ''.join(f'\t{k}: {v}\n' for k, v in vars(args).items())) github_utils.verify_token(args.token) repositories = github_utils.get_students_repositories( token=args.token, org_name=args.org_name, repo_filter=args.repo_filter) num_fail = 0 for repo in repositories: try: create_or_update_ref(repo=repo, branch_name=args.branch) add_push_restrictions(repo=repo, branch_name=args.branch) print(f'{Fore.GREEN}Repo: {repo.full_name}') except Exception as e: print(f'{Fore.RED}Repo: {repo.full_name}') pprint.pprint(vars(repo)) print(f'{Fore.RED}{e}') num_fail += 1 print('\nSummary:') print(f'\tTotal number of repositories: {len(repositories)}') print(f'\tTotal number failed: {num_fail}') if num_fail > 0: raise Exception(f'{Fore.RED}Couldn\'t create protected branches')
def main(args): print('\n\n' + 'Changing default protection'.center(80, '=')) args = parser.parse_args(args) print('Args:\n' + ''.join(f'\t{k}: {v}\n' for k, v in vars(args).items())) github_utils.verify_token(args.token) repositories = github_utils.get_students_repositories( token=args.token, org_name=args.org_name, repo_filter=args.repo_filter ) num_fail = 0 for repo in repositories: print(f'Repo: {repo.full_name}') try: set_default_branch(token=args.token, repo_full_name=repo.full_name, branch_name=args.branch) except Exception as e: print(e) num_fail += 1 print('\nSummary:') print(f'\tTotal number of repositories: {len(repositories)}') print(f'\tTotal number failed: {num_fail}') if num_fail > 0: raise Exception(f'{Fore.RED}Couldn\'t change default branches')
def main(args): print('\n\n' + 'Submitting files to Moss'.center(80, '=')) args = parser.parse_args(args) print('Args:\n' + ''.join(f'\t{k}: {v}\n' for k, v in vars(args).items())) github_utils.verify_token(args.token) moss = mosspy.Moss(args.user_id, language=args.l) moss.setIgnoreLimit(args.m) moss.setCommentString(args.c) moss.setNumberOfMatchingFiles(args.n) moss.setExperimentalServer(opt=int(args.x)) moss.setDirectoryMode(mode=int(args.d)) repositories = github_utils.get_students_repositories( token=args.token, org_name=args.org_name, repo_filter=args.repo_filter) if args.base_files_repo_fullname != '': repo = github_utils.get_repo(fullname=args.base_files_repo_fullname, token=args.token) add_base_files(moss=moss, base_files=args.paths, repo=repo) print(f'Org: {args.org_name}') add_paths(moss=moss, paths=args.paths, repositories=repositories) report_url = moss.send() print(f'Report url: {report_url}') save_report(moss=moss, report_name=args.report_name, report_url=report_url)
def main(args): print('\n\n' + 'Creating grading branches and pull requests'.center(80, '=')) args = parser.parse_args(args) print('Args:\n' + ''.join(f'\t{k}: {v}\n' for k, v in vars(args).items())) github_utils.verify_token(args.token) repositories = github_utils.get_students_repositories( token=args.token, org_name=args.org_name, repo_filter=args.repo_filter) num_fail = 0 for repo in repositories: try: create_or_update_ref(repo=repo, base=args.base) add_push_restrictions(repo=repo, base=args.base) print(f'{Fore.GREEN}Repo: {repo.full_name}') except Exception as e: print(f'{Fore.RED}Repo: {repo.full_name}') pprint.pprint(vars(repo)) print(f'{Fore.RED}{e}') num_fail += 1 try: repo.create_pull(title=args.pull_request_title, body=args.pull_request_body, base=args.base, head=args.head, maintainer_can_modify=False, draft=False) except Exception as e: print(f'\t{Fore.RED}Pull request already exists') for pull in repo.get_pulls(): print(f'\t{pull}') print(e) print('\nSummary:') print(f'\tTotal number of repositories: {len(repositories)}') print(f'\tTotal number failed: {num_fail}') if num_fail > 0: raise Exception(f'{Fore.RED}Couldn\'t create protected branches')
def delete_repos(token, org_name, repo_filter): repos = github_utils.get_students_repositories(token=token, org_name=org_name, repo_filter=repo_filter) for repo in repos: print(f'{Fore.GREEN}Deleting: {repo.full_name}') repo.delete()