From 70e0f94042856c824127d97a55d89ffee699b205 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 9 Apr 2024 19:30:05 +0100 Subject: [PATCH] feat(server): Assess next action for repo --- src/server/actors/repo/branch.rs | 9 ++++++ src/server/actors/repo/mod.rs | 53 +++++++++++++++++++++++++++++-- src/server/actors/repo/status.rs | 10 ++++++ src/server/actors/repo/webhook.rs | 34 ++++++++++++++++++++ 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/server/actors/repo/status.rs create mode 100644 src/server/actors/repo/webhook.rs diff --git a/src/server/actors/repo/branch.rs b/src/server/actors/repo/branch.rs index b64778a1..177833d9 100644 --- a/src/server/actors/repo/branch.rs +++ b/src/server/actors/repo/branch.rs @@ -66,3 +66,12 @@ pub async fn validate_positions( let dev = commit_histories.dev[0].clone(); addr.do_send(StartMonitoring { main, next, dev }); } + +pub async fn advance_next( + _next: forge::Commit, + _repo_details: crate::server::config::RepoDetails, + _addr: actix::prelude::Addr, + _net: kxio::network::Network, +) { + // TODO: (#14) advance next one commit towards dev +} diff --git a/src/server/actors/repo/mod.rs b/src/server/actors/repo/mod.rs index 775192f3..6684a592 100644 --- a/src/server/actors/repo/mod.rs +++ b/src/server/actors/repo/mod.rs @@ -1,5 +1,7 @@ mod branch; mod config; +mod status; +mod webhook; use actix::prelude::*; use kxio::network::Network; @@ -10,9 +12,12 @@ use crate::server::{ forge, }; +use self::webhook::WebhookId; + pub struct RepoActor { details: RepoDetails, config: Option, // INFO: if [None] then send [StartRepo] to populate it + webhook_id: Option, // INFO: if [None] then no webhook is configured net: Network, } impl RepoActor { @@ -20,12 +25,27 @@ impl RepoActor { Self { details, config: None, + webhook_id: None, net, } } } impl Actor for RepoActor { type Context = Context; + fn stopping(&mut self, ctx: &mut Self::Context) -> Running { + match self.webhook_id.take() { + Some(webhook_id) => { + let repo_details = self.details.clone(); + let addr = ctx.address(); + let net = self.net.clone(); + webhook::unregister(webhook_id, repo_details, addr, net) + .into_actor(self) + .wait(ctx); + Running::Continue + } + None => Running::Stop, + } + } } #[derive(Message)] @@ -69,7 +89,36 @@ pub struct StartMonitoring { } impl Handler for RepoActor { type Result = (); - fn handle(&mut self, _msg: StartMonitoring, _ctx: &mut Self::Context) -> Self::Result { - info!(%self.details, "Monitoring started"); + fn handle(&mut self, msg: StartMonitoring, ctx: &mut Self::Context) -> Self::Result { + info!("Monitoring started"); + let next_ahead_of_main = msg.main != msg.next; + let dev_ahead_of_next = msg.next != msg.dev; + let repo_details = self.details.clone(); + let addr = ctx.address(); + let net = self.net.clone(); + if next_ahead_of_main { + status::check_next(msg.next, repo_details, addr, net) + .into_actor(self) + .wait(ctx); + } else if dev_ahead_of_next { + branch::advance_next(msg.next, repo_details, addr, net) + .into_actor(self) + .wait(ctx); + } else if self.webhook_id.is_none() { + webhook::register(repo_details, addr, net) + .into_actor(self) + .wait(ctx) + // TODO: (#18) watch for changes on dev + } + } +} + +#[derive(Message)] +#[rtype(result = "()")] +pub struct WebhookRegistered(pub WebhookId); +impl Handler for RepoActor { + type Result = (); + fn handle(&mut self, msg: WebhookRegistered, _ctx: &mut Self::Context) -> Self::Result { + self.webhook_id.replace(msg.0); } } diff --git a/src/server/actors/repo/status.rs b/src/server/actors/repo/status.rs new file mode 100644 index 00000000..23aa1f2e --- /dev/null +++ b/src/server/actors/repo/status.rs @@ -0,0 +1,10 @@ +use crate::server::forge; + +pub async fn check_next( + _next: forge::Commit, + _repo_details: crate::server::config::RepoDetails, + _addr: actix::prelude::Addr, + _net: kxio::network::Network, +) { + // TODO: (#13) check statuses for next head - if ok, advance main to next and reassess +} diff --git a/src/server/actors/repo/webhook.rs b/src/server/actors/repo/webhook.rs new file mode 100644 index 00000000..65fb5cc1 --- /dev/null +++ b/src/server/actors/repo/webhook.rs @@ -0,0 +1,34 @@ +use std::ops::Deref; + +#[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 + } +} + +pub async fn unregister( + _webhook_id: WebhookId, + _repo_details: crate::server::config::RepoDetails, + _addr: actix::prelude::Addr, + _net: kxio::network::Network, +) { + // TODO: (#17) unregister webhook + // on success - stop the actor +} + +pub async fn register( + _repo_details: crate::server::config::RepoDetails, + _addr: actix::prelude::Addr, + _net: kxio::network::Network, +) { + // TODO: (#15) register webhook - on success send webhook id to RepoActor +}