@@ -32,6 +32,68 @@ pub struct ProjectRepoMergeResult {
3232 pub status_publication_error : Option < String > ,
3333}
3434
35+ /// Machine-readable recovery metadata for a failed pull-request merge.
36+ #[ derive( Debug , Serialize ) ]
37+ #[ serde( rename_all = "camelCase" ) ]
38+ pub struct ProjectPullRequestMergeRecovery {
39+ action : String ,
40+ target_branch : String ,
41+ source_branch : String ,
42+ }
43+
44+ /// Structured pull-request merge failure returned across the Tauri boundary.
45+ #[ derive( Debug , Serialize ) ]
46+ #[ serde( rename_all = "camelCase" ) ]
47+ pub struct ProjectPullRequestMergeError {
48+ code : String ,
49+ message : String ,
50+ recovery : Option < ProjectPullRequestMergeRecovery > ,
51+ }
52+
53+ impl ProjectPullRequestMergeError {
54+ fn new ( code : & str , message : impl Into < String > ) -> Self {
55+ Self {
56+ code : code. to_string ( ) ,
57+ message : message. into ( ) ,
58+ recovery : None ,
59+ }
60+ }
61+
62+ fn conflict ( target_branch : String , source_branch : String ) -> Self {
63+ Self {
64+ code : "merge_conflict" . to_string ( ) ,
65+ message : "Pull request has merge conflicts." . to_string ( ) ,
66+ recovery : Some ( ProjectPullRequestMergeRecovery {
67+ action : "open_terminal" . to_string ( ) ,
68+ target_branch,
69+ source_branch,
70+ } ) ,
71+ }
72+ }
73+ }
74+
75+ impl From < String > for ProjectPullRequestMergeError {
76+ fn from ( message : String ) -> Self {
77+ Self :: new ( "merge_failed" , message)
78+ }
79+ }
80+
81+ fn classify_merge_error (
82+ message : String ,
83+ has_conflicts : bool ,
84+ target_branch : & str ,
85+ source_branch : & str ,
86+ ) -> ProjectPullRequestMergeError {
87+ if has_conflicts {
88+ ProjectPullRequestMergeError :: conflict ( target_branch. to_string ( ) , source_branch. to_string ( ) )
89+ } else {
90+ ProjectPullRequestMergeError :: new (
91+ "merge_failed" ,
92+ format ! ( "Pull request merge failed: {message}" ) ,
93+ )
94+ }
95+ }
96+
3597struct ProjectRepoMergeGitResult {
3698 message : String ,
3799 merge_commit : String ,
@@ -403,7 +465,7 @@ pub async fn merge_project_pull_request(
403465 input : ProjectPullRequestMergeInput ,
404466 app : AppHandle ,
405467 state : State < ' _ , AppState > ,
406- ) -> Result < ProjectRepoMergeResult , String > {
468+ ) -> Result < ProjectRepoMergeResult , ProjectPullRequestMergeError > {
407469 let ProjectPullRequestMergeInput {
408470 target_clone_url,
409471 source_clone_url,
@@ -420,10 +482,12 @@ pub async fn merge_project_pull_request(
420482 validate_workspace_clone_url ( & source_clone_url, & state) ?;
421483 let target_owner = target_owner. trim ( ) . to_ascii_lowercase ( ) ;
422484 if target_owner. len ( ) != 64 || !target_owner. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) ) {
423- return Err ( "Invalid target repository owner." . to_string ( ) ) ;
485+ return Err ( "Invalid target repository owner." . to_string ( ) . into ( ) ) ;
424486 }
425487 if clone_url_owner ( & target_clone_url) . as_deref ( ) != Some ( target_owner. as_str ( ) ) {
426- return Err ( "Target clone URL does not match the repository owner." . to_string ( ) ) ;
488+ return Err ( "Target clone URL does not match the repository owner."
489+ . to_string ( )
490+ . into ( ) ) ;
427491 }
428492 let owner_identity = project_owner_identity ( & app, & state, & target_owner) ?;
429493 let merger_pubkey = owner_identity. keys . public_key ( ) . to_hex ( ) ;
@@ -432,7 +496,9 @@ pub async fn merge_project_pull_request(
432496 let source_branch = normalize_branch_option ( Some ( & source_branch) )
433497 . ok_or_else ( || "Invalid source branch." . to_string ( ) ) ?;
434498 if target_branch == source_branch && same_repository ( & target_clone_url, & source_clone_url) {
435- return Err ( "Source and target branches must be different." . to_string ( ) ) ;
499+ return Err ( "Source and target branches must be different."
500+ . to_string ( )
501+ . into ( ) ) ;
436502 }
437503 let expected_commit = normalize_commit ( & expected_commit)
438504 . ok_or_else ( || "Invalid pull request commit." . to_string ( ) ) ?;
@@ -444,87 +510,109 @@ pub async fn merge_project_pull_request(
444510 ) ?;
445511 let auth = build_git_auth_config_for_keys ( & owner_identity. keys ) ?;
446512
447- let git_result = tauri:: async_runtime:: spawn_blocking ( move || {
448- let temp_dir = tempfile:: tempdir ( ) . map_err ( |error| format ! ( "create temp dir: {error}" ) ) ?;
449- let repo_dir = temp_dir. path ( ) . join ( "repo" ) ;
450- let repo_path = repo_dir
451- . to_str ( )
452- . ok_or_else ( || "temporary repository path is not UTF-8" . to_string ( ) ) ?;
453- run_git (
454- & [
455- "clone" ,
456- "--filter=blob:none" ,
457- "--no-tags" ,
458- "--branch" ,
459- target_branch. as_str ( ) ,
460- "--single-branch" ,
461- "--end-of-options" ,
462- target_clone_url. as_str ( ) ,
463- repo_path,
464- ] ,
465- None ,
466- & auth,
467- ) ?;
468- run_git (
469- & [
470- "fetch" ,
471- "--quiet" ,
472- "--end-of-options" ,
473- source_clone_url. as_str ( ) ,
474- source_branch. as_str ( ) ,
475- ] ,
476- Some ( & repo_dir) ,
477- & auth,
478- ) ?;
479- let source_head = run_git ( & [ "rev-parse" , "FETCH_HEAD" ] , Some ( & repo_dir) , & auth)
480- . ok ( )
481- . and_then ( |output| first_output_line ( & output) )
482- . ok_or_else ( || "Could not resolve the pull request branch." . to_string ( ) ) ?;
483- if source_head. to_ascii_lowercase ( ) != expected_commit {
484- return Err (
485- "The pull request branch changed. Refresh the pull request before merging."
486- . to_string ( ) ,
487- ) ;
488- }
489-
490- let merge_email = format ! ( "{merger_pubkey}@users.noreply.buzz" ) ;
491- run_git (
492- & [
493- "-c" ,
494- "user.name=Buzz User" ,
495- "-c" ,
496- format ! ( "user.email={merge_email}" ) . as_str ( ) ,
497- "merge" ,
498- "--no-edit" ,
499- "--end-of-options" ,
500- expected_commit. as_str ( ) ,
501- ] ,
502- Some ( & repo_dir) ,
503- & auth,
504- )
505- . map_err ( |error| format ! ( "Pull request cannot be merged cleanly: {error}" ) ) ?;
506- let merge_commit = run_git ( & [ "rev-parse" , "HEAD" ] , Some ( & repo_dir) , & auth)
507- . ok ( )
508- . and_then ( |output| first_output_line ( & output) )
509- . ok_or_else ( || "Could not resolve the merge commit." . to_string ( ) ) ?;
510- run_git (
511- & [
512- "push" ,
513- "--end-of-options" ,
514- "origin" ,
515- format ! ( "HEAD:{target_branch}" ) . as_str ( ) ,
516- ] ,
517- Some ( & repo_dir) ,
518- & auth,
519- ) ?;
513+ let git_result = tauri:: async_runtime:: spawn_blocking (
514+ move || -> Result < ProjectRepoMergeGitResult , ProjectPullRequestMergeError > {
515+ let temp_dir =
516+ tempfile:: tempdir ( ) . map_err ( |error| format ! ( "create temp dir: {error}" ) ) ?;
517+ let repo_dir = temp_dir. path ( ) . join ( "repo" ) ;
518+ let repo_path = repo_dir
519+ . to_str ( )
520+ . ok_or_else ( || "temporary repository path is not UTF-8" . to_string ( ) ) ?;
521+ run_git (
522+ & [
523+ "clone" ,
524+ "--filter=blob:none" ,
525+ "--no-tags" ,
526+ "--branch" ,
527+ target_branch. as_str ( ) ,
528+ "--single-branch" ,
529+ "--end-of-options" ,
530+ target_clone_url. as_str ( ) ,
531+ repo_path,
532+ ] ,
533+ None ,
534+ & auth,
535+ ) ?;
536+ run_git (
537+ & [
538+ "fetch" ,
539+ "--quiet" ,
540+ "--end-of-options" ,
541+ source_clone_url. as_str ( ) ,
542+ source_branch. as_str ( ) ,
543+ ] ,
544+ Some ( & repo_dir) ,
545+ & auth,
546+ ) ?;
547+ let source_head = run_git ( & [ "rev-parse" , "FETCH_HEAD" ] , Some ( & repo_dir) , & auth)
548+ . ok ( )
549+ . and_then ( |output| first_output_line ( & output) )
550+ . ok_or_else ( || "Could not resolve the pull request branch." . to_string ( ) ) ?;
551+ if source_head. to_ascii_lowercase ( ) != expected_commit {
552+ return Err ( ProjectPullRequestMergeError :: new (
553+ "branch_changed" ,
554+ "The pull request branch changed. Refresh the pull request before merging."
555+ . to_string ( ) ,
556+ ) ) ;
557+ }
520558
521- Ok ( ProjectRepoMergeGitResult {
522- message : format ! ( "Merged {source_branch} into {target_branch}." ) ,
523- merge_commit,
524- } )
525- } )
559+ let merge_email = format ! ( "{merger_pubkey}@users.noreply.buzz" ) ;
560+ let merge_result = run_git (
561+ & [
562+ "-c" ,
563+ "user.name=Buzz User" ,
564+ "-c" ,
565+ format ! ( "user.email={merge_email}" ) . as_str ( ) ,
566+ "merge" ,
567+ "--no-edit" ,
568+ "--end-of-options" ,
569+ expected_commit. as_str ( ) ,
570+ ] ,
571+ Some ( & repo_dir) ,
572+ & auth,
573+ ) ;
574+ if let Err ( error) = merge_result {
575+ let has_conflicts = run_git (
576+ & [ "diff" , "--name-only" , "--diff-filter=U" ] ,
577+ Some ( & repo_dir) ,
578+ & auth,
579+ )
580+ . is_ok_and ( |output| !output. trim ( ) . is_empty ( ) ) ;
581+ return Err ( classify_merge_error (
582+ error,
583+ has_conflicts,
584+ & target_branch,
585+ & source_branch,
586+ ) ) ;
587+ }
588+ let merge_commit = run_git ( & [ "rev-parse" , "HEAD" ] , Some ( & repo_dir) , & auth)
589+ . ok ( )
590+ . and_then ( |output| first_output_line ( & output) )
591+ . ok_or_else ( || "Could not resolve the merge commit." . to_string ( ) ) ?;
592+ run_git (
593+ & [
594+ "push" ,
595+ "--end-of-options" ,
596+ "origin" ,
597+ format ! ( "HEAD:{target_branch}" ) . as_str ( ) ,
598+ ] ,
599+ Some ( & repo_dir) ,
600+ & auth,
601+ ) ?;
602+
603+ Ok ( ProjectRepoMergeGitResult {
604+ message : format ! ( "Merged {source_branch} into {target_branch}." ) ,
605+ merge_commit,
606+ } )
607+ } ,
608+ )
526609 . await
527- . map_err ( |error| format ! ( "pull request merge task failed: {error}" ) ) ??;
610+ . map_err ( |error| {
611+ ProjectPullRequestMergeError :: new (
612+ "merge_task_failed" ,
613+ format ! ( "pull request merge task failed: {error}" ) ,
614+ )
615+ } ) ??;
528616 let status_event = build_merged_status_event (
529617 & owner_identity. keys ,
530618 & repo_address,
@@ -554,8 +642,9 @@ pub async fn merge_project_pull_request(
554642#[ cfg( test) ]
555643mod tests {
556644 use super :: {
557- build_merged_status_event, build_review_request_event, normalize_commit, same_repository,
558- validate_merge_status_metadata,
645+ build_merged_status_event, build_review_request_event, classify_merge_error,
646+ normalize_commit, same_repository, validate_merge_status_metadata,
647+ ProjectPullRequestMergeError ,
559648 } ;
560649 use nostr:: { Event , JsonUtil , Keys , Timestamp } ;
561650
@@ -583,6 +672,51 @@ mod tests {
583672 ) ) ;
584673 }
585674
675+ #[ test]
676+ fn merge_conflict_error_has_stable_recovery_metadata ( ) {
677+ let error =
678+ ProjectPullRequestMergeError :: conflict ( "main" . to_string ( ) , "feature/demo" . to_string ( ) ) ;
679+
680+ assert_eq ! ( error. code, "merge_conflict" ) ;
681+ assert_eq ! ( error. message, "Pull request has merge conflicts." ) ;
682+ let recovery = error. recovery . expect ( "conflict recovery" ) ;
683+ assert_eq ! ( recovery. action, "open_terminal" ) ;
684+ assert_eq ! ( recovery. target_branch, "main" ) ;
685+ assert_eq ! ( recovery. source_branch, "feature/demo" ) ;
686+ }
687+
688+ #[ test]
689+ fn merge_conflict_error_serializes_for_tauri_clients ( ) {
690+ let error =
691+ ProjectPullRequestMergeError :: conflict ( "main" . to_string ( ) , "feature/demo" . to_string ( ) ) ;
692+ let value = serde_json:: to_value ( error) . expect ( "serialize merge conflict" ) ;
693+
694+ assert_eq ! ( value[ "code" ] , "merge_conflict" ) ;
695+ assert_eq ! ( value[ "recovery" ] [ "targetBranch" ] , "main" ) ;
696+ assert_eq ! ( value[ "recovery" ] [ "sourceBranch" ] , "feature/demo" ) ;
697+ }
698+
699+ #[ test]
700+ fn merge_error_classification_only_recovers_conflicts ( ) {
701+ let conflict = classify_merge_error (
702+ "CONFLICT (content): Merge conflict in src/main.rs" . to_string ( ) ,
703+ true ,
704+ "main" ,
705+ "feature/demo" ,
706+ ) ;
707+ assert_eq ! ( conflict. code, "merge_conflict" ) ;
708+ assert ! ( conflict. recovery. is_some( ) ) ;
709+
710+ let other = classify_merge_error (
711+ "fatal: refusing to merge unrelated histories" . to_string ( ) ,
712+ false ,
713+ "main" ,
714+ "feature/demo" ,
715+ ) ;
716+ assert_eq ! ( other. code, "merge_failed" ) ;
717+ assert ! ( other. recovery. is_none( ) ) ;
718+ }
719+
586720 #[ test]
587721 fn merged_status_is_signed_by_repository_owner ( ) {
588722 let keys = Keys :: generate ( ) ;
0 commit comments