diff --git a/src/server/actors/repo/branch.rs b/src/server/actors/repo/branch.rs index 14e06fb2..669a0f7f 100644 --- a/src/server/actors/repo/branch.rs +++ b/src/server/actors/repo/branch.rs @@ -1,9 +1,16 @@ -pub(crate) async fn validate_positions( - _config: crate::server::config::RepoConfig, +use crate::server::forge; + +#[tracing::instrument(fields(forge_name = %repo_details.forge.name, repo_name = %repo_details.name))] +pub async fn validate_positions( + repo_details: crate::server::config::RepoDetails, + config: crate::server::config::RepoConfig, _addr: actix::prelude::Addr, - _net: kxio::network::Network, + net: kxio::network::Network, ) { - // TODO: validate repo - next is no more than one commit ahead of main - // TODO: validate repo - dev is same as or is ahead of next + let _commit_histories = match repo_details.forge.forge_type { + crate::server::config::ForgeType::ForgeJo => { + forge::forgejo::get_commit_histories(&repo_details, &config, &net) + } + }; todo!() } diff --git a/src/server/actors/repo/mod.rs b/src/server/actors/repo/mod.rs index 07340817..eb8693f2 100644 --- a/src/server/actors/repo/mod.rs +++ b/src/server/actors/repo/mod.rs @@ -48,9 +48,10 @@ impl Handler for RepoActor { let config = msg.0; info!(%self.details, %config, "Config loaded"); self.config.replace(config.clone()); + let repo_details = self.details.clone(); let addr = ctx.address(); let net = self.net.clone(); - branch::validate_positions(config, addr, net) + branch::validate_positions(repo_details, config, addr, net) .into_actor(self) .wait(ctx); } diff --git a/src/server/forge/forgejo/mod.rs b/src/server/forge/forgejo/mod.rs index ef68c369..1a57b9e4 100644 --- a/src/server/forge/forgejo/mod.rs +++ b/src/server/forge/forgejo/mod.rs @@ -1 +1,66 @@ +use kxio::network; +use terrors::OneOf; +use tracing::{error, info}; + +use crate::server::{config::BranchName, forge}; + +use super::CommitHistories; + pub mod config; + +pub async fn get_commit_histories( + repo_details: &crate::server::config::RepoDetails, + config: &crate::server::config::RepoConfig, + net: &kxio::network::Network, +) -> Result> { + let main = (get_commit_history(repo_details, &config.branches().main(), net).await)?; + let next = (get_commit_history(repo_details, &config.branches().next(), net).await)?; + let dev = (get_commit_history(repo_details, &config.branches().dev(), net).await)?; + let histories = CommitHistories { main, next, dev }; + Ok(histories) +} + +#[tracing::instrument(fields(%branch_name))] +async fn get_commit_history( + repo_details: &crate::server::config::RepoDetails, + branch_name: &BranchName, + net: &kxio::network::Network, +) -> Result, OneOf<(network::NetworkError,)>> { + 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?sha={branch_name}&stat=false&files=false&token={token}" + )); + + info!(%url, "Fetching commits"); + let request = network::NetRequest::new( + network::RequestMethod::Get, + url, + network::NetRequestHeaders::new(), + network::RequestBody::None, + network::ResponseType::Json, + None, + network::NetRequestLogging::Both, + ); + let result = net.get::>(request).await; + let response = result.map_err(|e| { + error!(?e, "Failed to get commit history"); + OneOf::new(e) + })?; + let commits = response + .response_body() + .unwrap_or_default() + .into_iter() + .map(|commit| commit.sha) + .map(|sha| forge::Commit { sha }) + .collect(); + + Ok(commits) +} + +#[allow(dead_code)] +#[derive(Debug, Default, serde::Deserialize)] +struct Commit { + sha: String, +} diff --git a/src/server/forge/mod.rs b/src/server/forge/mod.rs index 607ec122..8d35f4c2 100644 --- a/src/server/forge/mod.rs +++ b/src/server/forge/mod.rs @@ -1 +1,13 @@ pub mod forgejo; + +#[derive(Clone, Debug)] +pub struct CommitHistories { + pub main: Vec, + pub next: Vec, + pub dev: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Commit { + pub sha: String, +}