From e270843ad9531d8c86ba503982667d626f74a8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Pe=C3=B1a=20Segarra?= Date: Fri, 3 Jun 2022 23:25:10 +0200 Subject: [PATCH] fix: remove is_default_branch_renamed and always update Snyk's branch if it differs from GitHub one --- app/app.py | 44 ++++---------------------------------------- app/gh_repo.py | 31 ------------------------------- 2 files changed, 4 insertions(+), 71 deletions(-) diff --git a/app/app.py b/app/app.py index 825724b..4de83e1 100755 --- a/app/app.py +++ b/app/app.py @@ -9,7 +9,6 @@ from app.models import ImportStatus from app.gh_repo import ( get_gh_repo_status, - is_default_branch_renamed, is_gh_repo_truncated, get_git_tree_from_api ) @@ -97,47 +96,12 @@ def run(): snyk_repo.full_name, f"Default branch name changed from {snyk_repo.branch}" \ f" -> {gh_repo_status.repo_default_branch}") - app_print(snyk_repo.org_name, - snyk_repo.full_name, - "Checking if existing default branch was just renamed?") - try: - if snyk_repo.origin == "github": - is_default_renamed = is_default_branch_renamed( - snyk_repo, gh_repo_status.repo_default_branch, - common.GITHUB_TOKEN) - elif snyk_repo.origin == "github-enterprise": - is_default_renamed = is_default_branch_renamed( - snyk_repo, gh_repo_status.repo_default_branch, - common.GITHUB_ENTERPRISE_TOKEN, - True) - - except RuntimeError as err: - raise RuntimeError("Failed to query GitHub repository!") from err - - if not is_default_renamed: - app_print(snyk_repo.org_name, - snyk_repo.full_name, - "It's a different branch, update snyk projects...") - updated_projects = snyk_repo.update_branch( - gh_repo_status.repo_default_branch, - common.ARGS.dry_run) - for project in updated_projects: - if not common.ARGS.dry_run: - app_print(snyk_repo.org_name, - snyk_repo.full_name, - f"Monitored branch set to " \ - f"{gh_repo_status.repo_default_branch} " \ - f"for: {project['manifest']}") - else: - app_print(snyk_repo.org_name, - snyk_repo.full_name, - "Default branch was renamed, updating project branch") - updated_projects = snyk_repo.update_branch( + updated_projects = snyk_repo.update_branch( gh_repo_status.repo_default_branch, common.ARGS.dry_run) - for project in updated_projects: - if not common.ARGS.dry_run: - app_print(snyk_repo.org_name, + for project in updated_projects: + if not common.ARGS.dry_run: + app_print(snyk_repo.org_name, snyk_repo.full_name, f"Monitored branch set to " \ f"{gh_repo_status.repo_default_branch} " \ diff --git a/app/gh_repo.py b/app/gh_repo.py index 2117700..6ac9860 100755 --- a/app/gh_repo.py +++ b/app/gh_repo.py @@ -255,34 +255,3 @@ def get_gh_repo_status(snyk_gh_repo): repo_default_branch ) return repo_status - -def is_default_branch_renamed(snyk_gh_repo, new_branch, github_token, github_enterprise=False): - """detect if default branch has been renamed""" - is_renamed = False - headers = {"Authorization": "Bearer %s"} - headers["Authorization"] = headers["Authorization"] % (github_token) - if not github_enterprise: - request_url = f"https://api.github.com/repos/{snyk_gh_repo.full_name}" \ - f"/branches/{snyk_gh_repo.branch}" - #print("requestURL: " + request_url) - else: - request_url = f"https://{common.GITHUB_ENTERPRISE_HOST}" \ - f"/api/v3/repos/{snyk_gh_repo.full_name}/branches/{snyk_gh_repo.branch}" - try: - response = requests.get(url=request_url, allow_redirects=False, headers=headers) - - if response.status_code in (301, 302): - print('redirect response url: ' + response.headers["Location"]) - if str(response.headers["Location"]).endswith(f"/{new_branch}"): - # print('the redirect is pointing to the new branch') - is_renamed = True - # else: - # print('the redirect is pointing to a different branch') - else: - is_renamed = False - except requests.exceptions.RequestException as err: - print(f"exception trying to determine renamed status: {err.response}") - #log this to file - is_renamed = True - - return is_renamed