Skip to content

Commit

Permalink
feat(new_bp_comparisons): New Branch Protection Comparisons previousl…
Browse files Browse the repository at this point in the history
…y omitted (#43)

* feat(branch_protections.py): Added Bypass Pull request 

Added the comparisons for users and teams with special privileges' as well as ability to set the ones who can bypass pull requests

* feat(__init__.py): Function objary_to_list

Can now create a list of an attribute from an attribute on the objects in another list or array
  • Loading branch information
shiro authored May 25, 2023
1 parent c4a164d commit ef6dad4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
71 changes: 64 additions & 7 deletions repo_manager/github/branch_protections.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from repo_manager.schemas.branch_protection import BranchProtection
from repo_manager.schemas.branch_protection import ProtectionOptions
from repo_manager.utils import attr_to_kwarg

from repo_manager.utils import objary_to_list

def diff_option(key: str, expected: Any, repo_value: Any) -> str | None:
if expected is not None:
Expand All @@ -32,6 +32,8 @@ def edit_protection( # nosec
required_approving_review_count=NotSet,
user_push_restrictions=NotSet,
team_push_restrictions=NotSet,
user_bypass_pull_request_allowances=NotSet,
team_bypass_pull_request_allowances=NotSet,
required_linear_history=NotSet,
allow_force_pushes=NotSet,
allow_deletions=NotSet,
Expand All @@ -49,6 +51,8 @@ def edit_protection( # nosec
:required_approving_review_count: int
:user_push_restrictions: list of strings
:team_push_restrictions: list of strings
:user_bypass_pull_request_allowances: list of strings
:team_bypass_pull_request_allowances: list of strings
NOTE: The GitHub API groups strict and contexts together, both must
be submitted. Take care to pass both as arguments even if only one is
changing. Use edit_required_status_checks() to avoid this.
Expand Down Expand Up @@ -89,6 +93,8 @@ def edit_protection( # nosec
or dismiss_stale_reviews is not NotSet
or require_code_owner_reviews is not NotSet
or required_approving_review_count is not NotSet
or user_bypass_pull_request_allowances is not NotSet
or team_bypass_pull_request_allowances is not NotSet
):
post_parameters["required_pull_request_reviews"] = {}
if dismiss_stale_reviews is not NotSet:
Expand All @@ -101,12 +107,26 @@ def edit_protection( # nosec
post_parameters["required_pull_request_reviews"][
"required_approving_review_count"
] = required_approving_review_count
if dismissal_users is not NotSet:
post_parameters["required_pull_request_reviews"]["dismissal_restrictions"] = {"users": dismissal_users}
if dismissal_teams is not NotSet:
if "dismissal_restrictions" not in post_parameters["required_pull_request_reviews"]:
post_parameters["required_pull_request_reviews"]["dismissal_restrictions"] = {}
post_parameters["required_pull_request_reviews"]["dismissal_restrictions"]["teams"] = dismissal_teams
if dismissal_users is not NotSet or dismissal_teams is not NotSet:
if dismissal_users is NotSet:
dismissal_teams = []
if dismissal_teams is NotSet:
dismissal_teams = []
post_parameters["required_pull_request_reviews"]["dismissal_restrictions"] \
= {
"users": dismissal_users,
"teams": dismissal_teams,
}
if user_bypass_pull_request_allowances is not NotSet or team_bypass_pull_request_allowances is not NotSet:
if user_bypass_pull_request_allowances is NotSet:
user_bypass_pull_request_allowances = []
if team_bypass_pull_request_allowances is NotSet:
team_bypass_pull_request_allowances = []
post_parameters["required_pull_request_reviews"]["bypass_pull_request_allowances"] \
= {
"users": user_bypass_pull_request_allowances,
"teams": team_bypass_pull_request_allowances,
}
else:
post_parameters["required_pull_request_reviews"] = None

Expand Down Expand Up @@ -177,6 +197,18 @@ def edit_protection( # nosec
kwargs,
transform_key="dismissal_teams",
)
attr_to_kwarg(
"users",
protection_config.pr_options.dismissal_restrictions,
kwargs,
transform_key="user_bypass_pull_request_allowances",
)
attr_to_kwarg(
"teams",
protection_config.pr_options.dismissal_restrictions,
kwargs,
transform_key="team_bypass_pull_request_allowances",
)

if repo.organization is not None:
attr_to_kwarg(
Expand Down Expand Up @@ -384,6 +416,31 @@ def check_repo_branch_protections(
)

# TODO: Figure out how to diff Restriction options
# I figured out some of them....
dismissal_teams = [] if (this_protection.required_pull_request_reviews is None) else \
objary_to_list("slug", this_protection.required_pull_request_reviews.dismissal_teams)
dismissal_teams.sort()
if (config_bp.protection.pr_options.dismissal_restrictions.teams is not None):
config_bp.protection.pr_options.dismissal_restrictions.teams.sort()
diffs.append(
diff_option(
"dismissal_teams",
config_bp.protection.pr_options.dismissal_restrictions.teams,
dismissal_teams,
)
)
dismissal_users = [] if (this_protection.required_pull_request_reviews is None) else \
objary_to_list("name", this_protection.required_pull_request_reviews.dismissal_users)
dismissal_users.sort()
if (config_bp.protection.pr_options.dismissal_restrictions.teams is not None):
config_bp.protection.pr_options.dismissal_restrictions.teams.sort()
diffs.append(
diff_option(
"dismissal_users",
config_bp.protection.pr_options.dismissal_restrictions.users,
dismissal_users,
)
)

diffs = [i for i in diffs if i is not None]
if len(diffs) > 0:
Expand Down
7 changes: 7 additions & 0 deletions repo_manager/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from actions_toolkit import core as actions_toolkit

# Needed to handle extracting certain attributes/fields from nested objects and lists
from itertools import repeat

from repo_manager.github import get_github_client

from ._inputs import INPUTS
Expand Down Expand Up @@ -104,3 +107,7 @@ def attr_to_kwarg(attr_name: str, obj: Any, kwargs: dict, transform_key: str = N
kwargs[attr_name] = value
else:
kwargs[transform_key] = value

# Allows use to extract a certain field on a list of objects into a list of strings etc.
def objary_to_list(attr_name: str, obj: Any):
return list(map(getattr, obj, repeat(attr_name)))

0 comments on commit ef6dad4

Please sign in to comment.