manager: The big refactoring

Clean up the logic in main to make it more readable.  Factor out common
functionality into the respective modules.
This commit is contained in:
Rahix 2025-05-22 20:27:08 +02:00
parent bc54cd91fd
commit 785ff37107
5 changed files with 171 additions and 140 deletions

View file

@ -72,64 +72,43 @@ async fn run() -> anyhow::Result<()> {
timestamp,
};
let new_comment_id = if meta.action == event_meta::IssueAction::Opened {
let bot_comment = issue::find_bot_comment(&ctx, meta.issue.number)
.await
.with_context(|| {
format!(
"Failed searching for bot comment for issue #{}",
meta.issue.number
)
})?;
let id = match bot_comment {
Some(comment) => Some(comment.id),
None => {
let res = issue::make_bot_comment(&ctx, meta.issue.number).await;
match res {
Ok(id) => Some(id),
Err(e) => {
log::warn!(
"Error while creating the informational comment on issue #{}:\n{e:?}",
meta.issue.number
);
None
}
if meta.action == event_meta::IssueAction::Opened {
log::debug!("Running for a newly opened issue. Taking care of the comment first...");
match issue::find_or_make_bot_comment(&ctx, meta.issue.number).await {
Ok((_comment, is_new)) => {
if is_new {
log::info!(
"Waiting for a minute, as the issue is brand new. We will likely catch some more updates this way!"
);
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
}
}
};
log::info!(
"Waiting for a minute, as the issue is brand new. We will likely catch some more updates this way!"
);
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
id
} else {
None
};
Err(err) => {
log::warn!(
"Error while commenting on new issue #{}, continuing anyway... Error: {err:?}",
meta.issue.number
);
}
}
}
log::info!("Collecting tree from issue metadata...");
let tree = collect::collect_tree(&ctx)
.await
.context("Failed to collect the techtree from issue metadata")?;
log::info!("Rendering and publishing techtree to git repository...");
render::render_and_publish(&ctx, &tree)
let rendered_tree = render::render(&ctx, &tree)
.await
.context("Failed to render and publish the techtree to git")?;
.context("Failed to render the techtree")?;
render::publish(&ctx, &rendered_tree)
.await
.context("Failed to publish rendered tree to git")?;
// Wiki is disabled because the tree is too big for mermaid to handle
if false {
let mermaid = tree.to_mermaid(&ctx.repo_url.to_string());
let wiki_text = format!(
r##"This page is automatically updated to show the latest and greatest FAFO techtree:
```mermaid
{mermaid}
```
"##
);
log::info!("Updating the wiki overview...");
wiki::update_wiki_overview(&ctx, wiki_text)
wiki::update_wiki_from_tree(&ctx, &tree)
.await
.context("Failed to update the techtree wiki page")?;
}
@ -138,52 +117,23 @@ async fn run() -> anyhow::Result<()> {
let subtree = tree.subtree_for_issue(issue).unwrap();
let hash = subtree.stable_hash();
let comment_id = if new_comment_id.is_some() && issue == meta.issue.number {
new_comment_id.unwrap()
} else {
let bot_comment = issue::find_bot_comment(&ctx, issue)
let (bot_comment, _is_new) = issue::find_or_make_bot_comment(&ctx, issue)
.await
.with_context(|| format!("Failed to find or make bot comment in issue #{issue}"))?;
if bot_comment.body.contains(&hash) {
log::info!("Issue #{issue} is up-to-date, not editing comment.");
issue::remove_stale_label(&ctx, issue)
.await
.with_context(|| format!("Failed searching for bot comment for issue #{issue}"))?;
.with_context(|| format!("Failed to remove `Stale` label from issue #{issue}"))?;
if let Some(bot_comment) = bot_comment {
if bot_comment.body.contains(&hash) {
log::info!("Issue #{issue} is up-to-date, not editing comment.");
issue::remove_stale_label(&ctx, issue)
.await
.with_context(|| {
format!("Failed to remove `Stale` label from issue #{issue}")
})?;
continue 'issues;
}
bot_comment.id
} else {
log::warn!("Missing bot comment in issue #{issue}");
issue::make_bot_comment(&ctx, issue)
.await
.with_context(|| {
format!("Failed to create a retrospective bot comment on issue #{issue}")
})?
}
};
let mermaid = subtree.to_mermaid(&ctx.repo_url.to_string());
let full_text = format!(
r##"## Partial Techtree
```mermaid
{mermaid}
```
<small>Digest: {hash}; Last Updated: {timestamp}</small>"##,
timestamp = ctx.timestamp,
);
continue 'issues;
}
log::info!("Updating bot comment in issue #{issue} ...");
issue::update_bot_comment(&ctx, comment_id, full_text)
issue::update_bot_comment_from_subtree(&ctx, bot_comment.id, &subtree, &hash)
.await
.with_context(|| format!("Failed to update the bot comment in issue #{issue}"))?;
.with_context(|| format!("Failed to update bot comment in issue #{issue}"))?;
issue::remove_stale_label(&ctx, issue)
.await