Skip to content

Commit

Permalink
change: exclude teams defined in the default config from the exclude_…
Browse files Browse the repository at this point in the history
…team filter
  • Loading branch information
netomi committed Jan 9, 2025
1 parent 05d5e84 commit 45129c0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Changed

- Changes the `exclude_team` filter to not consider teams defined in the default config.
- Converted workflow related settings into an embedded model object.
- Included option `repo-filter` of diff related operations already when getting live data from GitHub to speed up execution.

Expand Down
1 change: 1 addition & 0 deletions otterdog/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ValidationContext:
secret_resolver: SecretResolver
template_dir: str
org_members: set[str]
default_team_names: set[str]
exclude_teams_pattern: Pattern | None
validation_failures: list[tuple[FailureType, str]] = dataclasses.field(default_factory=list)

Expand Down
25 changes: 21 additions & 4 deletions otterdog/models/github_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ def set_repositories(self, repos: list[Repository]) -> None:
async def validate(
self,
config: OtterdogConfig,
jsonnet_config: JsonnetConfig,
secret_resolver: SecretResolver,
template_dir: str,
provider: GitHubProvider,
) -> ValidationContext:
# only retrieve the list of current organization members if there are teams defined
Expand All @@ -158,11 +158,16 @@ async def validate(
else:
org_members = set()

default_org = GitHubOrganization.from_model_data(
jsonnet_config.default_org_config_for_org_id(self.project_name, self.github_id)
)

context = ValidationContext(
self,
secret_resolver,
template_dir,
jsonnet_config.template_dir,
org_members,
{t.name for t in default_org.teams},
config.exclude_teams_pattern,
)
self.settings.validate(context, self)
Expand Down Expand Up @@ -540,11 +545,23 @@ async def _load_roles() -> None:
@debug_times("teams")
async def _load_teams() -> None:
if jsonnet_config.default_team_config is not None:
default_org = GitHubOrganization.from_model_data(
jsonnet_config.default_org_config_for_org_id(project_name, github_id)
)

github_teams = await provider.get_org_teams(github_id)
for team in github_teams:
if exclude_teams is not None and exclude_teams.match(team["slug"]):
team_name = team["name"]
team_slug = team["slug"]

default_org.get_team(team_name)
if (
exclude_teams is not None
and exclude_teams.match(team_slug)
and default_org.get_team(team_name) is None
):
continue
team_members = await provider.get_org_team_members(github_id, team["slug"])
team_members = await provider.get_org_team_members(github_id, team_slug)
team["members"] = team_members
org.add_team(Team.from_provider_data(github_id, team))
else:
Expand Down
6 changes: 5 additions & 1 deletion otterdog/models/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def validate(self, context: ValidationContext, parent_object: Any) -> None:
# execute custom validation rules if present
self.execute_custom_validation_if_present(context, "validate-team.py")

if context.exclude_teams_pattern is not None and context.exclude_teams_pattern.match(self.name):
if (
context.exclude_teams_pattern is not None
and context.exclude_teams_pattern.match(self.name)
and self.name not in context.default_team_names
):
context.add_failure(
FailureType.ERROR,
f"{self.get_model_header(parent_object)} has 'name' of value '{self.name}', "
Expand Down
2 changes: 1 addition & 1 deletion otterdog/operations/diff_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async def _generate_diff_internal(self, org_config: OrganizationConfig) -> int:
validation_infos,
validation_warnings,
validation_errors,
) = await self._validator.validate(expected_org, jsonnet_config.template_dir, self.gh_client)
) = await self._validator.validate(expected_org, jsonnet_config, self.gh_client)
if validation_errors > 0:
self.printer.println("Planning aborted due to validation errors.")
return validation_errors
Expand Down
9 changes: 5 additions & 4 deletions otterdog/operations/validate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# *******************************************************************************
# Copyright (c) 2023-2024 Eclipse Foundation and others.
# Copyright (c) 2023-2025 Eclipse Foundation and others.
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-v20.html
Expand All @@ -19,6 +19,7 @@

if TYPE_CHECKING:
from otterdog.config import OrganizationConfig
from otterdog.jsonnet import JsonnetConfig


class ValidateOperation(Operation):
Expand Down Expand Up @@ -64,7 +65,7 @@ async def execute(

async with GitHubProvider(credentials) as provider:
validation_infos, validation_warnings, validation_errors = await self.validate(
organization, jsonnet_config.template_dir, provider
organization, jsonnet_config, provider
)
validation_count = validation_infos + validation_warnings + validation_errors

Expand Down Expand Up @@ -98,13 +99,13 @@ async def execute(
async def validate(
self,
organization: GitHubOrganization,
template_dir: str,
jsonnet_config: JsonnetConfig,
provider: GitHubProvider,
) -> tuple[int, int, int]:
if organization.secrets_resolved is True:
raise RuntimeError("validation requires an unresolved model.")

context = await organization.validate(self.config, self.credential_resolver, template_dir, provider)
context = await organization.validate(self.config, jsonnet_config, self.credential_resolver, provider)

validation_infos = 0
validation_warnings = 0
Expand Down

0 comments on commit 45129c0

Please sign in to comment.