alitheg commented on code in PR #1376: URL: https://github.com/apache/tooling-trusted-releases/pull/1376#discussion_r3557753301
########## atr/shared/catalogue_diff.py: ########## @@ -0,0 +1,228 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import dataclasses + +import pydantic + +import atr.shared.catalogue_rows as catalogue_rows + + [email protected](frozen=True) +class DbSnapshot: + committee_keys: frozenset[str] + project_committee: dict[str, str] + release_project: dict[str, str] + artifact_by_dist: dict[tuple[str, str], tuple[str, str, str, str | None]] + managed_project_keys: frozenset[str] + # Every release key foundation-wide, unlike the workspace-scoped release_project + release_keys: frozenset[str] = dataclasses.field(default_factory=frozenset) + + [email protected](frozen=True) +class Conflict: + table: str + key: str + reason: str + + [email protected](frozen=True) +class ArtifactRehome: + dist: tuple[str, str] + from_pk: tuple[str, str, str] + to_row: catalogue_rows.ArtifactRow + + [email protected](frozen=True) +class ReleaseMove: + key: str + from_project: str + to_project: str + to_committee: str + cross_committee: bool + row: catalogue_rows.ReleaseRow + + [email protected] +class CatalogueDiff: + adds: list[catalogue_rows.Row] = dataclasses.field(default_factory=list) + artifact_rehomes: list[ArtifactRehome] = dataclasses.field(default_factory=list) + release_moves: list[ReleaseMove] = dataclasses.field(default_factory=list) + conflicts: list[Conflict] = dataclasses.field(default_factory=list) + unchanged: int = 0 + + @property + def counts(self) -> dict[str, int]: + return { + "add": len(self.adds), + "artifact_rehome": len(self.artifact_rehomes), + "release_move": len(self.release_moves), + "conflict": len(self.conflicts), + "unchanged": self.unchanged, + } + + +def classify_catalogue( + rows_by_table: dict[str, list[dict[str, str]]], snapshot: DbSnapshot, committee_key: str +) -> CatalogueDiff: + diff = CatalogueDiff() + projects = _parse(diff, "projects", "key", catalogue_rows.ProjectRow, rows_by_table.get("projects", [])) + releases = _parse(diff, "releases", "key", catalogue_rows.ReleaseRow, rows_by_table.get("releases", [])) + artifacts = _parse( + diff, "artifacts", "artifact_path", catalogue_rows.ArtifactRow, rows_by_table.get("artifacts", []) + ) + _classify_projects(projects, snapshot, diff) + _classify_releases(releases, snapshot, diff, committee_key) + _classify_artifacts(artifacts, snapshot, diff) + return diff + + +def _artifact_reference_conflict( + row: catalogue_rows.ArtifactRow, snapshot: DbSnapshot, known_projects: set[str], known_releases: set[str] +) -> Conflict | None: + path = str(row.artifact_path) + project_key = str(row.project_key) + if project_key in snapshot.managed_project_keys: + return Conflict(table="artifacts", key=path, reason="target project has live workflow data") + if project_key not in known_projects: + return Conflict(table="artifacts", key=path, reason=f"project '{project_key}' does not exist") + release_key = str(row.release_key) if row.release_key else "" + if release_key and (release_key not in known_releases): + return Conflict(table="artifacts", key=path, reason=f"release '{release_key}' does not exist") + return None + + +def _classify_artifacts(rows: list[catalogue_rows.ArtifactRow], snapshot: DbSnapshot, diff: CatalogueDiff) -> None: + # A project or release added earlier in this diff counts as present + known_projects = set(snapshot.project_committee) | { + str(add.key) for add in diff.adds if isinstance(add, catalogue_rows.ProjectRow) + } + known_releases = ( + set(snapshot.release_project) + | {str(add.key) for add in diff.adds if isinstance(add, catalogue_rows.ReleaseRow)} + | {f"{move.to_project}-{move.row.version}" for move in diff.release_moves} + ) + existing_pks = {(pk[0], pk[1], pk[2]) for pk in snapshot.artifact_by_dist.values()} + for row in rows: + reference_conflict = _artifact_reference_conflict(row, snapshot, known_projects, known_releases) + if reference_conflict is not None: + diff.conflicts.append(reference_conflict) + continue + path = str(row.artifact_path) + pk = (str(row.project_key), str(row.version), path) + suffix = str(row.download_path_suffix) if row.download_path_suffix else "" + matched = snapshot.artifact_by_dist.get((suffix, path)) if suffix else None Review Comment: The path _is_ the identity, so we should be using it to look up the existing one, and it's the only real conflict in seeding/correcting artifacts (you can't use the same full artifact path in more than one release). The add path now checks to make sure there's no conflict there, but I'm considering actually having the uploaded artifact CSV be an exhaustive list - remove all that don't match from the same PMC. This is for correcting the seeds, after all. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
