Compare commits

..

1 commit

Author SHA1 Message Date
84af6da22f WIP: refactor(git): more derive_more replacing boilerplate
All checks were successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
/ test (push) Successful in 2s
ci/woodpecker/push/tag-created Pipeline was successful
2024-05-15 20:14:17 +01:00
9 changed files with 142 additions and 36 deletions

View file

@ -7,13 +7,17 @@ use tracing::{debug, info};
const CHECK_INTERVAL: Duration = Duration::from_secs(1);
#[derive(Debug, Clone, Message)]
#[rtype(result = "()")]
#[derive(Debug, Clone)]
pub struct WatchFile;
impl Message for WatchFile {
type Result = ();
}
#[derive(Debug, Message)]
#[rtype(result = "()")]
#[derive(Debug)]
pub struct FileUpdated;
impl Message for FileUpdated {
type Result = ();
}
#[derive(Debug, derive_more::From, derive_more::Display)]
pub enum Error {
@ -68,3 +72,13 @@ impl Handler<WatchFile> for FileWatcher {
}
}
}
// impl Handler<Stop> for FileWatcher {
// type Result = anyhow::Result<()>;
//
// fn handle(&mut self, _msg: Stop, ctx: &mut Self::Context) -> Self::Result {
// warn!("Stopping file watcher actor");
// self.run_interval.take();
// ctx.stop();
// Ok(())
// }
// }

View file

@ -16,8 +16,6 @@ use crate::{actors::repo::webhook::WebhookAuth, config::Webhook, gitforge, types
use self::webhook::WebhookId;
#[derive(Debug, derive_more::Display)]
#[display("{}:{}:{}", generation, details.forge.forge_name(), details.repo_alias)]
pub struct RepoActor {
generation: Generation,
message_token: MessageToken,
@ -81,6 +79,17 @@ impl Actor for RepoActor {
}
}
}
impl std::fmt::Display for RepoActor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}:{}:{}",
self.generation,
self.details.forge.forge_name(),
self.details.repo_alias
)
}
}
#[derive(Message)]
#[rtype(result = "()")]
@ -127,7 +136,7 @@ impl Handler<LoadConfigFromRepo> for RepoActor {
#[derive(Message)]
#[rtype(result = "()")]
struct LoadedConfig(RepoConfig);
struct LoadedConfig(pub RepoConfig);
impl Handler<LoadedConfig> for RepoActor {
type Result = ();
#[tracing::instrument(name = "RepoActor::LoadedConfig", skip_all, fields(repo = %self.details, branches = %msg.0))]
@ -142,11 +151,16 @@ impl Handler<LoadedConfig> for RepoActor {
}
}
#[derive(derive_more::Constructor, Message)]
#[derive(Message)]
#[rtype(result = "()")]
pub struct ValidateRepo {
message_token: MessageToken,
}
impl ValidateRepo {
pub const fn new(message_token: MessageToken) -> Self {
Self { message_token }
}
}
impl Handler<ValidateRepo> for RepoActor {
type Result = ();
#[tracing::instrument(name = "RepoActor::ValidateRepo", skip_all, fields(repo = %self.details, token = %msg.message_token))]
@ -194,19 +208,17 @@ impl Handler<ValidateRepo> for RepoActor {
}
}
#[derive(Debug, derive_more::Constructor, Message)]
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct StartMonitoring {
main: git::Commit,
next: git::Commit,
dev: git::Commit,
dev_commit_history: Vec<git::Commit>,
pub main: git::Commit,
pub next: git::Commit,
pub dev: git::Commit,
pub dev_commit_history: Vec<git::Commit>,
}
impl Handler<StartMonitoring> for RepoActor {
type Result = ();
#[tracing::instrument(name = "RepoActor::StartMonitoring", skip_all,
fields(token = %self.message_token, repo = %self.details, main = %msg.main, next= %msg.next, dev = %msg.dev))
]
#[tracing::instrument(name = "RepoActor::StartMonitoring", skip_all, fields(token = %self.message_token, repo = %self.details, main = %msg.main, next= %msg.next, dev = %msg.dev))]
fn handle(&mut self, msg: StartMonitoring, ctx: &mut Self::Context) -> Self::Result {
info!("Message Received");
let Some(repo_config) = self.details.repo_config.clone() else {
@ -247,7 +259,7 @@ impl Handler<StartMonitoring> for RepoActor {
#[derive(Message)]
#[rtype(result = "()")]
pub struct WebhookRegistered(WebhookId, WebhookAuth);
pub struct WebhookRegistered(pub WebhookId, pub WebhookAuth);
impl Handler<WebhookRegistered> for RepoActor {
type Result = ();
#[tracing::instrument(name = "RepoActor::WebhookRegistered", skip_all, fields(repo = %self.details, webhook_id = %msg.0))]
@ -260,7 +272,7 @@ impl Handler<WebhookRegistered> for RepoActor {
#[derive(Message)]
#[rtype(result = "()")]
pub struct AdvanceMainTo(git::Commit);
pub struct AdvanceMainTo(pub git::Commit);
impl Handler<AdvanceMainTo> for RepoActor {
type Result = ();
#[tracing::instrument(name = "RepoActor::AdvanceMainTo", skip_all, fields(repo = %self.details, commit = %msg.0))]

View file

@ -5,7 +5,7 @@ use kxio::network::{self, json};
use tracing::{debug, info, warn};
use ulid::DecodeError;
use std::{collections::HashMap, str::FromStr};
use std::{collections::HashMap, fmt::Display, ops::Deref, str::FromStr};
use crate::{
actors::{
@ -15,12 +15,27 @@ use crate::{
config::{Webhook, WebhookUrl},
};
#[derive(
Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Deref, derive_more::Display,
)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WebhookId(String);
impl WebhookId {
#[allow(dead_code)]
pub const fn new(id: String) -> Self {
Self(id)
}
}
impl Deref for WebhookId {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Display for WebhookId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Clone, Debug, PartialEq, Eq, derive_more::Deref)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WebhookAuth(ulid::Ulid);
impl WebhookAuth {
pub fn from_str(authorisation: &str) -> Result<Self, DecodeError> {
@ -36,6 +51,12 @@ impl WebhookAuth {
format!("Basic {}", self.0.to_string())
}
}
impl Deref for WebhookAuth {
type Target = ulid::Ulid;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[tracing::instrument(skip_all, fields(%webhook_id))]
pub async fn unregister(webhook_id: WebhookId, repo_details: RepoDetails, net: network::Network) {

View file

@ -3,21 +3,46 @@ use actix::prelude::*;
use crate::actors::repo::webhook::WebhookAuth;
#[derive(Message, Debug, Clone, derive_more::Constructor)]
#[derive(Message, Debug, Clone)]
#[rtype(result = "()")]
pub struct WebhookMessage {
id: String,
path: String,
authorisation: String,
// query: String,
// headers: warp::http::HeaderMap,
body: String,
}
impl WebhookMessage {
pub const fn new(
id: String,
path: String,
// query: String,
// headers: warp::http::HeaderMap,
body: String,
authorisation: String,
) -> Self {
Self {
id,
path,
// query,
// headers,
body,
authorisation,
}
}
pub const fn id(&self) -> &String {
&self.id
}
pub const fn path(&self) -> &String {
&self.path
}
// pub const fn query(&self) -> &String {
// &self.query
// }
// pub const fn headers(&self) -> &warp::http::HeaderMap {
// &self.headers
// }
pub const fn body(&self) -> &String {
&self.body
}

View file

@ -42,9 +42,11 @@ impl Actor for WebhookActor {
}
}
#[derive(Debug, Message)]
#[rtype(result = "()")]
#[derive(Debug)]
pub struct ShutdownWebhook;
impl Message for ShutdownWebhook {
type Result = ();
}
impl Handler<ShutdownWebhook> for WebhookActor {
type Result = ();

View file

@ -8,7 +8,15 @@ use crate::gitforge::{self, ForgeFileError};
pub async fn load(
details: &RepoDetails,
forge: &gitforge::Forge,
) -> Result<RepoConfig, OneOf<(ForgeFileError, crate::config::Error, toml::de::Error, Error)>> {
) -> Result<
RepoConfig,
OneOf<(
ForgeFileError,
crate::config::Error,
toml::de::Error,
RepoConfigValidationErrors,
)>,
> {
let contents = forge
.file_contents_get(&details.branch, ".git-next.toml")
.await
@ -19,33 +27,42 @@ pub async fn load(
}
#[derive(Debug)]
pub enum Error {
pub enum RepoConfigValidationErrors {
Forge(gitforge::ForgeBranchError),
BranchNotFound(BranchName),
}
pub async fn validate(config: RepoConfig, forge: &gitforge::Forge) -> Result<RepoConfig, Error> {
pub async fn validate(
config: RepoConfig,
forge: &gitforge::Forge,
) -> Result<RepoConfig, RepoConfigValidationErrors> {
let branches = forge.branches_get_all().await.map_err(|e| {
error!(?e, "Failed to list branches");
Error::Forge(e)
RepoConfigValidationErrors::Forge(e)
})?;
if !branches
.iter()
.any(|branch| branch == &config.branches().main())
{
return Err(Error::BranchNotFound(config.branches().main()));
return Err(RepoConfigValidationErrors::BranchNotFound(
config.branches().main(),
));
}
if !branches
.iter()
.any(|branch| branch == &config.branches().next())
{
return Err(Error::BranchNotFound(config.branches().next()));
return Err(RepoConfigValidationErrors::BranchNotFound(
config.branches().next(),
));
}
if !branches
.iter()
.any(|branch| branch == &config.branches().dev())
{
return Err(Error::BranchNotFound(config.branches().dev()));
return Err(RepoConfigValidationErrors::BranchNotFound(
config.branches().dev(),
));
}
Ok(config)
}

View file

@ -89,8 +89,13 @@ impl Webhook {
}
/// The URL for the webhook where forges should send their updates
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::AsRef)]
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize)]
pub struct WebhookUrl(String);
impl AsRef<str> for WebhookUrl {
fn as_ref(&self) -> &str {
&self.0
}
}
/// The directory to store server data, such as cloned repos
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize)]

View file

@ -59,7 +59,12 @@ impl super::ForgeLike for ForgeJoEnv {
dev,
dev_commit_history,
}) => {
addr.do_send(StartMonitoring::new(main, next, dev, dev_commit_history));
addr.do_send(StartMonitoring {
main,
next,
dev,
dev_commit_history,
});
}
Err(err) => {
warn!("{}", err);

View file

@ -1,4 +1,4 @@
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct MessageToken(u32);
impl MessageToken {
pub fn new() -> Self {
@ -8,3 +8,8 @@ impl MessageToken {
Self(self.0 + 1)
}
}
impl std::fmt::Display for MessageToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}