getCurrentRepoSlugUncached gets the current repository slug (owner/repo) using gh CLI (uncached) Falls back to git remote parsing if gh CLI is not available
()
| 18 | // getCurrentRepoSlugUncached gets the current repository slug (owner/repo) using gh CLI (uncached) |
| 19 | // Falls back to git remote parsing if gh CLI is not available |
| 20 | func getCurrentRepoSlugUncached() (string, error) { |
| 21 | repoLog.Print("Fetching current repository slug") |
| 22 | |
| 23 | // Try gh CLI first (most reliable) |
| 24 | repoLog.Print("Attempting to get repository slug via gh CLI") |
| 25 | output, err := workflow.RunGH("Fetching repository info...", "repo", "view", "--json", "owner,name", "--jq", ".owner.login + \"/\" + .name") |
| 26 | if err == nil { |
| 27 | repoSlug := strings.TrimSpace(string(output)) |
| 28 | if repoSlug != "" { |
| 29 | // Validate format (should be owner/repo) |
| 30 | parts := strings.Split(repoSlug, "/") |
| 31 | if len(parts) == 2 && parts[0] != "" && parts[1] != "" { |
| 32 | repoLog.Printf("Successfully got repository slug via gh CLI: %s", repoSlug) |
| 33 | return repoSlug, nil |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Fallback to git remote parsing if gh CLI is not available or fails |
| 39 | repoLog.Print("gh CLI failed, falling back to git remote parsing") |
| 40 | gitCmd := exec.Command("git", "remote", "get-url", "origin") |
| 41 | gitOutput, err := gitCmd.Output() |
| 42 | if err != nil { |
| 43 | repoLog.Printf("Failed to get git remote URL: %v", err) |
| 44 | return "", fmt.Errorf("failed to get current repository (gh CLI and git remote both failed): %w", err) |
| 45 | } |
| 46 | |
| 47 | remoteURL := strings.TrimSpace(string(gitOutput)) |
| 48 | repoLog.Printf("Parsing git remote URL: %s", remoteURL) |
| 49 | |
| 50 | // Delegate to the shared helper which supports both HTTPS and SSH formats, |
| 51 | // including GitHub Enterprise hosts configured via getGitHubHost(). |
| 52 | repoPath := parseGitHubRepoSlugFromURL(remoteURL) |
| 53 | if repoPath == "" { |
| 54 | return "", fmt.Errorf("remote URL does not appear to be a GitHub repository: %s", remoteURL) |
| 55 | } |
| 56 | |
| 57 | // Validate format (should be owner/repo) |
| 58 | parts := strings.Split(repoPath, "/") |
| 59 | if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
| 60 | repoLog.Printf("Invalid repository format: %s", repoPath) |
| 61 | return "", fmt.Errorf("invalid repository format: %s. Expected format: owner/repo. Example: github/gh-aw", repoPath) |
| 62 | } |
| 63 | |
| 64 | repoLog.Printf("Successfully parsed repository slug from git remote: %s", repoPath) |
| 65 | return repoPath, nil |
| 66 | } |
| 67 | |
| 68 | // GetCurrentRepoSlug gets the current repository slug with caching. |
| 69 | // This is the recommended function to use for repository access across the codebase. |
nothing calls this directly
no test coverage detected