From d332515dee3bea08a1f47a7bcb5d4586a86035d6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dylan=20A=C3=AFssi?= <dylan.aissi@collabora.com>
Date: Wed, 10 Jul 2024 16:26:30 +0200
Subject: [PATCH] trigger-updates: make it compatible with new reports format
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Late 2021, dashboard's reports gained their own format. See
https://gitlab.apertis.org/infrastructure/dashboard/-/commit/c0c77cb6ea0dce1b289a4df4d111e0c13dc0d7a3
Since then, trigger-updates was not compatible anymore.

Signed-off-by: Dylan Aïssi <dylan.aissi@collabora.com>
---
 bin/trigger-updates | 63 ++++++++++++++++++++++++---------------------
 1 file changed, 34 insertions(+), 29 deletions(-)

diff --git a/bin/trigger-updates b/bin/trigger-updates
index f2c4398..5f5bd67 100755
--- a/bin/trigger-updates
+++ b/bin/trigger-updates
@@ -23,39 +23,44 @@ def connect(gitlab_instance, gitlab_server_url, gitlab_api_token):
 
 def trigger_updates(gl, data, filterglob):
     all_packages = data["packages"]
-    all_count = sum(
-        len(package.get("updates", [])) for package in all_packages.values()
-    )
-    filtered_packages = [
-        p
-        for package_name, p in all_packages.items()
-        if fnmatch.fnmatch(package_name, filterglob)
-    ]
-    filtered_count = sum(
-        len(package.get("updates", [])) for package in filtered_packages
-    )
+    all_packages_reports = {
+        package: values
+        for package, values in all_packages.items()
+        if "reports" in values
+    }
+    all_packages_updates = {}
+    all_count = 0
+    filtered_count = 0
+    for package, values in all_packages_reports.items():
+        for report in values["reports"]:
+            if report["domain"] == "update" and report["kind"] == "available":
+                all_count += 1
+                all_packages_updates.update({package: values})
+                if fnmatch.fnmatch(package, filterglob):
+                    filtered_count += 1
     logging.info(
         f"Processing {filtered_count} updates matching the '{filterglob}' filter, {all_count} total"
     )
-    for package_name, package in all_packages.items():
+    for package_name, package in all_packages_updates.items():
         should_trigger = fnmatch.fnmatch(package_name, filterglob)
-        for update in package.get("updates", []):
-            path_with_namespace = package["git"]["path_with_namespace"]
-            ref = update.get("base", update["branch"])["name"]
-            print(
-                f"{path_with_namespace}:",
-                "Trigger" if should_trigger else "Skip",
-                ref,
-                update["branch"]["version"],
-                "→",
-                update["upstream"]["version"],
-            )
-            p = gl.projects.get(path_with_namespace, lazy=True)
-            update["pipeline"] = {"ref": ref}
-            if should_trigger:
-                pipeline = p.pipelines.create({"ref": ref})
-                print("  ", pipeline.web_url)
-                update["pipeline"].update(id=pipeline.id, web_url=pipeline.web_url)
+        for report in package["reports"]:
+            if report["domain"] == "update" and report["kind"] == "available":
+                path_with_namespace = package["git"]["path_with_namespace"]
+                ref = report.get("base")["name"]
+                print(
+                    f"{path_with_namespace}:",
+                    "Trigger" if should_trigger else "Skip",
+                    ref,
+                    report["base"]["version"],
+                    "→",
+                    report["upstream"]["version"],
+                )
+                p = gl.projects.get(path_with_namespace, lazy=True)
+                report["pipeline"] = {"ref": ref}
+                if should_trigger:
+                    pipeline = p.pipelines.create({"ref": ref})
+                    print("  ", pipeline.web_url)
+                    report["pipeline"].update(id=pipeline.id, web_url=pipeline.web_url)
 
 
 if __name__ == "__main__":
-- 
GitLab