feat(server): cache Repository in RepoActor
Some checks failed
ci/woodpecker/cron/push-next Pipeline was successful
ci/woodpecker/cron/tag-created Pipeline was successful
ci/woodpecker/cron/cron-docker-builder Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/push/push-next Pipeline failed

This commit is contained in:
Paul Campbell 2024-05-09 21:18:40 +01:00
parent b7aa417831
commit 992821d563
9 changed files with 41 additions and 15 deletions

View file

@ -10,7 +10,7 @@ use tracing::{debug, info, warn, Instrument};
use crate::server::{ use crate::server::{
actors::repo::webhook::WebhookAuth, actors::repo::webhook::WebhookAuth,
config::{RepoConfig, RepoDetails, Webhook}, config::{RepoConfig, RepoDetails, Webhook},
gitforge, gitforge::{self, Repository},
types::{MessageToken, ServerGeneration}, types::{MessageToken, ServerGeneration},
}; };
@ -26,6 +26,7 @@ pub struct RepoActor {
last_main_commit: Option<gitforge::Commit>, last_main_commit: Option<gitforge::Commit>,
last_next_commit: Option<gitforge::Commit>, last_next_commit: Option<gitforge::Commit>,
last_dev_commit: Option<gitforge::Commit>, last_dev_commit: Option<gitforge::Commit>,
repository: Option<Repository>,
net: Network, net: Network,
forge: gitforge::Forge, forge: gitforge::Forge,
} }
@ -55,6 +56,7 @@ impl RepoActor {
last_main_commit: None, last_main_commit: None,
last_next_commit: None, last_next_commit: None,
last_dev_commit: None, last_dev_commit: None,
repository: None,
net, net,
forge, forge,
} }
@ -100,7 +102,8 @@ impl Handler<CloneRepo> for RepoActor {
info!("Message Received"); info!("Message Received");
let gitdir = self.details.gitdir.clone(); let gitdir = self.details.gitdir.clone();
match self.forge.repo_clone(gitdir) { match self.forge.repo_clone(gitdir) {
Ok(_) => { Ok(repository) => {
self.repository.replace(repository);
if self.details.repo_config.is_none() { if self.details.repo_config.is_none() {
ctx.address().do_send(LoadConfigFromRepo); ctx.address().do_send(LoadConfigFromRepo);
} else { } else {

View file

@ -48,6 +48,7 @@ pub enum RepoCloneError {
Spawn(std::io::Error), Spawn(std::io::Error),
Validation(String), Validation(String),
GixClone(Box<gix::clone::Error>), GixClone(Box<gix::clone::Error>),
GixOpen(Box<gix::open::Error>),
GixFetch(Box<gix::clone::fetch::Error>), GixFetch(Box<gix::clone::fetch::Error>),
} }
impl std::error::Error for RepoCloneError {} impl std::error::Error for RepoCloneError {}
@ -60,6 +61,7 @@ impl std::fmt::Display for RepoCloneError {
Self::Spawn(err) => write!(f, "Spawning comming: {:?}", err), Self::Spawn(err) => write!(f, "Spawning comming: {:?}", err),
Self::Validation(err) => write!(f, "Validation: {}", err), Self::Validation(err) => write!(f, "Validation: {}", err),
Self::GixClone(err) => write!(f, "Clone: {:?}", err), Self::GixClone(err) => write!(f, "Clone: {:?}", err),
Self::GixOpen(err) => write!(f, "Open: {:?}", err),
Self::GixFetch(err) => write!(f, "Fetch: {:?}", err), Self::GixFetch(err) => write!(f, "Fetch: {:?}", err),
} }
} }
@ -69,6 +71,11 @@ impl From<gix::clone::Error> for RepoCloneError {
Self::GixClone(Box::new(value)) Self::GixClone(Box::new(value))
} }
} }
impl From<gix::open::Error> for RepoCloneError {
fn from(value: gix::open::Error) -> Self {
Self::GixOpen(Box::new(value))
}
}
impl From<gix::clone::fetch::Error> for RepoCloneError { impl From<gix::clone::fetch::Error> for RepoCloneError {
fn from(value: gix::clone::fetch::Error) -> Self { fn from(value: gix::clone::fetch::Error) -> Self {
Self::GixFetch(Box::new(value)) Self::GixFetch(Box::new(value))

View file

@ -13,7 +13,7 @@ use crate::server::{
actors::repo::{RepoActor, StartMonitoring, ValidateRepo}, actors::repo::{RepoActor, StartMonitoring, ValidateRepo},
config::{BranchName, GitDir, RepoConfig, RepoDetails}, config::{BranchName, GitDir, RepoConfig, RepoDetails},
git, git,
gitforge::{self, forgejo::branch::ValidatedPositions, RepoCloneError}, gitforge::{self, forgejo::branch::ValidatedPositions, RepoCloneError, Repository},
types::{GitRef, MessageToken}, types::{GitRef, MessageToken},
}; };
@ -128,16 +128,19 @@ impl super::ForgeLike for ForgeJoEnv {
} }
} }
fn repo_clone(&self, gitdir: GitDir) -> Result<(), RepoCloneError> { fn repo_clone(&self, gitdir: GitDir) -> Result<Repository, RepoCloneError> {
if !gitdir.exists() { let repo = if !gitdir.exists() {
info!("Local copy not found - cloning..."); info!("Local copy not found - cloning...");
repo::clone(&self.repo_details, gitdir.clone())?; repo::clone(&self.repo_details, gitdir.clone())?
} } else {
repo::open(gitdir.clone())?
};
info!("Validating..."); info!("Validating...");
gitdir gitdir
.validate(&self.repo_details) .validate(&self.repo_details)
.map_err(|e| RepoCloneError::Validation(e.to_string())) .map_err(|e| RepoCloneError::Validation(e.to_string()))
.inspect(|_| info!("Validation - OK")) .inspect(|_| info!("Validation - OK"))?;
Ok(repo)
} }
} }

View file

@ -4,17 +4,17 @@ use tracing::info;
use crate::server::{ use crate::server::{
config::{GitDir, RepoDetails}, config::{GitDir, RepoDetails},
gitforge::RepoCloneError, gitforge::{RepoCloneError, Repository},
}; };
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub fn clone(repo_details: &RepoDetails, gitdir: GitDir) -> Result<(), RepoCloneError> { pub fn clone(repo_details: &RepoDetails, gitdir: GitDir) -> Result<Repository, RepoCloneError> {
use secrecy::ExposeSecret; use secrecy::ExposeSecret;
let origin = repo_details.origin(); let origin = repo_details.origin();
let (_repository, _outcome) = let (repository, _outcome) =
gix::prepare_clone_bare(origin.expose_secret().as_str(), gitdir.deref())? gix::prepare_clone_bare(origin.expose_secret().as_str(), gitdir.deref())?
.fetch_only(gix::progress::Discard, &AtomicBool::new(false))?; .fetch_only(gix::progress::Discard, &AtomicBool::new(false))?;
info!("Cloned - OK"); info!("Cloned - OK");
Ok(()) // TODO: (#69) return Repository inside a newtype to store in the RepoActor for reuse else where Ok(repository.into_sync().into())
} }

View file

@ -1,3 +1,5 @@
mod clone; mod clone;
mod open;
pub use clone::clone; pub use clone::clone;
pub use open::open;

View file

@ -0,0 +1,8 @@
use crate::server::{
config::GitDir,
gitforge::{RepoCloneError, Repository},
};
pub fn open(gitdir: GitDir) -> Result<Repository, RepoCloneError> {
Ok(gix::open(gitdir)?.into_sync().into())
}

View file

@ -1,7 +1,7 @@
use crate::server::{ use crate::server::{
actors::repo::RepoActor, actors::repo::RepoActor,
config::{BranchName, GitDir, RepoConfig}, config::{BranchName, GitDir, RepoConfig},
gitforge::{self, RepoCloneError}, gitforge::{self, RepoCloneError, Repository},
types::{GitRef, MessageToken}, types::{GitRef, MessageToken},
}; };
@ -53,7 +53,7 @@ impl super::ForgeLike for MockForgeEnv {
todo!() todo!()
} }
fn repo_clone(&self, _gitdir: GitDir) -> Result<(), RepoCloneError> { fn repo_clone(&self, _gitdir: GitDir) -> Result<Repository, RepoCloneError> {
todo!() todo!()
} }
} }

View file

@ -56,7 +56,7 @@ pub trait ForgeLike {
async fn commit_status(&self, commit: &Commit) -> CommitStatus; async fn commit_status(&self, commit: &Commit) -> CommitStatus;
/// Clones a repo to disk. /// Clones a repo to disk.
fn repo_clone(&self, gitdir: GitDir) -> Result<(), RepoCloneError>; fn repo_clone(&self, gitdir: GitDir) -> Result<Repository, RepoCloneError>;
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

View file

@ -96,3 +96,6 @@ impl std::fmt::Display for Message {
write!(f, "{}", self.0) write!(f, "{}", self.0)
} }
} }
#[derive(Debug, Clone, derive_more::From)]
pub struct Repository(gix::ThreadSafeRepository);