Skip to content

Commit f76807b

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

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

pkg/github/repositories.go

Lines changed: 11 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,15 @@ 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
}
1933+
return nil, fmt.Errorf("could not resolve ref %q as a branch or a tag", originalRef)
19431934
}
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-
}
1935+
1936+
// The tag lookup failed for a different reason.
1937+
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (tag)", resp, err)
1938+
return nil, fmt.Errorf("failed to get reference for tag '%s': %w", originalRef, err)
19491939
}
19501940
} else {
19511941
// The branch lookup failed for a different reason.
@@ -1958,6 +1948,9 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
19581948
if reference == nil {
19591949
reference, resp, err = githubClient.Git.GetRef(ctx, owner, repo, ref)
19601950
if err != nil {
1951+
if ref == "refs/heads/main" {
1952+
return nil, fmt.Errorf("could not find branch 'main'. Some repositories use 'master' as the default branch name")
1953+
}
19611954
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get final reference", resp, err)
19621955
return nil, fmt.Errorf("failed to get final reference for %q: %w", ref, err)
19631956
}

0 commit comments

Comments
 (0)