staxman-old/src/route/stack/history.rs

80 lines
1.7 KiB
Rust

use std::collections::HashMap;
use askama::Template;
use axum::{
extract::{Path, State},
response::Redirect,
Form,
};
use serde::Deserialize;
use serde_json::json;
use crate::{
git::history::CommitInfo,
http::error::Result,
http::response::{reply, HandlerResponse},
node::nix::parse_arion_compose,
stack::compose,
AppState,
};
use super::edit::edit_stack_int;
#[derive(Template)]
#[template(path = "stack/history.html")]
struct HistoryTemplate {
stack_name: String,
commits: Vec<CommitInfo>,
}
#[derive(Deserialize)]
pub(super) struct RestoreForm {
oid: String,
}
pub(super) async fn list(
Path(stack_name): Path<String>,
State(state): State<AppState>,
) -> HandlerResponse {
let compose_file = compose::get(&state.stack_dir, &stack_name).await?;
let info = parse_arion_compose(&compose_file)?;
let commits = state.repository.get_history(&stack_name)?;
let mut newest_first = commits.clone();
newest_first.reverse();
reply(
json!({
"commits": commits,
}),
HistoryTemplate {
stack_name: info.project,
commits: newest_first,
},
)
}
pub(super) async fn restore(
Path(stack_name): Path<String>,
State(state): State<AppState>,
Form(form): Form<RestoreForm>,
) -> Result<Redirect> {
let path = compose::path(&stack_name);
let source = state.repository.get_file_at_commit(&path, &form.oid)?;
let short_id = form.oid[..6].to_string();
let mut files = HashMap::new();
files.insert(path.to_string_lossy().into_owned(), source);
edit_stack_int(
state,
&stack_name,
&files,
format!("Revert to {}", short_id).as_str(),
)
.await?;
Ok(Redirect::to(".."))
}