From patchwork Tue Dec 10 16:09:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Romain GARBAGE X-Patchwork-Id: 34745 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 11FF827BBEA; Tue, 10 Dec 2024 16:10:14 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 828B327BBE2 for ; Tue, 10 Dec 2024 16:10:13 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tL2oH-0000vZ-Sa; Tue, 10 Dec 2024 11:10:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tL2oG-0000ta-4i for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:04 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tL2oF-0005Su-Qv for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:In-Reply-To:References:Subject; bh=SNm9hoixEYB9xCpt/5m/VDAQ1eQMm21+KbvaCLYwlQc=; b=vH6cWspfT9HcqIYUtWbB42rF5K29BKN3aZSDSQ7f5Ds13wiJTvetwFUKT2buwwSd/xEnpdLy221TOMtkHdz6f9YM2W8x0kzPtMqUTpQZ21mGdMSZQB/ccy+V8CIoIcvhqQdZxC2C8cS+JC5ZUoS+Zn+vxj70zwb03PWTfp/0KOq9wjKTv/1IAVJKf5nh0yD0unFGlQiCyBRVFdqfMxwhdcaUo+v2+gt+BDYjPxWnbYk9S6GP0h6IXn9CqC41QFIIPmy/DHMqNfWLWs4vAtg5AiC2RAZIrihXW9ph6J+aSp4d/u9EGpvNeSMNqZEiqNaYd2VWrrTq2nU3DQt/d9f3/g==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tL2oF-0001Wl-KM for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#74769] [PATCH Cuirass 1/4] tests: Move procedure definition. References: <20241210160824.31748-1-romain.garbage@inria.fr> In-Reply-To: <20241210160824.31748-1-romain.garbage@inria.fr> Resent-From: Romain GARBAGE Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 10 Dec 2024 16:10:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74769 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 74769@debbugs.gnu.org Cc: ludovic.courtes@inria.fr, Romain GARBAGE Received: via spool by 74769-submit@debbugs.gnu.org id=B74769.17338469905727 (code B ref 74769); Tue, 10 Dec 2024 16:10:03 +0000 Received: (at 74769) by debbugs.gnu.org; 10 Dec 2024 16:09:50 +0000 Received: from localhost ([127.0.0.1]:59206 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tL2o1-0001UE-Ct for submit@debbugs.gnu.org; Tue, 10 Dec 2024 11:09:50 -0500 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:32084) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tL2ny-0001TJ-Id for 74769@debbugs.gnu.org; Tue, 10 Dec 2024 11:09:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inria.fr; s=dc; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=SNm9hoixEYB9xCpt/5m/VDAQ1eQMm21+KbvaCLYwlQc=; b=WmwuChJ7vj0+gRs3H6YTApn8n1qbC6/oiTll8RRb33FSJNib+i0p2eiH AJBe0YbSf+lyzypV0uahjJMwCUOcZx3x2o0QlOxHL43fAPBYEyMJECfXT QlpdLsHT+Uyv2wNb9si//Z6mS0ed3CYu6UZRRauNJ6yM9qGPk5v+Al1HX U=; Authentication-Results: mail3-relais-sop.national.inria.fr; dkim=none (message not signed) header.i=none; spf=SoftFail smtp.mailfrom=romain.garbage@inria.fr; dmarc=fail (p=none dis=none) d=inria.fr X-IronPort-AV: E=Sophos;i="6.12,222,1728943200"; d="scan'208";a="103964548" Received: from unknown (HELO guix-A102.bordeaux.inria.fr) ([193.50.110.191]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Dec 2024 17:09:40 +0100 From: Romain GARBAGE Date: Tue, 10 Dec 2024 17:09:25 +0100 Message-ID: <20241210160929.14180-1-romain.garbage@inria.fr> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * tests/common.scm (specifications-equal?): New variable. * tests/gitlab.scm (specifications-equal?): Remove variable. --- tests/common.scm | 30 +++++++++++++++++++++++++++++- tests/gitlab.scm | 24 ------------------------ 2 files changed, 29 insertions(+), 25 deletions(-) base-commit: e96f0887923d4d1cd4e35073fcffdb978d7e1e10 diff --git a/tests/common.scm b/tests/common.scm index a807498..5054ea0 100644 --- a/tests/common.scm +++ b/tests/common.scm @@ -20,7 +20,9 @@ #:use-module ((cuirass base) #:select (%bridge-socket-file-name)) #:use-module (cuirass database) #:use-module (cuirass parameters) + #:use-module (cuirass specification) #:use-module (cuirass utils) + #:use-module (guix channels) #:use-module ((fibers scheduler) #:select (current-scheduler)) #:use-module (ice-9 popen) #:use-module (ice-9 rdelim) @@ -28,7 +30,8 @@ retry test-init-db! with-guix-daemon - wait-for-bridge)) + wait-for-bridge + specifications-equal?)) (define %db (make-parameter #f)) @@ -121,3 +124,28 @@ Return the socket on success and #f on failure." (sockaddr:path address) (strerror (system-error-errno args))) #f))))))) + +(define (specifications-equal? spec1 spec2) + "Return true if SPEC2 and SPEC2 are equivalent, false otherwise." + (and (eq? (specification-name spec1) + (specification-name spec2)) + (equal? (specification-build spec1) + (specification-build spec2)) + (= (specification-priority spec1) + (specification-priority spec2)) + (= (specification-period spec1) + (specification-period spec2)) + (equal? (specification-systems spec1) + (specification-systems spec2)) + (equal? (map channel-name + (specification-channels spec1)) + (map channel-name + (specification-channels spec2))) + (equal? (map channel-url + (specification-channels spec1)) + (map channel-url + (specification-channels spec2))) + (equal? (map channel-branch + (specification-channels spec1)) + (map channel-branch + (specification-channels spec2))))) diff --git a/tests/gitlab.scm b/tests/gitlab.scm index ca6cad5..adf94cc 100644 --- a/tests/gitlab.scm +++ b/tests/gitlab.scm @@ -157,30 +157,6 @@ } }") -(define (specifications-equal? spec1 spec2) - (and (eq? (specification-name spec1) - (specification-name spec2)) - (equal? (specification-build spec1) - (specification-build spec2)) - (= (specification-priority spec1) - (specification-priority spec2)) - (= (specification-period spec1) - (specification-period spec2)) - (equal? (specification-systems spec1) - (specification-systems spec2)) - (equal? (map channel-name - (specification-channels spec1)) - (map channel-name - (specification-channels spec2))) - (equal? (map channel-url - (specification-channels spec1)) - (map channel-url - (specification-channels spec2))) - (equal? (map channel-branch - (specification-channels spec1)) - (map channel-branch - (specification-channels spec2))))) - (test-assert "default-json" (specifications-equal? (let ((event (json->gitlab-event default-mr-json))) From patchwork Tue Dec 10 16:09:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Romain GARBAGE X-Patchwork-Id: 34746 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id B319427BBE9; Tue, 10 Dec 2024 16:10:28 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 2501827BBE2 for ; Tue, 10 Dec 2024 16:10:26 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tL2oI-0000wd-GU; Tue, 10 Dec 2024 11:10:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tL2oH-0000vP-IP for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:05 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tL2oH-0005Ue-2T for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=HCBMbFO+H0H3dcFuf2bOvFh5Gpu5S5Vi5RmSpRWW1go=; b=rnSTMyIr2/fVO7H8LUYTQu92nVO7sLJ4BihdJjxHk7Z/sIiF/iQ6/nIaf94/GKcVfXa2dh6bi1o3txcpfeACrkEsXtmqC0PQF31BMYLo9eOftF+EiWS4WXgSfw8pFNCk+rKAzK8DJ0tTSAA/YFNcWKRNkCgK/lHAhfS/kLxWw7PXieSLtcyAiHGCuAv5wz9aZlmL3U/CRmTtf/X4j+oW9HkT0Zwg20wts2PmnIyI5khiutj0HJbEx01b3LSWiKbypTxZSRb9ZNRcf8GFgPGvbs4rXyBbGzzBgs+AcWSj0c8l0n6hI/QTw5PeBwqO0WUYweSg+W1tHOkAjcorNtqQbQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tL2oG-0001XT-QB for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:04 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#74769] [PATCH Cuirass 2/4] forgejo: Add module for Forgejo JSON objects definition. Resent-From: Romain GARBAGE Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 10 Dec 2024 16:10:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74769 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 74769@debbugs.gnu.org Cc: ludovic.courtes@inria.fr, Romain GARBAGE Received: via spool by 74769-submit@debbugs.gnu.org id=B74769.17338469965784 (code B ref 74769); Tue, 10 Dec 2024 16:10:04 +0000 Received: (at 74769) by debbugs.gnu.org; 10 Dec 2024 16:09:56 +0000 Received: from localhost ([127.0.0.1]:59210 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tL2o5-0001Uo-2r for submit@debbugs.gnu.org; Tue, 10 Dec 2024 11:09:56 -0500 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:32084) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tL2nz-0001TJ-E1 for 74769@debbugs.gnu.org; Tue, 10 Dec 2024 11:09:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inria.fr; s=dc; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HCBMbFO+H0H3dcFuf2bOvFh5Gpu5S5Vi5RmSpRWW1go=; b=mmEZueDTMt99bLbIMML9Mh1k3WuHCQzFQqnL1lZVWAl3ubxxyKTl32CA nRFCXPL0+8nT219VgN8y215JrnJHBci0NxltEu4UGe1ALHaLcjAd8EJCV QcQ9gLmNi4IQeYakYe1HDPIHjrEsJGvdyNiOsQ1D9W+tMpnKr+C60Q134 U=; Authentication-Results: mail3-relais-sop.national.inria.fr; dkim=none (message not signed) header.i=none; spf=SoftFail smtp.mailfrom=romain.garbage@inria.fr; dmarc=fail (p=none dis=none) d=inria.fr X-IronPort-AV: E=Sophos;i="6.12,222,1728943200"; d="scan'208";a="103964549" Received: from unknown (HELO guix-A102.bordeaux.inria.fr) ([193.50.110.191]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Dec 2024 17:09:40 +0100 From: Romain GARBAGE Date: Tue, 10 Dec 2024 17:09:26 +0100 Message-ID: <20241210160929.14180-2-romain.garbage@inria.fr> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241210160929.14180-1-romain.garbage@inria.fr> References: <20241210160929.14180-1-romain.garbage@inria.fr> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * Makefile.am: Add src/cuirass/forgejo.scm and tests/forgejo.scm. * src/cuirass/forgejo.scm: Add , , , and record types. (forgejo-pull-request->specification): New variable. * tests/forgejo.scm: Add tests. fixup tests --- Makefile.am | 2 + src/cuirass/forgejo.scm | 133 ++++++++++++++++++++++++++++++++++++++++ tests/forgejo.scm | 79 ++++++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 src/cuirass/forgejo.scm create mode 100644 tests/forgejo.scm diff --git a/Makefile.am b/Makefile.am index 1123eb1..fca6b9f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -52,6 +52,7 @@ dist_pkgmodule_DATA = \ src/cuirass/store.scm \ src/cuirass/base.scm \ src/cuirass/database.scm \ + src/cuirass/forgejo.scm \ src/cuirass/gitlab.scm \ src/cuirass/http.scm \ src/cuirass/logging.scm \ @@ -167,6 +168,7 @@ TESTS = \ ## tests/basic.sh # takes too long to execute tests/store.scm \ tests/database.scm \ + tests/forgejo.scm \ tests/gitlab.scm \ tests/http.scm \ tests/metrics.scm \ diff --git a/src/cuirass/forgejo.scm b/src/cuirass/forgejo.scm new file mode 100644 index 0000000..9dda2c5 --- /dev/null +++ b/src/cuirass/forgejo.scm @@ -0,0 +1,133 @@ +;;; forgejo.scm -- Forgejo JSON mappings +;;; Copyright © 2024 Romain Garbage +;;; +;;; This file is part of Cuirass. +;;; +;;; Cuirass is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; Cuirass is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with Cuirass. If not, see . + +(define-module (cuirass forgejo) + #:use-module (cuirass specification) + #:use-module (json) + #:use-module (web http) + #:use-module (guix channels) + #:use-module (ice-9 match) + #:export (forgejo-pull-request-event-pull-request + forgejo-pull-request-event-action + json->forgejo-pull-request-event + + forgejo-repository-name + forgejo-repository-url + + json->forgejo-pull-request + + forgejo-pull-request->specification)) + +;;; Commentary: +;;; +;;; This module implements a subset of the Forgejo Webhook API described at +;;; . +;;; +;;; Code: + +;; This declares a specific header for internal consumption, specifically when +;; generating requests during tests. +(declare-opaque-header! "X-Forgejo-Event") + +(define-json-mapping + make-forgejo-repository + forgejo-repository? + json->forgejo-repository + (name forgejo-repository-name "name" + string->symbol) + (url forgejo-repository-url "clone_url")) + +;; This maps to the top level JSON object. +(define-json-mapping + make-forgejo-pull-request-event + forgejo-pull-request-event? + json->forgejo-pull-request-event + (action forgejo-pull-request-event-action "action" + string->symbol) + (pull-request forgejo-pull-request-event-pull-request "pull_request" + json->forgejo-pull-request)) + +(define-json-mapping + make-forgejo-pull-request + forgejo-pull-request? + json->forgejo-pull-request + (number forgejo-pull-request-number "number") + (base forgejo-pull-request-base "base" + json->forgejo-repository-reference) + (head forgejo-pull-request-head "head" + json->forgejo-repository-reference)) + +;; This mapping is used to define various JSON objects such as "base" or +;; "head". +(define-json-mapping + make-forgejo-repository-reference + forgejo-repository-reference? + json->forgejo-repository-reference + (label forgejo-repository-reference-label "label") + (ref forgejo-repository-reference-ref "ref") + (sha forgejo-repository-reference-sha "sha") + (repository forgejo-repository-reference-repository "repo" + json->forgejo-repository)) + +(define* (forgejo-pull-request->specification pull-request #:optional (cuirass-options #f)) + "Returns a SPECIFICATION built out of a FORGEJO-PULL-REQUEST." + (let* ((source-repo-reference (forgejo-pull-request-head pull-request)) + (project-name (forgejo-repository-name + (forgejo-repository-reference-repository + (forgejo-pull-request-base pull-request)))) + (source-branch (forgejo-repository-reference-ref source-repo-reference)) + (source-url (forgejo-repository-url + (forgejo-repository-reference-repository source-repo-reference))) + (id (forgejo-pull-request-number pull-request)) + (name-prefix (if (and cuirass-options + (jobset-options-name-prefix cuirass-options)) + (jobset-options-name-prefix cuirass-options) + 'forgejo-pull-requests)) + (spec-name (string->symbol + (format #f "~a-~a-~a-~a" name-prefix + project-name + source-branch + id))) + (build (if (and cuirass-options + (jobset-options-build cuirass-options)) + (jobset-options-build cuirass-options) + `(channels ,project-name))) + (period (if (and cuirass-options + (jobset-options-period cuirass-options)) + (jobset-options-period cuirass-options) + 3600)) + (priority (if (and cuirass-options + (jobset-options-priority cuirass-options)) + (jobset-options-priority cuirass-options) + 1)) + (systems (if (and cuirass-options + (jobset-options-systems cuirass-options)) + (jobset-options-systems cuirass-options) + (list "x86_64-linux")))) + (specification + (name spec-name) + (build build) + (channels + (cons* (channel + (name project-name) + (url source-url) + (branch source-branch)) + %default-channels)) + (priority priority) + (period period) + (systems systems)))) diff --git a/tests/forgejo.scm b/tests/forgejo.scm new file mode 100644 index 0000000..bb8f768 --- /dev/null +++ b/tests/forgejo.scm @@ -0,0 +1,79 @@ +;;; forgejo.scm -- tests for (cuirass forgejo) module +;;; Copyright © 2024 Romain GARBAGE +;;; +;;; This file is part of Cuirass. +;;; +;;; Cuirass is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; Cuirass is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with Cuirass. If not, see . + +(use-modules (cuirass forgejo) + (cuirass specification) + (cuirass utils) + (tests common) + (guix channels) + (json) + (fibers) + (squee) + (web uri) + (web client) + (web response) + (rnrs bytevectors) + (srfi srfi-1) + (srfi srfi-64) + (ice-9 threads) + (ice-9 match)) + +(define default-pull-request-json + "{ + \"action\": \"opened\", + \"pull_request\": { + \"number\": 1, + \"state\": \"open\", + \"base\": { + \"label\": \"base-label\", + \"ref\": \"base-branch\", + \"sha\": \"666af40e8a059fa05c7048a7ac4f2eccbbd0183b\", + \"repo\": { + \"name\": \"project-name\", + \"clone_url\": \"https://forgejo.instance.test/base-repo/project-name.git\" + } + }, + \"head\": { + \"label\": \"test-label\", + \"ref\": \"test-branch\", + \"sha\": \"582af40e8a059fa05c7048a7ac4f2eccbbd0183b\", + \"repo\": { + \"name\": \"fork-name\", + \"clone_url\": \"https://forgejo.instance.test/source-repo/fork-name.git\" + } + } + } + }") + +(test-assert "default-json" + (specifications-equal? + (let ((event (json->forgejo-pull-request-event default-pull-request-json))) + (forgejo-pull-request->specification + (forgejo-pull-request-event-pull-request event))) + (specification + (name 'forgejo-pull-requests-project-name-test-branch-1) + (build '(channels . (project-name))) + (channels + (cons* (channel + (name 'project-name) + (url "https://forgejo.instance.test/source-repo/fork-name.git") + (branch "test-branch")) + %default-channels)) + (priority 1) + (period 3600) + (systems (list "x86_64-linux"))))) From patchwork Tue Dec 10 16:09:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Romain GARBAGE X-Patchwork-Id: 34747 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 5E7DA27BBEA; Tue, 10 Dec 2024 16:10:29 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 0E0DE27BBE2 for ; Tue, 10 Dec 2024 16:10:29 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tL2oK-0000yY-ON; Tue, 10 Dec 2024 11:10:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tL2oI-0000xi-Vj for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:06 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tL2oI-0005V0-KK for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=TJJwp1j+lEv0QRkr3b5qR3WAeV3Kc1+vnZfhvyMLHp8=; b=S0WJtte9BAnyVShmKz8Tjat0y4oGrG9mqHAx2oDgX3cHlCgJDhxlB9S1C+CYX8F66sV1QnPtsfOiG4b18+jTyGrE8lT9MDGiAgCTg0Oezf/VlEKc4XAtrOrEZ45Ij2+kA7cEeiWCvQAgF3EBaNe+8laJYawQXfngZ5J8qHdpWKFCOwl1rm9PB8x2zNkCUMTbdaDfMUBBlxVEYLzga9NpzoY1lZ3PNSZTO97g2Kl94ffjWpy15Rzq6RAyx9BWfEragrgGm4k9+LYC7ow5ujpnHFQNAWNZ5cPYvTM3Xqz0Rw4IhIjWt6FJDHBRCNQfWVsl19saNhmWY78CbzsErEXpRA==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tL2oI-0001Y2-Be for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:06 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#74769] [PATCH Cuirass 3/4] tests: Explicit Gitlab endpoint related variables. Resent-From: Romain GARBAGE Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 10 Dec 2024 16:10:06 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74769 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 74769@debbugs.gnu.org Cc: ludovic.courtes@inria.fr, Romain GARBAGE Received: via spool by 74769-submit@debbugs.gnu.org id=B74769.17338469965792 (code B ref 74769); Tue, 10 Dec 2024 16:10:06 +0000 Received: (at 74769) by debbugs.gnu.org; 10 Dec 2024 16:09:56 +0000 Received: from localhost ([127.0.0.1]:59212 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tL2o8-0001VF-4b for submit@debbugs.gnu.org; Tue, 10 Dec 2024 11:09:56 -0500 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:32084) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tL2o3-0001TJ-CI for 74769@debbugs.gnu.org; Tue, 10 Dec 2024 11:09:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inria.fr; s=dc; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TJJwp1j+lEv0QRkr3b5qR3WAeV3Kc1+vnZfhvyMLHp8=; b=pUgIKPDrHSA5WeqRu0XmneVWkvUHg+T44cY1N02jtDgbVfIQbh5NpEME oaXxEYiQMjg0b/Jmi2jztS1CWRe4UVT2mVwmjZUoT+xBp3kR/Y+CDh1Te J5j+TCrCmSm2zhdqKQqcEl6oO+WJoeEG1IVbiT9xpBybSrnG8h5fN3+MZ k=; Authentication-Results: mail3-relais-sop.national.inria.fr; dkim=none (message not signed) header.i=none; spf=SoftFail smtp.mailfrom=romain.garbage@inria.fr; dmarc=fail (p=none dis=none) d=inria.fr X-IronPort-AV: E=Sophos;i="6.12,222,1728943200"; d="scan'208";a="103964552" Received: from unknown (HELO guix-A102.bordeaux.inria.fr) ([193.50.110.191]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Dec 2024 17:09:40 +0100 From: Romain GARBAGE Date: Tue, 10 Dec 2024 17:09:27 +0100 Message-ID: <20241210160929.14180-3-romain.garbage@inria.fr> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241210160929.14180-1-romain.garbage@inria.fr> References: <20241210160929.14180-1-romain.garbage@inria.fr> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * tests/http.scm: Rename mr-* variables to gitlab-merge-request-*. --- tests/http.scm | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/http.scm b/tests/http.scm index 7b8ab03..9712787 100644 --- a/tests/http.scm +++ b/tests/http.scm @@ -90,7 +90,7 @@ (channel . "packages") (directory . "dir2"))))))) -(define mr-json-open +(define gitlab-merge-request-json-open "{ \"event_type\": \"merge_request\", \"project\": { @@ -108,7 +108,7 @@ } }") -(define mr-json-close +(define gitlab-merge-request-json-close "{ \"event_type\": \"merge_request\", \"project\": { @@ -126,8 +126,8 @@ } }") -(define mr-spec - (let ((event (json->gitlab-event mr-json-open))) +(define gitlab-merge-request-spec + (let ((event (json->gitlab-event gitlab-merge-request-json-open))) (gitlab-merge-request->specification (gitlab-event-value event) (gitlab-event-project event)))) @@ -438,24 +438,24 @@ (specification-channels spec))))))) (test-equal "/admin/gitlab/event creates a spec from a new merge request" - (specification-name mr-spec) + (specification-name gitlab-merge-request-spec) (begin - (http-post-json (test-cuirass-uri "/admin/gitlab/event") mr-json-open) - (specification-name (db-get-specification (specification-name mr-spec))))) + (http-post-json (test-cuirass-uri "/admin/gitlab/event") gitlab-merge-request-json-open) + (specification-name (db-get-specification (specification-name gitlab-merge-request-spec))))) (test-equal "/admin/gitlab/event error when a merge request has already been created" 400 - (response-code (http-post-json (test-cuirass-uri "/admin/gitlab/event") mr-json-open))) + (response-code (http-post-json (test-cuirass-uri "/admin/gitlab/event") gitlab-merge-request-json-open))) (test-assert "/admin/gitlab/event removes a spec from a closed merge request" (begin - (http-post-json (test-cuirass-uri "/admin/gitlab/event") mr-json-close) - (not (db-get-specification (specification-name mr-spec))))) + (http-post-json (test-cuirass-uri "/admin/gitlab/event") gitlab-merge-request-json-close) + (not (db-get-specification (specification-name gitlab-merge-request-spec))))) (test-equal "/admin/gitlab/event error when a merge request has already been closed" 404 (response-code (http-post-json (test-cuirass-uri "/admin/gitlab/event") - mr-json-close))) + gitlab-merge-request-json-close))) (test-assert "db-close" (begin From patchwork Tue Dec 10 16:09:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Romain GARBAGE X-Patchwork-Id: 34748 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 01D2127BBEA; Tue, 10 Dec 2024 16:10:35 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 2E2B527BBE2 for ; Tue, 10 Dec 2024 16:10:34 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tL2oM-00010E-6F; Tue, 10 Dec 2024 11:10:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tL2oK-0000yB-AZ for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:08 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tL2oK-0005VM-0j for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=qI+CsIJ2GocqiGNyWBT0ZRymw9jOh4mnDHHKwu9FCVk=; b=IfeW7u4pYQPkfDyunBNbstObjEnTpaDIi4gnYqASltrq3jv8gYif/Zv2W+9cH0QGq8YY9UJnB1vnO+uSEZqyBXuE4i5SwqOXU5v9RpyK5iPRPq2vPnawb13fps0sU7lMFdTOJmknf0sMyxisiDTNk+tzHYEKrVYhrmZisX9u3VF6WQuCTbbAVTFPCFQuqjpJ7aJ3P5fU1IOBztB84SANMEiTGKyS87T1FCSBZ56GkSiIMGLiN35RzhrfWZbhZ4fq+Gvbw3RUNQ5g1u0N7Dsx9YLc28eiPbewpRcaDPTA3lPa+WvRA3KSx3MlNhVBJgNW5BrulMX1M3VOCZeTpzMVSg==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tL2oJ-0001Ye-QR for guix-patches@gnu.org; Tue, 10 Dec 2024 11:10:07 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#74769] [PATCH Cuirass 4/4] http: Add admin/forgejo/event. Resent-From: Romain GARBAGE Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 10 Dec 2024 16:10:07 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74769 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 74769@debbugs.gnu.org Cc: ludovic.courtes@inria.fr, Romain GARBAGE Received: via spool by 74769-submit@debbugs.gnu.org id=B74769.17338469985812 (code B ref 74769); Tue, 10 Dec 2024 16:10:07 +0000 Received: (at 74769) by debbugs.gnu.org; 10 Dec 2024 16:09:58 +0000 Received: from localhost ([127.0.0.1]:59214 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tL2o9-0001VN-Ka for submit@debbugs.gnu.org; Tue, 10 Dec 2024 11:09:58 -0500 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:32084) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tL2o4-0001TJ-Ae for 74769@debbugs.gnu.org; Tue, 10 Dec 2024 11:09:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inria.fr; s=dc; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qI+CsIJ2GocqiGNyWBT0ZRymw9jOh4mnDHHKwu9FCVk=; b=K88Ux1JeXmz3PmSr3XMAKERA0gi5Ase3d2/MSkIp9Q8Q+fec5Bmfwte5 uQ5o32m5W95Ftg0QzhRJFutd5079GvqnbMnqlj7ZLhZabRzrMfOk/Tlq2 v5MhV+F6yymwP8gH/AX4Cv9iYc2j1QLD32FRo50aylQyiY8NeMiWP6g28 U=; Authentication-Results: mail3-relais-sop.national.inria.fr; dkim=none (message not signed) header.i=none; spf=SoftFail smtp.mailfrom=romain.garbage@inria.fr; dmarc=fail (p=none dis=none) d=inria.fr X-IronPort-AV: E=Sophos;i="6.12,222,1728943200"; d="scan'208";a="103964554" Received: from unknown (HELO guix-A102.bordeaux.inria.fr) ([193.50.110.191]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Dec 2024 17:09:41 +0100 From: Romain GARBAGE Date: Tue, 10 Dec 2024 17:09:28 +0100 Message-ID: <20241210160929.14180-4-romain.garbage@inria.fr> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241210160929.14180-1-romain.garbage@inria.fr> References: <20241210160929.14180-1-romain.garbage@inria.fr> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * src/cuirass/http.scm (url-handler): Add "/admin/forgejo/event". * tests/http.scm: Add tests for the "/admin/forgejo/event" endpoint. * doc/cuirass.texi: Reorganize "Interfacing Cuirass..." section. Add documentation for the "/admin/forgejo/event" endpoint. --- doc/cuirass.texi | 38 ++++++++++++++++-- src/cuirass/http.scm | 62 +++++++++++++++++++++++++++++ tests/http.scm | 92 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 186 insertions(+), 6 deletions(-) diff --git a/doc/cuirass.texi b/doc/cuirass.texi index 13739c9..895d91f 100644 --- a/doc/cuirass.texi +++ b/doc/cuirass.texi @@ -1284,11 +1284,14 @@ This request accepts a mandatory parameter. Limit query result to nr elements. This parameter is @emph{mandatory}. @end table -@section Interfacing Cuirass with a GitLab Server +@section Interfacing Cuirass with a Git forge -Cuirass supports integration with GitLab through the @dfn{webhook} mechanism: -a POST request is sent by a GitLab instance whenever a specific event is -triggered. So far, Cuirass only support merge-request events. +Cuirass supports integration with various forges through the +@dfn{webhook} mechanism: a POST request is sent by the forge instance +whenever a specific event is triggered. So far, Cuirass only support +merge-request/pull-request events. + +@subsection Interfacing with a GitLab Server Sending a merge request event on the @code{/admin/gitlab/event} endpoint allows controlling a specific jobset related to the merge request @@ -1330,6 +1333,33 @@ A JSON list of strings. Each string must be a supported system, i.e. @code{"systems": [ "x86_64-linux", "aarch64-linux" ]} @end table +@subsection Interfacing with a Forgejo Server + +Sending a merge request event on the @code{/admin/forgejo/event} +endpoint allows controlling a specific jobset related to the merge +request content. This interface expect the JSON data to contain the +following keys: +@table @code +@item "action" +@item "pull_request.number" +@item "pull_request.state" +@item "pull_request.base.label" +@item "pull_request.base.ref" +@item "pull_request.base.sha" +@item "pull_request.base.repo.name" +@item "pull_request.base.repo.clone_url" +@item "pull_request.head.label" +@item "pull_request.head.ref" +@item "pull_request.head.sha" +@item "pull_request.head.repo.name" +@item "pull_request.head.repo.clone_url" +@end table + +The resulting jobset, named as +@code{forgejo-pull-requests-@var{pull_request}.@var{base}.@var{pull_request}.@var{name}-@var{pull_request}.@var{head}.@var{ref}-@var{pull_request}.@var{number}}, +is set to build the channel corresponding to the source branch in the +merge request data with a priority of 1. + @c ********************************************************************* @node Database @chapter Database schema diff --git a/src/cuirass/http.scm b/src/cuirass/http.scm index 881e803..a9c3cda 100644 --- a/src/cuirass/http.scm +++ b/src/cuirass/http.scm @@ -27,6 +27,7 @@ #:use-module (cuirass config) #:use-module (cuirass database) #:use-module ((cuirass base) #:select (evaluation-log-file)) + #:use-module (cuirass forgejo) #:use-module (cuirass gitlab) #:use-module (cuirass metrics) #:use-module (cuirass utils) @@ -779,6 +780,67 @@ return DEFAULT." (event-type (respond-json-with-error 400 (format #f "Event type \"~a\" not supported." event-type)))) (respond-json-with-error 400 "This API only supports JSON.")))) + ;; Define an API for Forgejo events + (('POST "admin" "forgejo" "event") + (let* ((params (utf8->string body)) + (event-type (assoc-ref (request-headers request) 'x-forgejo-event)) + (content-type (assoc-ref (request-headers request) 'content-type)) + (json? (equal? (car content-type) + 'application/json))) + (if json? + (match event-type + ("pull_request" + (let* ((event (json->forgejo-pull-request-event params)) + (pull-request (forgejo-pull-request-event-pull-request event)) + (spec (forgejo-pull-request->specification pull-request)) + (spec-name (specification-name spec))) + (match (forgejo-pull-request-event-action event) + ;; New pull request. + ((or 'opened 'reopened) + (if (not (db-get-specification spec-name)) + (begin + (db-add-or-update-specification spec) + + (unless (call-bridge `(register-jobset ,spec-name) + bridge) + (log-warning + "cannot notify bridge of the addition of jobset '~a'" + spec-name)) + (respond + (build-response #:code 200 + #:headers + `((location . ,(string->uri-reference "/")))) + #:body "")) + (begin + (log-warning "jobset '~a' already exists" spec-name) + (respond-json-with-error 400 "Jobset already exists.")))) + ;; Closed or merged pull request. + ('closed + (if (db-get-specification spec-name) + (begin + (call-bridge `(remove-jobset ,spec-name) bridge) + (log-info "Removed jobset '~a'" spec-name) + (respond + (build-response #:code 200 + #:headers + `((location . ,(string->uri-reference "/")))) + #:body "")) + (begin + (log-warning "cannot find jobset '~a'" spec-name) + (respond-json-with-error 404 "Jobset not found.")))) + ;; 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."))) + (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.")))) + (('POST "admin" "specification" "add") (let* ((spec (body->specification body)) (name (specification-name spec))) diff --git a/tests/http.scm b/tests/http.scm index 9712787..9393043 100644 --- a/tests/http.scm +++ b/tests/http.scm @@ -22,6 +22,7 @@ (use-modules ((cuirass base) #:select (%bridge-socket-file-name)) (cuirass http) (cuirass database) + (cuirass forgejo) (cuirass gitlab) (cuirass specification) (cuirass utils) @@ -43,8 +44,9 @@ (call-with-values (lambda () (http-get uri)) (lambda (response body) body))) -(define (http-post-json uri body) - (http-post uri #:body body #:headers '((content-type application/json)))) +(define* (http-post-json uri body #:optional (extra-headers '())) + (http-post uri #:body body #:headers (append '((content-type application/json)) + extra-headers))) (define (wait-until-ready port) ;; Wait until the server is accepting connections. @@ -132,6 +134,65 @@ (gitlab-event-value event) (gitlab-event-project event)))) +(define forgejo-pull-request-json-open + "{ + \"action\": \"opened\", + \"pull_request\": { + \"number\": 1, + \"state\": \"open\", + \"base\": { + \"label\": \"base-label\", + \"ref\": \"base-branch\", + \"sha\": \"666af40e8a059fa05c7048a7ac4f2eccbbd0183b\", + \"repo\": { + \"name\": \"project-name\", + \"clone_url\": \"https://forgejo.instance.test/base-repo/project-name.git\" + } + }, + \"head\": { + \"label\": \"test-label\", + \"ref\": \"test-branch\", + \"sha\": \"582af40e8a059fa05c7048a7ac4f2eccbbd0183b\", + \"repo\": { + \"name\": \"fork-name\", + \"clone_url\": \"https://forgejo.instance.test/source-repo/fork-name.git\" + } + } + } + }") + +(define forgejo-pull-request-json-close + "{ + \"action\": \"closed\", + \"pull_request\": { + \"number\": 1, + \"state\": \"closed\", + \"base\": { + \"label\": \"base-label\", + \"ref\": \"base-branch\", + \"sha\": \"666af40e8a059fa05c7048a7ac4f2eccbbd0183b\", + \"repo\": { + \"name\": \"project-name\", + \"clone_url\": \"https://forgejo.instance.test/base-repo/project-name.git\" + } + }, + \"head\": { + \"label\": \"test-label\", + \"ref\": \"test-branch\", + \"sha\": \"582af40e8a059fa05c7048a7ac4f2eccbbd0183b\", + \"repo\": { + \"name\": \"fork-name\", + \"clone_url\": \"https://forgejo.instance.test/source-repo/fork-name.git\" + } + } + } + }") + +(define forgejo-pull-request-specification + (forgejo-pull-request->specification + (forgejo-pull-request-event-pull-request + (json->forgejo-pull-request-event forgejo-pull-request-json-open)))) + (define-syntax-rule (with-cuirass-register exp ...) (with-guix-daemon (let ((pid #f)) @@ -457,6 +518,33 @@ (response-code (http-post-json (test-cuirass-uri "/admin/gitlab/event") gitlab-merge-request-json-close))) + (test-equal "/admin/forgejo/event creates a spec from a new pull request" + (specification-name forgejo-pull-request-specification) + (begin + (http-post-json (test-cuirass-uri "/admin/forgejo/event") + forgejo-pull-request-json-open + '((x-forgejo-event . "pull_request"))) + (specification-name (db-get-specification (specification-name forgejo-pull-request-specification))))) + + (test-equal "/admin/forgejo/event error when a pull request has already been created" + 400 + (response-code (http-post-json (test-cuirass-uri "/admin/forgejo/event") + forgejo-pull-request-json-open + '((x-forgejo-event . "pull_request"))))) + + (test-assert "/admin/forgejo/event removes a spec from a closed pull request" + (begin + (http-post-json (test-cuirass-uri "/admin/forgejo/event") + forgejo-pull-request-json-close + '((x-forgejo-event . "pull_request"))) + (not (db-get-specification (specification-name forgejo-pull-request-specification))))) + + (test-equal "/admin/forgejo/event error when a pull request has already been closed" + 404 + (response-code (http-post-json (test-cuirass-uri "/admin/forgejo/event") + forgejo-pull-request-json-close + '((x-forgejo-event . "pull_request"))))) + (test-assert "db-close" (begin (db-close (%db))