git-next/crates/server/src/gitforge/mod.rs
Paul Campbell 58e991b2b7
All checks were successful
Rust / build (push) Successful in 2m35s
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
test(git): make repository more testable
Adds a layer around Repository to allow the use of a mock.

Mock has still to be implemented.
2024-05-18 20:37:03 +01:00

104 lines
2.7 KiB
Rust

#![allow(dead_code)]
use git::OpenRepository;
use git_next_config::{BranchName, GitDir, RepoConfig};
use git_next_git::{self as git, GitRef, RepoDetails};
use kxio::network::Network;
#[cfg(feature = "forgejo")]
mod forgejo;
#[cfg(feature = "github")]
mod github;
mod mock_forge;
mod types;
pub use types::*;
mod errors;
pub use errors::*;
use crate::types::MessageToken;
#[async_trait::async_trait]
pub trait ForgeLike {
fn name(&self) -> String;
/// Returns a list of all branches in the repo.
async fn branches_get_all(&self) -> Result<Vec<BranchName>, ForgeBranchError>;
/// Returns the contents of the file.
async fn file_contents_get(
&self,
branch: &BranchName,
file_path: &str,
) -> Result<String, ForgeFileError>;
/// Assesses the relative positions of the main, next and dev branch and updates their
/// positions as needed.
async fn branches_validate_positions(
&self,
repository: OpenRepository,
repo_config: RepoConfig,
addr: actix::prelude::Addr<super::actors::repo::RepoActor>,
message_token: MessageToken,
);
/// Moves a branch to a new commit.
fn branch_reset(
&self,
repository: &OpenRepository,
branch_name: BranchName,
to_commit: GitRef,
force: git::push::Force,
) -> git::push::Result;
/// Checks the results of any (e.g. CI) status checks for the commit.
async fn commit_status(&self, commit: &git::Commit) -> CommitStatus;
/// Clones a repo to disk.
fn repo_clone(&self, gitdir: GitDir) -> Result<OpenRepository, git::repository::Error>;
}
#[derive(Clone, Debug)]
pub enum Forge {
Mock(mock_forge::MockForgeEnv),
#[allow(clippy::enum_variant_names)]
#[cfg(feature = "forgejo")]
ForgeJo(forgejo::ForgeJoEnv),
#[cfg(feature = "github")]
Github(github::GithubEnv),
}
impl Forge {
pub const fn new_mock() -> Self {
Self::Mock(mock_forge::MockForgeEnv::new())
}
#[cfg(feature = "forgejo")]
pub const fn new_forgejo(
repo_details: RepoDetails,
net: Network,
repo: git::Repository,
) -> Self {
Self::ForgeJo(forgejo::ForgeJoEnv::new(repo_details, net, repo))
}
#[cfg(feature = "github")]
pub const fn new_github(net: Network) -> Self {
Self::Github(github::GithubEnv::new(net))
}
}
impl std::ops::Deref for Forge {
type Target = dyn ForgeLike;
fn deref(&self) -> &Self::Target {
match self {
Self::Mock(env) => env,
#[cfg(feature = "forgejo")]
Self::ForgeJo(env) => env,
#[cfg(feature = "github")]
Forge::Github(env) => env,
}
}
}
#[cfg(test)]
pub mod tests;