branch: main
commit 828fd7597005856a59b6e8ae2ddf151cdf986318
Author: Romain GARBAGE <[email protected]>
AuthorDate: Fri Apr 4 18:34:37 2025 +0200

    forgejo: Add support for pull request review creation.
    
    * src/cuirass/forges/forgejo.scm (forgejo-api-pull-request-create-review,
    %forgejo-review-state, forgejo-valid-review-state?): New variables.
    
    Signed-off-by: Ludovic Courtès <[email protected]>
---
 src/cuirass/forges/forgejo.scm | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/src/cuirass/forges/forgejo.scm b/src/cuirass/forges/forgejo.scm
index 5ef640f..eef6885 100644
--- a/src/cuirass/forges/forgejo.scm
+++ b/src/cuirass/forges/forgejo.scm
@@ -249,6 +249,15 @@ METHOD. Returns the body of the response as a Guile 
object."
 ;;;
 (define %forgejo-api-base-path "/api/v1")
 
+;; Possible values of the State field of a Review object according to
+;; modules/migration/review.go in Forgejo source code.
+(define %forgejo-review-state '(PENDING APPROVED REQUEST_CHANGES COMMENT))
+
+(define (forgejo-valid-review-state? state)
+  "Returns TRUE if STATE is a valid ReviewState value according to Forgejo API,
+FALSE otherwise."
+  (->bool (member state %forgejo-review-state)))
+
 ;; PATHs are defined e.g. here: <https://codeberg.org/api/swagger>.
 (define (api-build-endpoint path)
   "Returns an API endpoint built from PATH as defined in the documentation."
@@ -286,6 +295,36 @@ JSON. Returns the content of the updated pull-request."
                     #:method 'PATCH
                     #:body changes)))
 
+(define* (forgejo-api-pull-request-create-review server token
+                                                 #:key owner
+                                                       repository
+                                                       pull-request-index
+                                                       review-message
+                                                       (review-state 'COMMENT))
+  "Creates a pull request review for the pull request identified by
+PULL-REQUEST-INDEX containing the message REVIEW-MESSAGE, a string, and the
+status REVIEW-STATUS, a symbol that can be either 'COMMENT, 'APPROVED or
+'REQUEST-CHANGES. Returns the review ID, a number."
+  (unless (forgejo-valid-review-state? review-state)
+    (raise
+     (condition
+      (&forgejo-api-error
+       (message (format #f
+                        "forgejo-api-pull-request-create-review: ~a is not a 
valid review state"
+                        review-state))))))
+  (let* ((body (scm->json-string `((body . ,review-message)
+                                   (comments . ())
+                                   (commit_id . "")
+                                   (event . ,review-state))))
+         (response (forgejo-request server
+                                    (api-build-endpoint
+                                     (format #f "/repos/~a/~a/pulls/~a/reviews"
+                                             owner repository 
pull-request-index))
+                                    #:token token
+                                    #:method 'POST
+                                    #:body body)))
+    (assoc-ref response "id")))
+
 ;;; Extra helper procedures using the API.
 (define* (update-forgejo-pull-request server token #:key owner
                                       repository

Reply via email to