git-next/crates/forge-forgejo/src/lib.rs

85 lines
2.7 KiB
Rust
Raw Normal View History

use git_next_git as git;
2024-05-23 16:50:36 +01:00
use kxio::network::{self, Network};
use tracing::{error, warn};
#[derive(Clone, Debug)]
2024-05-23 19:36:05 +01:00
pub struct ForgeJo {
repo_details: git::RepoDetails,
net: Network,
}
2024-05-23 19:36:05 +01:00
impl ForgeJo {
pub const fn new(repo_details: git::RepoDetails, net: Network) -> Self {
Self { repo_details, net }
}
}
#[async_trait::async_trait]
2024-05-23 19:36:05 +01:00
impl git::ForgeLike for ForgeJo {
fn forge_alias(&self) -> String {
"forgejo".to_string()
}
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status {
let repo_details = &self.repo_details;
let hostname = &repo_details.forge.hostname();
let repo_path = &repo_details.repo_path;
let api_token = &repo_details.forge.token();
use secrecy::ExposeSecret;
let token = api_token.expose_secret();
let url = network::NetUrl::new(format!(
"https://{hostname}/api/v1/repos/{repo_path}/commits/{commit}/status?token={token}"
));
let request = network::NetRequest::new(
network::RequestMethod::Get,
url,
network::NetRequestHeaders::new(),
network::RequestBody::None,
network::ResponseType::Json,
None,
network::NetRequestLogging::None,
);
let result = self.net.get::<CombinedStatus>(request).await;
match result {
Ok(response) => {
match response.response_body() {
Some(status) => match status.state {
CommitStatusState::Success => git::commit::Status::Pass,
CommitStatusState::Pending => git::commit::Status::Pending,
CommitStatusState::Failure => git::commit::Status::Fail,
CommitStatusState::Error => git::commit::Status::Fail,
CommitStatusState::Blank => git::commit::Status::Pending,
},
None => {
warn!("No status found for commit");
git::commit::Status::Pending // assume issue is transient and allow retry
}
}
}
Err(e) => {
error!(?e, "Failed to get commit status");
git::commit::Status::Pending // assume issue is transient and allow retry
}
}
}
}
#[derive(Debug, serde::Deserialize)]
pub struct CombinedStatus {
pub state: CommitStatusState,
}
#[derive(Debug, serde::Deserialize)]
pub enum CommitStatusState {
#[serde(rename = "success")]
Success,
#[serde(rename = "pending")]
Pending,
#[serde(rename = "failure")]
Failure,
#[serde(rename = "error")]
Error,
#[serde(rename = "")]
Blank,
}