feat(server): Assess next action for repo

This commit is contained in:
Paul Campbell 2024-04-09 19:30:05 +01:00
parent cdaca8258d
commit 70e0f94042
4 changed files with 104 additions and 2 deletions

View file

@ -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<super::RepoActor>,
_net: kxio::network::Network,
) {
// TODO: (#14) advance next one commit towards dev
}

View file

@ -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<RepoConfig>, // INFO: if [None] then send [StartRepo] to populate it
webhook_id: Option<WebhookId>, // 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<Self>;
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<StartMonitoring> 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<WebhookRegistered> for RepoActor {
type Result = ();
fn handle(&mut self, msg: WebhookRegistered, _ctx: &mut Self::Context) -> Self::Result {
self.webhook_id.replace(msg.0);
}
}

View file

@ -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<super::RepoActor>,
_net: kxio::network::Network,
) {
// TODO: (#13) check statuses for next head - if ok, advance main to next and reassess
}

View file

@ -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<super::RepoActor>,
_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<super::RepoActor>,
_net: kxio::network::Network,
) {
// TODO: (#15) register webhook - on success send webhook id to RepoActor
}