From a64841eb92a1cece95001ec5ed87e3a0b59d9375 Mon Sep 17 00:00:00 2001
From: Arun Isaac
Date: Sat, 9 Apr 2022 00:30:45 +0530
Subject: forge: Do not try to trigger CI jobs when there are none.

* guix/forge/forge.scm (ci-jobs-trigger-gexp): Import (ice-9
match). Do not try to trigger CI jobs when there are none to be
triggered.
(forge-service-type): Do not set up cron jobs and webhooks when there
are no CI jobs to be triggered.
---
 guix/forge/forge.scm | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/guix/forge/forge.scm b/guix/forge/forge.scm
index f99576f..f9ef272 100644
--- a/guix/forge/forge.scm
+++ b/guix/forge/forge.scm
@@ -89,18 +89,33 @@
 <forge-laminar-job> objects."
   (with-imported-modules '((guix build utils))
     #~(begin
-        (use-modules (guix build utils))
+        (use-modules (guix build utils)
+                     (ice-9 match))
         ;; TODO: Only trigger on updates to the main/master branch.
-        (display "Triggering continuous integration jobs..." (current-error-port))
-        (newline (current-error-port))
-        (when #$reason
-          (setenv "LAMINAR_REASON" #$reason))
-        (apply invoke
-               #$(file-append laminar "/bin/laminarc")
-               "queue" '#$(filter-map (lambda (job)
-                                        (and (forge-laminar-job-trigger? job)
-                                             (forge-laminar-job-name job)))
-                                      ci-jobs)))))
+
+        ;; Trigger jobs if there are jobs that need to be
+        ;; triggered.
+        ;;
+        ;; Even if there are none, we still need to manage the
+        ;; post-receive-hook to ensure that it does not go
+        ;; stale. Suppose that in one generation the user configures
+        ;; CI jobs, but removes them in the next generation. If we did
+        ;; not write to the post-receive-hook in the second
+        ;; generation, it would still retain its previous contents and
+        ;; trigger the jobs from the first generation.
+        (match '#$(filter-map (lambda (job)
+                                (and (forge-laminar-job-trigger? job)
+                                     (forge-laminar-job-name job)))
+                              ci-jobs)
+          (() #t)
+          (job-names
+           (display "Triggering continuous integration jobs..." (current-error-port))
+           (newline (current-error-port))
+           (when #$reason
+             (setenv "LAMINAR_REASON" #$reason))
+           (apply invoke
+                  #$(file-append laminar "/bin/laminarc")
+                  "queue" job-names))))))
 
 (define (forge-activation config)
   (let ((projects
@@ -271,6 +286,8 @@ clone and does not include the .git directory."
                                           (filter-map (lambda (project)
                                                         (and (eq? (forge-project-ci-jobs-trigger project)
                                                                   'cron)
+                                                             (any forge-laminar-job-trigger?
+                                                                  (forge-project-ci-jobs project))
                                                              #~(job '(next-day)
                                                                     #$(program-file
                                                                        (string-append (forge-project-name project)
@@ -285,6 +302,8 @@ clone and does not include the .git directory."
                                           (filter-map (lambda (project)
                                                         (and (eq? (forge-project-ci-jobs-trigger project)
                                                                   'webhook)
+                                                             (any forge-laminar-job-trigger?
+                                                                  (forge-project-ci-jobs project))
                                                              (webhook-hook
                                                               (id (forge-project-name project))
                                                               (run (ci-jobs-trigger-gexp
-- 
cgit v1.2.3