2024-05-23 16:19:28 +01:00
|
|
|
use git_next_git as git;
|
2024-05-23 16:50:36 +01:00
|
|
|
|
2024-04-16 22:21:55 +01:00
|
|
|
use kxio::network::{self, Network};
|
2024-05-26 09:44:56 +01:00
|
|
|
use tracing::{error, warn};
|
2024-04-16 22:21:55 +01:00
|
|
|
|
2024-05-04 12:37:35 +01:00
|
|
|
#[derive(Clone, Debug)]
|
2024-05-23 19:36:05 +01:00
|
|
|
pub struct ForgeJo {
|
2024-05-23 16:19:28 +01:00
|
|
|
repo_details: git::RepoDetails,
|
2024-04-16 22:21:55 +01:00
|
|
|
net: Network,
|
|
|
|
}
|
2024-05-23 19:36:05 +01:00
|
|
|
impl ForgeJo {
|
2024-05-26 09:44:56 +01:00
|
|
|
pub const fn new(repo_details: git::RepoDetails, net: Network) -> Self {
|
|
|
|
Self { repo_details, net }
|
2024-04-16 22:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
2024-05-23 19:36:05 +01:00
|
|
|
impl git::ForgeLike for ForgeJo {
|
2024-05-29 19:22:05 +01:00
|
|
|
fn forge_alias(&self) -> String {
|
2024-04-16 22:21:55 +01:00
|
|
|
"forgejo".to_string()
|
|
|
|
}
|
|
|
|
|
2024-05-23 08:30:58 +01:00
|
|
|
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status {
|
2024-04-16 22:21:55 +01:00
|
|
|
let repo_details = &self.repo_details;
|
2024-05-15 07:55:05 +01:00
|
|
|
let hostname = &repo_details.forge.hostname();
|
2024-04-20 20:49:38 +01:00
|
|
|
let repo_path = &repo_details.repo_path;
|
2024-05-15 07:55:05 +01:00
|
|
|
let api_token = &repo_details.forge.token();
|
2024-04-16 22:21:55 +01:00
|
|
|
use secrecy::ExposeSecret;
|
|
|
|
let token = api_token.expose_secret();
|
|
|
|
let url = network::NetUrl::new(format!(
|
2024-04-20 20:49:38 +01:00
|
|
|
"https://{hostname}/api/v1/repos/{repo_path}/commits/{commit}/status?token={token}"
|
2024-04-16 22:21:55 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
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 {
|
2024-05-23 08:30:58 +01:00
|
|
|
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,
|
2024-04-16 22:21:55 +01:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
warn!("No status found for commit");
|
2024-05-23 08:30:58 +01:00
|
|
|
git::commit::Status::Pending // assume issue is transient and allow retry
|
2024-04-16 22:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!(?e, "Failed to get commit status");
|
2024-05-23 08:30:58 +01:00
|
|
|
git::commit::Status::Pending // assume issue is transient and allow retry
|
2024-04-16 22:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
|
|
|
}
|