Skip to content

Commit ec9bd31

Browse files
committed
add ref == "" case in switch statement, use originalRef instead of ref in case definitions and some of the logic
1 parent 6ddfbed commit ec9bd31

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

pkg/github/repositories.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,30 +1393,27 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
13931393
originalRef := ref // Keep original ref for clearer error messages down the line.
13941394

13951395
// 2) If no SHA is provided, we try to resolve the ref into a fully-qualified format.
1396-
// 2a) If ref is empty, determine the default branch.
1397-
if ref == "" {
1396+
var reference *github.Reference
1397+
var resp *github.Response
1398+
var err error
1399+
1400+
switch {
1401+
case originalRef == "":
1402+
// 2a) If ref is empty, determine the default branch.
13981403
repoInfo, resp, err := githubClient.Repositories.Get(ctx, owner, repo)
13991404
if err != nil {
14001405
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get repository info", resp, err)
14011406
return nil, fmt.Errorf("failed to get repository info: %w", err)
14021407
}
14031408
ref = fmt.Sprintf("refs/heads/%s", repoInfo.GetDefaultBranch())
1404-
1405-
}
1406-
1407-
var reference *github.Reference
1408-
var resp *github.Response
1409-
var err error
1410-
1411-
switch {
1412-
case strings.HasPrefix(ref, "refs/"):
1409+
case strings.HasPrefix(originalRef, "refs/"):
14131410
// 2b) Already fully qualified. The reference will be fetched at the end.
1414-
case strings.HasPrefix(ref, "heads/") || strings.HasPrefix(ref, "tags/"):
1411+
case strings.HasPrefix(originalRef, "heads/") || strings.HasPrefix(originalRef, "tags/"):
14151412
// 2c) Partially qualified. Make it fully qualified.
1416-
ref = "refs/" + ref
1413+
ref = "refs/" + originalRef
14171414
default:
14181415
// 2d) It's a short name, so we try to resolve it to either a branch or a tag.
1419-
branchRef := "refs/heads/" + ref
1416+
branchRef := "refs/heads/" + originalRef
14201417
reference, resp, err = githubClient.Git.GetRef(ctx, owner, repo, branchRef)
14211418

14221419
if err == nil {
@@ -1425,7 +1422,7 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
14251422
// The branch lookup failed. Check if it was a 404 Not Found error.
14261423
ghErr, isGhErr := err.(*github.ErrorResponse)
14271424
if isGhErr && ghErr.Response.StatusCode == http.StatusNotFound {
1428-
tagRef := "refs/tags/" + ref
1425+
tagRef := "refs/tags/" + originalRef
14291426
reference, resp, err = githubClient.Git.GetRef(ctx, owner, repo, tagRef)
14301427
if err == nil {
14311428
ref = tagRef // It's a tag.

0 commit comments

Comments
 (0)