Skip to content

Commit 564bb7d

Browse files
committed
fix linting by using inverted if instead of empty if block
1 parent b9e5ea4 commit 564bb7d

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

pkg/github/repositories.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,45 +1403,45 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
14031403

14041404
originalRef := ref // Keep original ref for clearer error messages down the line.
14051405

1406-
if strings.HasPrefix(ref, "refs/") {
1407-
// 2b) Already fully qualified. We will use it.
1408-
} else if strings.HasPrefix(ref, "heads/") || strings.HasPrefix(ref, "tags/") {
1409-
ref = "refs/" + ref // 2c) Partially qualified. Make it fully qualified.
1410-
} else {
1411-
// 2d) Short name. Try to resolve it as a branch or tag.
1412-
_, resp, err := githubClient.Git.GetRef(ctx, owner, repo, "refs/heads/"+ref)
1413-
1414-
// try to resolve the ref as a branch first.
1415-
if err == nil {
1416-
ref = "refs/heads/" + ref // It's a branch.
1406+
// 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
14171411
} else {
1418-
// The branch lookup failed. Check if it was a 404 Not Found error.
1419-
// If it was, we will try to resolve it as a tag.
1420-
ghErr, isGhErr := err.(*github.ErrorResponse)
1421-
if isGhErr && ghErr.Response.StatusCode == http.StatusNotFound {
1422-
// The branch wasn't found, so try as a tag.
1423-
_, resp2, err2 := githubClient.Git.GetRef(ctx, owner, repo, "refs/tags/"+ref)
1424-
if err2 == nil {
1425-
ref = "refs/tags/" + ref // It's a tag.
1426-
} else {
1427-
// The tag lookup also failed. Check if it was a 404 Not Found error.
1428-
ghErr2, isGhErr2 := err2.(*github.ErrorResponse)
1429-
if isGhErr2 && ghErr2.Response.StatusCode == http.StatusNotFound {
1430-
return nil, fmt.Errorf("could not resolve ref %q as a branch or a tag", originalRef)
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)
14311434
}
1432-
// The tag lookup failed for a different reason.
1433-
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (tag)", resp2, err2)
1434-
return nil, fmt.Errorf("failed to get reference for tag '%s': %w", originalRef, err2)
1435+
} 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)
14351439
}
1436-
} else {
1437-
// The branch lookup failed for a different reason.
1438-
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (branch)", resp, err)
1439-
return nil, fmt.Errorf("failed to get reference for branch '%s': %w", originalRef, err)
14401440
}
14411441
}
14421442
}
14431443

1444-
// Now that 'ref' is a valid, fully-qualified name, we get the definitive reference object.
1444+
// Now that 'ref' is fully qualified, we get the definitive reference object.
14451445
reference, resp, err := githubClient.Git.GetRef(ctx, owner, repo, ref)
14461446
if err != nil {
14471447
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get final reference", resp, err)

pkg/github/repositories_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,13 +2324,14 @@ func Test_resolveGitReference(t *testing.T) {
23242324
mock.WithRequestMatchHandler(
23252325
mock.GetReposGitRefByOwnerByRepoByRef,
23262326
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2327-
if strings.Contains(r.URL.Path, "/git/ref/heads/v1.0.0") {
2327+
switch {
2328+
case strings.Contains(r.URL.Path, "/git/ref/heads/v1.0.0"):
23282329
w.WriteHeader(http.StatusNotFound)
23292330
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
2330-
} else if strings.Contains(r.URL.Path, "/git/ref/tags/v1.0.0") {
2331+
case strings.Contains(r.URL.Path, "/git/ref/tags/v1.0.0"):
23312332
w.WriteHeader(http.StatusOK)
23322333
_, _ = w.Write([]byte(`{"ref": "refs/tags/v1.0.0", "object": {"sha": "tag-sha"}}`))
2333-
} else {
2334+
default:
23342335
t.Errorf("Unexpected path: %s", r.URL.Path)
23352336
w.WriteHeader(http.StatusNotFound)
23362337
}
@@ -2396,7 +2397,7 @@ func Test_resolveGitReference(t *testing.T) {
23962397
return mock.NewMockedHTTPClient(
23972398
mock.WithRequestMatchHandler(
23982399
mock.GetReposGitRefByOwnerByRepoByRef,
2399-
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2400+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
24002401
// Both branch and tag attempts should return 404
24012402
w.WriteHeader(http.StatusNotFound)
24022403
_, _ = w.Write([]byte(`{"message": "Not Found"}`))

0 commit comments

Comments
 (0)