manager: Factor out a common context object

Clean up the global data a bit by bundling it in a context object.
This commit is contained in:
Rahix 2025-05-22 18:47:31 +02:00
parent eebbd9c453
commit bc54cd91fd
5 changed files with 84 additions and 77 deletions

View file

@ -10,6 +10,21 @@ mod render;
mod tree;
mod wiki;
pub struct Context {
/// API Accessor object
pub forgejo: forgejo_api::Forgejo,
/// Repository Owner
pub owner: String,
/// Repository Name
pub repo: String,
/// URL of the repository page
pub repo_url: url::Url,
/// URL of the repository with authentication information attached
pub repo_auth_url: url::Url,
/// Human readable timestamp of this manager run
pub timestamp: String,
}
async fn run() -> anyhow::Result<()> {
let meta = if std::env::var("TECHTREE_FAKE").ok().is_some() {
log::warn!("Fake tree!");
@ -48,21 +63,29 @@ async fn run() -> anyhow::Result<()> {
let forgejo = Forgejo::new(auth, server_url).context("Could not create API access object")?;
let ctx = Context {
forgejo,
owner: meta.issue.repository.owner.clone(),
repo: meta.issue.repository.name.clone(),
repo_url,
repo_auth_url,
timestamp,
};
let new_comment_id = if meta.action == event_meta::IssueAction::Opened {
let bot_comment =
issue::find_bot_comment(&forgejo, &meta.issue.repository, meta.issue.number)
.await
.with_context(|| {
format!(
"Failed searching for bot comment for issue #{}",
meta.issue.number
)
})?;
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(&forgejo, &meta, meta.issue.number).await;
let res = issue::make_bot_comment(&ctx, meta.issue.number).await;
match res {
Ok(id) => Some(id),
Err(e) => {
@ -86,17 +109,17 @@ async fn run() -> anyhow::Result<()> {
None
};
let tree = collect::collect_tree(&forgejo, &meta)
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(&tree, &timestamp, &repo_auth_url)
render::render_and_publish(&ctx, &tree)
.await
.context("Failed to render and publish the techtree to git")?;
if false {
let mermaid = tree.to_mermaid(&repo_url.to_string());
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:
@ -106,14 +129,9 @@ async fn run() -> anyhow::Result<()> {
"##
);
log::info!("Updating the wiki overview...");
wiki::update_wiki_overview(
&forgejo,
&meta.issue.repository,
timestamp.to_string(),
wiki_text,
)
.await
.context("Failed to update the techtree wiki page")?;
wiki::update_wiki_overview(&ctx, wiki_text)
.await
.context("Failed to update the techtree wiki page")?;
}
'issues: for issue in tree.iter_issues() {
@ -123,14 +141,14 @@ async fn run() -> anyhow::Result<()> {
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(&forgejo, &meta.issue.repository, issue)
let bot_comment = issue::find_bot_comment(&ctx, issue)
.await
.with_context(|| format!("Failed searching for bot comment for 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(&forgejo, &meta.issue.repository, issue)
issue::remove_stale_label(&ctx, issue)
.await
.with_context(|| {
format!("Failed to remove `Stale` label from issue #{issue}")
@ -142,7 +160,7 @@ async fn run() -> anyhow::Result<()> {
} else {
log::warn!("Missing bot comment in issue #{issue}");
issue::make_bot_comment(&forgejo, &meta, issue)
issue::make_bot_comment(&ctx, issue)
.await
.with_context(|| {
format!("Failed to create a retrospective bot comment on issue #{issue}")
@ -150,7 +168,7 @@ async fn run() -> anyhow::Result<()> {
}
};
let mermaid = subtree.to_mermaid(&repo_url.to_string());
let mermaid = subtree.to_mermaid(&ctx.repo_url.to_string());
let full_text = format!(
r##"## Partial Techtree
@ -158,15 +176,16 @@ async fn run() -> anyhow::Result<()> {
{mermaid}
```
<small>Digest: {hash}; Last Updated: {timestamp}</small>"##
<small>Digest: {hash}; Last Updated: {timestamp}</small>"##,
timestamp = ctx.timestamp,
);
log::info!("Updating bot comment in issue #{issue} ...");
issue::update_bot_comment(&forgejo, &meta.issue.repository, comment_id, full_text)
issue::update_bot_comment(&ctx, comment_id, full_text)
.await
.with_context(|| format!("Failed to update the bot comment in issue #{issue}"))?;
issue::remove_stale_label(&forgejo, &meta.issue.repository, issue)
issue::remove_stale_label(&ctx, issue)
.await
.with_context(|| format!("Failed to remove `Stale` label from issue #{issue}"))?;
}