Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider github-hostname when checking trusted_teams #1070

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions whd/pull_request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections.abc
import datetime
import functools
import logging
Expand Down Expand Up @@ -265,13 +266,40 @@ def _should_label(
github_helper: GitHubRepositoryHelper,
sender_login: str,
owner: str,
github_hostname: str,
) -> bool:
trusted_teams = job_mapping.trusted_teams()

def iter_trusted_teams_for_hostname(
trusted_teams: collections.abc.Iterable[str],
hostname: str,
) -> collections.abc.Generator[str, None, None]:
for trusted_team in trusted_teams:
parts = trusted_team.split('/')

if len(parts) == 2:
yield trusted_team
continue

elif len(parts) == 3:
host, org, team = parts
if not host == hostname:
continue

yield f'{org}/{team}'

else:
raise ValueError('team must either be <hostname>/<org>/<team> or <org>/<team>')

trusted_teams = list(iter_trusted_teams_for_hostname(
trusted_teams=job_mapping.trusted_teams(),
hostname=github_hostname,
))

if trusted_teams:
if (
any(
github_helper.is_team_member(team_name=t, user_login=sender_login)
for t in trusted_teams
github_helper.is_team_member(team_name=team, user_login=sender_login)
for team in trusted_teams
)
):
return True
Expand Down Expand Up @@ -312,7 +340,13 @@ def set_pr_labels(
)

if pr_event.action() is PullRequestAction.OPENED:
if _should_label(job_mapping, github_helper, sender_login, owner):
if _should_label(
job_mapping=job_mapping,
github_helper=github_helper,
sender_login=sender_login,
owner=owner,
github_hostname=pr_event.hostname(),
):
logger.info(
f"New pull request by trusted member '{sender_login}' in "
f"'{repository_path}' found. Setting required labels '{required_labels}'."
Expand All @@ -335,7 +369,13 @@ def set_pr_labels(
)
return False
elif pr_event.action() is PullRequestAction.SYNCHRONIZE:
if _should_label(job_mapping, github_helper, sender_login, owner):
if _should_label(
job_mapping=job_mapping,
github_helper=github_helper,
sender_login=sender_login,
owner=owner,
github_hostname=pr_event.hostname(),
):
logger.info(
f"Update to pull request #{pr_number} by trusted member '{sender_login}' "
f" in '{repository_path}' found. "
Expand Down