From e732034aea5bce177bc5c913261c20f11395be94 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Mon, 11 Dec 2023 00:17:54 -0500 Subject: [PATCH] merging a pr does not return a body --- src/lib.rs | 23 +++++++++++++++++++++++ src/repository.rs | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a036a05..9e5029b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,6 +118,16 @@ impl Forgejo { self.execute_str(request).await } + async fn post_unit( + &self, + path: &str, + body: &T, + ) -> Result<(), ForgejoError> { + let url = self.url.join("api/v1/").unwrap().join(path).unwrap(); + let request = self.client.post(url).json(body).build()?; + self.execute_unit(request).await + } + async fn post_raw( &self, path: &str, @@ -179,6 +189,19 @@ impl Forgejo { } } + /// Like `execute`, but returns unit. + async fn execute_unit(&self, request: Request) -> Result<(), ForgejoError> { + let response = self.client.execute(request).await?; + match response.status() { + status if status.is_success() => Ok(()), + status if status.is_client_error() => Err(ForgejoError::ApiError( + status, + response.json::().await?.message.unwrap_or_else(|| String::from("[no message]")), + )), + status => Err(ForgejoError::UnexpectedStatusCode(status)), + } + } + /// Like `execute`, but returns `Ok(None)` on 404. async fn execute_opt( &self, diff --git a/src/repository.rs b/src/repository.rs index 8624530..cea567a 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -44,7 +44,7 @@ impl Forgejo { pr: u64, opts: MergePullRequestOption, ) -> Result<(), ForgejoError> { - self.post(&format!("repos/{owner}/{repo}/pulls/{pr}/merge"), &opts) + self.post_unit(&format!("repos/{owner}/{repo}/pulls/{pr}/merge"), &opts) .await }