Skip to content

Replace gh repo view shell-out with repository.Current() - #5901

Merged
pelikhan merged 5 commits into
mainfrom
copilot/replace-gh-repo-view
Dec 8, 2025
Merged

Replace gh repo view shell-out with repository.Current()#5901
pelikhan merged 5 commits into
mainfrom
copilot/replace-gh-repo-view

Conversation

Copilot AI commented Dec 8, 2025

Copy link
Copy Markdown
Contributor

Eliminates unnecessary process spawning in repository detection by using the native repository.Current() function from go-gh/v2.

Changes

  • Import: Added github.com/cli/go-gh/v2/pkg/repository
  • Implementation: Replaced gh.Exec("repo", "view", "--json", "nameWithOwner", "-q", ".nameWithOwner") with repository.Current() in getCurrentRepositoryUncached()
  • Validation: Added empty field check for repo.Owner and repo.Name before formatting

Before

stdOut, _, err := gh.Exec("repo", "view", "--json", "nameWithOwner", "-q", ".nameWithOwner")
if err != nil {
    return "", fmt.Errorf("failed to get repository name: %w", err)
}
repo := strings.TrimSpace(stdOut.String())

After

repo, err := repository.Current()
if err != nil {
    return "", fmt.Errorf("failed to get current repository: %w", err)
}
if repo.Owner == "" || repo.Name == "" {
    return "", fmt.Errorf("repository owner or name is empty (owner: %q, name: %q)", repo.Owner, repo.Name)
}
repoName := fmt.Sprintf("%s/%s", repo.Owner, repo.Name)

Performance: ~5-10x faster (no subprocess overhead)
Behavior: Automatically respects GH_REPO environment variable

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/user
    • Triggering command: /usr/bin/gh gh api user --jq .login tags/v5 GO111MODULE /opt/hostedtoolcache/go/1.25.0/x64/bin/go GOINSECURE GOMOD -d/gh-aw go env mplates from .github to pkg/cli/templates..." -d/gh-aw 1/x64/bin/node GOINSECURE GOMOD GOMODCACHE go (http block)
    • Triggering command: /usr/bin/gh gh api user --jq .login tags/v5 GO111MODULE /opt/hostedtoolcache/go/1.25.0/x64/bin/go GOINSECURE GOMOD GOMODCACHE go env -json GO111MODULE (http block)
    • Triggering command: /usr/bin/gh gh api user --jq .login 86/create_issue.js -d/gh-aw aeb25fa68b98648b702df66a9d2309a8958/log.json GOINSECURE GOMOD GOMODCACHE go env Lchm/pkvPopeW6dXr_bRyLchm GO111MODULE /opt/hostedtoolcache/go/1.25.0/x64/pkg/tool/linux_amd64/link GOINSECURE GOMOD GOMODCACHE /opt/hostedtoolc-address (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[plan] Replace gh repo view with repository.Current()</issue_title>
<issue_description>## Objective

Replace the shell-out gh.Exec("repo", "view", ...) call with the native repository.Current() function for detecting the current repository.

Context

This is a quick win that eliminates unnecessary process spawning and provides cleaner, more idiomatic Go code. The native function also respects the GH_REPO environment variable automatically.

Files to Modify

  • Update: pkg/workflow/repository_features_validation.go (line ~182)

Current Implementation

stdOut, _, err := gh.Exec("repo", "view", "--json", "nameWithOwner", "-q", ".nameWithOwner")
if err != nil {
    return "", fmt.Errorf("failed to get repository name: %w", err)
}
repo := strings.TrimSpace(stdOut.String())

Target Implementation

import "github.com/cli/go-gh/v2/pkg/repository"

repo, err := repository.Current()
if err != nil {
    return "", fmt.Errorf("failed to get repository name: %w", err)
}
repoName := fmt.Sprintf("%s/%s", repo.Owner(), repo.Name())

Acceptance Criteria

  • gh.Exec("repo", "view", ...) replaced with repository.Current()
  • Import github.com/cli/go-gh/v2/pkg/repository added
  • Error handling preserved
  • All unit tests pass (make test-unit)
  • Code builds successfully (make build)

Expected Impact

  • Performance: ~5-10x faster (no process spawning)
  • Code Quality: More readable and maintainable
  • Reliability: Better error messages from native function

Estimated Effort

5-10 minutes
Related to #5828

AI generated by Plan Command for discussion #5826

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits December 8, 2025 19:32
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Replace gh repo view with repository.Current() Replace gh repo view shell-out with repository.Current() Dec 8, 2025
Copilot AI requested a review from mnkiefer December 8, 2025 19:55
@pelikhan
pelikhan marked this pull request as ready for review December 8, 2025 20:08
@pelikhan
pelikhan merged commit 9b17561 into main Dec 8, 2025
5 checks passed
@pelikhan
pelikhan deleted the copilot/replace-gh-repo-view branch December 8, 2025 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[plan] Replace gh repo view with repository.Current()

3 participants