Compare commits
No commits in common. "7e79f4877a8a31e1a8610af5d9ca33a8bce7c651" and "da5bc6950817afb84a55b1e11a709ad199d1a2cd" have entirely different histories.
7e79f4877a
...
da5bc69508
11 changed files with 201 additions and 285 deletions
|
@ -18,7 +18,7 @@ steps:
|
||||||
- event: tag
|
- event: tag
|
||||||
ref: refs/tags/v*
|
ref: refs/tags/v*
|
||||||
# INFO: https://woodpecker-ci.org/plugins/Docker%20Buildx
|
# INFO: https://woodpecker-ci.org/plugins/Docker%20Buildx
|
||||||
image: docker.io/woodpeckerci/plugin-docker-buildx:4.0.0
|
image: docker.io/woodpeckerci/plugin-docker-buildx:3.2.1
|
||||||
settings:
|
settings:
|
||||||
username: kemitix
|
username: kemitix
|
||||||
repo: git.kemitix.net/kemitix/git-next
|
repo: git.kemitix.net/kemitix/git-next
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
pub mod repo;
|
pub mod repo;
|
||||||
pub mod server;
|
|
||||||
pub mod webhook;
|
pub mod webhook;
|
||||||
|
|
|
@ -11,14 +11,14 @@ use crate::server::{
|
||||||
actors::repo::webhook::WebhookAuth,
|
actors::repo::webhook::WebhookAuth,
|
||||||
config::{RepoConfig, RepoDetails, Webhook},
|
config::{RepoConfig, RepoDetails, Webhook},
|
||||||
gitforge,
|
gitforge,
|
||||||
types::{MessageToken, ServerGeneration},
|
types::MessageToken,
|
||||||
};
|
};
|
||||||
|
|
||||||
use self::webhook::WebhookId;
|
use self::webhook::WebhookId;
|
||||||
|
|
||||||
pub struct RepoActor {
|
pub struct RepoActor {
|
||||||
generation: ServerGeneration,
|
|
||||||
message_token: MessageToken,
|
message_token: MessageToken,
|
||||||
|
span: tracing::Span,
|
||||||
details: RepoDetails,
|
details: RepoDetails,
|
||||||
webhook: Webhook,
|
webhook: Webhook,
|
||||||
webhook_id: Option<WebhookId>, // INFO: if [None] then no webhook is configured
|
webhook_id: Option<WebhookId>, // INFO: if [None] then no webhook is configured
|
||||||
|
@ -30,12 +30,8 @@ pub struct RepoActor {
|
||||||
forge: gitforge::Forge,
|
forge: gitforge::Forge,
|
||||||
}
|
}
|
||||||
impl RepoActor {
|
impl RepoActor {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(details: RepoDetails, webhook: Webhook, net: Network) -> Self {
|
||||||
details: RepoDetails,
|
let span = tracing::info_span!("RepoActor", repo = %details);
|
||||||
webhook: Webhook,
|
|
||||||
generation: ServerGeneration,
|
|
||||||
net: Network,
|
|
||||||
) -> Self {
|
|
||||||
let forge = match details.forge.forge_type {
|
let forge = match details.forge.forge_type {
|
||||||
#[cfg(feature = "forgejo")]
|
#[cfg(feature = "forgejo")]
|
||||||
crate::server::config::ForgeType::ForgeJo => {
|
crate::server::config::ForgeType::ForgeJo => {
|
||||||
|
@ -46,8 +42,8 @@ impl RepoActor {
|
||||||
};
|
};
|
||||||
debug!(?forge, "new");
|
debug!(?forge, "new");
|
||||||
Self {
|
Self {
|
||||||
generation,
|
|
||||||
message_token: MessageToken::new(),
|
message_token: MessageToken::new(),
|
||||||
|
span,
|
||||||
details,
|
details,
|
||||||
webhook,
|
webhook,
|
||||||
webhook_id: None,
|
webhook_id: None,
|
||||||
|
@ -62,14 +58,13 @@ impl RepoActor {
|
||||||
}
|
}
|
||||||
impl Actor for RepoActor {
|
impl Actor for RepoActor {
|
||||||
type Context = Context<Self>;
|
type Context = Context<Self>;
|
||||||
#[tracing::instrument(name = "RepoActor::stopping", skip_all)]
|
|
||||||
fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
|
fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
|
||||||
|
let _gaurd = self.span.enter();
|
||||||
info!("Checking webhook");
|
info!("Checking webhook");
|
||||||
match self.webhook_id.take() {
|
match self.webhook_id.take() {
|
||||||
Some(webhook_id) => {
|
Some(webhook_id) => {
|
||||||
let repo_details = self.details.clone();
|
let repo_details = self.details.clone();
|
||||||
let net = self.net.clone();
|
let net = self.net.clone();
|
||||||
info!(%webhook_id, "Unregistring webhook");
|
|
||||||
webhook::unregister(webhook_id, repo_details, net)
|
webhook::unregister(webhook_id, repo_details, net)
|
||||||
.in_current_span()
|
.in_current_span()
|
||||||
.into_actor(self)
|
.into_actor(self)
|
||||||
|
@ -84,8 +79,8 @@ impl std::fmt::Display for RepoActor {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{}:{}:{}",
|
"{}/{}",
|
||||||
self.generation, self.details.forge.forge_name, self.details.repo_alias
|
self.details.forge.forge_name, self.details.repo_alias
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,198 +0,0 @@
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use actix::prelude::*;
|
|
||||||
|
|
||||||
use kxio::{fs::FileSystem, network::Network};
|
|
||||||
use tracing::{error, info};
|
|
||||||
|
|
||||||
use crate::server::{
|
|
||||||
actors::{
|
|
||||||
repo::{CloneRepo, RepoActor},
|
|
||||||
webhook::{AddWebhookRecipient, WebhookActor, WebhookRouter},
|
|
||||||
},
|
|
||||||
config::{
|
|
||||||
ForgeConfig, ForgeName, GitDir, RepoAlias, RepoDetails, ServerConfig, ServerRepoConfig,
|
|
||||||
ServerStorage, Webhook,
|
|
||||||
},
|
|
||||||
types::ServerGeneration,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, derive_more::Display, derive_more::From)]
|
|
||||||
pub enum Error {
|
|
||||||
#[display("Failed to create data directories")]
|
|
||||||
FailedToCreateDataDirectory(kxio::fs::Error),
|
|
||||||
|
|
||||||
#[display("The forge data path is not a directory: {path:?}")]
|
|
||||||
ForgeDirIsNotDirectory {
|
|
||||||
path: PathBuf,
|
|
||||||
},
|
|
||||||
|
|
||||||
Config(crate::server::config::Error),
|
|
||||||
|
|
||||||
Io(std::io::Error),
|
|
||||||
}
|
|
||||||
type Result<T> = core::result::Result<T, Error>;
|
|
||||||
|
|
||||||
pub struct Server {
|
|
||||||
generation: ServerGeneration,
|
|
||||||
fs: FileSystem,
|
|
||||||
net: Network,
|
|
||||||
}
|
|
||||||
impl Actor for Server {
|
|
||||||
type Context = Context<Self>;
|
|
||||||
}
|
|
||||||
impl Handler<ServerConfig> for Server {
|
|
||||||
type Result = ();
|
|
||||||
|
|
||||||
#[allow(clippy::cognitive_complexity)] // TODO: (#75) reduce complexity
|
|
||||||
fn handle(&mut self, msg: ServerConfig, _ctx: &mut Self::Context) -> Self::Result {
|
|
||||||
let server_config = msg;
|
|
||||||
// Server Storage
|
|
||||||
let dir = server_config.storage().path();
|
|
||||||
if !dir.exists() {
|
|
||||||
info!(?dir, "server storage doesn't exist - creating it");
|
|
||||||
if let Err(err) = self.fs.dir_create(dir) {
|
|
||||||
error!(?err, ?dir, "Failed to create server storage");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let Ok(canon) = dir.canonicalize() else {
|
|
||||||
error!(?dir, "Failed to confirm server storage");
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
info!(dir = ?canon, "server storage");
|
|
||||||
|
|
||||||
// Forge directories in Server Storage
|
|
||||||
let server_storage = server_config.storage();
|
|
||||||
if let Err(err) = self.create_forge_data_directories(&server_config, dir) {
|
|
||||||
error!(?err, "Failure creating forge storage");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Webhook Server
|
|
||||||
info!("Starting Webhook Server...");
|
|
||||||
let webhook_router = WebhookRouter::new().start();
|
|
||||||
let webhook = server_config.webhook();
|
|
||||||
|
|
||||||
// Forge Actors
|
|
||||||
for (forge_name, forge_config) in server_config.forges() {
|
|
||||||
self.create_forge_repos(forge_config, forge_name.clone(), server_storage, webhook)
|
|
||||||
.into_iter()
|
|
||||||
.map(|a| self.start_actor(a))
|
|
||||||
.map(|(alias, addr)| AddWebhookRecipient(alias, addr.recipient()))
|
|
||||||
.for_each(|msg| webhook_router.do_send(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
WebhookActor::new(webhook_router.recipient()).start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Server {
|
|
||||||
pub const fn new(generation: ServerGeneration, fs: FileSystem, net: Network) -> Self {
|
|
||||||
Self {
|
|
||||||
generation,
|
|
||||||
fs,
|
|
||||||
net,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn create_forge_data_directories(
|
|
||||||
&self,
|
|
||||||
server_config: &ServerConfig,
|
|
||||||
server_dir: &std::path::Path,
|
|
||||||
) -> Result<()> {
|
|
||||||
for (forge_name, _forge_config) in server_config.forges() {
|
|
||||||
let forge_dir: PathBuf = (&forge_name).into();
|
|
||||||
let path = server_dir.join(&forge_dir);
|
|
||||||
if self.fs.path_exists(&path)? {
|
|
||||||
if !self.fs.path_is_dir(&path)? {
|
|
||||||
return Err(Error::ForgeDirIsNotDirectory { path });
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
info!(%forge_name, ?path, "creating storage");
|
|
||||||
self.fs.dir_create_all(&path)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_forge_repos(
|
|
||||||
&self,
|
|
||||||
forge_config: &ForgeConfig,
|
|
||||||
forge_name: ForgeName,
|
|
||||||
server_storage: &ServerStorage,
|
|
||||||
webhook: &Webhook,
|
|
||||||
) -> Vec<(ForgeName, RepoAlias, RepoActor)> {
|
|
||||||
let span =
|
|
||||||
tracing::info_span!("create_forge_repos", name = %forge_name, config = %forge_config);
|
|
||||||
|
|
||||||
let _guard = span.enter();
|
|
||||||
info!("Creating Forge");
|
|
||||||
let mut repos = vec![];
|
|
||||||
let creator = self.create_actor(forge_name, forge_config.clone(), server_storage, webhook);
|
|
||||||
for (repo_alias, server_repo_config) in forge_config.repos() {
|
|
||||||
let forge_repo = creator((repo_alias, server_repo_config));
|
|
||||||
info!(
|
|
||||||
alias = %forge_repo.1,
|
|
||||||
"Created Repo"
|
|
||||||
);
|
|
||||||
repos.push(forge_repo);
|
|
||||||
}
|
|
||||||
repos
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_actor(
|
|
||||||
&self,
|
|
||||||
forge_name: ForgeName,
|
|
||||||
forge_config: ForgeConfig,
|
|
||||||
server_storage: &ServerStorage,
|
|
||||||
webhook: &Webhook,
|
|
||||||
) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeName, RepoAlias, RepoActor) {
|
|
||||||
let server_storage = server_storage.clone();
|
|
||||||
let webhook = webhook.clone();
|
|
||||||
let net = self.net.clone();
|
|
||||||
let generation = self.generation;
|
|
||||||
move |(repo_alias, server_repo_config)| {
|
|
||||||
let span = tracing::info_span!("create_actor", alias = %repo_alias, config = %server_repo_config);
|
|
||||||
let _guard = span.enter();
|
|
||||||
info!("Creating Repo");
|
|
||||||
let gitdir = server_repo_config.gitdir().map_or_else(
|
|
||||||
|| {
|
|
||||||
GitDir::from(
|
|
||||||
server_storage
|
|
||||||
.path()
|
|
||||||
.join(forge_name.to_string())
|
|
||||||
.join(repo_alias.to_string()),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|gitdir| gitdir,
|
|
||||||
);
|
|
||||||
// INFO: can't canonicalise gitdir as the path needs to exist to do that and we may not
|
|
||||||
// have cloned the repo yet
|
|
||||||
let repo_details = RepoDetails::new(
|
|
||||||
generation,
|
|
||||||
&repo_alias,
|
|
||||||
server_repo_config,
|
|
||||||
&forge_name,
|
|
||||||
&forge_config,
|
|
||||||
gitdir,
|
|
||||||
);
|
|
||||||
info!("Starting Repo Actor");
|
|
||||||
let actor = RepoActor::new(repo_details, webhook.clone(), generation, net.clone());
|
|
||||||
(forge_name.clone(), repo_alias, actor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn start_actor(
|
|
||||||
&self,
|
|
||||||
actor: (ForgeName, RepoAlias, RepoActor),
|
|
||||||
) -> (RepoAlias, Addr<RepoActor>) {
|
|
||||||
let (forge_name, repo_alias, actor) = actor;
|
|
||||||
let span = tracing::info_span!("start_actor", forge = %forge_name, repo = %repo_alias);
|
|
||||||
let _guard = span.enter();
|
|
||||||
info!("Starting");
|
|
||||||
let addr = actor.start();
|
|
||||||
addr.do_send(CloneRepo);
|
|
||||||
info!("Started");
|
|
||||||
(repo_alias, addr)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,3 @@
|
||||||
use actix::prelude::*;
|
|
||||||
|
|
||||||
pub mod load;
|
pub mod load;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -14,8 +12,6 @@ use serde::Deserialize;
|
||||||
use kxio::fs::FileSystem;
|
use kxio::fs::FileSystem;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
use crate::server::types::ServerGeneration;
|
|
||||||
|
|
||||||
#[derive(Debug, derive_more::From, derive_more::Display)]
|
#[derive(Debug, derive_more::From, derive_more::Display)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
Io(std::io::Error),
|
Io(std::io::Error),
|
||||||
|
@ -27,8 +23,7 @@ impl std::error::Error for Error {}
|
||||||
type Result<T> = core::result::Result<T, Error>;
|
type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
/// Mapped from the `git-next-server.toml` file
|
/// Mapped from the `git-next-server.toml` file
|
||||||
#[derive(Debug, PartialEq, Eq, Deserialize, Message)]
|
#[derive(Debug, PartialEq, Eq, Deserialize)]
|
||||||
#[rtype(result = "()")]
|
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
webhook: Webhook,
|
webhook: Webhook,
|
||||||
storage: ServerStorage,
|
storage: ServerStorage,
|
||||||
|
@ -361,7 +356,6 @@ impl Deref for BranchName {
|
||||||
/// The derived information about a repo, used to interact with it
|
/// The derived information about a repo, used to interact with it
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct RepoDetails {
|
pub struct RepoDetails {
|
||||||
pub generation: ServerGeneration,
|
|
||||||
pub repo_alias: RepoAlias,
|
pub repo_alias: RepoAlias,
|
||||||
pub repo_path: RepoPath,
|
pub repo_path: RepoPath,
|
||||||
pub branch: BranchName,
|
pub branch: BranchName,
|
||||||
|
@ -371,7 +365,6 @@ pub struct RepoDetails {
|
||||||
}
|
}
|
||||||
impl RepoDetails {
|
impl RepoDetails {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
generation: ServerGeneration,
|
|
||||||
repo_alias: &RepoAlias,
|
repo_alias: &RepoAlias,
|
||||||
server_repo_config: &ServerRepoConfig,
|
server_repo_config: &ServerRepoConfig,
|
||||||
forge_name: &ForgeName,
|
forge_name: &ForgeName,
|
||||||
|
@ -379,7 +372,6 @@ impl RepoDetails {
|
||||||
gitdir: GitDir,
|
gitdir: GitDir,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
generation,
|
|
||||||
repo_alias: repo_alias.clone(),
|
repo_alias: repo_alias.clone(),
|
||||||
repo_path: RepoPath(server_repo_config.repo.clone()),
|
repo_path: RepoPath(server_repo_config.repo.clone()),
|
||||||
repo_config: server_repo_config.repo_config(),
|
repo_config: server_repo_config.repo_config(),
|
||||||
|
@ -419,8 +411,7 @@ impl Display for RepoDetails {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"gen-{}:{}:{}/{}:{}@{}/{}@{}",
|
"{}:{}/{}:{}@{}/{}@{}",
|
||||||
self.generation,
|
|
||||||
self.forge.forge_type,
|
self.forge.forge_type,
|
||||||
self.forge.forge_name,
|
self.forge.forge_name,
|
||||||
self.repo_alias,
|
self.repo_alias,
|
||||||
|
|
|
@ -157,7 +157,6 @@ fn repo_details_find_default_push_remote_finds_correct_remote() -> Result<()> {
|
||||||
let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?;
|
let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?;
|
||||||
let mut repo_details = common::repo_details(
|
let mut repo_details = common::repo_details(
|
||||||
1,
|
1,
|
||||||
ServerGeneration::new(),
|
|
||||||
common::forge_details(1, ForgeType::MockForge),
|
common::forge_details(1, ForgeType::MockForge),
|
||||||
None,
|
None,
|
||||||
GitDir::new(&cwd), // Server GitDir - should be ignored
|
GitDir::new(&cwd), // Server GitDir - should be ignored
|
||||||
|
@ -181,7 +180,6 @@ fn gitdir_validate_should_pass_a_valid_git_repo() -> Result<()> {
|
||||||
let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?;
|
let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?;
|
||||||
let mut repo_details = common::repo_details(
|
let mut repo_details = common::repo_details(
|
||||||
1,
|
1,
|
||||||
ServerGeneration::new(),
|
|
||||||
common::forge_details(1, ForgeType::MockForge),
|
common::forge_details(1, ForgeType::MockForge),
|
||||||
None,
|
None,
|
||||||
GitDir::new(&cwd), // Server GitDir - should be ignored
|
GitDir::new(&cwd), // Server GitDir - should be ignored
|
||||||
|
@ -200,7 +198,6 @@ fn gitdir_validate_should_fail_a_non_git_dir() {
|
||||||
let cwd = fs.base();
|
let cwd = fs.base();
|
||||||
let repo_details = common::repo_details(
|
let repo_details = common::repo_details(
|
||||||
1,
|
1,
|
||||||
ServerGeneration::new(),
|
|
||||||
common::forge_details(1, ForgeType::MockForge),
|
common::forge_details(1, ForgeType::MockForge),
|
||||||
None,
|
None,
|
||||||
GitDir::new(cwd), // Server GitDir - should be ignored
|
GitDir::new(cwd), // Server GitDir - should be ignored
|
||||||
|
@ -214,7 +211,6 @@ fn gitdir_validate_should_fail_a_git_repo_with_wrong_remote() {
|
||||||
let_assert!(Ok(cwd) = std::env::current_dir().map_err(RepoValidationError::Io));
|
let_assert!(Ok(cwd) = std::env::current_dir().map_err(RepoValidationError::Io));
|
||||||
let mut repo_details = common::repo_details(
|
let mut repo_details = common::repo_details(
|
||||||
1,
|
1,
|
||||||
ServerGeneration::new(),
|
|
||||||
common::forge_details(1, ForgeType::MockForge),
|
common::forge_details(1, ForgeType::MockForge),
|
||||||
None,
|
None,
|
||||||
GitDir::new(&cwd), // Server GitDir - should be ignored
|
GitDir::new(&cwd), // Server GitDir - should be ignored
|
||||||
|
|
|
@ -51,17 +51,6 @@ pub async fn validate_positions(
|
||||||
);
|
);
|
||||||
return Err(Error::BranchHasNoCommits(repo_config.branches().main()));
|
return Err(Error::BranchHasNoCommits(repo_config.branches().main()));
|
||||||
};
|
};
|
||||||
// Dev must be on main branch, or user must rebase it
|
|
||||||
let dev_has_main = commit_histories.dev.iter().any(|commit| commit == &main);
|
|
||||||
if !dev_has_main {
|
|
||||||
warn!(
|
|
||||||
"Dev branch '{}' is not based on main branch '{}' - user should rebase onto main branch '{}'",
|
|
||||||
repo_config.branches().dev(),
|
|
||||||
repo_config.branches().main(),
|
|
||||||
repo_config.branches().main(),
|
|
||||||
);
|
|
||||||
return Err(Error::DevBranchNotBasedOn(repo_config.branches().main()));
|
|
||||||
}
|
|
||||||
// verify that next is an ancestor of dev, and force update to it main if it isn't
|
// verify that next is an ancestor of dev, and force update to it main if it isn't
|
||||||
let Some(next) = commit_histories.next.first().cloned() else {
|
let Some(next) = commit_histories.next.first().cloned() else {
|
||||||
warn!(
|
warn!(
|
||||||
|
@ -130,7 +119,16 @@ pub async fn validate_positions(
|
||||||
);
|
);
|
||||||
return Err(Error::DevBranchNotBasedOn(repo_config.branches().next())); // dev is not based on next
|
return Err(Error::DevBranchNotBasedOn(repo_config.branches().next())); // dev is not based on next
|
||||||
}
|
}
|
||||||
|
let dev_has_main = commit_histories.dev.iter().any(|commit| commit == &main);
|
||||||
|
if !dev_has_main {
|
||||||
|
warn!(
|
||||||
|
"Dev branch '{}' is not based on main branch '{}' - user should rebase onto main branch '{}'",
|
||||||
|
repo_config.branches().dev(),
|
||||||
|
repo_config.branches().main(),
|
||||||
|
repo_config.branches().main(),
|
||||||
|
);
|
||||||
|
return Err(Error::DevBranchNotBasedOn(repo_config.branches().main()));
|
||||||
|
}
|
||||||
let Some(dev) = commit_histories.dev.first().cloned() else {
|
let Some(dev) = commit_histories.dev.first().cloned() else {
|
||||||
warn!(
|
warn!(
|
||||||
"No commits on dev branch '{}'",
|
"No commits on dev branch '{}'",
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
use crate::server::{
|
use crate::server::config::{
|
||||||
config::{
|
|
||||||
ApiToken, BranchName, ForgeDetails, ForgeName, ForgeType, GitDir, Hostname, RepoAlias,
|
ApiToken, BranchName, ForgeDetails, ForgeName, ForgeType, GitDir, Hostname, RepoAlias,
|
||||||
RepoBranches, RepoConfig, RepoDetails, RepoPath, User,
|
RepoBranches, RepoConfig, RepoDetails, RepoPath, User,
|
||||||
},
|
|
||||||
types::ServerGeneration,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn forge_details(n: u32, forge_type: ForgeType) -> ForgeDetails {
|
pub fn forge_details(n: u32, forge_type: ForgeType) -> ForgeDetails {
|
||||||
|
@ -33,13 +30,11 @@ pub fn forge_name(n: u32) -> ForgeName {
|
||||||
}
|
}
|
||||||
pub fn repo_details(
|
pub fn repo_details(
|
||||||
n: u32,
|
n: u32,
|
||||||
generation: ServerGeneration,
|
|
||||||
forge: ForgeDetails,
|
forge: ForgeDetails,
|
||||||
repo_config: Option<RepoConfig>,
|
repo_config: Option<RepoConfig>,
|
||||||
gitdir: GitDir,
|
gitdir: GitDir,
|
||||||
) -> RepoDetails {
|
) -> RepoDetails {
|
||||||
RepoDetails {
|
RepoDetails {
|
||||||
generation,
|
|
||||||
repo_alias: repo_alias(n),
|
repo_alias: repo_alias(n),
|
||||||
repo_path: repo_path(n),
|
repo_path: repo_path(n),
|
||||||
gitdir,
|
gitdir,
|
||||||
|
|
|
@ -2,10 +2,7 @@ use assert2::let_assert;
|
||||||
|
|
||||||
use kxio::network::{MockNetwork, StatusCode};
|
use kxio::network::{MockNetwork, StatusCode};
|
||||||
|
|
||||||
use crate::server::{
|
use crate::server::config::{BranchName, ForgeType};
|
||||||
config::{BranchName, ForgeType},
|
|
||||||
types::ServerGeneration,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -17,7 +14,6 @@ fn test_name() {
|
||||||
let net = Network::new_mock();
|
let net = Network::new_mock();
|
||||||
let repo_details = common::repo_details(
|
let repo_details = common::repo_details(
|
||||||
1,
|
1,
|
||||||
ServerGeneration::new(),
|
|
||||||
common::forge_details(1, ForgeType::MockForge),
|
common::forge_details(1, ForgeType::MockForge),
|
||||||
Some(common::repo_config(1)),
|
Some(common::repo_config(1)),
|
||||||
GitDir::new(fs.base()),
|
GitDir::new(fs.base()),
|
||||||
|
@ -44,7 +40,6 @@ async fn test_branches_get() {
|
||||||
|
|
||||||
let repo_details = common::repo_details(
|
let repo_details = common::repo_details(
|
||||||
1,
|
1,
|
||||||
ServerGeneration::new(),
|
|
||||||
common::forge_details(1, ForgeType::MockForge),
|
common::forge_details(1, ForgeType::MockForge),
|
||||||
Some(common::repo_config(1)),
|
Some(common::repo_config(1)),
|
||||||
GitDir::new(fs.base()),
|
GitDir::new(fs.base()),
|
||||||
|
|
|
@ -13,9 +13,30 @@ use tracing::{error, info, level_filters::LevelFilter};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
fs::FileSystem,
|
fs::FileSystem,
|
||||||
server::{actors::server::Server, types::ServerGeneration},
|
server::{
|
||||||
|
actors::webhook,
|
||||||
|
config::{ForgeConfig, ForgeName, GitDir, RepoAlias, ServerStorage, Webhook},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use self::{actors::repo::RepoActor, config::ServerRepoConfig};
|
||||||
|
|
||||||
|
#[derive(Debug, derive_more::Display, derive_more::From)]
|
||||||
|
pub enum Error {
|
||||||
|
#[display("Failed to create data directories")]
|
||||||
|
FailedToCreateDataDirectory(kxio::fs::Error),
|
||||||
|
|
||||||
|
#[display("The forge data path is not a directory: {path:?}")]
|
||||||
|
ForgeDirIsNotDirectory {
|
||||||
|
path: PathBuf,
|
||||||
|
},
|
||||||
|
|
||||||
|
Config(crate::server::config::Error),
|
||||||
|
|
||||||
|
Io(std::io::Error),
|
||||||
|
}
|
||||||
|
type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
pub fn init(fs: FileSystem) {
|
pub fn init(fs: FileSystem) {
|
||||||
let file_name = "git-next-server.toml";
|
let file_name = "git-next-server.toml";
|
||||||
let pathbuf = PathBuf::from(file_name);
|
let pathbuf = PathBuf::from(file_name);
|
||||||
|
@ -40,10 +61,6 @@ pub fn init(fs: FileSystem) {
|
||||||
|
|
||||||
pub async fn start(fs: FileSystem, net: Network) {
|
pub async fn start(fs: FileSystem, net: Network) {
|
||||||
init_logging();
|
init_logging();
|
||||||
let generation = ServerGeneration::new();
|
|
||||||
{
|
|
||||||
let span = tracing::info_span!("Server", %generation);
|
|
||||||
let _guard = span.enter();
|
|
||||||
info!("Starting Server...");
|
info!("Starting Server...");
|
||||||
let server_config = match config::ServerConfig::load(&fs) {
|
let server_config = match config::ServerConfig::load(&fs) {
|
||||||
Ok(server_config) => server_config,
|
Ok(server_config) => server_config,
|
||||||
|
@ -52,14 +69,158 @@ pub async fn start(fs: FileSystem, net: Network) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
// Server Storage
|
||||||
|
let dir = server_config.storage().path();
|
||||||
|
if !dir.exists() {
|
||||||
|
info!(?dir, "server storage doesn't exist - creating it");
|
||||||
|
if let Err(err) = fs.dir_create(dir) {
|
||||||
|
error!(?err, ?dir, "Failed to create server storage");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let Ok(canon) = dir.canonicalize() else {
|
||||||
|
error!(?dir, "Failed to confirm servier storage");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
info!(dir = ?canon, "server storage");
|
||||||
|
|
||||||
let server = Server::new(generation, fs.clone(), net.clone()).start();
|
// Forge directories in Server Storage
|
||||||
server.do_send(server_config);
|
let server_storage = server_config.storage();
|
||||||
|
if let Err(err) = create_forge_data_directories(&server_config, &fs, &dir) {
|
||||||
|
error!(?err, "Failure creating forge storage");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Webhook Server
|
||||||
|
info!("Starting Webhook Server...");
|
||||||
|
let webhook_router = webhook::WebhookRouter::new().start();
|
||||||
|
let webhook = server_config.webhook();
|
||||||
|
|
||||||
|
// Forge Actors
|
||||||
|
for (forge_name, forge_config) in server_config.forges() {
|
||||||
|
create_forge_repos(
|
||||||
|
forge_config,
|
||||||
|
forge_name.clone(),
|
||||||
|
server_storage,
|
||||||
|
webhook,
|
||||||
|
&net,
|
||||||
|
)
|
||||||
|
.into_iter()
|
||||||
|
.map(start_actor)
|
||||||
|
.map(|(alias, addr)| webhook::AddWebhookRecipient(alias, addr.recipient()))
|
||||||
|
.for_each(|msg| webhook_router.do_send(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
let webhook_server = webhook::WebhookActor::new(webhook_router.recipient()).start();
|
||||||
info!("Server running - Press Ctrl-C to stop...");
|
info!("Server running - Press Ctrl-C to stop...");
|
||||||
let _ = actix_rt::signal::ctrl_c().await;
|
let _ = actix_rt::signal::ctrl_c().await;
|
||||||
info!("Ctrl-C received, shutting down...");
|
info!("Ctrl-C received, shutting down...");
|
||||||
|
drop(webhook_server);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_forge_data_directories(
|
||||||
|
server_config: &config::ServerConfig,
|
||||||
|
fs: &FileSystem,
|
||||||
|
server_dir: &&std::path::Path,
|
||||||
|
) -> Result<()> {
|
||||||
|
for (forge_name, _forge_config) in server_config.forges() {
|
||||||
|
let forge_dir: PathBuf = (&forge_name).into();
|
||||||
|
let path = server_dir.join(&forge_dir);
|
||||||
|
if fs.path_exists(&path)? {
|
||||||
|
if !fs.path_is_dir(&path)? {
|
||||||
|
return Err(Error::ForgeDirIsNotDirectory { path });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
info!(%forge_name, ?path, "creating storage");
|
||||||
|
fs.dir_create_all(&path)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_forge_repos(
|
||||||
|
forge_config: &ForgeConfig,
|
||||||
|
forge_name: ForgeName,
|
||||||
|
server_storage: &ServerStorage,
|
||||||
|
webhook: &Webhook,
|
||||||
|
net: &Network,
|
||||||
|
) -> Vec<(ForgeName, RepoAlias, RepoActor)> {
|
||||||
|
let span =
|
||||||
|
tracing::info_span!("create_forge_repos", name = %forge_name, config = %forge_config);
|
||||||
|
let _guard = span.enter();
|
||||||
|
info!("Creating Forge");
|
||||||
|
let mut repos = vec![];
|
||||||
|
let creator = create_actor(
|
||||||
|
forge_name,
|
||||||
|
forge_config.clone(),
|
||||||
|
server_storage,
|
||||||
|
webhook,
|
||||||
|
net,
|
||||||
|
);
|
||||||
|
for (repo_alias, server_repo_config) in forge_config.repos() {
|
||||||
|
let forge_repo = creator((repo_alias, server_repo_config));
|
||||||
|
info!(
|
||||||
|
alias = %forge_repo.1,
|
||||||
|
"Created Repo"
|
||||||
|
);
|
||||||
|
repos.push(forge_repo);
|
||||||
|
}
|
||||||
|
repos
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_actor(
|
||||||
|
forge_name: ForgeName,
|
||||||
|
forge_config: config::ForgeConfig,
|
||||||
|
server_storage: &ServerStorage,
|
||||||
|
webhook: &Webhook,
|
||||||
|
net: &Network,
|
||||||
|
) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeName, RepoAlias, RepoActor) {
|
||||||
|
let server_storage = server_storage.clone();
|
||||||
|
let webhook = webhook.clone();
|
||||||
|
let net = net.clone();
|
||||||
|
move |(repo_alias, server_repo_config)| {
|
||||||
|
let span =
|
||||||
|
tracing::info_span!("create_actor", alias = %repo_alias, config = %server_repo_config);
|
||||||
|
let _guard = span.enter();
|
||||||
|
info!("Creating Repo");
|
||||||
|
let gitdir = server_repo_config.gitdir().map_or_else(
|
||||||
|
|| {
|
||||||
|
GitDir::from(
|
||||||
|
server_storage
|
||||||
|
.path()
|
||||||
|
.join(forge_name.to_string())
|
||||||
|
.join(repo_alias.to_string()),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|gitdir| gitdir,
|
||||||
|
);
|
||||||
|
// INFO: can't canonicalise gitdir as the path needs to exist to do that and we may not
|
||||||
|
// have cloned the repo yet
|
||||||
|
let repo_details = config::RepoDetails::new(
|
||||||
|
&repo_alias,
|
||||||
|
server_repo_config,
|
||||||
|
&forge_name,
|
||||||
|
&forge_config,
|
||||||
|
gitdir,
|
||||||
|
);
|
||||||
|
info!("Starting Repo Actor");
|
||||||
|
let actor = actors::repo::RepoActor::new(repo_details, webhook.clone(), net.clone());
|
||||||
|
(forge_name.clone(), repo_alias, actor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_actor(
|
||||||
|
actor: (ForgeName, RepoAlias, actors::repo::RepoActor),
|
||||||
|
) -> (RepoAlias, Addr<actors::repo::RepoActor>) {
|
||||||
|
let (forge_name, repo_alias, actor) = actor;
|
||||||
|
let span = tracing::info_span!("start_actor", forge = %forge_name, repo = %repo_alias);
|
||||||
|
let _guard = span.enter();
|
||||||
|
info!("Starting");
|
||||||
|
let addr = actor.start();
|
||||||
|
addr.do_send(actors::repo::CloneRepo);
|
||||||
|
info!("Started");
|
||||||
|
(repo_alias, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_logging() {
|
pub fn init_logging() {
|
||||||
|
|
|
@ -35,19 +35,3 @@ impl std::fmt::Display for MessageToken {
|
||||||
write!(f, "{}", self.0)
|
write!(f, "{}", self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
||||||
pub struct ServerGeneration(u32);
|
|
||||||
impl ServerGeneration {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self::default()
|
|
||||||
}
|
|
||||||
pub const fn next(&self) -> Self {
|
|
||||||
Self(self.0 + 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl std::fmt::Display for ServerGeneration {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "{}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue