manager: Render graphviz tree and push it to git

This commit is contained in:
Rahix 2025-05-22 14:44:25 +02:00
parent 3bd79f176e
commit 8b66d2551a
3 changed files with 81 additions and 1 deletions

View file

@ -1 +1,2 @@
/target/
/render-git/

View file

@ -4,6 +4,7 @@ use forgejo_api::Forgejo;
mod collect;
mod event_meta;
mod issue;
mod render;
mod tree;
mod wiki;
@ -21,7 +22,7 @@ async fn run() -> anyhow::Result<()> {
meta.issue.number
);
let timestamp = chrono::Local::now().format("%Y-%m-%d %H:%M:%S");
let timestamp = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
log::info!("Timestamp of this run is {timestamp}");
let token =
@ -33,6 +34,10 @@ async fn run() -> anyhow::Result<()> {
)
.context("Failed parsing GITHUB_SERVER_URL as a url")?;
let mut repo_auth_url = server_url.join(&format!("{}/{}", meta.issue.repository.owner, meta.issue.repository.name)).unwrap();
repo_auth_url.set_username("forgejo-actions").unwrap();
repo_auth_url.set_password(Some(&token)).unwrap();
let forgejo = Forgejo::new(auth, server_url).context("Could not create API access object")?;
let new_comment_id = if meta.action == event_meta::IssueAction::Opened {
@ -56,6 +61,11 @@ async fn run() -> anyhow::Result<()> {
.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)
.await
.context("Failed to render and publish the techtree to git")?;
let mermaid = tree.to_mermaid();
let wiki_text = format!(
r##"This page is automatically updated to show the latest and greatest FAFO techtree:

View file

@ -0,0 +1,69 @@
use std::process::Command;
use anyhow::Context as _;
pub async fn render_and_publish(
tree: &crate::tree::Tree,
timestamp: &str,
repo_auth_url: &url::Url,
) -> anyhow::Result<()> {
let render_repo = std::path::PathBuf::from("render-git");
if render_repo.is_dir() {
log::info!("Found old {render_repo:?} repository, removing...");
std::fs::remove_dir_all(&render_repo).context("Failed to remove stale render repository")?;
}
std::fs::create_dir(&render_repo).context("Failed creating directory for rendered graph")?;
Command::new("git")
.arg("-C")
.arg(&render_repo)
.arg("init")
.status()
.context("Failed to initialize render repository")?;
let dot_file = render_repo.join("techtree.dot");
let svg_file = render_repo.join("techtree.svg");
let dot_source = tree.to_dot();
std::fs::write(&dot_file, dot_source.as_bytes())
.context("Failed to write `dot` source file")?;
Command::new("dot")
.args(["-T", "svg"])
.arg("-o")
.arg(&svg_file)
.arg(&dot_file)
.status()
.context("Failed to generate svg graph from dot source")?;
Command::new("git")
.arg("-C")
.arg(&render_repo)
.arg("add")
.arg(dot_file.file_name().unwrap())
.arg(svg_file.file_name().unwrap())
.status()
.context("Failed to add generated graph files to git index")?;
Command::new("git")
.arg("-C")
.arg(&render_repo)
.arg("commit")
.args(["-m", &format!("Updated techtree at {timestamp}")])
.status()
.context("Failed to add generated graph files to git index")?;
Command::new("git")
.arg("-C")
.arg(&render_repo)
.arg("push")
.arg("--force")
.arg(repo_auth_url.to_string())
.arg("HEAD:refs/heads/render")
.status()
.context("Failed to push rendered graph to forgejo repository")?;
Ok(())
}