alitheg commented on code in PR #1376:
URL: 
https://github.com/apache/tooling-trusted-releases/pull/1376#discussion_r3557819746


##########
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:

Review Comment:
   This is a check actually intended to prevent using these tools to manipulate 
ATR-managed releases (where we'll have on-disk files, revisions, etc).  But we 
should be checking the source as well.



-- 
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]

Reply via email to