Compare commits

...

5 commits

Author SHA1 Message Date
7e79f4877a fix(server): don't try to reset next when dev is not based on main
Some checks are pending
ci/woodpecker/push/cron-docker-builder Pipeline is pending
ci/woodpecker/push/push-next Pipeline is pending
ci/woodpecker/push/tag-created Pipeline is pending
2024-05-07 18:59:51 +01:00
b1638a65fc feat(server): give Server an incremental generation counter
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
2024-05-07 18:59:51 +01:00
9b211179bf feat(server): extract Server actor
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
2024-05-07 18:59:51 +01:00
96994f2390 chore: fix typo
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
2024-05-07 18:59:51 +01:00
Renovate Bot
60f91b91df chore(deps): update docker.io/woodpeckerci/plugin-docker-buildx docker tag to v4
All checks were successful
ci/woodpecker/pr/cron-docker-builder Pipeline was successful
ci/woodpecker/pr/push-next Pipeline was successful
ci/woodpecker/pr/tag-created Pipeline was successful
ci/woodpecker/pull_request_closed/cron-docker-builder Pipeline was successful
ci/woodpecker/pull_request_closed/push-next Pipeline was successful
ci/woodpecker/pull_request_closed/tag-created 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 was successful
2024-05-07 10:47:30 +00:00
11 changed files with 285 additions and 201 deletions

View file

@ -18,7 +18,7 @@ steps:
- event: tag
ref: refs/tags/v*
# INFO: https://woodpecker-ci.org/plugins/Docker%20Buildx
image: docker.io/woodpeckerci/plugin-docker-buildx:3.2.1
image: docker.io/woodpeckerci/plugin-docker-buildx:4.0.0
settings:
username: kemitix
repo: git.kemitix.net/kemitix/git-next

View file

@ -1,2 +1,3 @@
pub mod repo;
pub mod server;
pub mod webhook;

View file

@ -11,14 +11,14 @@ use crate::server::{
actors::repo::webhook::WebhookAuth,
config::{RepoConfig, RepoDetails, Webhook},
gitforge,
types::MessageToken,
types::{MessageToken, ServerGeneration},
};
use self::webhook::WebhookId;
pub struct RepoActor {
generation: ServerGeneration,
message_token: MessageToken,
span: tracing::Span,
details: RepoDetails,
webhook: Webhook,
webhook_id: Option<WebhookId>, // INFO: if [None] then no webhook is configured
@ -30,8 +30,12 @@ pub struct RepoActor {
forge: gitforge::Forge,
}
impl RepoActor {
pub(crate) fn new(details: RepoDetails, webhook: Webhook, net: Network) -> Self {
let span = tracing::info_span!("RepoActor", repo = %details);
pub(crate) fn new(
details: RepoDetails,
webhook: Webhook,
generation: ServerGeneration,
net: Network,
) -> Self {
let forge = match details.forge.forge_type {
#[cfg(feature = "forgejo")]
crate::server::config::ForgeType::ForgeJo => {
@ -42,8 +46,8 @@ impl RepoActor {
};
debug!(?forge, "new");
Self {
generation,
message_token: MessageToken::new(),
span,
details,
webhook,
webhook_id: None,
@ -58,13 +62,14 @@ impl RepoActor {
}
impl Actor for RepoActor {
type Context = Context<Self>;
#[tracing::instrument(name = "RepoActor::stopping", skip_all)]
fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
let _gaurd = self.span.enter();
info!("Checking webhook");
match self.webhook_id.take() {
Some(webhook_id) => {
let repo_details = self.details.clone();
let net = self.net.clone();
info!(%webhook_id, "Unregistring webhook");
webhook::unregister(webhook_id, repo_details, net)
.in_current_span()
.into_actor(self)
@ -79,8 +84,8 @@ impl std::fmt::Display for RepoActor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}/{}",
self.details.forge.forge_name, self.details.repo_alias
"{}:{}:{}",
self.generation, self.details.forge.forge_name, self.details.repo_alias
)
}
}

198
src/server/actors/server.rs Normal file
View file

@ -0,0 +1,198 @@
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)
}
}

View file

@ -1,3 +1,5 @@
use actix::prelude::*;
pub mod load;
use std::{
@ -12,6 +14,8 @@ use serde::Deserialize;
use kxio::fs::FileSystem;
use tracing::info;
use crate::server::types::ServerGeneration;
#[derive(Debug, derive_more::From, derive_more::Display)]
pub enum Error {
Io(std::io::Error),
@ -23,7 +27,8 @@ impl std::error::Error for Error {}
type Result<T> = core::result::Result<T, Error>;
/// Mapped from the `git-next-server.toml` file
#[derive(Debug, PartialEq, Eq, Deserialize)]
#[derive(Debug, PartialEq, Eq, Deserialize, Message)]
#[rtype(result = "()")]
pub struct ServerConfig {
webhook: Webhook,
storage: ServerStorage,
@ -356,6 +361,7 @@ impl Deref for BranchName {
/// The derived information about a repo, used to interact with it
#[derive(Clone, Debug)]
pub struct RepoDetails {
pub generation: ServerGeneration,
pub repo_alias: RepoAlias,
pub repo_path: RepoPath,
pub branch: BranchName,
@ -365,6 +371,7 @@ pub struct RepoDetails {
}
impl RepoDetails {
pub fn new(
generation: ServerGeneration,
repo_alias: &RepoAlias,
server_repo_config: &ServerRepoConfig,
forge_name: &ForgeName,
@ -372,6 +379,7 @@ impl RepoDetails {
gitdir: GitDir,
) -> Self {
Self {
generation,
repo_alias: repo_alias.clone(),
repo_path: RepoPath(server_repo_config.repo.clone()),
repo_config: server_repo_config.repo_config(),
@ -411,7 +419,8 @@ impl Display for RepoDetails {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}:{}/{}:{}@{}/{}@{}",
"gen-{}:{}:{}/{}:{}@{}/{}@{}",
self.generation,
self.forge.forge_type,
self.forge.forge_name,
self.repo_alias,

View file

@ -157,6 +157,7 @@ fn repo_details_find_default_push_remote_finds_correct_remote() -> Result<()> {
let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?;
let mut repo_details = common::repo_details(
1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge),
None,
GitDir::new(&cwd), // Server GitDir - should be ignored
@ -180,6 +181,7 @@ fn gitdir_validate_should_pass_a_valid_git_repo() -> Result<()> {
let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?;
let mut repo_details = common::repo_details(
1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge),
None,
GitDir::new(&cwd), // Server GitDir - should be ignored
@ -198,6 +200,7 @@ fn gitdir_validate_should_fail_a_non_git_dir() {
let cwd = fs.base();
let repo_details = common::repo_details(
1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge),
None,
GitDir::new(cwd), // Server GitDir - should be ignored
@ -211,6 +214,7 @@ fn gitdir_validate_should_fail_a_git_repo_with_wrong_remote() {
let_assert!(Ok(cwd) = std::env::current_dir().map_err(RepoValidationError::Io));
let mut repo_details = common::repo_details(
1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge),
None,
GitDir::new(&cwd), // Server GitDir - should be ignored

View file

@ -51,6 +51,17 @@ pub async fn validate_positions(
);
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
let Some(next) = commit_histories.next.first().cloned() else {
warn!(
@ -119,16 +130,7 @@ pub async fn validate_positions(
);
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 {
warn!(
"No commits on dev branch '{}'",

View file

@ -1,6 +1,9 @@
use crate::server::config::{
ApiToken, BranchName, ForgeDetails, ForgeName, ForgeType, GitDir, Hostname, RepoAlias,
RepoBranches, RepoConfig, RepoDetails, RepoPath, User,
use crate::server::{
config::{
ApiToken, BranchName, ForgeDetails, ForgeName, ForgeType, GitDir, Hostname, RepoAlias,
RepoBranches, RepoConfig, RepoDetails, RepoPath, User,
},
types::ServerGeneration,
};
pub fn forge_details(n: u32, forge_type: ForgeType) -> ForgeDetails {
@ -30,11 +33,13 @@ pub fn forge_name(n: u32) -> ForgeName {
}
pub fn repo_details(
n: u32,
generation: ServerGeneration,
forge: ForgeDetails,
repo_config: Option<RepoConfig>,
gitdir: GitDir,
) -> RepoDetails {
RepoDetails {
generation,
repo_alias: repo_alias(n),
repo_path: repo_path(n),
gitdir,

View file

@ -2,7 +2,10 @@ use assert2::let_assert;
use kxio::network::{MockNetwork, StatusCode};
use crate::server::config::{BranchName, ForgeType};
use crate::server::{
config::{BranchName, ForgeType},
types::ServerGeneration,
};
use super::*;
@ -14,6 +17,7 @@ fn test_name() {
let net = Network::new_mock();
let repo_details = common::repo_details(
1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge),
Some(common::repo_config(1)),
GitDir::new(fs.base()),
@ -40,6 +44,7 @@ async fn test_branches_get() {
let repo_details = common::repo_details(
1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge),
Some(common::repo_config(1)),
GitDir::new(fs.base()),

View file

@ -13,30 +13,9 @@ use tracing::{error, info, level_filters::LevelFilter};
use crate::{
fs::FileSystem,
server::{
actors::webhook,
config::{ForgeConfig, ForgeName, GitDir, RepoAlias, ServerStorage, Webhook},
},
server::{actors::server::Server, types::ServerGeneration},
};
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) {
let file_name = "git-next-server.toml";
let pathbuf = PathBuf::from(file_name);
@ -61,166 +40,26 @@ pub fn init(fs: FileSystem) {
pub async fn start(fs: FileSystem, net: Network) {
init_logging();
info!("Starting Server...");
let server_config = match config::ServerConfig::load(&fs) {
Ok(server_config) => server_config,
Err(err) => {
error!("Failed to load config file. Error: {}", err);
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 generation = ServerGeneration::new();
{
let span = tracing::info_span!("Server", %generation);
let _guard = span.enter();
info!("Starting Server...");
let server_config = match config::ServerConfig::load(&fs) {
Ok(server_config) => server_config,
Err(err) => {
error!("Failed to load config file. Error: {}", err);
return;
}
};
// Forge directories in Server Storage
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;
let server = Server::new(generation, fs.clone(), net.clone()).start();
server.do_send(server_config);
}
// 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...");
let _ = actix_rt::signal::ctrl_c().await;
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() {

View file

@ -35,3 +35,19 @@ impl std::fmt::Display for MessageToken {
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)
}
}