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 #104 from snyk-labs/develop
Browse files Browse the repository at this point in the history
release changes
  • Loading branch information
scott-es authored Jun 12, 2022
2 parents 642a071 + debf17e commit 77ccee0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.csv
*.log
__pycache__*
.venv
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
2 changes: 1 addition & 1 deletion app/utils/snyk_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def process_import_status_checks(import_status_checks):
def update_project_branch(project_id, project_name, org_id, new_branch_name):
""" update snyk project monitored branch """
org = common.snyk_client.organizations.get(org_id)
path = "org/{org.id}/project/{project_id}"
path = f"org/{org.id}/project/{project_id}"

payload = {
"branch": new_branch_name
Expand Down

0 comments on commit 77ccee0

Please sign in to comment.