Skip to content

[task] Standardize Repository Slug Functions #3649

@github-actions

Description

@github-actions

Objective

Eliminate ~40-60 lines of duplicate repository slug retrieval code by standardizing on a single canonical implementation with caching.

Context

The analysis in #3525 identified 4 different implementations of repository slug retrieval across CLI command files, all using gh repo view but with different output formats and caching strategies.

Duplicate Functions:

  1. getCurrentRepo (pr_command.go:100) - Returns owner, repo separately
  2. GetCurrentRepoSlug (repo.go:95) - Returns "owner/repo" string with caching ✓ Keep this one
  3. getCurrentRepoSlugUncached (repo.go:31) - Returns "owner/repo" string without caching
  4. getCurrentRepositoryInfo (trial_command.go:461) - Wrapper around GetCurrentRepoSlug

Approach

Step 1: Identify Canonical Implementation

Keep GetCurrentRepoSlug() from pkg/cli/repo.go as the canonical implementation because:

  • It returns the standard "owner/repo" format
  • It has caching for performance
  • It's already well-tested
  • It's in a dedicated file for repository operations

Step 2: Remove Duplicate Implementations

Delete the following functions and their usages:

  • getCurrentRepo from pr_command.go (line 100)
  • getCurrentRepositoryInfo from trial_command.go (line 461)

Keep getCurrentRepoSlugUncached as it serves a specific purpose (no caching).

Step 3: Update Callers

Replace all calls to duplicate functions:

In pr_command.go:

// Before:
owner, repo, err := getCurrentRepo()
if err != nil {
    return err
}

// After:
import "github.com/githubnext/gh-aw/pkg/cli"

slug, err := GetCurrentRepoSlug()
if err != nil {
    return err
}
parts := strings.Split(slug, "/")
if len(parts) != 2 {
    return fmt.Errorf("invalid repo format: %s", slug)
}
owner, repo := parts[0], parts[1]

In trial_command.go:

// Before:
slug, err := getCurrentRepositoryInfo()

// After:
slug, err := GetCurrentRepoSlug()

Step 4: Add Helper for Splitting Slug

If splitting owner/repo is common, add a helper to repo.go:

// SplitRepoSlug splits "owner/repo" into owner and repo
func SplitRepoSlug(slug string) (owner, repo string, err error) {
    parts := strings.Split(slug, "/")
    if len(parts) != 2 {
        return "", "", fmt.Errorf("invalid repo format: %s", slug)
    }
    return parts[0], parts[1], nil
}

Files to Modify

  • pkg/cli/pr_command.go - Remove getCurrentRepo, update callers
  • pkg/cli/trial_command.go - Remove getCurrentRepositoryInfo, update callers
  • pkg/cli/repo.go - Optionally add SplitRepoSlug helper

Acceptance Criteria

  • getCurrentRepo function removed from pr_command.go
  • getCurrentRepositoryInfo function removed from trial_command.go
  • All calls to removed functions updated to use GetCurrentRepoSlug()
  • Optional: SplitRepoSlug helper added if needed
  • All existing tests pass without modification
  • make test-unit passes
  • make lint passes
  • Estimated 40-60 lines of code removed

Benefits

  • Single canonical function with caching
  • Consistent behavior across all CLI commands
  • Clear API with single responsibility
  • Reduced confusion for developers

Estimated Impact

AI generated by Plan Command for #3525

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions