Skip to content

Commit b6976b8

Browse files
committed
refactor
1 parent e7f84bf commit b6976b8

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

pkg/github/repositories.go

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,8 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
13901390
return &raw.ContentOpts{Ref: "", SHA: sha}, nil
13911391
}
13921392

1393+
originalRef := ref // Keep original ref for clearer error messages down the line.
1394+
13931395
// 2) If no SHA is provided, we try to resolve the ref into a fully-qualified format.
13941396
// 2a) If ref is empty, determine the default branch.
13951397
if ref == "" {
@@ -1401,51 +1403,56 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
14011403
ref = repoInfo.GetDefaultBranch()
14021404
}
14031405

1404-
originalRef := ref // Keep original ref for clearer error messages down the line.
1406+
var reference *github.Reference
1407+
var resp *github.Response
1408+
var err error
14051409

14061410
// Only enter the resolution logic if the ref is NOT already fully qualified.
1407-
if !strings.HasPrefix(ref, "refs/") {
1408-
if strings.HasPrefix(ref, "heads/") || strings.HasPrefix(ref, "tags/") {
1409-
// 2c) It's partially qualified. Make it fully qualified.
1410-
ref = "refs/" + ref
1411+
switch {
1412+
case strings.HasPrefix(ref, "refs/"):
1413+
// 2b) Already fully qualified. The reference will be fetched at the end.
1414+
case strings.HasPrefix(ref, "heads/") || strings.HasPrefix(ref, "tags/"):
1415+
// 2c) Partially qualified. Make it fully qualified.
1416+
ref = "refs/" + ref
1417+
default:
1418+
// 2d) It's a short name, so we try to resolve it to either a branch or a tag.
1419+
branchRef := "refs/heads/" + ref
1420+
reference, resp, err = githubClient.Git.GetRef(ctx, owner, repo, branchRef)
1421+
1422+
if err == nil {
1423+
ref = branchRef // It's a branch.
14111424
} else {
1412-
// 2d) It's a short name; try to resolve it as a branch or tag.
1413-
_, resp, err := githubClient.Git.GetRef(ctx, owner, repo, "refs/heads/"+ref)
1414-
1415-
if err == nil {
1416-
ref = "refs/heads/" + ref // It's a branch.
1417-
} else {
1418-
// The branch lookup failed. Check if it was a 404 Not Found error.
1419-
ghErr, isGhErr := err.(*github.ErrorResponse)
1420-
if isGhErr && ghErr.Response.StatusCode == http.StatusNotFound {
1421-
// The branch wasn't found, so try as a tag.
1422-
_, resp2, err2 := githubClient.Git.GetRef(ctx, owner, repo, "refs/tags/"+ref)
1423-
if err2 == nil {
1424-
ref = "refs/tags/" + ref // It's a tag.
1425-
} else {
1426-
// The tag lookup failed. Check if it was a 404 Not Found error.
1427-
ghErr2, isGhErr2 := err2.(*github.ErrorResponse)
1428-
if isGhErr2 && ghErr2.Response.StatusCode == http.StatusNotFound {
1429-
return nil, fmt.Errorf("could not resolve ref %q as a branch or a tag", originalRef)
1430-
}
1431-
// The tag lookup failed for a different reason.
1432-
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (tag)", resp2, err2)
1433-
return nil, fmt.Errorf("failed to get reference for tag '%s': %w", originalRef, err2)
1434-
}
1425+
// The branch lookup failed. Check if it was a 404 Not Found error.
1426+
ghErr, isGhErr := err.(*github.ErrorResponse)
1427+
if isGhErr && ghErr.Response.StatusCode == http.StatusNotFound {
1428+
tagRef := "refs/tags/" + ref
1429+
reference, resp, err = githubClient.Git.GetRef(ctx, owner, repo, tagRef)
1430+
if err == nil {
1431+
ref = tagRef // It's a tag.
14351432
} else {
1436-
// The branch lookup failed for a different reason.
1437-
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (branch)", resp, err)
1438-
return nil, fmt.Errorf("failed to get reference for branch '%s': %w", originalRef, err)
1433+
// The tag lookup also failed. Check if it was a 404 Not Found error.
1434+
ghErr2, isGhErr2 := err.(*github.ErrorResponse)
1435+
if isGhErr2 && ghErr2.Response.StatusCode == http.StatusNotFound {
1436+
return nil, fmt.Errorf("could not resolve ref %q as a branch or a tag", originalRef)
1437+
}
1438+
// The tag lookup failed for a different reason.
1439+
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (tag)", resp, err)
1440+
return nil, fmt.Errorf("failed to get reference for tag '%s': %w", originalRef, err)
14391441
}
1442+
} else {
1443+
// The branch lookup failed for a different reason.
1444+
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (branch)", resp, err)
1445+
return nil, fmt.Errorf("failed to get reference for branch '%s': %w", originalRef, err)
14401446
}
14411447
}
14421448
}
14431449

1444-
// Now that 'ref' is fully qualified, we get the definitive reference object.
1445-
reference, resp, err := githubClient.Git.GetRef(ctx, owner, repo, ref)
1446-
if err != nil {
1447-
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get final reference", resp, err)
1448-
return nil, fmt.Errorf("failed to get final reference for %q: %w", ref, err)
1450+
if reference == nil {
1451+
reference, resp, err = githubClient.Git.GetRef(ctx, owner, repo, ref)
1452+
if err != nil {
1453+
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get final reference", resp, err)
1454+
return nil, fmt.Errorf("failed to get final reference for %q: %w", ref, err)
1455+
}
14491456
}
14501457

14511458
sha = reference.GetObject().GetSHA()

0 commit comments

Comments
 (0)