Skip to content

Commit 8988b78

Browse files
committed
Return custom errors when main ref is supplied
1 parent cccf36e commit 8988b78

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

pkg/github/repositories.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ func looksLikeSHA(s string) bool {
18551855
// 1. If a specific commit `sha` is provided, it takes precedence and is used directly,
18561856
// and all reference resolution is skipped.
18571857
//
1858-
// 1a. If `sha` is empty but `ref` looks like a commit SHA (7-40 hexadecimal characters),
1858+
// 1a. If `sha` is empty but `ref` looks like a commit SHA (40 hexadecimal characters),
18591859
// it is returned as-is without any API calls or reference resolution.
18601860
//
18611861
// 2. If no `sha` is provided and `ref` does not look like a SHA, the function resolves
@@ -1927,25 +1927,14 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
19271927
// The tag lookup also failed. Check if it was a 404 Not Found error.
19281928
ghErr2, isGhErr2 := err.(*github.ErrorResponse)
19291929
if isGhErr2 && ghErr2.Response.StatusCode == http.StatusNotFound {
1930-
switch originalRef {
1931-
case "main":
1932-
// Try "master" next.
1933-
branchRef = "refs/heads/master"
1934-
reference, resp, err = githubClient.Git.GetRef(ctx, owner, repo, branchRef)
1935-
if err == nil {
1936-
ref = branchRef // It's the "master" branch.
1937-
break
1938-
}
1939-
return nil, fmt.Errorf("attempted to resolve ref %q as 'main' but not found, and 'master' also not found", originalRef)
1940-
default:
1941-
return nil, fmt.Errorf("could not resolve ref %q as a branch or a tag", originalRef)
1930+
if originalRef == "main" {
1931+
return nil, fmt.Errorf("could not find branch or tag 'main'. Some repositories use 'master' as the default branch name")
19421932
}
19431933
}
1944-
if err != nil {
1945-
// The tag lookup failed for a different reason.
1946-
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (tag)", resp, err)
1947-
return nil, fmt.Errorf("failed to get reference for tag '%s': %w", originalRef, err)
1948-
}
1934+
1935+
// The tag lookup failed for a different reason.
1936+
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (tag)", resp, err)
1937+
return nil, fmt.Errorf("failed to get reference for tag '%s': %w", originalRef, err)
19491938
}
19501939
} else {
19511940
// The branch lookup failed for a different reason.
@@ -1958,6 +1947,9 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
19581947
if reference == nil {
19591948
reference, resp, err = githubClient.Git.GetRef(ctx, owner, repo, ref)
19601949
if err != nil {
1950+
if ref == "refs/heads/main" {
1951+
return nil, fmt.Errorf("could not find branch 'main'. Some repositories use 'master' as the default branch name")
1952+
}
19611953
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get final reference", resp, err)
19621954
return nil, fmt.Errorf("failed to get final reference for %q: %w", ref, err)
19631955
}

0 commit comments

Comments
 (0)