1use anyhow::{bail, Result};
2use apm_core::{git, ticket, ticket_fmt};
3use chrono::Utc;
4use std::path::Path;
5use crate::ctx::CmdContext;
6
7pub fn run(root: &Path, id_arg: &str, field: String, value: String, no_aggressive: bool) -> Result<()> {
8 let ctx = CmdContext::load(root, no_aggressive)?;
9 let id = ticket::resolve_id_in_slice(&ctx.tickets, id_arg)?;
10 let mut tickets = ctx.tickets;
11
12 if field == "depends_on" && value != "-" {
13 let ids: Vec<String> = value
14 .split(',')
15 .map(|s| s.trim().to_string())
16 .filter(|s| !s.is_empty())
17 .collect();
18 if !ids.is_empty() {
19 let ticket = tickets.iter().find(|t| t.frontmatter.id == id)
20 .ok_or_else(|| anyhow::anyhow!("ticket {id:?} not found"))?;
21 let strategy = apm_core::validate::active_completion_strategy(&ctx.config);
22 apm_core::validate::check_depends_on_rules(
23 &strategy,
24 ticket.frontmatter.epic.as_deref(),
25 ticket.frontmatter.target_branch.as_deref(),
26 &ids,
27 &tickets,
28 &ctx.config.project.default_branch,
29 )?;
30 }
31 }
32
33 let Some(t) = tickets.iter_mut().find(|t| t.frontmatter.id == id) else {
34 bail!("ticket {id:?} not found");
35 };
36 if field == "owner" {
37 ticket::check_owner(root, t)?;
38 let local = apm_core::config::LocalConfig::load(root);
39 apm_core::validate::validate_owner(&ctx.config, &local, &value)?;
40 }
41 ticket::set_field(&mut t.frontmatter, &field, &value)?;
42 t.frontmatter.updated_at = Some(Utc::now());
43
44 let content = t.serialize()?;
45 let rel_path = format!(
46 "{}/{}",
47 ctx.config.tickets.dir.to_string_lossy(),
48 t.path.file_name().unwrap().to_string_lossy()
49 );
50 let branch = t
51 .frontmatter
52 .branch
53 .clone()
54 .or_else(|| ticket_fmt::branch_name_from_path(&t.path))
55 .unwrap_or_else(|| format!("ticket/{id}"));
56
57 git::commit_to_branch(
58 root,
59 &branch,
60 &rel_path,
61 &content,
62 &format!("ticket({id}): set {field} = {value}"),
63 )?;
64
65 if ctx.aggressive {
66 if let Err(e) = git::push_branch(root, &branch) {
67 eprintln!("warning: push failed: {e:#}");
68 }
69 }
70
71 println!("{id}: {field} = {value}");
72 Ok(())
73}