Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions pkg/cli/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/github/gh-aw/pkg/console"
"github.com/github/gh-aw/pkg/fileutil"
"github.com/github/gh-aw/pkg/gitutil"
"github.com/github/gh-aw/pkg/logger"
)

Expand All @@ -22,16 +23,7 @@ func isGitRepo() bool {

// findGitRoot finds the root directory of the git repository
func findGitRoot() (string, error) {
gitLog.Print("Finding git root directory")
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
output, err := cmd.Output()
if err != nil {
gitLog.Printf("Failed to find git root: %v", err)
return "", fmt.Errorf("not in a git repository or git command failed: %w", err)
}
gitRoot := strings.TrimSpace(string(output))
gitLog.Printf("Found git root: %s", gitRoot)
return gitRoot, nil
return gitutil.FindGitRoot()
}

// findGitRootForPath finds the root directory of the git repository containing the specified path
Expand Down
16 changes: 2 additions & 14 deletions pkg/cli/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/github/gh-aw/pkg/constants"
"github.com/github/gh-aw/pkg/logger"
"github.com/github/gh-aw/pkg/stringutil"
)

var githubLog = logger.New("cli:github")
Expand All @@ -25,7 +26,7 @@ func getGitHubHost() string {
for _, envVar := range envVars {
if value := os.Getenv(envVar); value != "" {
githubLog.Printf("Resolved GitHub host from %s: %s", envVar, value)
return normalizeGitHubHostURL(value)
return stringutil.NormalizeGitHubHostURL(value)
}
}

Expand All @@ -34,19 +35,6 @@ func getGitHubHost() string {
return defaultHost
}

// normalizeGitHubHostURL ensures the host URL has https:// scheme and no trailing slashes
func normalizeGitHubHostURL(rawHostURL string) string {
// Remove all trailing slashes
normalized := strings.TrimRight(rawHostURL, "/")

// Add https:// scheme if no scheme is present
if !strings.HasPrefix(normalized, "https://") && !strings.HasPrefix(normalized, "http://") {
normalized = "https://" + normalized
}

return normalized
}

// getGitHubHostForRepo returns the GitHub host URL for a specific repository.
// The gh-aw repository (github/gh-aw) always uses public GitHub (https://github.com)
// regardless of enterprise GitHub host settings, since gh-aw itself is only available
Expand Down
16 changes: 2 additions & 14 deletions pkg/cli/update_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ import (
"github.com/github/gh-aw/pkg/workflow"
)

// extractBaseRepo extracts the base repository (owner/repo) from an action path
// that may include subfolders (e.g., "actions/cache/restore" -> "actions/cache")
func extractBaseRepo(actionPath string) string {
parts := strings.Split(actionPath, "/")
if len(parts) >= 2 {
// Return owner/repo (first two segments)
return parts[0] + "/" + parts[1]
}
// If less than 2 parts, return as-is (shouldn't happen in practice)
return actionPath
}

// isCoreAction returns true if the repo is a GitHub-maintained core action (actions/* org).
// Core actions are always updated to the latest major version without requiring --major.
func isCoreAction(repo string) bool {
Expand Down Expand Up @@ -172,7 +160,7 @@ func getLatestActionRelease(repo, currentVersion string, allowMajor, verbose boo
updateLog.Printf("Getting latest release for %s@%s (allowMajor=%v)", repo, currentVersion, allowMajor)

// Extract base repository (e.g., "actions/cache/restore" -> "actions/cache")
baseRepo := extractBaseRepo(repo)
baseRepo := gitutil.ExtractBaseRepo(repo)
updateLog.Printf("Using base repository: %s for action: %s", baseRepo, repo)

// Use gh CLI to get releases
Expand Down Expand Up @@ -282,7 +270,7 @@ func getLatestActionReleaseViaGit(repo, currentVersion string, allowMajor, verbo
}

// Extract base repository (e.g., "actions/cache/restore" -> "actions/cache")
baseRepo := extractBaseRepo(repo)
baseRepo := gitutil.ExtractBaseRepo(repo)
updateLog.Printf("Using base repository: %s for action: %s (git fallback)", baseRepo, repo)

githubHost := getGitHubHostForRepo(baseRepo)
Expand Down
10 changes: 6 additions & 4 deletions pkg/cli/update_actions_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package cli

import (
"testing"

"github.com/github/gh-aw/pkg/gitutil"
)

func TestExtractBaseRepo_Integration(t *testing.T) {
Expand All @@ -29,9 +31,9 @@ func TestExtractBaseRepo_Integration(t *testing.T) {

for actionPath, expectedBase := range realWorldCases {
t.Run(actionPath, func(t *testing.T) {
got := extractBaseRepo(actionPath)
got := gitutil.ExtractBaseRepo(actionPath)
if got != expectedBase {
t.Errorf("extractBaseRepo(%q) = %q, want %q", actionPath, got, expectedBase)
t.Errorf("gitutil.ExtractBaseRepo(%q) = %q, want %q", actionPath, got, expectedBase)
}
})
}
Expand All @@ -56,11 +58,11 @@ func TestExtractBaseRepoAPICompatibility(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.actionPath, func(t *testing.T) {
baseRepo := extractBaseRepo(tc.actionPath)
baseRepo := gitutil.ExtractBaseRepo(tc.actionPath)

// Verify the base repo matches expected format
if baseRepo != tc.wantRepoPath {
t.Errorf("extractBaseRepo(%q) = %q, want %q", tc.actionPath, baseRepo, tc.wantRepoPath)
t.Errorf("gitutil.ExtractBaseRepo(%q) = %q, want %q", tc.actionPath, baseRepo, tc.wantRepoPath)
}

// Verify it can be used to construct a valid API path
Expand Down
6 changes: 4 additions & 2 deletions pkg/cli/update_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cli
import (
"encoding/json"
"testing"

"github.com/github/gh-aw/pkg/gitutil"
)

func TestActionKeyVersionConsistency(t *testing.T) {
Expand Down Expand Up @@ -146,9 +148,9 @@ func TestExtractBaseRepo(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractBaseRepo(tt.actionPath)
got := gitutil.ExtractBaseRepo(tt.actionPath)
if got != tt.want {
t.Errorf("extractBaseRepo(%q) = %q, want %q", tt.actionPath, got, tt.want)
t.Errorf("gitutil.ExtractBaseRepo(%q) = %q, want %q", tt.actionPath, got, tt.want)
}
})
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/gitutil/gitutil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gitutil

import (
"fmt"
"os/exec"
"strings"

"github.com/github/gh-aw/pkg/logger"
Expand Down Expand Up @@ -39,3 +41,30 @@ func IsHexString(s string) bool {
}
return true
}

// ExtractBaseRepo extracts the base repository (owner/repo) from a repository path
// that may include subfolders.
// For "actions/checkout" -> "actions/checkout"
// For "github/codeql-action/upload-sarif" -> "github/codeql-action"
func ExtractBaseRepo(repoPath string) string {
parts := strings.Split(repoPath, "/")
if len(parts) >= 2 {
return parts[0] + "/" + parts[1]
}
return repoPath
}

// FindGitRoot finds the root directory of the git repository.
// Returns an error if not in a git repository or if the git command fails.
func FindGitRoot() (string, error) {
log.Print("Finding git root directory")
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
output, err := cmd.Output()
if err != nil {
log.Printf("Failed to find git root: %v", err)
return "", fmt.Errorf("not in a git repository or git command failed: %w", err)
}
gitRoot := strings.TrimSpace(string(output))
log.Printf("Found git root: %s", gitRoot)
return gitRoot, nil
}
16 changes: 2 additions & 14 deletions pkg/parser/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/github/gh-aw/pkg/constants"
"github.com/github/gh-aw/pkg/logger"
"github.com/github/gh-aw/pkg/stringutil"
)

var githubLog = logger.New("parser:github")
Expand All @@ -30,7 +31,7 @@ func GetGitHubHost() string {
for _, envVar := range envVars {
if value := os.Getenv(envVar); value != "" {
githubLog.Printf("Resolved GitHub host from %s: %s", envVar, value)
return normalizeGitHubHostURL(value)
return stringutil.NormalizeGitHubHostURL(value)
}
}

Expand All @@ -39,19 +40,6 @@ func GetGitHubHost() string {
return defaultHost
}

// normalizeGitHubHostURL ensures the host URL has https:// scheme and no trailing slashes
func normalizeGitHubHostURL(rawHostURL string) string {
// Remove all trailing slashes
normalized := strings.TrimRight(rawHostURL, "/")

// Add https:// scheme if no scheme is present
if !strings.HasPrefix(normalized, "https://") && !strings.HasPrefix(normalized, "http://") {
normalized = "https://" + normalized
}

return normalized
}

// GetGitHubHostForRepo returns the GitHub host URL for a specific repository.
// The gh-aw repository (github/gh-aw) always uses public GitHub (https://github.com)
// regardless of enterprise GitHub host settings, since gh-aw itself is only available
Expand Down
14 changes: 14 additions & 0 deletions pkg/stringutil/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import (
"strings"
)

// NormalizeGitHubHostURL ensures the host URL has a scheme (defaulting to https://) and no trailing slashes.
// It is safe to call with URLs that already have an http:// or https:// scheme.
func NormalizeGitHubHostURL(rawHostURL string) string {
// Remove all trailing slashes
normalized := strings.TrimRight(rawHostURL, "/")

// Add https:// scheme if no scheme is present
if !strings.HasPrefix(normalized, "https://") && !strings.HasPrefix(normalized, "http://") {
normalized = "https://" + normalized
}

return normalized
}

// ExtractDomainFromURL extracts the domain name from a URL string.
// Handles various URL formats including full URLs with protocols, URLs with ports,
// and plain domain names.
Expand Down
51 changes: 51 additions & 0 deletions pkg/stringutil/urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,54 @@ func BenchmarkExtractDomainFromURL(b *testing.B) {
})
}
}

func TestNormalizeGitHubHostURL(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "adds https scheme to plain hostname",
input: "myorg.ghe.com",
expected: "https://myorg.ghe.com",
},
{
name: "preserves existing https scheme",
input: "https://github.com",
expected: "https://github.com",
},
{
name: "preserves existing http scheme",
input: "http://github.example.com",
expected: "http://github.example.com",
},
{
name: "removes single trailing slash",
input: "https://github.com/",
expected: "https://github.com",
},
{
name: "removes multiple trailing slashes",
input: "https://github.com///",
expected: "https://github.com",
},
{
name: "adds https and removes trailing slash from plain hostname",
input: "myorg.ghe.com/",
expected: "https://myorg.ghe.com",
},
{
name: "public GitHub URL unchanged",
input: "https://github.com",
expected: "https://github.com",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := NormalizeGitHubHostURL(tt.input)
assert.Equal(t, tt.expected, result, "Normalized URL should match expected")
})
}
}
17 changes: 3 additions & 14 deletions pkg/workflow/action_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

"github.com/github/gh-aw/pkg/gitutil"
"github.com/github/gh-aw/pkg/logger"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func (r *ActionResolver) ResolveSHA(repo, version string) (string, error) {
}

resolverLog.Printf("Cache miss for %s@%s, querying GitHub API", repo, version)
resolverLog.Printf("This may take a moment as we query GitHub API at /repos/%s/git/ref/tags/%s", extractBaseRepo(repo), version)
resolverLog.Printf("This may take a moment as we query GitHub API at /repos/%s/git/ref/tags/%s", gitutil.ExtractBaseRepo(repo), version)

// Resolve using GitHub CLI
sha, err := r.resolveFromGitHub(repo, version)
Expand All @@ -75,7 +76,7 @@ func (r *ActionResolver) ResolveSHA(repo, version string) (string, error) {
// resolveFromGitHub uses gh CLI to resolve the SHA for an action@version
func (r *ActionResolver) resolveFromGitHub(repo, version string) (string, error) {
// Extract base repository (for actions like "github/codeql-action/upload-sarif")
baseRepo := extractBaseRepo(repo)
baseRepo := gitutil.ExtractBaseRepo(repo)
resolverLog.Printf("Extracted base repository: %s from %s", baseRepo, repo)

// Use gh api to get the git ref for the tag
Expand Down Expand Up @@ -105,15 +106,3 @@ func (r *ActionResolver) resolveFromGitHub(repo, version string) (string, error)

return sha, nil
}

// extractBaseRepo extracts the base repository from a repo path
// For "actions/checkout" -> "actions/checkout"
// For "github/codeql-action/upload-sarif" -> "github/codeql-action"
func extractBaseRepo(repo string) string {
parts := strings.Split(repo, "/")
if len(parts) >= 2 {
// Take first two parts (owner/repo)
return parts[0] + "/" + parts[1]
}
return repo
}
5 changes: 3 additions & 2 deletions pkg/workflow/action_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/github/gh-aw/pkg/gitutil"
"github.com/github/gh-aw/pkg/testutil"
)

Expand Down Expand Up @@ -39,9 +40,9 @@ func TestExtractBaseRepo(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := extractBaseRepo(tt.repo)
result := gitutil.ExtractBaseRepo(tt.repo)
if result != tt.expected {
t.Errorf("extractBaseRepo(%q) = %q, want %q", tt.repo, result, tt.expected)
t.Errorf("gitutil.ExtractBaseRepo(%q) = %q, want %q", tt.repo, result, tt.expected)
}
})
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/workflow/git_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"strings"

"github.com/github/gh-aw/pkg/console"
"github.com/github/gh-aw/pkg/gitutil"
"github.com/github/gh-aw/pkg/logger"
"github.com/github/gh-aw/pkg/tty"
)
Expand All @@ -53,13 +54,11 @@ var gitHelpersLog = logger.New("workflow:git_helpers")
// This function is safe to call from any context and won't cause errors if git is not available.
func findGitRoot() string {
gitHelpersLog.Print("Attempting to find git root directory")
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
output, err := cmd.Output()
gitRoot, err := gitutil.FindGitRoot()
if err != nil {
gitHelpersLog.Printf("Could not find git root (not a git repo or git not available): %v", err)
return ""
}
gitRoot := strings.TrimSpace(string(output))
gitHelpersLog.Printf("Found git root: %s", gitRoot)
return gitRoot
}
Expand Down
Loading