Skip to content

Commit 568c883

Browse files
authored
Standardize repository slug retrieval on single cached implementation (#3658)
1 parent 16164e9 commit 568c883

4 files changed

Lines changed: 49 additions & 37 deletions

File tree

pkg/cli/pr_command.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,6 @@ func parsePRURL(prURL string) (owner, repo string, prNumber int, err error) {
9696
return parser.ParsePRURL(prURL)
9797
}
9898

99-
// getCurrentRepo gets the current repository information using gh CLI
100-
func getCurrentRepo() (owner, repo string, err error) {
101-
cmd := exec.Command("gh", "repo", "view", "--json", "owner,name")
102-
output, err := cmd.Output()
103-
if err != nil {
104-
return "", "", fmt.Errorf("failed to get current repository info: %w", err)
105-
}
106-
107-
var repoInfo struct {
108-
Owner struct {
109-
Login string `json:"login"`
110-
} `json:"owner"`
111-
Name string `json:"name"`
112-
}
113-
114-
if err := json.Unmarshal(output, &repoInfo); err != nil {
115-
return "", "", fmt.Errorf("failed to parse repository info: %w", err)
116-
}
117-
118-
return repoInfo.Owner.Login, repoInfo.Name, nil
119-
}
120-
12199
// checkRepositoryAccess checks if the current user has write access to the target repository
122100
func checkRepositoryAccess(owner, repo string) (bool, error) {
123101
// Get current user
@@ -593,10 +571,14 @@ func transferPR(prURL, targetRepo string, verbose bool) error {
593571
targetOwner, targetRepoName = parts[0], parts[1]
594572
} else {
595573
// Use current repository as target
596-
targetOwner, targetRepoName, err = getCurrentRepo()
574+
slug, err := GetCurrentRepoSlug()
597575
if err != nil {
598576
return fmt.Errorf("failed to determine target repository: %w", err)
599577
}
578+
targetOwner, targetRepoName, err = SplitRepoSlug(slug)
579+
if err != nil {
580+
return fmt.Errorf("failed to parse target repository: %w", err)
581+
}
600582
}
601583

602584
if verbose {
@@ -615,12 +597,39 @@ func transferPR(prURL, targetRepo string, verbose bool) error {
615597
if targetRepo != "" {
616598
// Check if we're already in the target repository
617599
if isGitRepo() {
618-
currentOwner, currentRepoName, err := getCurrentRepo()
619-
if err == nil && currentOwner == targetOwner && currentRepoName == targetRepoName {
620-
// We're already in the target repo
621-
workingDir = "."
600+
slug, err := GetCurrentRepoSlug()
601+
if err == nil {
602+
currentOwner, currentRepoName, err := SplitRepoSlug(slug)
603+
if err == nil && currentOwner == targetOwner && currentRepoName == targetRepoName {
604+
// We're already in the target repo
605+
workingDir = "."
606+
} else {
607+
// We need to clone the target repository
608+
if verbose {
609+
fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Cloning target repository %s/%s...", targetOwner, targetRepoName)))
610+
}
611+
tempDir, err := os.MkdirTemp("", "gh-aw-pr-transfer-repo-")
612+
if err != nil {
613+
return fmt.Errorf("failed to create temp directory for repo: %w", err)
614+
}
615+
616+
cloneCmd := exec.Command("gh", "repo", "clone", fmt.Sprintf("%s/%s", targetOwner, targetRepoName), tempDir)
617+
if err := cloneCmd.Run(); err != nil {
618+
os.RemoveAll(tempDir)
619+
return fmt.Errorf("failed to clone target repository: %w", err)
620+
}
621+
622+
workingDir = tempDir
623+
needsCleanup = true
624+
625+
// Change to the cloned repository directory
626+
if err := os.Chdir(tempDir); err != nil {
627+
os.RemoveAll(tempDir)
628+
return fmt.Errorf("failed to change to cloned repository directory: %w", err)
629+
}
630+
}
622631
} else {
623-
// We need to clone the target repository
632+
// Error getting current repo, clone anyway
624633
if verbose {
625634
fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Cloning target repository %s/%s...", targetOwner, targetRepoName)))
626635
}

pkg/cli/repo.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,12 @@ func GetCurrentRepoSlug() (string, error) {
104104
repoLog.Printf("Using cached repository slug: %s", currentRepoSlugResult)
105105
return currentRepoSlugResult, nil
106106
}
107+
108+
// SplitRepoSlug splits "owner/repo" into owner and repo
109+
func SplitRepoSlug(slug string) (owner, repo string, err error) {
110+
parts := strings.Split(slug, "/")
111+
if len(parts) != 2 {
112+
return "", "", fmt.Errorf("invalid repo format: %s", slug)
113+
}
114+
return parts[0], parts[1], nil
115+
}

pkg/cli/spec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func parseRepoSpec(repoSpec string) (*RepoSpec, error) {
9696
repo = fmt.Sprintf("%s/%s", pathParts[0], pathParts[1])
9797
} else if repo == "." {
9898
// Handle current directory as repo (local workflow)
99-
currentRepo, err := getCurrentRepositoryInfo()
99+
currentRepo, err := GetCurrentRepoSlug()
100100
if err != nil {
101101
return nil, fmt.Errorf("failed to get current repository info: %w", err)
102102
}
@@ -269,7 +269,7 @@ func parseLocalWorkflowSpec(spec string) (*WorkflowSpec, error) {
269269
}
270270

271271
// Get current repository info
272-
repoInfo, err := getCurrentRepositoryInfo()
272+
repoInfo, err := GetCurrentRepoSlug()
273273
if err != nil {
274274
return nil, fmt.Errorf("failed to get current repository info for local workflow: %w", err)
275275
}

pkg/cli/trial_command.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func RunWorkflowTrials(workflowSpecs []string, logicalRepoSpec string, cloneRepo
219219
} else {
220220
// Fall back to current repository for logical-repo mode
221221
var err error
222-
logicalRepoSlug, err = getCurrentRepositoryInfo()
222+
logicalRepoSlug, err = GetCurrentRepoSlug()
223223
if err != nil {
224224
return fmt.Errorf("failed to determine simulated host repository: %w", err)
225225
}
@@ -456,12 +456,6 @@ func RunWorkflowTrials(workflowSpecs []string, logicalRepoSpec string, cloneRepo
456456

457457
}
458458

459-
// getCurrentRepositoryInfo determines the current repository from the gh CLI (cached)
460-
// This is a wrapper around GetCurrentRepoSlug for backward compatibility
461-
func getCurrentRepositoryInfo() (string, error) {
462-
return GetCurrentRepoSlug()
463-
}
464-
465459
// getCurrentGitHubUsername gets the current GitHub username from gh CLI
466460
func getCurrentGitHubUsername() (string, error) {
467461
cmd := exec.Command("gh", "api", "user", "--jq", ".login")

0 commit comments

Comments
 (0)