Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #95 from marcpenya-tf/64/delete-and-re-import-proj…
Browse files Browse the repository at this point in the history
…ect-if-default-branch-name-has-been-updated

fix: update project branch if default branch was renamed
  • Loading branch information
scott-es authored Jun 12, 2022
2 parents 815a3ed + e270843 commit debf17e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 67 deletions.
46 changes: 10 additions & 36 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -97,41 +96,16 @@ 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,
"Branch was just renamed, leaving as-is")
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: #find deltas
app_print(snyk_repo.org_name,
snyk_repo.full_name,
Expand Down
31 changes: 0 additions & 31 deletions app/gh_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit debf17e

Please sign in to comment.