@@ -569,38 +569,69 @@ function create_results_pr(target_repo, fallback_repo, branch_name, folder_name,
569569 user = GitHub. whoami (auth= auth)
570570 source_repo = user. login * " /" * repo_name
571571
572+ # Get the default branch (usually main) - need this early for fork sync
573+ default_branch = target_repo_obj. default_branch
574+ @info " 📋 Default branch: $default_branch "
575+
572576 # First, check if we need to create the target repository
573577 if actual_target_repo == fallback_repo
574578 @info " 📋 Target repository doesn't exist, using fallback: $actual_target_repo "
575579 end
576580
577581 # Always try to create a fork first (this ensures we have a fork to work with)
578582 fork_repo_obj = nothing
583+ fork_existed = false
579584 try
580- @info " 📋 Creating/updating fork of $actual_target_repo ..."
585+ @info " 📋 Creating fork of $actual_target_repo ..."
581586 fork_repo_obj = GitHub. fork (target_repo_obj, auth= auth)
582- @info " ✅ Fork created/updated : $(user. login) /$repo_name "
583- # Wait a moment for fork to be ready
587+ @info " ✅ Fork created: $(user. login) /$repo_name "
588+ # Wait a moment for new fork to be ready
584589 sleep (3 )
585590 catch e
586- @info " 📋 Fork may already exist, trying to access existing fork..."
587- try
588- fork_repo_obj = GitHub. repo (source_repo, auth= auth)
589- @info " ✅ Using existing fork: $source_repo "
590- catch e2
591- error (" Failed to create or access fork: $e2 " )
591+ if contains (string (e), " already exists" ) || contains (string (e), " already forked" )
592+ @info " 📋 Fork already exists, accessing existing fork..."
593+ fork_existed = true
594+ try
595+ fork_repo_obj = GitHub. repo (source_repo, auth= auth)
596+ @info " ✅ Using existing fork: $source_repo "
597+ catch e2
598+ error (" Failed to access existing fork: $e2 " )
599+ end
600+ else
601+ error (" Failed to create fork: $e " )
592602 end
593603 end
594604
595- # Get the default branch (usually main)
596- default_branch = target_repo_obj. default_branch
597- @info " 📋 Default branch: $default_branch "
605+ # If fork already existed, sync it with upstream to ensure it's up to date
606+ if fork_existed
607+ @info " 📋 Syncing existing fork with upstream..."
608+ try
609+ # Get the latest commit from upstream default branch
610+ upstream_ref = GitHub. reference (target_repo_obj, " heads/$default_branch " , auth= auth)
611+ upstream_sha = upstream_ref. object[" sha" ]
612+
613+ # Update the fork's default branch to match upstream
614+ try
615+ GitHub. update_ref (fork_repo_obj, " heads/$default_branch " , upstream_sha, auth= auth)
616+ @info " ✅ Fork synced with upstream (SHA: $upstream_sha )"
617+ catch sync_error
618+ @warn " Could not sync fork automatically: $sync_error "
619+ @info " 📋 Proceeding with existing fork state..."
620+ end
621+
622+ # Small delay to ensure sync is complete
623+ sleep (1 )
624+ catch e
625+ @warn " Could not check upstream state: $e "
626+ @info " 📋 Proceeding with existing fork state..."
627+ end
628+ end
598629
599- # Get the SHA of the default branch from the fork
630+ # Get the SHA of the default branch from the fork (should be synced now)
600631 try
601632 main_branch_ref = GitHub. reference (fork_repo_obj, " heads/$default_branch " , auth= auth)
602633 base_sha = main_branch_ref. object[" sha" ]
603- @info " 📋 Base SHA: $base_sha "
634+ @info " 📋 Base SHA from fork : $base_sha "
604635 catch e
605636 # If the fork doesn't have the branch yet, get it from the target
606637 @info " 📋 Getting base SHA from target repository..."
0 commit comments