From a5e94214053022f82004001aaf83110d93e9a901 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 10 Apr 2024 11:48:24 +0100 Subject: [PATCH] feat(server): implement forge::forgejo::get_commit_history Closes kemitix/git-next#13 --- src/server/forge/forgejo/mod.rs | 51 +++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/server/forge/forgejo/mod.rs b/src/server/forge/forgejo/mod.rs index b9db699e..c2448112 100644 --- a/src/server/forge/forgejo/mod.rs +++ b/src/server/forge/forgejo/mod.rs @@ -1,6 +1,6 @@ use kxio::network; use terrors::OneOf; -use tracing::{error, info}; +use tracing::{debug, error, info, warn}; use crate::server::{actors::repo::status::Status, config::BranchName, forge}; @@ -33,7 +33,7 @@ async fn get_commit_history( "https://{hostname}/api/v1/repos/{path}/commits?sha={branch_name}&stat=false&files=false&token={token}" )); - info!(%url, "Fetching commits"); + debug!(%url, "Fetching commits"); let request = network::NetRequest::new( network::RequestMethod::Get, url, @@ -66,13 +66,48 @@ struct Commit { } pub async fn get_commit_status( - _next: forge::Commit, - _repo_details: crate::server::config::RepoDetails, - _net: network::Network, + next: forge::Commit, + repo_details: crate::server::config::RepoDetails, + net: network::Network, ) -> Status { - // TODO: (#13) check statuses for next head - if ok, advance main to next and reassess - // https://{hostname}/api/v1/repos/{path}/commits/{commit}/status?token={token} - Status::Pending + let hostname = &repo_details.forge.hostname; + let path = &repo_details.repo; + let token = &repo_details.forge.token; + let url = network::NetUrl::new(format!( + "https://{hostname}/api/v1/repos/{path}/commits/{next}/status?token={token}" + )); + + info!(%url, "Fetching commit status"); + let request = network::NetRequest::new( + network::RequestMethod::Get, + url, + network::NetRequestHeaders::new(), + network::RequestBody::None, + network::ResponseType::Json, + None, + network::NetRequestLogging::None, + ); + let result = net.get::(request).await; + match result { + Ok(response) => { + match response.response_body() { + Some(status) => match status.state { + CommitStatusState::Success => Status::Pass, + CommitStatusState::Pending => Status::Pending, + CommitStatusState::Failure => Status::Fail, + CommitStatusState::Error => Status::Fail, + }, + None => { + warn!("No status found for commit"); + Status::Pending // assume issue is transient and allow retry + } + } + } + Err(e) => { + error!(?e, "Failed to get commit status"); + Status::Pending // assume issue is transient and allow retry + } + } } #[derive(Debug, serde::Deserialize)]