Subject: [bug#76809] [PATCH Cuirass 3/3] http: Update Forgejo jobset when PR is updated.

From: Romain GARBAGE <[email-masked]>
Date: Fri, 7 Mar 2025 12:00:25 +0100

* src/cuirass/http.scm (url-handler): Update Forgejo jobset when PR is updated.
* tests/http.scm (forgejo-pull-request-json-update): New variable.
("/admin/forgejo/event updates a spec"): New test.
---
 src/cuirass/http.scm | 14 ++++++++------
 tests/http.scm       | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/src/cuirass/http.scm b/src/cuirass/http.scm
index cfb72e4..a826ef1 100644
--- a/src/cuirass/http.scm
+++ b/src/cuirass/http.scm
@@ -840,12 +840,14 @@ return DEFAULT."
                   ;; Pull request is updated.
                   ('synchronized
                    (if (db-get-specification spec-name)
-                       (if (call-bridge `(trigger-jobset ,(specification-name spec))
-                                        bridge)
-                           (respond-json (scm->json-string `((jobset . ,spec-name))))
-                           (begin
-                             (log-warning "evaluation hook disabled")
-                             (respond-json-with-error 400 "Evaluation hook disabled.")))
+                       (begin
+                         (db-add-or-update-specification spec)
+                         (if (call-bridge `(trigger-jobset ,spec-name)
+                                          bridge)
+                             (respond-json (scm->json-string `((jobset . ,spec-name))))
+                             (begin
+                               (log-warning "evaluation hook disabled for spec ~a" spec-name)
+                               (respond-json-with-error 400 "Evaluation hook disabled."))))
                        (respond-json-with-error 404 "Jobset not found."))))))
              (_ (respond-json-with-error 400 (format #f "Event type \"~a\" not supported." event-type))))
            (respond-json-with-error 400 "This API only supports JSON."))))
diff --git a/tests/http.scm b/tests/http.scm
index a57a4ab..ead4bf3 100644
--- a/tests/http.scm
+++ b/tests/http.scm
@@ -177,6 +177,36 @@
     }
   }")
 
+(define forgejo-pull-request-json-update
+  "{
+    \"action\": \"synchronized\",
+    \"pull_request\": {
+      \"number\": 1,
+      \"state\": \"open\",
+      \"url\": \"https://forgejo.instance.test/base-repo/pulls/1\",
+      \"base\": {
+        \"label\": \"base-label\",
+        \"ref\": \"base-branch\",
+        \"sha\": \"666af40e8a059fa05c7048a7ac4f2eccbbd0183b\",
+        \"repo\": {
+          \"name\": \"project-name\",
+          \"clone_url\": \"https://forgejo.instance.test/base-repo/project-name.git\",
+          \"html_url\": \"https://forgejo.instance.test/base-repo/project-name\"
+        }
+      },
+      \"head\": {
+        \"label\": \"test-label\",
+        \"ref\": \"test-branch\",
+        \"sha\": \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",
+        \"repo\": {
+          \"name\": \"fork-name\",
+          \"clone_url\": \"https://forgejo.instance.test/source-repo/fork-name.git\",
+          \"html_url\": \"https://forgejo.instance.test/source-repo/fork-name\"
+        }
+      }
+    }
+  }")
+
 (define forgejo-pull-request-json-close
   "{
     \"action\": \"closed\",
@@ -572,6 +602,19 @@
                                     forgejo-pull-request-json-open
                                     '((x-forgejo-event . "pull_request")))))
 
+   (test-assert "/admin/forgejo/event updates a spec"
+     (let* ((spec (forgejo-pull-request->specification
+               (forgejo-pull-request-event-pull-request
+                (json->forgejo-pull-request-event forgejo-pull-request-json-update))))
+            (spec-name (specification-name forgejo-pull-request-specification)))
+       (specifications=?
+        (pk 'expected spec)
+        (begin
+          (http-post-json (test-cuirass-uri "/admin/forgejo/event")
+                          forgejo-pull-request-json-update
+                          '((x-forgejo-event . "pull_request")))
+          (pk 'actual (db-get-specification spec-name))))))
+
    (test-assert "/admin/forgejo/event removes a spec from a closed pull request"
      (begin
        (http-post-json (test-cuirass-uri "/admin/forgejo/event")
-- 
2.48.1