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

@ -10,11 +10,12 @@ pub struct BotCommentInfo {
pub async fn make_bot_comment(
ctx: &crate::Context,
issue_number: u64,
) -> anyhow::Result<CommentId> {
) -> anyhow::Result<BotCommentInfo> {
let initial_message =
"_Please be patient, this issue is currently being integrated into the techtree..._";
let res = ctx.forgejo
let res = ctx
.forgejo
.issue_create_comment(
&ctx.owner,
&ctx.repo,
@ -26,14 +27,18 @@ pub async fn make_bot_comment(
)
.await?;
Ok(res.id.unwrap())
Ok(BotCommentInfo {
id: res.id.unwrap(),
body: initial_message.to_owned(),
})
}
pub async fn find_bot_comment(
ctx: &crate::Context,
issue_number: u64,
) -> anyhow::Result<Option<BotCommentInfo>> {
let mut comments = ctx.forgejo
let mut comments = ctx
.forgejo
.issue_get_comments(
&ctx.owner,
&ctx.repo,
@ -58,6 +63,27 @@ pub async fn find_bot_comment(
}))
}
/// Find existing bot comment or create a new one.
///
/// Returns a tuple of the comment information and a boolean indicating whether the comment was
/// newly created.
pub async fn find_or_make_bot_comment(
ctx: &crate::Context,
issue_number: u64,
) -> anyhow::Result<(BotCommentInfo, bool)> {
if let Some(comment) = find_bot_comment(ctx, issue_number)
.await
.context("Failed to search for bot comment in issue")?
{
Ok((comment, false))
} else {
make_bot_comment(ctx, issue_number)
.await
.context("Failed to make new bot comment in issue")
.map(|c| (c, true))
}
}
pub async fn update_bot_comment(
ctx: &crate::Context,
id: CommentId,
@ -79,11 +105,32 @@ pub async fn update_bot_comment(
Ok(())
}
pub async fn remove_stale_label(
pub async fn update_bot_comment_from_subtree(
ctx: &crate::Context,
issue_number: u64,
id: CommentId,
subtree: &crate::tree::Subtree<'_>,
hash: &str,
) -> anyhow::Result<()> {
let labels = ctx.forgejo
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,
);
update_bot_comment(&ctx, id, full_text).await?;
Ok(())
}
pub async fn remove_stale_label(ctx: &crate::Context, issue_number: u64) -> anyhow::Result<()> {
let labels = ctx
.forgejo
.issue_get_labels(&ctx.owner, &ctx.repo, issue_number)
.await
.context("Failed fetching issue labels")?;
@ -97,7 +144,8 @@ pub async fn remove_stale_label(
if let Some(stale_label_id) = stale_label_id {
log::info!("Removing `Stale` label from issue #{issue_number}...");
let res = ctx.forgejo
let res = ctx
.forgejo
.issue_remove_label(
&ctx.owner,
&ctx.repo,