feat(server/forgejo): fetch commit histories

This commit is contained in:
Paul Campbell 2024-04-09 16:16:01 +01:00
parent bdea942bcb
commit 9eb7660f7b
4 changed files with 91 additions and 6 deletions

View file

@ -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<super::RepoActor>,
_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!()
}

View file

@ -48,9 +48,10 @@ impl Handler<LoadedConfig> 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);
}

View file

@ -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<CommitHistories, OneOf<(network::NetworkError,)>> {
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<Vec<forge::Commit>, 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::<Vec<Commit>>(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,
}

View file

@ -1 +1,13 @@
pub mod forgejo;
#[derive(Clone, Debug)]
pub struct CommitHistories {
pub main: Vec<Commit>,
pub next: Vec<Commit>,
pub dev: Vec<Commit>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Commit {
pub sha: String,
}