forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.7 MiB37e985dTahinli2 hours ago363 commits

pulls_sync.rs829 lines, 25.8 KiBHistoryRawDownload
1use std::collections::HashMap;
2use std::path::Path;
3
4use forge_core::reviews::{ReviewComment, Side};
5use forge_core::{Mergeable, PullRequest, Repo};
6use forge_git::pulls::{
7 MergeCheck, blob_at, check_merge, fetch_into_pull_ref, is_ancestor, merge_base,
8};
9use serde_json::json;
10use similar::{ChangeTag, TextDiff};
11
12use crate::internal::is_zero_oid;
13use crate::notify::{self, Activity};
14use crate::state::AppState;
15use crate::webhooks::emit::{emit_item_action, lookup_sender};
16
17pub async fn sync_pulls_for_ref(
18 state: &AppState,
19 repo: &Repo,
20 actor_id: &str,
21 refname: &str,
22 old: &str,
23 new: &str,
24) -> forge_core::Result<()> {
25 let Some(branch) = refname.strip_prefix("refs/heads/") else {
26 return Ok(());
27 };
28 let heads = state.store.open_pulls_for_head(&repo.id, branch).await?;
29 for (base_repo_id, number, issue_id) in heads {
30 if let Err(e) = sync_head(
31 state,
32 repo,
33 actor_id,
34 &base_repo_id,
35 number,
36 &issue_id,
37 branch,
38 old,
39 new,
40 )
41 .await
42 {
43 tracing::error!(error = %e, repo_id = %repo.id, number, "pull sync: head update failed");
44 }
45 crate::live::bump_pull(state, &repo.id, &issue_id);
46 }
47 let bases = state.store.open_pulls_for_base(&repo.id, branch).await?;
48 for (number, issue_id) in bases {
49 if let Err(e) = sync_base(state, repo, actor_id, number, &issue_id, old, new).await {
50 tracing::error!(error = %e, repo_id = %repo.id, number, "pull sync: base update failed");
51 }
52 crate::live::bump_pull(state, &repo.id, &issue_id);
53 }
54 Ok(())
55}
56
57#[allow(clippy::too_many_arguments)]
58async fn sync_head(
59 state: &AppState,
60 head_repo: &Repo,
61 actor_id: &str,
62 base_repo_id: &str,
63 number: i64,
64 issue_id: &str,
65 branch: &str,
66 old: &str,
67 new: &str,
68) -> forge_core::Result<()> {
69 let actor = (!actor_id.is_empty()).then_some(actor_id);
70 let Some(pr) = state.store.pull_by_number(base_repo_id, number).await? else {
71 return Ok(());
72 };
73 if is_zero_oid(new) {
74 state
75 .store
76 .add_event(
77 issue_id,
78 actor,
79 "head_deleted",
80 json!({"branch": branch, "last_head": pr.head_oid}),
81 )
82 .await?;
83 return Ok(());
84 }
85 let Some(base_repo) = state.store.repo_by_id(base_repo_id).await? else {
86 return Ok(());
87 };
88 let base_path = forge_git::repo::repo_path(&state.git.data_dir, &base_repo.id)?;
89 let head_path = forge_git::repo::repo_path(&state.git.data_dir, &head_repo.id)?;
90 fetch_into_pull_ref(&state.git, &base_path, &head_path, new, number).await?;
91 state.store.update_pull_head(issue_id, new).await?;
92 crate::merge_queue::drop_pull(state, &base_repo, issue_id, "head_changed").await;
93 if pr.auto_merge.is_some() {
94 state
95 .store
96 .add_event(
97 issue_id,
98 actor,
99 "auto_merge_disabled",
100 json!({"reason": "push"}),
101 )
102 .await?;
103 }
104 mark_outdated_threads(state, &base_path, issue_id, new).await;
105 state
106 .store
107 .add_event(
108 issue_id,
109 actor,
110 "head_updated",
111 json!({"from": old, "to": new}),
112 )
113 .await?;
114 let sender = lookup_sender(state, actor_id).await;
115 emit_item_action(state, &base_repo, &sender, true, number, "synchronize").await;
116 let Some(pr) = state.store.pull_by_number(base_repo_id, number).await? else {
117 return Ok(());
118 };
119 notify::dispatch(
120 state,
121 &base_repo,
122 &pr.issue,
123 actor_id,
124 Activity::Pushed,
125 None,
126 )
127 .await;
128 crate::ci::on_pull_event(state, &base_repo, &pr, actor_id).await;
129 refresh_mergeability(state, &base_repo, &pr).await
130}
131
132const MAX_REMAP_BYTES: usize = 1024 * 1024;
133
134async fn mark_outdated_threads(state: &AppState, base_path: &Path, issue_id: &str, new_head: &str) {
135 let threads = match state.store.review_threads(issue_id, None).await {
136 Ok(threads) => threads,
137 Err(e) => {
138 tracing::error!(error = %e, issue_id, "pull sync: loading review threads failed");
139 return;
140 }
141 };
142 let mut cache = HashMap::new();
143 let mut texts = HashMap::new();
144 let mut outdated = Vec::new();
145 let mut moved = Vec::new();
146 for thread in threads {
147 let root = thread.root;
148 if root.outdated {
149 continue;
150 }
151 let old = cached_blob(state, base_path, &mut cache, &root.commit_oid, &root.path).await;
152 let new = cached_blob(state, base_path, &mut cache, new_head, &root.path).await;
153 let (old, new) = match (old, new) {
154 (Ok(old), Ok(new)) => (old, new),
155 (Err(e), _) | (_, Err(e)) => {
156 tracing::error!(error = %e, comment_id = %root.id, "pull sync: blob lookup failed");
157 continue;
158 }
159 };
160 if old == new {
161 continue;
162 }
163 let target = if root.side == Side::New && old.is_some() && new.is_some() {
164 match remap_root_line(base_path, &mut texts, &root, new_head) {
165 Ok(target) => target,
166 Err(e) => {
167 tracing::error!(error = %e, comment_id = %root.id, "pull sync: text lookup failed");
168 continue;
169 }
170 }
171 } else {
172 None
173 };
174 match target {
175 Some(line) => moved.push((root.id.clone(), line)),
176 None => outdated.push(root.id.clone()),
177 }
178 }
179 for (id, line) in &moved {
180 if let Err(e) = state
181 .store
182 .relocate_review_comment(id, *line, new_head)
183 .await
184 {
185 tracing::error!(error = %e, comment_id = %id, "pull sync: relocating comment failed");
186 }
187 }
188 if outdated.is_empty() {
189 return;
190 }
191 if let Err(e) = state.store.mark_outdated(issue_id, &outdated).await {
192 tracing::error!(error = %e, issue_id, "pull sync: marking comments outdated failed");
193 }
194}
195
196fn remap_root_line(
197 base_path: &Path,
198 texts: &mut HashMap<(String, String), Option<String>>,
199 root: &ReviewComment,
200 new_head: &str,
201) -> forge_core::Result<Option<u32>> {
202 let old = cached_text(base_path, texts, &root.commit_oid, &root.path)?;
203 let new = cached_text(base_path, texts, new_head, &root.path)?;
204 Ok(match (old, new) {
205 (Some(old), Some(new)) => unchanged_line(&old, &new, root.line),
206 _ => None,
207 })
208}
209
210fn unchanged_line(old: &str, new: &str, old_line: u32) -> Option<u32> {
211 let index = usize::try_from(old_line).ok()?.checked_sub(1)?;
212 TextDiff::from_lines(old, new)
213 .iter_all_changes()
214 .find(|change| change.tag() == ChangeTag::Equal && change.old_index() == Some(index))
215 .and_then(|change| change.new_index())
216 .and_then(|index| u32::try_from(index + 1).ok())
217}
218
219fn cached_text(
220 base_path: &Path,
221 cache: &mut HashMap<(String, String), Option<String>>,
222 commit: &str,
223 path: &str,
224) -> forge_core::Result<Option<String>> {
225 let key = (commit.to_owned(), path.to_owned());
226 if let Some(text) = cache.get(&key) {
227 return Ok(text.clone());
228 }
229 let text = forge_git::browse::read_text_blob(base_path, commit, path, MAX_REMAP_BYTES)?;
230 cache.insert(key, text.clone());
231 Ok(text)
232}
233
234async fn cached_blob(
235 state: &AppState,
236 base_path: &Path,
237 cache: &mut HashMap<(String, String), Option<String>>,
238 commit: &str,
239 path: &str,
240) -> forge_core::Result<Option<String>> {
241 let key = (commit.to_owned(), path.to_owned());
242 if let Some(blob) = cache.get(&key) {
243 return Ok(blob.clone());
244 }
245 let blob = blob_at(&state.git, base_path, commit, path).await?;
246 cache.insert(key, blob.clone());
247 Ok(blob)
248}
249
250async fn sync_base(
251 state: &AppState,
252 repo: &Repo,
253 actor_id: &str,
254 number: i64,
255 issue_id: &str,
256 old: &str,
257 new: &str,
258) -> forge_core::Result<()> {
259 if is_zero_oid(new) {
260 return Ok(());
261 }
262 let Some(pr) = state.store.pull_by_number(&repo.id, number).await? else {
263 return Ok(());
264 };
265 let base_path = forge_git::repo::repo_path(&state.git.data_dir, &repo.id)?;
266 if is_ancestor(&state.git, &base_path, &pr.head_oid, new).await? {
267 let was_contained =
268 !is_zero_oid(old) && is_ancestor(&state.git, &base_path, &pr.head_oid, old).await?;
269 if !was_contained {
270 state.store.mark_merged(issue_id, actor_id, new).await?;
271 let sender = lookup_sender(state, actor_id).await;
272 emit_item_action(state, repo, &sender, true, number, "merged").await;
273 notify::dispatch(state, repo, &pr.issue, actor_id, Activity::Merged, None).await;
274 return Ok(());
275 }
276 }
277 refresh_mergeability(state, repo, &pr).await
278}
279
280pub async fn refresh_mergeability(
281 state: &AppState,
282 base_repo: &Repo,
283 pr: &PullRequest,
284) -> forge_core::Result<()> {
285 let base_path = forge_git::repo::repo_path(&state.git.data_dir, &base_repo.id)?;
286 let Some(base_tip) = forge_git::browse::default_branch_commit(&base_path, &pr.base_branch)?
287 else {
288 return Ok(());
289 };
290 let (mergeable, files) =
291 match check_merge(&state.git, &base_path, &base_tip, &pr.head_oid).await? {
292 MergeCheck::Clean { .. } => (Mergeable::Clean, Vec::new()),
293 MergeCheck::Conflict { files } => (Mergeable::Conflict, files),
294 MergeCheck::UpToDate => (Mergeable::UpToDate, Vec::new()),
295 };
296 let base_of_head = merge_base(&state.git, &base_path, &base_tip, &pr.head_oid).await?;
297 state
298 .store
299 .set_mergeability(
300 &pr.issue.id,
301 &base_tip,
302 base_of_head.as_deref(),
303 mergeable,
304 &files,
305 )
306 .await?;
307 crate::live::bump_pull(state, &base_repo.id, &pr.issue.id);
308 Ok(())
309}
310
311#[cfg(test)]
312mod tests {
313 use std::path::{Path, PathBuf};
314
315 use forge_core::{IssueState, NewPull, Visibility};
316 use forge_git::repo::{init_bare, repo_path};
317 use tempfile::TempDir;
318
319 use super::*;
320 use crate::post_update::after_ref_update;
321 use crate::test_support::test_state;
322
323 fn git(dir: &Path, args: &[&str]) -> String {
324 let out = std::process::Command::new("git")
325 .arg("-C")
326 .arg(dir)
327 .args(args)
328 .env("GIT_AUTHOR_NAME", "test")
329 .env("GIT_AUTHOR_EMAIL", "test@example.com")
330 .env("GIT_COMMITTER_NAME", "test")
331 .env("GIT_COMMITTER_EMAIL", "test@example.com")
332 .env("GIT_CONFIG_NOSYSTEM", "1")
333 .env("GIT_CONFIG_GLOBAL", "/dev/null")
334 .output()
335 .expect("git runs");
336 assert!(
337 out.status.success(),
338 "git {args:?} failed: {}",
339 String::from_utf8_lossy(&out.stderr)
340 );
341 String::from_utf8_lossy(&out.stdout).trim().to_string()
342 }
343
344 fn commit(work: &Path, file: &str, contents: &str) -> String {
345 std::fs::write(work.join(file), contents).expect("write file");
346 git(work, &["add", file]);
347 git(
348 work,
349 &[
350 "-c",
351 "commit.gpgsign=false",
352 "commit",
353 "-q",
354 "-m",
355 &format!("update {file}"),
356 ],
357 );
358 git(work, &["rev-parse", "HEAD"])
359 }
360
361 struct Fixture {
362 _tmp: TempDir,
363 state: AppState,
364 repo: Repo,
365 owner_id: String,
366 bare: PathBuf,
367 work: PathBuf,
368 }
369
370 async fn fixture() -> Fixture {
371 let tmp = tempfile::tempdir().expect("tempdir");
372 let state = test_state(tmp.path()).await;
373 let owner = state
374 .store
375 .create_user("tahinli", None, "unused", false)
376 .await
377 .expect("user");
378 let repo = state
379 .store
380 .create_repo(&owner.id, "proj", "", Visibility::Private, "main")
381 .await
382 .expect("repo");
383 let bare = repo_path(&state.git.data_dir, &repo.id).expect("repo path");
384 init_bare(&state.git, &bare, "main")
385 .await
386 .expect("init bare");
387 let work = tmp.path().join("work");
388 git(tmp.path(), &["init", "-b", "main", "work"]);
389 Fixture {
390 _tmp: tmp,
391 state,
392 repo,
393 owner_id: owner.id,
394 bare,
395 work,
396 }
397 }
398
399 impl Fixture {
400 fn bare_str(&self) -> &str {
401 self.bare.to_str().expect("utf8 path")
402 }
403
404 fn push(&self, refspec: &str) {
405 git(&self.work, &["push", self.bare_str(), refspec]);
406 }
407
408 async fn push_and_sync(&self, branch: &str, old: &str, new: &str) {
409 self.push(&format!("{new}:refs/heads/{branch}"));
410 after_ref_update(
411 &self.state,
412 &self.repo,
413 &self.owner_id,
414 &format!("refs/heads/{branch}"),
415 old,
416 new,
417 )
418 .await
419 .expect("post-update");
420 }
421
422 async fn pull(&self, number: i64) -> PullRequest {
423 self.state
424 .store
425 .pull_by_number(&self.repo.id, number)
426 .await
427 .expect("lookup")
428 .expect("pull exists")
429 }
430 }
431
432 #[tokio::test]
433 async fn pushing_to_head_branch_updates_the_pull() {
434 let fx = fixture().await;
435 let base = commit(&fx.work, "file.txt", "one");
436 fx.push("main");
437 git(&fx.work, &["checkout", "-b", "feature"]);
438 let f1 = commit(&fx.work, "feature.txt", "f1");
439 fx.push("feature");
440 let pr = fx
441 .state
442 .store
443 .create_pull(&NewPull {
444 repo_id: &fx.repo.id,
445 author_id: &fx.owner_id,
446 title: "Feature",
447 body: "",
448 base_branch: "main",
449 head_repo_id: &fx.repo.id,
450 head_branch: "feature",
451 head_oid: &f1,
452 base_oid: &base,
453 })
454 .await
455 .expect("create pull");
456 let number = pr.issue.number;
457
458 let f2 = commit(&fx.work, "feature.txt", "f2");
459 fx.push_and_sync("feature", &f1, &f2).await;
460
461 let pr = fx.pull(number).await;
462 assert_eq!(pr.head_oid, f2);
463 assert_eq!(pr.mergeable, Mergeable::Clean);
464 let pull_ref = git(
465 &fx.bare,
466 &["rev-parse", &format!("refs/pull/{number}/head")],
467 );
468 assert_eq!(pull_ref, f2);
469 let events = fx
470 .state
471 .store
472 .list_events(&pr.issue.id)
473 .await
474 .expect("events");
475 let updated = events
476 .iter()
477 .find(|e| e.kind == "head_updated")
478 .expect("head_updated event");
479 assert_eq!(updated.data["from"], f1);
480 assert_eq!(updated.data["to"], f2);
481 assert_eq!(updated.actor_id.as_deref(), Some(fx.owner_id.as_str()));
482 }
483
484 #[tokio::test]
485 async fn deleting_head_branch_keeps_last_head() {
486 let fx = fixture().await;
487 let base = commit(&fx.work, "file.txt", "one");
488 fx.push("main");
489 git(&fx.work, &["checkout", "-b", "feature"]);
490 let f1 = commit(&fx.work, "feature.txt", "f1");
491 fx.push("feature");
492 let pr = fx
493 .state
494 .store
495 .create_pull(&NewPull {
496 repo_id: &fx.repo.id,
497 author_id: &fx.owner_id,
498 title: "Feature",
499 body: "",
500 base_branch: "main",
501 head_repo_id: &fx.repo.id,
502 head_branch: "feature",
503 head_oid: &f1,
504 base_oid: &base,
505 })
506 .await
507 .expect("create pull");
508 let zero = "0".repeat(40);
509 git(&fx.bare, &["update-ref", "-d", "refs/heads/feature"]);
510 after_ref_update(
511 &fx.state,
512 &fx.repo,
513 &fx.owner_id,
514 "refs/heads/feature",
515 &f1,
516 &zero,
517 )
518 .await
519 .expect("post-update");
520
521 let pr = fx.pull(pr.issue.number).await;
522 assert_eq!(pr.head_oid, f1);
523 let events = fx
524 .state
525 .store
526 .list_events(&pr.issue.id)
527 .await
528 .expect("events");
529 assert!(events.iter().any(|e| e.kind == "head_deleted"));
530 }
531
532 #[tokio::test]
533 async fn pushing_conflicting_base_marks_pull_conflicting() {
534 let fx = fixture().await;
535 let base = commit(&fx.work, "file.txt", "one");
536 fx.push("main");
537 git(&fx.work, &["checkout", "-b", "feature"]);
538 let f1 = commit(&fx.work, "file.txt", "feature");
539 fx.push("feature");
540 let pr = fx
541 .state
542 .store
543 .create_pull(&NewPull {
544 repo_id: &fx.repo.id,
545 author_id: &fx.owner_id,
546 title: "Feature",
547 body: "",
548 base_branch: "main",
549 head_repo_id: &fx.repo.id,
550 head_branch: "feature",
551 head_oid: &f1,
552 base_oid: &base,
553 })
554 .await
555 .expect("create pull");
556 assert_eq!(pr.mergeable, Mergeable::Unknown);
557
558 git(&fx.work, &["checkout", "main"]);
559 let m1 = commit(&fx.work, "file.txt", "main");
560 fx.push_and_sync("main", &base, &m1).await;
561
562 let pr = fx.pull(pr.issue.number).await;
563 assert_eq!(pr.mergeable, Mergeable::Conflict);
564 assert_eq!(pr.conflict_files, vec!["file.txt".to_string()]);
565 assert_eq!(pr.base_oid, m1);
566 assert_eq!(pr.merge_base.as_deref(), Some(base.as_str()));
567 }
568
569 #[tokio::test]
570 async fn pushing_a_changed_file_marks_its_comments_outdated() {
571 use forge_core::reviews::Side;
572
573 let fx = fixture().await;
574 commit(&fx.work, "b.txt", "b0");
575 let base = commit(&fx.work, "a.txt", "a0");
576 fx.push("main");
577 git(&fx.work, &["checkout", "-b", "feature"]);
578 let f1 = commit(&fx.work, "a.txt", "a1");
579 fx.push("feature");
580 let pr = fx
581 .state
582 .store
583 .create_pull(&NewPull {
584 repo_id: &fx.repo.id,
585 author_id: &fx.owner_id,
586 title: "Feature",
587 body: "",
588 base_branch: "main",
589 head_repo_id: &fx.repo.id,
590 head_branch: "feature",
591 head_oid: &f1,
592 base_oid: &base,
593 })
594 .await
595 .expect("create pull");
596 let store = &fx.state.store;
597 store
598 .add_review_comment(
599 &pr.issue.id,
600 &fx.owner_id,
601 false,
602 "a.txt",
603 Side::New,
604 1,
605 &f1,
606 "",
607 "on a",
608 )
609 .await
610 .expect("comment on a");
611 store
612 .add_review_comment(
613 &pr.issue.id,
614 &fx.owner_id,
615 false,
616 "b.txt",
617 Side::New,
618 1,
619 &f1,
620 "",
621 "on b",
622 )
623 .await
624 .expect("comment on b");
625
626 let f2 = commit(&fx.work, "a.txt", "a2");
627 fx.push_and_sync("feature", &f1, &f2).await;
628
629 let threads = store
630 .review_threads(&pr.issue.id, None)
631 .await
632 .expect("threads");
633 let outdated_at = |path: &str| {
634 threads
635 .iter()
636 .find(|t| t.root.path == path)
637 .map(|t| t.root.outdated)
638 };
639 assert_eq!(outdated_at("a.txt"), Some(true));
640 assert_eq!(outdated_at("b.txt"), Some(false));
641 }
642
643 #[tokio::test]
644 async fn base_push_containing_head_marks_pull_merged() {
645 let fx = fixture().await;
646 let base = commit(&fx.work, "file.txt", "one");
647 fx.push("main");
648 git(&fx.work, &["checkout", "-b", "feature"]);
649 let f1 = commit(&fx.work, "feature.txt", "f1");
650 fx.push("feature");
651 let pr = fx
652 .state
653 .store
654 .create_pull(&NewPull {
655 repo_id: &fx.repo.id,
656 author_id: &fx.owner_id,
657 title: "Feature",
658 body: "",
659 base_branch: "main",
660 head_repo_id: &fx.repo.id,
661 head_branch: "feature",
662 head_oid: &f1,
663 base_oid: &base,
664 })
665 .await
666 .expect("create pull");
667
668 git(&fx.work, &["checkout", "main"]);
669 let side = commit(&fx.work, "other.txt", "other");
670 git(
671 &fx.work,
672 &["merge", "--no-ff", "feature", "-m", "merge feature"],
673 );
674 let merge = git(&fx.work, &["rev-parse", "HEAD"]);
675 assert_ne!(merge, side);
676 fx.push_and_sync("main", &side, &merge).await;
677
678 let pr = fx.pull(pr.issue.number).await;
679 assert_eq!(pr.mergeable, Mergeable::Merged);
680 assert_eq!(pr.merge_commit.as_deref(), Some(merge.as_str()));
681 assert!(pr.merged_at.is_some());
682 assert_eq!(pr.issue.state, IssueState::Closed);
683 let events = fx
684 .state
685 .store
686 .list_events(&pr.issue.id)
687 .await
688 .expect("events");
689 assert!(events.iter().any(|e| e.kind == "merged"));
690 }
691
692 #[test]
693 fn unchanged_line_follows_insertions_and_skips_changed_lines() {
694 let old = "a\nb\nc\n";
695 let new = "x\na\nb\nc\n";
696 assert_eq!(unchanged_line(old, new, 1), Some(2));
697 assert_eq!(unchanged_line(old, new, 2), Some(3));
698 assert_eq!(unchanged_line(old, new, 3), Some(4));
699 assert_eq!(unchanged_line(old, new, 0), None);
700 assert_eq!(unchanged_line(old, new, 9), None);
701 assert_eq!(unchanged_line("a\nb\nc\n", "a\nB\nc\n", 2), None);
702 assert_eq!(unchanged_line("a\nb\nc\n", "a\nB\nc\n", 3), Some(3));
703 assert_eq!(unchanged_line("a\nb\n", "a\n", 2), None);
704 }
705
706 #[tokio::test]
707 async fn pushing_moved_lines_relocates_comments_and_changed_lines_go_outdated() {
708 use forge_core::reviews::Side;
709
710 let fx = fixture().await;
711 let base = commit(&fx.work, "b.txt", "b0");
712 fx.push("main");
713 git(&fx.work, &["checkout", "-b", "feature"]);
714 let f1 = commit(&fx.work, "a.txt", "a\nb\nc\n");
715 fx.push("feature");
716 let pr = fx
717 .state
718 .store
719 .create_pull(&NewPull {
720 repo_id: &fx.repo.id,
721 author_id: &fx.owner_id,
722 title: "Feature",
723 body: "",
724 base_branch: "main",
725 head_repo_id: &fx.repo.id,
726 head_branch: "feature",
727 head_oid: &f1,
728 base_oid: &base,
729 })
730 .await
731 .expect("create pull");
732 let store = &fx.state.store;
733 for (line, body) in [(2, "on b"), (3, "on c")] {
734 store
735 .add_review_comment(
736 &pr.issue.id,
737 &fx.owner_id,
738 false,
739 "a.txt",
740 Side::New,
741 line,
742 &f1,
743 "",
744 body,
745 )
746 .await
747 .expect("comment");
748 }
749 let f2 = commit(&fx.work, "a.txt", "x\na\nb\nC\n");
750 fx.push_and_sync("feature", &f1, &f2).await;
751
752 let threads = store
753 .review_threads(&pr.issue.id, None)
754 .await
755 .expect("threads");
756 let on = |body: &str| {
757 threads
758 .iter()
759 .map(|t| &t.root)
760 .find(|r| r.body == body)
761 .expect("root")
762 .clone()
763 };
764 let moved = on("on b");
765 assert!(!moved.outdated);
766 assert_eq!(moved.line, 3);
767 assert_eq!(moved.commit_oid, f2);
768 assert_eq!(moved.diff_hunk, "");
769 assert!(on("on c").outdated);
770 }
771
772 #[tokio::test]
773 async fn deleting_a_file_marks_its_comments_outdated() {
774 use forge_core::reviews::Side;
775
776 let fx = fixture().await;
777 let base = commit(&fx.work, "b.txt", "b0");
778 fx.push("main");
779 git(&fx.work, &["checkout", "-b", "feature"]);
780 let f1 = commit(&fx.work, "a.txt", "a\nb\n");
781 fx.push("feature");
782 let pr = fx
783 .state
784 .store
785 .create_pull(&NewPull {
786 repo_id: &fx.repo.id,
787 author_id: &fx.owner_id,
788 title: "Feature",
789 body: "",
790 base_branch: "main",
791 head_repo_id: &fx.repo.id,
792 head_branch: "feature",
793 head_oid: &f1,
794 base_oid: &base,
795 })
796 .await
797 .expect("create pull");
798 fx.state
799 .store
800 .add_review_comment(
801 &pr.issue.id,
802 &fx.owner_id,
803 false,
804 "a.txt",
805 Side::New,
806 1,
807 &f1,
808 "",
809 "on a",
810 )
811 .await
812 .expect("comment");
813
814 git(&fx.work, &["rm", "a.txt"]);
815 git(&fx.work, &["commit", "-m", "remove a"]);
816 let f2 = git(&fx.work, &["rev-parse", "HEAD"]);
817 fx.push_and_sync("feature", &f1, &f2).await;
818
819 let threads = fx
820 .state
821 .store
822 .review_threads(&pr.issue.id, None)
823 .await
824 .expect("threads");
825 assert_eq!(threads.len(), 1);
826 assert!(threads[0].root.outdated);
827 assert_eq!(threads[0].root.commit_oid, f1);
828 }
829}