createTransferPR creates a new PR in the target repository
(targetOwner, targetRepo string, prInfo *PRInfo, branchName string, verbose bool)
| 402 | |
| 403 | // createTransferPR creates a new PR in the target repository |
| 404 | func createTransferPR(targetOwner, targetRepo string, prInfo *PRInfo, branchName string, verbose bool) error { |
| 405 | // Check if user has write access to target repository |
| 406 | hasWriteAccess, err := checkRepositoryAccess(targetOwner, targetRepo) |
| 407 | if err != nil && verbose { |
| 408 | fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Could not check repository access: %v", err))) |
| 409 | } |
| 410 | |
| 411 | var forkOwner, forkRepo string |
| 412 | var needsFork bool |
| 413 | |
| 414 | if !hasWriteAccess { |
| 415 | needsFork = true |
| 416 | if verbose { |
| 417 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("No write access to target repository, using fork workflow...")) |
| 418 | } |
| 419 | |
| 420 | forkOwner, forkRepo, err = createForkIfNeeded(targetOwner, targetRepo, verbose) |
| 421 | if err != nil { |
| 422 | return fmt.Errorf("failed to create fork: %w", err) |
| 423 | } |
| 424 | |
| 425 | // Add fork as remote if not already present |
| 426 | remoteName := "fork" |
| 427 | githubHost := getGitHubHost() |
| 428 | forkRepoURL := fmt.Sprintf("%s/%s/%s.git", githubHost, forkOwner, forkRepo) |
| 429 | |
| 430 | // Check if fork remote exists |
| 431 | checkRemoteCmd := exec.Command("git", "remote", "get-url", remoteName) |
| 432 | if checkRemoteCmd.Run() != nil { |
| 433 | // Remote doesn't exist, add it |
| 434 | if verbose { |
| 435 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Adding fork remote: "+forkRepoURL)) |
| 436 | } |
| 437 | addRemoteCmd := exec.Command("git", "remote", "add", remoteName, forkRepoURL) |
| 438 | if err := addRemoteCmd.Run(); err != nil { |
| 439 | return fmt.Errorf("failed to add fork remote: %w", err) |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // Also ensure target repository is set as upstream remote if not already present |
| 444 | upstreamRemote := "upstream" |
| 445 | targetRepoURL := fmt.Sprintf("https://github.com/%s/%s.git", targetOwner, targetRepo) |
| 446 | |
| 447 | // Check if upstream remote exists and points to the right repo |
| 448 | checkUpstreamCmd := exec.Command("git", "remote", "get-url", upstreamRemote) |
| 449 | upstreamOutput, err := checkUpstreamCmd.Output() |
| 450 | if err != nil || strings.TrimSpace(string(upstreamOutput)) != targetRepoURL { |
| 451 | // Upstream doesn't exist or points to wrong repo, add/update it |
| 452 | if err != nil { |
| 453 | // Remote doesn't exist, add it |
| 454 | if verbose { |
| 455 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Adding upstream remote: "+targetRepoURL)) |
| 456 | } |
| 457 | addUpstreamCmd := exec.Command("git", "remote", "add", upstreamRemote, targetRepoURL) |
| 458 | if err := addUpstreamCmd.Run(); err != nil { |
| 459 | return fmt.Errorf("failed to add upstream remote: %w", err) |
| 460 | } |
| 461 | } else { |
no test coverage detected