refactor: flatten nested blocks with early returns
All checks were successful
Rust / build (push) Successful in 5m52s
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
Release Please / Release-plz (push) Successful in 1m4s

This commit is contained in:
Paul Campbell 2024-09-01 07:18:05 +01:00
parent f475095f4a
commit ca70c03e8b
7 changed files with 106 additions and 99 deletions

View file

@ -18,23 +18,24 @@ impl Handler<NotifyUser> for AlertsActor {
}; };
let net = self.net.clone(); let net = self.net.clone();
let shout = shout.clone(); let shout = shout.clone();
if let Some(user_notification) = self.history.sendable(msg.peel()) { let Some(user_notification) = self.history.sendable(msg.peel()) else {
async move { return;
if let Some(webhook_config) = shout.webhook() { };
send_webhook(&user_notification, webhook_config, &net).await; async move {
} if let Some(webhook_config) = shout.webhook() {
if let Some(email_config) = shout.email() { send_webhook(&user_notification, webhook_config, &net).await;
send_email(&user_notification, email_config); }
} if let Some(email_config) = shout.email() {
if let Some(desktop) = shout.desktop() { send_email(&user_notification, email_config);
if desktop { }
send_desktop_notification(&user_notification); if let Some(desktop) = shout.desktop() {
} if desktop {
send_desktop_notification(&user_notification);
} }
} }
.in_current_span()
.into_actor(self)
.wait(ctx);
} }
.in_current_span()
.into_actor(self)
.wait(ctx);
} }
} }

View file

@ -12,20 +12,21 @@ impl Handler<UnRegisterWebhook> for RepoActor {
type Result = (); type Result = ();
fn handle(&mut self, _msg: UnRegisterWebhook, ctx: &mut Self::Context) -> Self::Result { fn handle(&mut self, _msg: UnRegisterWebhook, ctx: &mut Self::Context) -> Self::Result {
if let Some(webhook_id) = self.webhook_id.take() { let Some(webhook_id) = self.webhook_id.take() else {
self.update_tui(RepoUpdate::UnregisteringWebhook); return;
let forge = self.forge.duplicate(); };
debug!("unregistering webhook"); self.update_tui(RepoUpdate::UnregisteringWebhook);
async move { let forge = self.forge.duplicate();
match forge.unregister_webhook(&webhook_id).await { debug!("unregistering webhook");
Ok(()) => debug!("unregistered webhook"), async move {
Err(err) => warn!(?err, "unregistering webhook"), match forge.unregister_webhook(&webhook_id).await {
} Ok(()) => debug!("unregistered webhook"),
Err(err) => warn!(?err, "unregistering webhook"),
} }
.in_current_span()
.into_actor(self)
.wait(ctx);
debug!("unregistering webhook done");
} }
.in_current_span()
.into_actor(self)
.wait(ctx);
debug!("unregistering webhook done");
} }
} }

View file

@ -59,32 +59,34 @@ impl Tui {
fn handle_input(&mut self, ctx: &mut <Self as actix::Actor>::Context) -> std::io::Result<()> { fn handle_input(&mut self, ctx: &mut <Self as actix::Actor>::Context) -> std::io::Result<()> {
if event::poll(std::time::Duration::from_millis(16))? { if event::poll(std::time::Duration::from_millis(16))? {
if let event::Event::Key(key) = event::read()? { let event::Event::Key(key) = event::read()? else {
if key.kind == KeyEventKind::Press { return Ok(());
match key.code { };
KeyCode::Char('q') => { if key.kind != KeyEventKind::Press {
ctx.stop(); return Ok(());
if let Err(err) = self.signal_shutdown.send(()) { }
tracing::error!(?err, "Failed to signal shutdown"); match key.code {
} KeyCode::Char('q') => {
} ctx.stop();
KeyCode::Char('j') | KeyCode::Down => self.scroll_view_state.scroll_down(), if let Err(err) = self.signal_shutdown.send(()) {
KeyCode::Char('k') | KeyCode::Up => self.scroll_view_state.scroll_up(), tracing::error!(?err, "Failed to signal shutdown");
KeyCode::Char('f') | KeyCode::PageDown => {
self.scroll_view_state.scroll_page_down();
}
KeyCode::Char('b') | KeyCode::PageUp => {
self.scroll_view_state.scroll_page_up();
}
KeyCode::Char('g') | KeyCode::Home => {
self.scroll_view_state.scroll_to_top();
}
KeyCode::Char('G') | KeyCode::End => {
self.scroll_view_state.scroll_to_bottom();
}
_ => (),
} }
} }
KeyCode::Char('j') | KeyCode::Down => self.scroll_view_state.scroll_down(),
KeyCode::Char('k') | KeyCode::Up => self.scroll_view_state.scroll_up(),
KeyCode::Char('f') | KeyCode::PageDown => {
self.scroll_view_state.scroll_page_down();
}
KeyCode::Char('b') | KeyCode::PageUp => {
self.scroll_view_state.scroll_page_up();
}
KeyCode::Char('g') | KeyCode::Home => {
self.scroll_view_state.scroll_to_top();
}
KeyCode::Char('G') | KeyCode::End => {
self.scroll_view_state.scroll_to_bottom();
}
_ => (),
} }
} }
Ok(()) Ok(())

View file

@ -5,7 +5,7 @@ use git_next_core::{ForgeAlias, RepoAlias};
use ratatui::{ use ratatui::{
buffer::Buffer, buffer::Buffer,
layout::{Alignment, Direction, Layout, Rect}, layout::{Alignment, Direction, Layout, Rect},
widgets::{block::Title, Block, Borders, Widget}, widgets::{block::Title, Block, Widget},
}; };
use crate::tui::{ use crate::tui::{

View file

@ -56,41 +56,44 @@ lazy_static::lazy_static! {
} }
impl From<LogLine> for Line<'_> { impl From<LogLine> for Line<'_> {
fn from(value: LogLine) -> Self { fn from(value: LogLine) -> Self {
if let Some(caps) = RE.captures(&value.raw) { match RE.captures(&value.raw) {
let pre = caps["pre"].to_owned(); Some(caps) => {
let hash = caps["hash"].to_owned(); let pre = caps["pre"].to_owned();
let message = caps["message"].to_owned(); let hash = caps["hash"].to_owned();
let mut branches = BRANCHES let message = caps["message"].to_owned();
.captures_iter(&caps["branches"]) let mut branches = BRANCHES
.map(|captures| captures["branch"].to_owned()) .captures_iter(&caps["branches"])
.filter(|branch| branch != "HEAD") .map(|captures| captures["branch"].to_owned())
.collect::<Vec<_>>(); .filter(|branch| branch != "HEAD")
if branches.is_empty() { .collect::<Vec<_>>();
// line without branches if branches.is_empty() {
Line::from(vec![ // line without branches
pre.into(), Line::from(vec![
" ".into(), pre.into(),
hash.into(), " ".into(),
" ".into(), hash.into(),
message.into(), " ".into(),
]) message.into(),
} else { ])
// line withbranches } else {
let mut spans = vec![pre.into(), " ".into(), hash.into(), " ".into()]; // line withbranches
branches.sort(); let mut spans = vec![pre.into(), " ".into(), hash.into(), " ".into()];
branches branches.sort();
.into_iter() branches
.map(|branch| format!("({branch})")) .into_iter()
.map(Span::from) .map(|branch| format!("({branch})"))
.map(|span| span.style(Style::default().fg(Color::White).bg(Color::Blue))) .map(Span::from)
.for_each(|span| spans.push(span)); .map(|span| span.style(Style::default().fg(Color::White).bg(Color::Blue)))
spans.push(" ".into()); .for_each(|span| spans.push(span));
spans.push(message.into()); spans.push(" ".into());
Line::from(spans) spans.push(message.into());
Line::from(spans)
}
}
None => {
// non-commit line
Line::from(value.raw.clone())
} }
} else {
// non-commit line
Line::from(value.raw.clone())
} }
} }
} }

View file

@ -122,18 +122,17 @@ fn reset_next_to_main(
next: &git::Commit, next: &git::Commit,
next_branch: &BranchName, next_branch: &BranchName,
) -> Error { ) -> Error {
if let Err(err) = git::push::reset( match git::push::reset(
open_repository, open_repository,
repo_details, repo_details,
next_branch, next_branch,
&main.clone().into(), &main.clone().into(),
&git::push::Force::From(next.clone().into()), &git::push::Force::From(next.clone().into()),
) { ) {
Error::NonRetryable(format!( Ok(()) => Error::Retryable(format!("Branch {next_branch} has been reset")),
Err(err) => Error::NonRetryable(format!(
"Failed to reset branch '{next_branch}' to commit '{next}': {err}" "Failed to reset branch '{next_branch}' to commit '{next}': {err}"
)) )),
} else {
Error::Retryable(format!("Branch {next_branch} has been reset"))
} }
} }

View file

@ -24,12 +24,13 @@ pub async fn unregister(
None, None,
network::NetRequestLogging::None, network::NetRequestLogging::None,
); );
let result = net.delete(request).await; match net.delete(request).await {
if let Err(e) = result { Err(e) => {
tracing::warn!("Failed to unregister webhook"); tracing::warn!("Failed to unregister webhook");
return Err(git::forge::webhook::Error::FailedToUnregister( Err(git::forge::webhook::Error::FailedToUnregister(
e.to_string(), e.to_string(),
)); ))
}
_ => Ok(()),
} }
Ok(())
} }