1use std::collections::BTreeMap;
5use std::path::{Path, PathBuf};
6
7use serde::Deserialize;
8use serde_json::Value;
9
10use crate::config::{Config, Followups, StateStore};
11use crate::error::Result;
12use crate::model::{Issue, IssueRef, ItemKind, PersistedState, PrRef, PrView};
13use crate::proc::{self, ExecOpts};
14use crate::style::{self, Style};
15use crate::textsim;
16use crate::{bail, logdim, spar_err};
17
18pub const FETCH_CEILING: usize = 500;
22
23pub const STATE_MARKER: &str = "<!-- spar:state";
26
27const WORKTREE_DIR: &str = ".spar-worktrees";
28const STATE_DIR: &str = ".spar";
29
30#[derive(Debug)]
31pub struct Repo {
32 root: PathBuf,
33 pub style: Style,
34 pub branch_prefix: String,
35 pub state_store: StateStore,
36 pub followups: Followups,
37}
38
39impl Repo {
40 pub fn open(root: impl AsRef<Path>, cfg: &Config) -> Result<Self> {
41 let root =
42 std::fs::canonicalize(root.as_ref()).unwrap_or_else(|_| root.as_ref().to_path_buf());
43 let inside = proc::run_str(
46 &["git", "rev-parse", "--is-inside-work-tree"],
47 &ExecOpts::new().cwd(&root).check(false).timeout_secs(30),
48 )
49 .unwrap_or_default();
50 if inside.trim() != "true" {
51 bail!("not a git repository: {}", root.display());
52 }
53 let repo = Self {
54 root,
55 style: cfg.style.clone(),
56 branch_prefix: cfg.loop_cfg.branch_prefix.clone(),
57 state_store: cfg.loop_cfg.state_store,
58 followups: cfg.loop_cfg.followups,
59 };
60 repo.self_exclude();
61 Ok(repo)
62 }
63
64 fn self_exclude(&self) {
72 let git_dir = self.git_try(&["rev-parse", "--path-format=absolute", "--git-common-dir"]);
73 let git_dir = git_dir.trim();
74 if git_dir.is_empty() {
75 return;
76 }
77 let path = Path::new(git_dir).join("info").join("exclude");
78 let existing = std::fs::read_to_string(&path).unwrap_or_default();
79
80 let wanted = [format!("/{WORKTREE_DIR}/"), format!("/{STATE_DIR}/")];
81 let missing: Vec<&String> = wanted
82 .iter()
83 .filter(|line| !existing.lines().any(|l| l.trim() == line.as_str()))
84 .collect();
85 if missing.is_empty() {
86 return;
87 }
88
89 use std::io::Write;
90 if let Some(parent) = path.parent() {
91 let _ = std::fs::create_dir_all(parent);
92 }
93 let mut block = String::new();
94 if !existing.is_empty() && !existing.ends_with('\n') {
95 block.push('\n');
96 }
97 block.push_str("\n# added by spar: its worktrees and run state\n");
98 for line in missing {
99 block.push_str(line);
100 block.push('\n');
101 }
102 if let Ok(mut file) = std::fs::OpenOptions::new()
103 .create(true)
104 .append(true)
105 .open(&path)
106 {
107 let _ = file.write_all(block.as_bytes());
108 }
109 }
110
111 pub fn root(&self) -> &Path {
112 &self.root
113 }
114
115 pub fn clean(&self, text: &str) -> Result<String> {
121 let out = style::scrub(text, &self.style);
122 let bad = style::violations(&out, &self.style);
123 if !bad.is_empty() {
124 bail!(
125 "style gate could not clean text ({}): {}",
126 bad.join(", "),
127 style::clip(&out, 300)
128 );
129 }
130 Ok(out)
131 }
132
133 pub fn clean_body(&self, text: &str) -> Result<String> {
135 self.clean(&style::body(text, &self.style))
136 }
137
138 pub fn clean_issue_body(&self, text: &str) -> Result<String> {
140 self.clean(&style::issue_body(text, &self.style))
141 }
142
143 pub fn clean_title(&self, text: &str) -> Result<String> {
154 Ok(style::title(&self.clean(text)?, &self.style))
155 }
156
157 fn git_opts(&self, cwd: Option<&Path>, check: bool) -> ExecOpts {
160 ExecOpts::new()
161 .cwd(cwd.unwrap_or(&self.root))
162 .check(check)
163 .timeout_secs(600)
164 }
165
166 pub fn git(&self, args: &[&str]) -> Result<String> {
167 self.git_at(None, args)
168 }
169
170 pub fn git_at(&self, cwd: Option<&Path>, args: &[&str]) -> Result<String> {
171 let mut argv = vec!["git".to_string()];
172 argv.extend(args.iter().map(|s| s.to_string()));
173 proc::run(&argv, &self.git_opts(cwd, true))
174 }
175
176 pub fn git_try(&self, args: &[&str]) -> String {
178 self.git_try_at(None, args)
179 }
180
181 pub fn git_try_at(&self, cwd: Option<&Path>, args: &[&str]) -> String {
182 let mut argv = vec!["git".to_string()];
183 argv.extend(args.iter().map(|s| s.to_string()));
184 proc::run(&argv, &self.git_opts(cwd, false)).unwrap_or_default()
185 }
186
187 pub fn default_branch(&self, configured: &str) -> String {
190 let refname = self.git_try(&["symbolic-ref", "refs/remotes/origin/HEAD"]);
191 match refname.trim().rsplit('/').next() {
192 Some(name) if !name.is_empty() => name.to_string(),
193 _ => configured.to_string(),
194 }
195 }
196
197 pub fn branch_for_issue(&self, issue: i64) -> String {
205 format!("{}issue-{issue}", self.branch_prefix)
206 }
207
208 pub fn branch_for_pr(&self, number: i64) -> String {
209 format!("{}pr-{number}", self.branch_prefix)
210 }
211
212 fn ledger_path(&self) -> PathBuf {
213 self.root.join(STATE_DIR).join("branches.json")
214 }
215
216 pub fn known_branches(&self) -> BTreeMap<String, BranchRecord> {
217 std::fs::read_to_string(self.ledger_path())
218 .ok()
219 .and_then(|text| serde_json::from_str(&text).ok())
220 .unwrap_or_default()
221 }
222
223 pub fn record_branch(&self, branch: &str, kind: &str, number: i64) {
224 let mut data = self.known_branches();
225 data.insert(
226 branch.to_string(),
227 BranchRecord {
228 kind: kind.to_string(),
229 number,
230 },
231 );
232 if let Err(e) = write_json_atomic(&self.ledger_path(), &data) {
233 logdim!("could not record branch {branch}: {e}");
234 }
235 }
236
237 pub fn forget_branch(&self, branch: &str) {
238 let mut data = self.known_branches();
239 if data.remove(branch).is_none() {
240 return;
241 }
242 if let Err(e) = write_json_atomic(&self.ledger_path(), &data) {
243 logdim!("could not update the branch record: {e}");
244 }
245 }
246
247 fn worktree_path(&self, name: &str) -> PathBuf {
250 self.root.join(WORKTREE_DIR).join(name)
251 }
252
253 pub fn worktree_add(&self, issue: i64, base: &str) -> Result<(PathBuf, String)> {
255 let branch = self.branch_for_issue(issue);
256 let path = self.worktree_path(&format!("issue-{issue}"));
257
258 self.git_try(&["fetch", "origin", base]);
259
260 self.git_try(&["fetch", "origin", &branch]);
269 let remote_branch = format!("origin/{branch}");
270 if self.rev_exists(&self.root, &remote_branch) {
271 let range = format!("origin/{base}..{remote_branch}");
272 let ahead: u32 = self
273 .git_try(&["rev-list", "--count", &range])
274 .trim()
275 .parse()
276 .unwrap_or(0);
277 if ahead > 0 {
278 bail!(
279 "origin/{branch} already has {ahead} commit(s) that are not on {base}, and no \
280 open pull request accounts for them. Rebuilding it would force push over \
281 that work.\nOpen a pull request for the branch and run `spar resume <pr>` to \
282 continue it, or delete it with `git push origin --delete {branch}` if it is \
283 stale."
284 );
285 }
286 }
287
288 self.worktree_remove(issue);
289 self.git_try(&["branch", "-D", &branch]);
290
291 if let Some(parent) = path.parent() {
292 std::fs::create_dir_all(parent)
293 .map_err(|e| spar_err!("could not create {}: {e}", parent.display()))?;
294 }
295
296 let path_str = path.display().to_string();
297 let remote_start = format!("origin/{base}");
298 let created = self
299 .git(&["worktree", "add", "-b", &branch, &path_str, &remote_start])
300 .or_else(|_| self.git(&["worktree", "add", "-b", &branch, &path_str, base]));
301
302 created.map_err(|e| {
305 spar_err!(
306 "could not create a worktree for issue #{issue}. {}\nIs `{base}` a real branch, \
307 and does `origin` exist?",
308 e.last_line()
309 )
310 })?;
311 self.record_branch(&branch, "issue", issue);
312 Ok((path, branch))
313 }
314
315 pub fn worktree_remove(&self, issue: i64) {
316 self.remove_worktree_at(&self.worktree_path(&format!("issue-{issue}")));
317 }
318
319 fn remove_worktree_at(&self, path: &Path) {
320 let path_str = path.display().to_string();
321 self.git_try(&["worktree", "remove", "--force", &path_str]);
322 if path.is_dir() {
323 let _ = std::fs::remove_dir_all(path);
324 }
325 self.git_try(&["worktree", "prune"]);
326 }
327
328 pub fn worktree_for_pr(&self, pr: &PrView) -> Result<(PathBuf, String)> {
330 let head = pr.head_ref_name.clone();
331 if head.trim().is_empty() {
332 bail!("PR #{} has no head branch to check out", pr.number);
333 }
334 let path = self.worktree_path(&format!("pr-{}", pr.number));
335 let local = self.branch_for_pr(pr.number);
336
337 self.git(&["fetch", "origin", &head]).map_err(|e| {
338 spar_err!(
339 "could not fetch the branch behind PR #{}: {}",
340 pr.number,
341 e.last_line()
342 )
343 })?;
344 self.remove_worktree_at(&path);
345 self.git_try(&["branch", "-D", &local]);
346
347 let path_str = path.display().to_string();
348 let start = format!("origin/{head}");
349 self.git(&["worktree", "add", "-B", &local, &path_str, &start])?;
350 self.record_branch(&local, "pr", pr.number);
351 Ok((path, head))
352 }
353
354 pub fn worktree_for_pr_head(&self, number: i64) -> Result<PathBuf> {
364 let path = self.worktree_path(&format!("review-{number}"));
365 let local_ref = review_ref(number);
366 let refspec = format!("+refs/pull/{number}/head:{local_ref}");
367
368 self.git(&["fetch", "origin", &refspec]).map_err(|e| {
369 spar_err!(
370 "could not fetch the head of PR #{number}. {}\nGitHub serves refs/pull/N/head for \
371 every pull request, so this usually means the number is wrong or `origin` does \
372 not point at the repository the PR is on.",
373 e.last_line()
374 )
375 })?;
376
377 if let Some(parent) = path.parent() {
378 std::fs::create_dir_all(parent)
379 .map_err(|e| spar_err!("could not create {}: {e}", parent.display()))?;
380 }
381 self.remove_worktree_at(&path);
382 let path_str = path.display().to_string();
383 self.git(&["worktree", "add", "--detach", &path_str, &local_ref])?;
384 Ok(path)
385 }
386
387 pub fn release_review_worktree(&self, number: i64) {
388 self.remove_worktree_at(&self.worktree_path(&format!("review-{number}")));
389 self.git_try(&["update-ref", "-d", &review_ref(number)]);
390 }
391
392 pub fn release_pr_worktree(&self, number: i64) {
393 let path = self.worktree_path(&format!("pr-{number}"));
394 self.remove_worktree_at(&path);
395 let local = self.branch_for_pr(number);
396 self.git_try(&["branch", "-D", &local]);
397 self.forget_branch(&local);
398 }
399
400 pub fn base_ref(&self, cwd: &Path, base: &str) -> String {
411 let remote = format!("origin/{base}");
412 if self.rev_exists(cwd, &remote) {
413 return remote;
414 }
415 if self.rev_exists(cwd, base) {
416 logdim!("origin/{base} does not resolve, comparing against local {base}");
417 return base.to_string();
418 }
419 logdim!("neither origin/{base} nor {base} resolves; results will be unreliable");
420 remote
421 }
422
423 fn rev_exists(&self, cwd: &Path, refname: &str) -> bool {
424 let spec = format!("{refname}^{{commit}}");
425 !self
426 .git_try_at(Some(cwd), &["rev-parse", "--verify", "--quiet", &spec])
427 .trim()
428 .is_empty()
429 }
430
431 pub fn has_changes(&self, cwd: &Path, base: &str) -> bool {
432 let range = format!("{}..HEAD", self.base_ref(cwd, base));
433 !self
434 .git_try_at(Some(cwd), &["log", &range, "--oneline"])
435 .trim()
436 .is_empty()
437 }
438
439 pub fn diff_stat(&self, cwd: &Path, base: &str) -> String {
440 let range = format!("{}...HEAD", self.base_ref(cwd, base));
441 let full = self.git_try_at(Some(cwd), &["diff", &range, "--shortstat"]);
442 full.trim().to_string()
443 }
444
445 pub fn rewrite_commits_if_needed(&self, cwd: &Path, base: &str) -> Result<()> {
450 let range = format!("{}..HEAD", self.base_ref(cwd, base));
451 let raw = self.git_try_at(Some(cwd), &["log", &range, "--format=%H%x00%B%x1e"]);
452
453 let offenders = raw
454 .split('\x1e')
455 .filter_map(|entry| entry.split_once('\0'))
456 .filter(|(_, body)| !style::violations(body, &self.style).is_empty())
457 .count();
458 if offenders == 0 {
459 return Ok(());
460 }
461 logdim!("{offenders} commit message(s) violated style rules, rewriting");
462
463 let exe = self_binary()?;
464 let filter = format!("{} scrub-filter", sh_quote(&exe.display().to_string()));
465
466 let argv: Vec<String> = [
467 "git",
468 "filter-branch",
469 "-f",
470 "--msg-filter",
471 &filter,
472 &range,
473 ]
474 .iter()
475 .map(|s| s.to_string())
476 .collect();
477 let opts = ExecOpts::new()
478 .cwd(cwd)
479 .check(false)
480 .timeout_secs(600)
481 .env("FILTER_BRANCH_SQUELCH_WARNING", "1")
482 .env("SPAR_BAN_EM_DASH", bool_env(self.style.ban_em_dash))
483 .env(
484 "SPAR_BAN_AI_ATTRIBUTION",
485 bool_env(self.style.ban_ai_attribution),
486 );
487 let _ = proc::run(&argv, &opts);
488
489 let after = self.git_try_at(Some(cwd), &["log", &range, "--format=%B"]);
490 if !style::violations(&after, &self.style).is_empty() {
491 bail!(
492 "commit messages still violate style rules after a rewrite. Fix them by hand in \
493 {} and rerun.",
494 cwd.display()
495 );
496 }
497 Ok(())
498 }
499
500 pub fn push(&self, cwd: &Path, branch: &str) -> Result<()> {
506 let refspec = format!("HEAD:{branch}");
507 self.git_at(
508 Some(cwd),
509 &["push", "--force-with-lease", "origin", &refspec],
510 )
511 .map(|_| ())
512 .map_err(|e| {
513 spar_err!(
514 "could not push to origin/{branch}. {}\nCheck push access and whether the \
515 branch moved under you.",
516 e.last_line()
517 )
518 })
519 }
520
521 pub fn gh(&self, args: &[&str]) -> Result<String> {
524 self.gh_at(None, args)
525 }
526
527 pub fn gh_at(&self, cwd: Option<&Path>, args: &[&str]) -> Result<String> {
528 let mut argv = vec!["gh".to_string()];
529 argv.extend(args.iter().map(|s| s.to_string()));
530 proc::run(
531 &argv,
532 &ExecOpts::new()
533 .cwd(cwd.unwrap_or(&self.root))
534 .timeout_secs(300),
535 )
536 }
537
538 pub fn gh_try(&self, args: &[&str]) -> String {
539 let mut argv = vec!["gh".to_string()];
540 argv.extend(args.iter().map(|s| s.to_string()));
541 proc::run(
542 &argv,
543 &ExecOpts::new()
544 .cwd(&self.root)
545 .check(false)
546 .timeout_secs(300),
547 )
548 .unwrap_or_default()
549 }
550
551 pub fn fetch_issues(&self, numbers: &[i64]) -> Result<Vec<Issue>> {
552 let mut issues = Vec::new();
553 for number in numbers {
554 let text = self
555 .gh(&[
556 "issue",
557 "view",
558 &number.to_string(),
559 "--json",
560 "number,title,body,labels,state,url",
561 ])
562 .map_err(|e| spar_err!("could not read issue #{number}: {}", e.last_line()))?;
563 let issue: Issue = serde_json::from_str(&text)
564 .map_err(|e| spar_err!("unexpected shape for issue #{number}: {e}"))?;
565 if issue.is_closed() {
566 crate::log!("issue #{number} is closed, skipping");
567 continue;
568 }
569 issues.push(issue);
570 }
571 if issues.is_empty() {
572 bail!("no open issues to work on");
573 }
574 Ok(issues)
575 }
576
577 fn open_numbers(&self, kind: &str, limit: usize, min_number: i64) -> Result<Vec<i64>> {
583 #[derive(Deserialize)]
584 struct Row {
585 number: i64,
586 }
587 let text = self.gh(&[
588 kind,
589 "list",
590 "--state",
591 "open",
592 "--limit",
593 &FETCH_CEILING.to_string(),
594 "--json",
595 "number",
596 ])?;
597 let rows: Vec<Row> = serde_json::from_str(text.trim()).unwrap_or_default();
598 let mut numbers: Vec<i64> = rows.into_iter().map(|r| r.number).collect();
599 numbers.sort_unstable();
600
601 let noun = if kind == "issue" { "issues" } else { "PRs" };
602 let found = numbers.len();
603 if min_number > 0 {
604 numbers.retain(|n| *n >= min_number);
605 let skipped = found - numbers.len();
606 if skipped > 0 {
607 crate::log!("{skipped} open {noun} below #{min_number} skipped");
608 }
609 }
610 if found >= FETCH_CEILING {
611 crate::log!(
612 "more than {FETCH_CEILING} open {noun}; only the first {FETCH_CEILING} were \
613 considered."
614 );
615 }
616 if numbers.len() > limit {
617 crate::log!(
618 "{} open {noun}, taking the {limit} lowest numbered. Raise --limit or name them \
619 explicitly.",
620 numbers.len()
621 );
622 numbers.truncate(limit);
623 }
624 Ok(numbers)
625 }
626
627 pub fn list_open_issues(&self, limit: usize, min_number: i64) -> Result<Vec<i64>> {
629 self.open_numbers("issue", limit, min_number)
630 }
631
632 pub fn list_open_prs(&self, limit: usize, min_number: i64) -> Result<Vec<i64>> {
633 self.open_numbers("pr", limit, min_number)
634 }
635
636 pub fn pr_for_branch(&self, branch: &str) -> Option<PrRef> {
637 let text = self.gh_try(&[
638 "pr",
639 "list",
640 "--head",
641 branch,
642 "--state",
643 "open",
644 "--json",
645 "number,url,title",
646 ]);
647 serde_json::from_str::<Vec<PrRef>>(text.trim())
648 .ok()
649 .and_then(|mut v| {
650 if v.is_empty() {
651 None
652 } else {
653 Some(v.remove(0))
654 }
655 })
656 }
657
658 pub fn item_kind(&self, number: i64) -> Result<ItemKind> {
664 let path = format!("repos/{{owner}}/{{repo}}/issues/{number}");
665 let text = self
666 .gh(&[
667 "api",
668 &path,
669 "--jq",
670 "if .pull_request then \"pr\" else \"issue\" end",
671 ])
672 .map_err(|e| {
673 spar_err!(
674 "no issue or pull request #{number} in this repository. {}",
675 e.last_line()
676 )
677 })?;
678 match text.trim() {
679 "pr" => Ok(ItemKind::Pr),
680 "issue" => Ok(ItemKind::Issue),
681 other => Err(spar_err!(
682 "could not tell whether #{number} is an issue or a pull request (got {other:?})"
683 )),
684 }
685 }
686
687 pub fn open_pr_for_issue(&self, issue: i64) -> Option<PrRef> {
693 if let Some(pr) = self.pr_for_branch(&self.branch_for_issue(issue)) {
694 return Some(pr);
695 }
696 let text = self.gh_try(&[
697 "pr",
698 "list",
699 "--state",
700 "open",
701 "--limit",
702 &FETCH_CEILING.to_string(),
703 "--json",
704 "number,url,title,closingIssuesReferences",
705 ]);
706 find_linked_pr(&text, issue)
707 }
708
709 pub fn pr_view(&self, number: i64) -> Result<PrView> {
710 let text = self.gh(&[
711 "pr",
712 "view",
713 &number.to_string(),
714 "--json",
715 "number,url,title,headRefName,baseRefName,state,closingIssuesReferences,isCrossRepository",
716 ])?;
717 serde_json::from_str(&text).map_err(|e| spar_err!("unexpected shape for PR #{number}: {e}"))
718 }
719
720 pub fn pr_state(&self, number: i64) -> String {
721 let text = self.gh_try(&["pr", "view", &number.to_string(), "--json", "state"]);
722 serde_json::from_str::<Value>(text.trim())
723 .ok()
724 .and_then(|v| v.get("state").and_then(Value::as_str).map(str::to_string))
725 .unwrap_or_default()
726 }
727
728 pub fn create_pr(
729 &self,
730 cwd: &Path,
731 branch: &str,
732 base: &str,
733 title: &str,
734 body: &str,
735 ) -> Result<PrRef> {
736 let title = self.clean_title(title)?;
737 let body = self.clean(body)?;
738 self.gh_at(
739 Some(cwd),
740 &[
741 "pr", "create", "--base", base, "--head", branch, "--title", &title, "--body",
742 &body,
743 ],
744 )
745 .map_err(|e| spar_err!("could not open a PR for {branch}. {}", e.last_line()))?;
746 self.pr_for_branch(branch).ok_or_else(|| {
747 spar_err!("PR creation reported success but none was found for {branch}")
748 })
749 }
750
751 pub fn comment_pr(&self, number: i64, body: &str) -> Result<()> {
752 let body = self.clean(body)?;
753 self.gh(&["pr", "comment", &number.to_string(), "--body", &body])
754 .map(|_| ())
755 }
756
757 pub fn comment_issue(&self, number: i64, body: &str) -> Result<()> {
758 let body = self.clean(body)?;
759 self.gh(&["issue", "comment", &number.to_string(), "--body", &body])
760 .map(|_| ())
761 }
762
763 pub fn close_issue(&self, number: i64, body: &str) -> Result<()> {
768 self.comment_issue(number, body)?;
769 let n = number.to_string();
770 if self
771 .gh(&["issue", "close", &n, "--reason", "not planned"])
772 .is_ok()
773 {
774 return Ok(());
775 }
776 self.gh(&["issue", "close", &n]).map(|_| ()).map_err(|e| {
778 spar_err!(
779 "commented on #{number} but could not close it: {}",
780 e.last_line()
781 )
782 })
783 }
784
785 pub fn create_issue(&self, title: &str, body: &str) -> Result<String> {
786 let title = self.clean_title(title)?;
787 let body = self.clean_issue_body(body)?;
788 Ok(self
789 .gh(&["issue", "create", "--title", &title, "--body", &body])?
790 .trim()
791 .to_string())
792 }
793}
794
795#[derive(Debug, Clone)]
797pub struct ExistingIssue {
798 pub number: i64,
799 pub url: String,
800 pub title: String,
801 pub body: String,
802 pub open: bool,
803}
804
805impl Repo {
806 pub fn find_similar_issue(&self, title: &str, body: &str) -> Option<ExistingIssue> {
814 #[derive(Deserialize)]
815 #[serde(rename_all = "camelCase")]
816 struct Row {
817 number: i64,
818 #[serde(default)]
819 title: String,
820 #[serde(default)]
821 url: String,
822 #[serde(default)]
823 body: String,
824 #[serde(default)]
825 state: String,
826 }
827 if title.trim().is_empty() {
828 return None;
829 }
830 let query: String = title
833 .chars()
834 .filter(|c| !matches!(c, '"' | '\'' | '\n' | '\r'))
835 .take(120)
836 .collect();
837 let text = self.gh_try(&[
838 "issue",
839 "list",
840 "--state",
841 "all",
842 "--limit",
843 "100",
844 "--search",
845 query.trim(),
846 "--json",
847 "number,title,url,body,state",
848 ]);
849 let rows: Vec<Row> = serde_json::from_str(text.trim()).unwrap_or_default();
850 let wanted = format!("{title} {body}");
851
852 rows.into_iter()
853 .find(|row| {
854 let theirs = format!("{} {}", row.title, row.body);
855 row.title.trim().eq_ignore_ascii_case(title.trim())
856 || textsim::same_subject(&wanted, &theirs)
857 })
858 .map(|row| ExistingIssue {
859 number: row.number,
860 url: row.url,
861 title: row.title,
862 open: row.state.eq_ignore_ascii_case("open"),
863 body: row.body,
864 })
865 }
866
867 pub fn find_issue_by_title(&self, title: &str) -> Option<String> {
869 #[derive(Deserialize)]
870 struct Row {
871 title: String,
872 url: String,
873 }
874 let needle = title.trim().to_lowercase();
875 if needle.is_empty() {
876 return None;
877 }
878 let query: String = title
880 .chars()
881 .filter(|c| !matches!(c, '"' | '\'' | '\n' | '\r'))
882 .take(120)
883 .collect();
884 let text = self.gh_try(&[
885 "issue",
886 "list",
887 "--state",
888 "all",
889 "--limit",
890 "100",
891 "--search",
892 query.trim(),
893 "--json",
894 "number,title,url",
895 ]);
896 serde_json::from_str::<Vec<Row>>(text.trim())
897 .ok()?
898 .into_iter()
899 .find(|row| row.title.trim().to_lowercase() == needle)
900 .map(|row| row.url)
901 }
902
903 pub fn merge_pr(&self, number: i64) -> Result<()> {
909 let n = number.to_string();
910 match self.gh(&["pr", "merge", &n, "--squash", "--delete-branch"]) {
911 Ok(_) => Ok(()),
912 Err(e) => {
913 if self.pr_state(number) == "MERGED" {
914 logdim!(
915 "PR #{number} merged; branch cleanup did not finish: {}",
916 e.last_line()
917 );
918 Ok(())
919 } else {
920 Err(spar_err!("could not merge PR #{number}. {}", e.last_line()))
921 }
922 }
923 }
924 }
925
926 pub fn append_local_followup(&self, title: &str, body: &str) -> Option<String> {
934 let path = self.root.join(STATE_DIR).join("followups.md");
935 let heading = format!("## {}", title.trim());
936 if let Ok(existing) = std::fs::read_to_string(&path) {
937 if existing.contains(&heading) {
938 logdim!("follow-up already noted: {title}");
939 return None;
940 }
941 }
942 if let Some(parent) = path.parent() {
943 let _ = std::fs::create_dir_all(parent);
944 }
945 use std::io::Write;
946 let entry = format!("{heading}\n\n{}\n\n", body.trim());
949 match std::fs::OpenOptions::new()
950 .create(true)
951 .append(true)
952 .open(&path)
953 {
954 Ok(mut file) => {
955 let _ = file.write_all(entry.as_bytes());
956 Some(format!("note: {}", title.trim()))
957 }
958 Err(e) => {
959 logdim!("could not write {}: {e}", path.display());
960 None
961 }
962 }
963 }
964
965 pub fn state_path(&self, number: i64) -> PathBuf {
973 self.root
974 .join(STATE_DIR)
975 .join("state")
976 .join(format!("pr-{number}.json"))
977 }
978
979 fn read_local_state(&self, number: i64) -> Option<PersistedState> {
980 let path = self.state_path(number);
981 let text = std::fs::read_to_string(&path).ok()?;
982 match serde_json::from_str(&text) {
983 Ok(state) => Some(state),
984 Err(_) => {
985 logdim!("could not read {}, starting fresh", path.display());
986 None
987 }
988 }
989 }
990
991 pub fn read_state(&self, pr: &PrView) -> Option<PersistedState> {
992 if let Some(local) = self.read_local_state(pr.number) {
993 return Some(local);
994 }
995 if self.state_store.writes_pr() {
996 return self.read_pr_state(pr.number);
997 }
998 None
999 }
1000
1001 fn read_pr_state(&self, number: i64) -> Option<PersistedState> {
1002 for body in self.state_comment_bodies(number).into_iter().rev() {
1003 if let Some(state) = parse_state_comment(&body) {
1004 return Some(state);
1005 }
1006 }
1007 None
1008 }
1009
1010 pub fn write_state(&self, number: i64, state: &PersistedState) -> Result<()> {
1011 if self.state_store.writes_local() {
1012 write_json_atomic(&self.state_path(number), state)?;
1013 }
1014 if self.state_store.writes_pr() {
1015 self.write_pr_state(number, state)?;
1016 }
1017 Ok(())
1018 }
1019
1020 fn write_pr_state(&self, number: i64, state: &PersistedState) -> Result<()> {
1021 let body = format!(
1025 "{STATE_MARKER}\n{}\n-->",
1026 serde_json::to_string_pretty(state)?
1027 );
1028 if let Some(id) = self.state_comment_id(number) {
1029 let path = format!("repos/{{owner}}/{{repo}}/issues/comments/{id}");
1030 let field = format!("body={body}");
1031 self.gh_try(&["api", "-X", "PATCH", &path, "-f", &field, "--silent"]);
1032 return Ok(());
1033 }
1034 self.gh(&["pr", "comment", &number.to_string(), "--body", &body])
1035 .map(|_| ())
1036 }
1037
1038 fn comments_json(&self, number: i64) -> Vec<Value> {
1039 let path = format!("repos/{{owner}}/{{repo}}/issues/{number}/comments");
1040 parse_comment_pages(&self.gh_try(&["api", "--paginate", &path]))
1041 }
1042
1043 fn state_comments(&self, number: i64) -> Vec<(i64, String)> {
1044 self.comments_json(number)
1045 .into_iter()
1046 .filter_map(|c| {
1047 let body = c.get("body").and_then(Value::as_str)?.to_string();
1048 if !body.contains("spar:state") {
1049 return None;
1050 }
1051 let id = c.get("id").and_then(Value::as_i64)?;
1052 Some((id, body))
1053 })
1054 .collect()
1055 }
1056
1057 fn state_comment_bodies(&self, number: i64) -> Vec<String> {
1058 self.state_comments(number)
1059 .into_iter()
1060 .map(|(_, b)| b)
1061 .collect()
1062 }
1063
1064 fn state_comment_id(&self, number: i64) -> Option<i64> {
1065 self.state_comments(number).last().map(|(id, _)| *id)
1066 }
1067
1068 pub fn clear_state(&self, number: i64) {
1070 let path = self.state_path(number);
1071 let _ = std::fs::remove_file(&path);
1072 let _ = std::fs::remove_file(path.with_extension("json.tmp"));
1073 }
1074
1075 pub fn prune_state(&self) -> Vec<String> {
1079 let base = self.root.join(STATE_DIR).join("state");
1080 let Ok(entries) = std::fs::read_dir(&base) else {
1081 return Vec::new();
1082 };
1083 let mut names: Vec<String> = entries
1084 .flatten()
1085 .filter_map(|e| e.file_name().to_str().map(str::to_string))
1086 .filter(|n| n.starts_with("pr-") && n.ends_with(".json"))
1087 .collect();
1088 names.sort();
1089
1090 let mut removed = Vec::new();
1091 for name in names {
1092 let Ok(number) = name[3..name.len() - 5].parse::<i64>() else {
1093 continue;
1094 };
1095 if is_finished(&self.pr_state(number)) {
1096 let _ = std::fs::remove_file(base.join(&name));
1097 removed.push(format!("state {name}"));
1098 }
1099 }
1100 removed
1101 }
1102
1103 pub fn prune_pr_state(&self, numbers: Option<Vec<i64>>) -> Vec<String> {
1107 #[derive(Deserialize)]
1108 struct Row {
1109 number: i64,
1110 }
1111 let numbers = numbers.unwrap_or_else(|| {
1112 let text = self.gh_try(&[
1113 "pr", "list", "--state", "all", "--limit", "200", "--json", "number",
1114 ]);
1115 serde_json::from_str::<Vec<Row>>(text.trim())
1116 .unwrap_or_default()
1117 .into_iter()
1118 .map(|r| r.number)
1119 .collect()
1120 });
1121
1122 let mut removed = Vec::new();
1123 for number in numbers {
1124 if !is_finished(&self.pr_state(number)) {
1125 continue;
1126 }
1127 for (id, _) in self.state_comments(number) {
1128 let path = format!("repos/{{owner}}/{{repo}}/issues/comments/{id}");
1129 self.gh_try(&["api", "-X", "DELETE", &path, "--silent"]);
1130 removed.push(format!("state comment on PR #{number}"));
1131 }
1132 }
1133 removed
1134 }
1135
1136 pub fn prune_worktrees(&self, force_all: bool) -> Vec<String> {
1143 let base = self.root.join(WORKTREE_DIR);
1144 let mut removed = Vec::new();
1145
1146 if let Ok(entries) = std::fs::read_dir(&base) {
1147 let mut names: Vec<String> = entries
1148 .flatten()
1149 .filter(|e| e.path().is_dir())
1150 .filter_map(|e| e.file_name().to_str().map(str::to_string))
1151 .collect();
1152 names.sort();
1153
1154 for name in names {
1155 if let Some(rest) = name.strip_prefix("review-") {
1158 let number: i64 = rest.parse().unwrap_or(-1);
1159 if !(force_all || is_finished(&self.pr_state(number))) {
1160 continue;
1161 }
1162 self.release_review_worktree(number);
1163 removed.push(name);
1164 continue;
1165 }
1166 let branch = format!("{}{name}", self.branch_prefix);
1167 if !(force_all || self.worktree_is_done(&branch)) {
1168 continue;
1169 }
1170 self.remove_worktree_at(&base.join(&name));
1171 self.git_try(&["branch", "-D", &branch]);
1172 self.forget_branch(&branch);
1173 removed.push(name);
1174 }
1175 }
1176 if !removed.is_empty() {
1177 self.git_try(&["worktree", "prune"]);
1178 }
1179 removed.extend(self.prune_branches(force_all));
1180 removed
1181 }
1182
1183 pub fn prune_branches(&self, force_all: bool) -> Vec<String> {
1190 let branches: Vec<String> = self.known_branches().keys().cloned().collect();
1191 if branches.is_empty() {
1192 return Vec::new();
1193 }
1194
1195 let checked_out: Vec<String> = self
1196 .git_try(&["worktree", "list", "--porcelain"])
1197 .lines()
1198 .filter_map(|l| l.strip_prefix("branch refs/heads/").map(str::to_string))
1199 .collect();
1200
1201 let existing: Vec<String> = self
1204 .git_try(&["for-each-ref", "refs/heads/", "--format=%(refname)"])
1205 .lines()
1206 .filter_map(|l| l.trim().strip_prefix("refs/heads/").map(str::to_string))
1207 .collect();
1208
1209 let mut removed = Vec::new();
1210 for branch in branches {
1211 if !existing.contains(&branch) {
1212 self.forget_branch(&branch); continue;
1214 }
1215 if checked_out.contains(&branch) {
1216 continue;
1217 }
1218 if !(force_all || self.worktree_is_done(&branch)) {
1219 continue;
1220 }
1221 match self.git(&["branch", "-D", &branch]) {
1222 Ok(_) => {
1223 self.forget_branch(&branch);
1224 removed.push(format!("branch {branch}"));
1225 }
1226 Err(e) => {
1227 logdim!("could not delete {branch}: {}", e.last_line());
1230 }
1231 }
1232 }
1233 removed
1234 }
1235
1236 fn worktree_is_done(&self, branch: &str) -> bool {
1238 #[derive(Deserialize)]
1239 struct Row {
1240 state: String,
1241 }
1242 let entry = branch
1243 .strip_prefix(self.branch_prefix.as_str())
1244 .unwrap_or(branch);
1245 if let Some(rest) = entry.strip_prefix("pr-") {
1246 return is_finished(&self.pr_state(rest.parse().unwrap_or(-1)));
1247 }
1248 if entry.starts_with("issue-") {
1249 let text = self.gh_try(&[
1250 "pr", "list", "--head", branch, "--state", "all", "--json", "state",
1251 ]);
1252 let rows: Vec<Row> = serde_json::from_str(text.trim()).unwrap_or_default();
1253 return !rows.is_empty() && rows.iter().all(|r| is_finished(&r.state));
1254 }
1255 false
1256 }
1257}
1258
1259#[derive(Debug, Clone, serde::Serialize, Deserialize)]
1264pub struct BranchRecord {
1265 pub kind: String,
1266 pub number: i64,
1267}
1268
1269pub fn review_ref(number: i64) -> String {
1272 format!("refs/spar/pr-{number}")
1273}
1274
1275pub fn is_finished(state: &str) -> bool {
1276 matches!(state.trim().to_uppercase().as_str(), "MERGED" | "CLOSED")
1277}
1278
1279pub fn write_json_atomic<T: serde::Serialize>(path: &Path, value: &T) -> Result<()> {
1282 if let Some(parent) = path.parent() {
1283 std::fs::create_dir_all(parent)
1284 .map_err(|e| spar_err!("could not create {}: {e}", parent.display()))?;
1285 }
1286 let tmp = path.with_extension(format!(
1287 "{}.tmp",
1288 path.extension().and_then(|e| e.to_str()).unwrap_or("json")
1289 ));
1290 std::fs::write(&tmp, serde_json::to_vec_pretty(value)?)
1291 .map_err(|e| spar_err!("could not write {}: {e}", tmp.display()))?;
1292 std::fs::rename(&tmp, path)
1293 .map_err(|e| spar_err!("could not replace {}: {e}", path.display()))?;
1294 Ok(())
1295}
1296
1297pub fn find_linked_pr(json: &str, issue: i64) -> Option<PrRef> {
1304 #[derive(Deserialize)]
1305 #[serde(rename_all = "camelCase")]
1306 struct Row {
1307 number: i64,
1308 #[serde(default)]
1309 url: String,
1310 #[serde(default)]
1311 title: String,
1312 #[serde(default)]
1313 closing_issues_references: Vec<IssueRef>,
1314 }
1315
1316 serde_json::from_str::<Vec<Row>>(json.trim())
1317 .ok()?
1318 .into_iter()
1319 .find(|row| {
1320 row.closing_issues_references
1321 .iter()
1322 .any(|linked| linked.number == issue)
1323 })
1324 .map(|row| PrRef {
1325 number: row.number,
1326 url: row.url,
1327 title: row.title,
1328 })
1329}
1330
1331pub fn parse_comment_pages(text: &str) -> Vec<Value> {
1338 let mut out = Vec::new();
1339 for value in serde_json::Deserializer::from_str(text.trim()).into_iter::<Value>() {
1340 match value {
1341 Ok(Value::Array(items)) => out.extend(items),
1342 Ok(other) => out.push(other),
1343 Err(_) => break,
1344 }
1345 }
1346 out
1347}
1348
1349pub fn parse_state_comment(body: &str) -> Option<PersistedState> {
1352 let marker = body.find(STATE_MARKER)?;
1353 let start = body[marker..].find('{')? + marker;
1354 let end = body.rfind('}')?;
1355 if end <= start {
1356 return None;
1357 }
1358 match serde_json::from_str(&body[start..=end]) {
1359 Ok(state) => Some(state),
1360 Err(_) => {
1361 logdim!("found a spar state comment but could not parse it");
1362 None
1363 }
1364 }
1365}
1366
1367pub fn self_binary() -> Result<PathBuf> {
1373 if let Some(path) = std::env::var_os("SPAR_SELF_BIN") {
1374 let path = PathBuf::from(path);
1375 if proc::is_executable(&path) {
1376 return Ok(path);
1377 }
1378 bail!(
1379 "SPAR_SELF_BIN is set to {}, which is not executable",
1380 path.display()
1381 );
1382 }
1383 std::env::current_exe()
1384 .map_err(|e| spar_err!("could not locate the spar binary for a commit rewrite: {e}"))
1385}
1386
1387fn bool_env(value: bool) -> &'static str {
1388 if value {
1389 "1"
1390 } else {
1391 "0"
1392 }
1393}
1394
1395pub fn sh_quote(text: &str) -> String {
1398 format!("'{}'", text.replace('\'', r"'\''"))
1399}
1400
1401pub fn style_from_env() -> Style {
1404 let flag = |key: &str| !matches!(std::env::var(key).as_deref(), Ok("0"));
1405 Style {
1406 ban_em_dash: flag("SPAR_BAN_EM_DASH"),
1407 ban_ai_attribution: flag("SPAR_BAN_AI_ATTRIBUTION"),
1408 ..Style::permissive()
1409 }
1410}
1411
1412#[cfg(test)]
1413mod tests {
1414 use super::*;
1415 use crate::config::StateStore;
1416 use crate::model::{Ledger, Status};
1417
1418 fn repo_for_titles() -> Repo {
1419 Repo {
1420 root: PathBuf::from("/nonexistent"),
1421 style: Style::default(),
1422 branch_prefix: String::new(),
1423 state_store: StateStore::Local,
1424 followups: crate::config::Followups::Issues,
1425 }
1426 }
1427
1428 #[test]
1432 fn clean_title_is_idempotent_even_when_the_scrub_lengthens_it() {
1433 let repo = repo_for_titles();
1434 for raw in [
1435 "Retry loop spins \u{2014} Retry-After parses to zero",
1436 "plain title",
1437 " spread over\nlines ",
1438 "\u{1F916} Generated with something",
1439 &format!("a \u{2014} {}", "very long title ".repeat(20)),
1440 &"x".repeat(300),
1441 &format!("{} \u{2014} end", "y".repeat(88)),
1442 &{
1447 let tail = "a\u{2014}b c\u{2014}d";
1448 let pad = Style::default().max_title_chars - tail.chars().count();
1449 format!("{}{tail}", "w".repeat(pad))
1450 },
1451 ] {
1452 let once = repo.clean_title(raw).unwrap();
1453 let twice = repo.clean_title(&once).unwrap();
1454 assert_eq!(once, twice, "not idempotent for {raw:?}");
1455 assert!(
1456 once.chars().count() <= repo.style.max_title_chars,
1457 "over budget: {once:?}"
1458 );
1459 assert!(style::violations(&once, &repo.style).is_empty(), "{once:?}");
1460 }
1461 }
1462
1463 #[test]
1464 fn a_title_with_an_em_dash_survives_as_readable_text() {
1465 let repo = repo_for_titles();
1466 assert_eq!(
1467 "Retry loop spins, Retry-After parses to zero",
1468 repo.clean_title("Retry loop spins \u{2014} Retry-After parses to zero")
1469 .unwrap()
1470 );
1471 }
1472
1473 #[test]
1474 fn sh_quote_survives_a_quote() {
1475 assert_eq!(r"'a'\''b'", sh_quote("a'b"));
1476 }
1477
1478 #[test]
1479 fn sh_quote_wraps_a_space() {
1480 assert_eq!(
1481 "'/Applications/My App/spar'",
1482 sh_quote("/Applications/My App/spar")
1483 );
1484 }
1485
1486 #[test]
1487 fn finished_states_are_recognised_case_insensitively() {
1488 assert!(is_finished("MERGED"));
1489 assert!(is_finished("closed"));
1490 assert!(!is_finished("OPEN"));
1491 assert!(!is_finished(""));
1492 }
1493
1494 fn state() -> PersistedState {
1495 PersistedState {
1496 version: 1,
1497 round: 4,
1498 next_actor: "codex".into(),
1499 status: Status::Pending,
1500 ledger: Ledger::new(),
1501 filed: vec![],
1502 }
1503 }
1504
1505 #[test]
1506 fn a_state_comment_round_trips() {
1507 let body = format!(
1508 "{STATE_MARKER}\n{}\n-->",
1509 serde_json::to_string(&state()).unwrap()
1510 );
1511 let back = parse_state_comment(&body).unwrap();
1512 assert_eq!(4, back.round);
1513 assert_eq!("codex", back.next_actor);
1514 }
1515
1516 #[test]
1518 fn the_state_block_is_an_html_comment() {
1519 let body = format!(
1520 "{STATE_MARKER}\n{}\n-->",
1521 serde_json::to_string(&state()).unwrap()
1522 );
1523 assert!(body.starts_with("<!--"));
1524 assert!(body.trim_end().ends_with("-->"));
1525 assert!(!body[..body.find('{').unwrap()].contains("-->"));
1526 }
1527
1528 #[test]
1529 fn an_unrelated_json_block_is_not_state() {
1530 assert!(parse_state_comment("here is a snippet\n```json\n{\"round\": 99}\n```").is_none());
1531 }
1532
1533 #[test]
1534 fn a_malformed_state_comment_is_none_not_a_panic() {
1535 assert!(parse_state_comment(&format!("{STATE_MARKER}\n{{not json\n-->")).is_none());
1536 }
1537
1538 #[test]
1539 fn atomic_write_leaves_no_temp_file() {
1540 let dir = std::env::temp_dir().join(format!("spar-atomic-{}", std::process::id()));
1541 let _ = std::fs::remove_dir_all(&dir);
1542 let path = dir.join("state").join("pr-7.json");
1543 write_json_atomic(&path, &state()).unwrap();
1544 let files: Vec<String> = std::fs::read_dir(path.parent().unwrap())
1545 .unwrap()
1546 .flatten()
1547 .filter_map(|e| e.file_name().to_str().map(str::to_string))
1548 .collect();
1549 assert_eq!(vec!["pr-7.json".to_string()], files);
1550 let _ = std::fs::remove_dir_all(&dir);
1551 }
1552
1553 #[test]
1554 fn atomic_write_overwrites_rather_than_accumulating() {
1555 let dir = std::env::temp_dir().join(format!("spar-overwrite-{}", std::process::id()));
1556 let _ = std::fs::remove_dir_all(&dir);
1557 let path = dir.join("pr-7.json");
1558 for round in 1..4 {
1559 let mut s = state();
1560 s.round = round;
1561 write_json_atomic(&path, &s).unwrap();
1562 }
1563 let back: PersistedState =
1564 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
1565 assert_eq!(3, back.round);
1566 let _ = std::fs::remove_dir_all(&dir);
1567 }
1568
1569 #[test]
1570 fn style_from_env_defaults_to_enforcing() {
1571 std::env::remove_var("SPAR_BAN_EM_DASH");
1572 std::env::remove_var("SPAR_BAN_AI_ATTRIBUTION");
1573 let style = style_from_env();
1574 assert!(style.ban_em_dash && style.ban_ai_attribution);
1575 assert!(
1576 !style.terse,
1577 "the commit filter must not truncate a commit message"
1578 );
1579 }
1580}
1581
1582#[cfg(test)]
1583mod comment_page_tests {
1584 use super::*;
1585
1586 #[test]
1587 fn a_single_merged_array_is_read() {
1588 let pages = parse_comment_pages(r#"[{"id":1,"body":"a"},{"id":2,"body":"b"}]"#);
1589 assert_eq!(2, pages.len());
1590 assert_eq!(Some(2), pages[1]["id"].as_i64());
1591 }
1592
1593 #[test]
1594 fn concatenated_pages_from_an_older_gh_are_read_too() {
1595 let pages = parse_comment_pages(r#"[{"id":1}][{"id":2}]"#);
1596 assert_eq!(2, pages.len());
1597 }
1598
1599 #[test]
1603 fn a_comment_body_containing_a_bracket_pair_is_not_mistaken_for_a_page_break() {
1604 let text = r#"[{"id":1,"body":"see [the docs][ref] for why"},{"id":2,"body":"ok"}]"#;
1605 let pages = parse_comment_pages(text);
1606 assert_eq!(2, pages.len(), "{pages:?}");
1607 assert!(pages[0]["body"].as_str().unwrap().contains("[ref]"));
1608 }
1609
1610 #[test]
1611 fn empty_output_is_no_comments_not_a_panic() {
1612 assert!(parse_comment_pages("").is_empty());
1613 assert!(parse_comment_pages(" ").is_empty());
1614 assert!(parse_comment_pages("[]").is_empty());
1615 }
1616
1617 #[test]
1618 fn a_gh_error_message_on_stdout_yields_nothing_rather_than_garbage() {
1619 assert!(parse_comment_pages("gh: Not Found (HTTP 404)").is_empty());
1620 }
1621
1622 #[test]
1623 fn state_is_found_in_the_last_matching_comment() {
1624 let payload = |round: u32| {
1625 format!(
1626 "{STATE_MARKER}\n{{\"version\":1,\"round\":{round},\"next_actor\":\"a\",\"status\":\"pending\",\"ledger\":{{}},\"filed\":[]}}\n-->"
1627 )
1628 };
1629 let text = serde_json::to_string(&serde_json::json!([
1630 {"id": 1, "body": payload(1)},
1631 {"id": 2, "body": "looks good to me"},
1632 {"id": 3, "body": payload(5)},
1633 ]))
1634 .unwrap();
1635 let pages = parse_comment_pages(&text);
1636 let last = pages
1637 .iter()
1638 .rev()
1639 .find_map(|c| parse_state_comment(c["body"].as_str().unwrap_or("")))
1640 .unwrap();
1641 assert_eq!(5, last.round);
1642 }
1643}
1644
1645#[cfg(test)]
1646mod linked_pr_tests {
1647 use super::*;
1648
1649 const REAL_PAYLOAD: &str = r#"[
1654 {"number":14252,"title":"fix: reject leading-dash branch names",
1655 "url":"https://github.com/cli/cli/pull/14252",
1656 "closingIssuesReferences":[{"id":"I_kwDO","number":14238,
1657 "repository":{"id":"MDEwOlJl","name":"cli","owner":{"id":"MDEy","login":"cli"}},
1658 "url":"https://github.com/cli/cli/issues/14238"}]},
1659 {"number":14217,"title":"another change",
1660 "url":"https://github.com/cli/cli/pull/14217",
1661 "closingIssuesReferences":[{"id":"I_kwDO","number":9761,
1662 "repository":{"id":"MDEwOlJl","name":"cli","owner":{"id":"MDEy","login":"cli"}},
1663 "url":"https://github.com/cli/cli/issues/9761"}]},
1664 {"number":14200,"title":"unlinked work",
1665 "url":"https://github.com/cli/cli/pull/14200","closingIssuesReferences":[]}
1666 ]"#;
1667
1668 #[test]
1669 fn a_linked_pr_is_found_whatever_its_branch_is_called() {
1670 let pr = find_linked_pr(REAL_PAYLOAD, 14238).expect("should find it");
1671 assert_eq!(14252, pr.number);
1672 assert_eq!("https://github.com/cli/cli/pull/14252", pr.url);
1673 }
1674
1675 #[test]
1676 fn the_right_pr_is_picked_out_of_several() {
1677 assert_eq!(14217, find_linked_pr(REAL_PAYLOAD, 9761).unwrap().number);
1678 }
1679
1680 #[test]
1681 fn an_issue_nobody_is_working_on_finds_nothing() {
1682 assert!(find_linked_pr(REAL_PAYLOAD, 99999).is_none());
1683 }
1684
1685 #[test]
1686 fn an_unlinked_pr_is_never_matched() {
1687 for issue in [14200, 0, 1] {
1689 if let Some(pr) = find_linked_pr(REAL_PAYLOAD, issue) {
1690 assert_ne!(14200, pr.number, "matched a PR that closes nothing");
1691 }
1692 }
1693 }
1694
1695 #[test]
1696 fn empty_or_broken_output_is_none_rather_than_a_panic() {
1697 assert!(find_linked_pr("", 1).is_none());
1698 assert!(find_linked_pr("[]", 1).is_none());
1699 assert!(find_linked_pr("gh: Not Found (HTTP 404)", 1).is_none());
1700 assert!(find_linked_pr("[{\"number\":", 1).is_none());
1701 }
1702
1703 #[test]
1705 fn pr_view_reads_the_cross_repository_flag() {
1706 let json = r#"{"number":7,"url":"u","title":"t","headRefName":"patch-1",
1707 "baseRefName":"main","state":"OPEN",
1708 "closingIssuesReferences":[],"isCrossRepository":true}"#;
1709 let pr: PrView = serde_json::from_str(json).unwrap();
1710 assert!(pr.is_cross_repository);
1711 assert!(pr.is_open());
1712
1713 let same_repo = json.replace("\"isCrossRepository\":true", "\"isCrossRepository\":false");
1714 assert!(
1715 !serde_json::from_str::<PrView>(&same_repo)
1716 .unwrap()
1717 .is_cross_repository
1718 );
1719 }
1720}
1721
1722#[cfg(test)]
1723mod min_number_tests {
1724 fn pick(open: &[i64], limit: usize, min_number: i64) -> Vec<i64> {
1730 let mut numbers: Vec<i64> = open.to_vec();
1731 numbers.sort_unstable();
1732 if min_number > 0 {
1733 numbers.retain(|n| *n >= min_number);
1734 }
1735 numbers.truncate(limit);
1736 numbers
1737 }
1738
1739 #[test]
1740 fn the_floor_is_applied_before_the_cap_not_after() {
1741 let open = [12, 13, 14, 480, 481, 482];
1742 assert_eq!(vec![480, 481], pick(&open, 2, 480));
1743 assert!(!pick(&open, 2, 480).is_empty());
1746 }
1747
1748 #[test]
1749 fn no_floor_keeps_the_old_behaviour() {
1750 assert_eq!(vec![12, 13], pick(&[12, 13, 14, 480], 2, 0));
1751 }
1752
1753 #[test]
1754 fn the_floor_is_inclusive() {
1755 assert_eq!(vec![480, 481], pick(&[479, 480, 481], 10, 480));
1756 }
1757
1758 #[test]
1759 fn a_floor_above_everything_open_yields_nothing() {
1760 assert!(pick(&[1, 2, 3], 10, 9999).is_empty());
1761 }
1762}