Skip to content

Commit

Permalink
fix(run.py): add check for branch protection before toggling admin en…
Browse files Browse the repository at this point in the history
…forcement to prevent unnecessary operations on unprotected branches

refactor(run.py): handle NotFoundError when getting branch protection to improve error handling
refactor(test_run.py): change lambda function to regular function for better readability
fix(test_run.py): add newline at end of file to adhere to PEP 8 style guide

Closes #38
  • Loading branch information
benjefferies committed Nov 30, 2023
1 parent 1e02f9a commit 2db31de
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
48 changes: 27 additions & 21 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,6 @@
from github3.exceptions import NotFoundError, GitHubException


def strtobool(val):
"""
Convert a string representation of truth to True or False.
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
else:
raise ValueError(f"Invalid truth value {val}")


def toggle_enforce_admin(options):
access_token, owner, repo_name, branch_name, retries, github_repository = options.access_token, options.owner, options.repo, options.branch, int(options.retries), options.github_repository
if not owner and not repo_name and github_repository and "/" in github_repository:
Expand All @@ -36,6 +19,10 @@ def toggle_enforce_admin(options):
# or using an access token
print(f"Getting branch protection settings for {owner}/{repo_name}")
protection = get_protection(access_token, branch_name, owner, repo_name)
if protection is None:
print("Branch is not protected. Skipping")
return

print(f"Enforce admins branch protection enabled? {protection.enforce_admins.enabled}")
# save the current status for use later on if desired
print(f"\"name=initial_status::{protection.enforce_admins.enabled}\" >> $GITHUB_OUTPUT")
Expand Down Expand Up @@ -65,17 +52,36 @@ def toggle_enforce_admin(options):
def get_protection(access_token, branch_name, owner, repo_name):
gh = login(token=access_token)
if gh is None:
print(f"Could not login. Have you provided credentials?")
raise exit(1)
raise RuntimeError("Could not login. Have you provided credentials?")

try:
repo = gh.repository(owner, repo_name)
except NotFoundError:
print(f"Could not find repo https://github.com/{owner}/{repo_name}")
raise
branch = repo.branch(branch_name)
protection = branch.protection()
return protection
try:
protection = branch.protection()
return protection
except NotFoundError:
print(f"Could not find branch {branch_name} in repo")


def strtobool(val):
"""
Convert a string representation of truth to True or False.
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
else:
raise ValueError(f"Invalid truth value {val}")


def enable(protection):
Expand Down
5 changes: 3 additions & 2 deletions test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def test_should_error_when_no_github_repository_or_owner_and_repo(self):
})

# When
to_error = lambda: toggle_enforce_admin(options)
def to_error():
toggle_enforce_admin(options)

# Then
self.assertRaises(RuntimeError, to_error)
Expand Down Expand Up @@ -180,4 +181,4 @@ def __setattr__(self, key, val):
if key in self.__dict__:
self.__dict__[key] = val
else:
self[key] = val
self[key] = val

0 comments on commit 2db31de

Please sign in to comment.