Skip to main content

orch/cli/
plan.rs

1//! `orch plan import` creates and updates issues from a roadmap file.
2
3use std::collections::{HashMap, HashSet};
4use std::path::Path;
5
6use anyhow::Context;
7
8use crate::config;
9use crate::github::http::GhHttp;
10use crate::github::types::GitHubIssue;
11use crate::plan::{
12    Binding, DepRef, ExistingIssue, HUMAN_LABEL, LinkEdit, NO_AGENT_LABEL, PLAN_LABEL, PlanDiff,
13    Task, UnmetExternal, diff_import, diff_links, issue_body_with, issue_title, labels_for,
14    marker_id, render_report, skip_unmet_ids, unmet_externals,
15};
16
17pub async fn import(
18    file: &str,
19    dry_run: bool,
20    milestone: Option<&str>,
21    skip_unmet: bool,
22) -> anyhow::Result<()> {
23    let path = Path::new(file);
24    let markdown = std::fs::read_to_string(path)
25        .with_context(|| format!("failed to read {}", path.display()))?;
26    let (roadmap, warnings) = crate::plan::parse_roadmap_for(&markdown, milestone)?;
27    for warning in warnings {
28        eprintln!("warning: {warning}");
29    }
30    if let Some(id) = milestone {
31        crate::plan::select(&roadmap, Some(id))?;
32    }
33    let repo = config::get_current_repo()
34        .context("'repo' not set — run `orch init` or set gh.repo in .orch.yml")?;
35    let rel_path = repo_relative(path).unwrap_or_else(|| {
36        let name = path.file_name().map_or_else(
37            || file.to_string(),
38            |name| name.to_string_lossy().into_owned(),
39        );
40        eprintln!("warning: {file} is outside the git checkout, the issue link uses {name}");
41        name
42    });
43
44    let http = GhHttp::new()?;
45    let branch = http.get_default_branch(&repo).await?;
46    let existing = load_existing(&http, &repo).await?;
47    let known: HashSet<String> = existing.iter().map(|issue| issue.task_id.clone()).collect();
48    let selected_all: Vec<Task> = crate::plan::select(&roadmap, milestone)?
49        .into_iter()
50        .cloned()
51        .collect();
52    let unmet = unmet_externals(&selected_all, &roadmap, &known)?;
53    let extras = extras_by_task(&unmet);
54    let body_for = |task: &Task| {
55        let extra = extras.get(&task.id).map_or(&[][..], Vec::as_slice);
56        issue_body_with(task, &repo, &branch, &rel_path, extra)
57    };
58    let mut diff = diff_import(&roadmap, &existing, milestone, body_for)?;
59    let skipped = if skip_unmet {
60        let skip = skip_unmet_ids(&selected_all, &unmet);
61        let mut skipped = Vec::new();
62        diff.tasks.retain(|plan| {
63            if plan.kind == crate::plan::ChangeKind::Create && skip.contains(&plan.task.id) {
64                skipped.push(plan.task.id.clone());
65                false
66            } else {
67                true
68            }
69        });
70        skipped
71    } else {
72        Vec::new()
73    };
74
75    let selected: Vec<Task> = diff.tasks.iter().map(|plan| plan.task.clone()).collect();
76    let bindings = bindings_for(&diff, &existing);
77    let current = load_blocked_by(&http, &repo, &diff).await?;
78    let links = diff_links(&selected, &bindings, &current)?;
79    if dry_run {
80        print!("{}", render_report(&diff, &links, &unmet, &skipped, true));
81        return Ok(());
82    }
83
84    apply(&http, &repo, &diff, &links, &rel_path, &branch, &extras).await?;
85    print!("{}", render_report(&diff, &links, &unmet, &skipped, false));
86    Ok(())
87}
88
89fn extras_by_task(unmet: &[UnmetExternal]) -> HashMap<String, Vec<String>> {
90    let mut extras: HashMap<String, Vec<String>> = HashMap::new();
91    for item in unmet {
92        extras
93            .entry(item.task_id.clone())
94            .or_default()
95            .push(item.blocking_task_id.clone());
96    }
97    extras
98}
99
100async fn load_existing(http: &GhHttp, repo: &str) -> anyhow::Result<Vec<ExistingIssue>> {
101    // No label filter, so an issue with the marker and no label still matches
102    let issues = match http.list_issues_with_state(repo, "", "all").await {
103        Ok(issues) => issues,
104        Err(err) if err.to_string().contains("404") => Vec::new(),
105        Err(err) => return Err(err),
106    };
107    existing_from_issues(issues)
108}
109
110fn existing_from_issues(issues: Vec<GitHubIssue>) -> anyhow::Result<Vec<ExistingIssue>> {
111    let mut existing = Vec::new();
112    for issue in issues {
113        let labeled = issue.labels.iter().any(|label| label.name == PLAN_LABEL);
114        // Only a label or a repo writer vouches for an unlabeled marker, so a stranger cannot bind an issue
115        let trusted = labeled
116            || matches!(
117                issue.author_association.as_deref(),
118                Some("OWNER" | "MEMBER" | "COLLABORATOR")
119            );
120        let body = issue.body.unwrap_or_default();
121        let Some(task_id) = marker_id(&body).filter(|_| trusted) else {
122            if labeled {
123                eprintln!(
124                    "warning: #{} has the {PLAN_LABEL} label and no plan marker",
125                    issue.number
126                );
127            }
128            continue;
129        };
130        if issue.id == 0 {
131            anyhow::bail!("issue #{} did not include a database id", issue.number);
132        }
133        existing.push(ExistingIssue {
134            task_id,
135            number: issue.number,
136            database_id: issue.id,
137            title: issue.title,
138            body,
139            labels: issue.labels.into_iter().map(|label| label.name).collect(),
140            state: issue.state,
141        });
142    }
143    Ok(existing)
144}
145
146fn bindings_for(diff: &PlanDiff, existing: &[ExistingIssue]) -> Vec<Binding> {
147    let mut bindings: Vec<Binding> = diff
148        .tasks
149        .iter()
150        .map(|plan| Binding {
151            task_id: plan.task.id.clone(),
152            number: plan.number,
153            database_id: plan.database_id,
154        })
155        .collect();
156    for issue in existing {
157        if bindings
158            .iter()
159            .any(|binding| binding.task_id == issue.task_id)
160        {
161            continue;
162        }
163        bindings.push(Binding {
164            task_id: issue.task_id.clone(),
165            number: Some(issue.number),
166            database_id: Some(issue.database_id),
167        });
168    }
169    bindings
170}
171
172async fn load_blocked_by(
173    http: &GhHttp,
174    repo: &str,
175    diff: &PlanDiff,
176) -> anyhow::Result<HashMap<u64, Vec<DepRef>>> {
177    let mut current = HashMap::new();
178    for plan in &diff.tasks {
179        let Some(number) = plan.number else {
180            continue;
181        };
182        if current.contains_key(&number) {
183            continue;
184        }
185        let blocked = match http.list_blocked_by(repo, number).await? {
186            crate::backends::BlockedByFetch::Native(items) => items,
187            crate::backends::BlockedByFetch::Unavailable => {
188                anyhow::bail!("blocked-by API unavailable for #{number}");
189            }
190            crate::backends::BlockedByFetch::Failed(err) => {
191                anyhow::bail!("blocked-by API failed for #{number}: {err}");
192            }
193        };
194        current.insert(
195            number,
196            blocked
197                .into_iter()
198                .map(|item| DepRef {
199                    number: u64::try_from(item.number).unwrap_or(0),
200                    database_id: u64::try_from(item.github_id).unwrap_or(0),
201                })
202                .collect(),
203        );
204    }
205    Ok(current)
206}
207
208async fn apply(
209    http: &GhHttp,
210    repo: &str,
211    diff: &PlanDiff,
212    links: &[LinkEdit],
213    rel_path: &str,
214    branch: &str,
215    blocked_external: &HashMap<String, Vec<String>>,
216) -> anyhow::Result<()> {
217    http.ensure_label(repo, PLAN_LABEL, "6f42c1", "Imported from a roadmap file")
218        .await?;
219    http.ensure_label(repo, HUMAN_LABEL, "fbca04", "A person does this task")
220        .await?;
221    http.ensure_label(repo, NO_AGENT_LABEL, "ededed", "Skip agent dispatch")
222        .await?;
223
224    let mut numbers: HashMap<String, (u64, u64)> = HashMap::new();
225    for plan in &diff.tasks {
226        if let (Some(number), Some(database_id)) = (plan.number, plan.database_id) {
227            numbers.insert(plan.task.id.clone(), (number, database_id));
228        }
229    }
230
231    for plan in &diff.tasks {
232        match plan.kind {
233            crate::plan::ChangeKind::Unchanged => {}
234            crate::plan::ChangeKind::Create => {
235                let title = issue_title(&plan.task);
236                let extra = blocked_external
237                    .get(&plan.task.id)
238                    .map_or(&[][..], Vec::as_slice);
239                let body = issue_body_with(&plan.task, repo, branch, rel_path, extra);
240                let labels = labels_for(&[], plan.task.owner_human);
241                let issue = http.create_issue(repo, &title, &body, &labels).await?;
242                if issue.id == 0 {
243                    anyhow::bail!(
244                        "created issue #{} did not include a database id",
245                        issue.number
246                    );
247                }
248                numbers.insert(plan.task.id.clone(), (issue.number, issue.id));
249            }
250            crate::plan::ChangeKind::Update => {
251                let number = plan.number.context("update is missing an issue number")?;
252                let title = issue_title(&plan.task);
253                let extra = blocked_external
254                    .get(&plan.task.id)
255                    .map_or(&[][..], Vec::as_slice);
256                let body = issue_body_with(&plan.task, repo, branch, rel_path, extra);
257                let labels = labels_for(&plan.existing_labels, plan.task.owner_human);
258                let issue = http
259                    .update_issue(repo, number, &title, &body, &labels)
260                    .await?;
261                let database_id = if issue.id == 0 {
262                    plan.database_id.unwrap_or(0)
263                } else {
264                    issue.id
265                };
266                if database_id == 0 {
267                    anyhow::bail!("issue #{number} did not include a database id");
268                }
269                numbers.insert(plan.task.id.clone(), (issue.number, database_id));
270            }
271        }
272    }
273
274    for link in links {
275        match link {
276            LinkEdit::Add {
277                task_id,
278                blocking_task_id,
279                blocking_database_id,
280                ..
281            } => {
282                let (number, _) = numbers
283                    .get(task_id)
284                    .with_context(|| format!("{task_id} has no issue number"))?;
285                let blocking_id = blocking_database_id.or_else(|| {
286                    numbers
287                        .get(blocking_task_id)
288                        .map(|(_, database_id)| *database_id)
289                });
290                let Some(blocking_id) = blocking_id else {
291                    anyhow::bail!("{task_id} depends on {blocking_task_id}, which has no issue id");
292                };
293                let blocking_id =
294                    i64::try_from(blocking_id).context("issue database id is out of range")?;
295                http.add_blocked_by(repo, *number, blocking_id).await?;
296            }
297            LinkEdit::Remove {
298                issue_number,
299                blocking_database_id,
300                ..
301            } => {
302                http.remove_blocked_by(repo, *issue_number, *blocking_database_id)
303                    .await?;
304            }
305        }
306    }
307    Ok(())
308}
309
310fn repo_relative(path: &Path) -> Option<String> {
311    let output = std::process::Command::new("git")
312        .args(["rev-parse", "--show-toplevel"])
313        .output()
314        .ok()?;
315    if !output.status.success() {
316        return None;
317    }
318    let top = String::from_utf8(output.stdout).ok()?;
319    let top = Path::new(top.trim()).to_path_buf();
320    let abs = if path.is_absolute() {
321        path.to_path_buf()
322    } else {
323        std::env::current_dir().ok()?.join(path)
324    };
325    let abs = abs.canonicalize().unwrap_or(abs);
326    let top = top.canonicalize().unwrap_or(top);
327    let rel = abs.strip_prefix(&top).ok()?;
328    Some(
329        rel.components()
330            .map(|component| component.as_os_str().to_string_lossy())
331            .collect::<Vec<_>>()
332            .join("/"),
333    )
334}
335
336#[cfg(test)]
337mod tests {
338    use super::*;
339    use crate::github::types::{GitHubLabel, GitHubUser};
340
341    fn issue(number: u64, body: &str, labels: &[&str]) -> GitHubIssue {
342        issue_by(number, body, labels, "OWNER")
343    }
344
345    fn issue_by(number: u64, body: &str, labels: &[&str], association: &str) -> GitHubIssue {
346        GitHubIssue {
347            number,
348            id: number * 10,
349            title: format!("issue {number}"),
350            body: Some(body.to_string()),
351            state: "open".to_string(),
352            labels: labels
353                .iter()
354                .map(|name| GitHubLabel {
355                    name: name.to_string(),
356                    color: None,
357                })
358                .collect(),
359            user: GitHubUser {
360                login: "u".to_string(),
361            },
362            created_at: String::new(),
363            updated_at: String::new(),
364            html_url: String::new(),
365            node_id: None,
366            pull_request: None,
367            author_association: Some(association.to_string()),
368            state_reason: None,
369        }
370    }
371
372    #[test]
373    fn marker_matches_with_or_without_the_label_and_no_marker_is_skipped() {
374        let found = existing_from_issues(vec![
375            issue(1, "<!-- orch-plan: M1-01 -->", &[]),
376            issue(2, "<!-- orch-plan: M1-02 -->", &[PLAN_LABEL]),
377            issue(3, "no marker", &[PLAN_LABEL]),
378            issue(4, "no marker", &[]),
379            issue_by(5, "<!-- orch-plan: M1-03 -->", &[], "NONE"),
380        ])
381        .unwrap();
382        let ids: Vec<&str> = found.iter().map(|item| item.task_id.as_str()).collect();
383        assert_eq!(ids, ["M1-01", "M1-02"]);
384    }
385}