Skip to content

Commit 38a00b8

Browse files
authored
Merge pull request cli#11481 from cli/eugene/test-for-release-FetchRefSHA
add test for FetchRefSHA
2 parents 6fa0035 + a45107f commit 38a00b8

2 files changed

Lines changed: 98 additions & 5 deletions

File tree

pkg/cmd/release/shared/fetch.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ type fetchResult struct {
136136
}
137137

138138
func FetchRefSHA(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface, tagName string) (string, error) {
139-
path := fmt.Sprintf("repos/%s/%s/git/refs/tags/%s", repo.RepoOwner(), repo.RepoName(), tagName)
139+
path := fmt.Sprintf("repos/%s/%s/git/ref/tags/%s", repo.RepoOwner(), repo.RepoName(), tagName)
140140
req, err := http.NewRequestWithContext(ctx, "GET", ghinstance.RESTPrefix(repo.RepoHost())+path, nil)
141141
if err != nil {
142142
return "", err
@@ -150,7 +150,6 @@ func FetchRefSHA(ctx context.Context, httpClient *http.Client, repo ghrepo.Inter
150150

151151
if resp.StatusCode == http.StatusNotFound {
152152
_, _ = io.Copy(io.Discard, resp.Body)
153-
// ErrRefNotFound
154153
return "", ErrReleaseNotFound
155154
}
156155

@@ -163,9 +162,9 @@ func FetchRefSHA(ctx context.Context, httpClient *http.Client, repo ghrepo.Inter
163162
SHA string `json:"sha"`
164163
} `json:"object"`
165164
}
165+
166166
if err := json.NewDecoder(resp.Body).Decode(&ref); err != nil {
167-
// release not found
168-
return "", ErrReleaseNotFound
167+
return "", fmt.Errorf("failed to parse ref response: %w", err)
169168
}
170169

171170
return ref.Object.SHA, nil
@@ -288,7 +287,7 @@ func StubFetchRelease(t *testing.T, reg *httpmock.Registry, owner, repoName, tag
288287
}
289288

290289
func StubFetchRefSHA(t *testing.T, reg *httpmock.Registry, owner, repoName, tagName, sha string) {
291-
path := fmt.Sprintf("repos/%s/%s/git/refs/tags/%s", owner, repoName, tagName)
290+
path := fmt.Sprintf("repos/%s/%s/git/ref/tags/%s", owner, repoName, tagName)
292291
reg.Register(
293292
httpmock.REST("GET", path),
294293
httpmock.StringResponse(fmt.Sprintf(`{"object": {"sha": "%s"}}`, sha)),
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package shared
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/cli/go-gh/v2/pkg/api"
9+
10+
"github.com/cli/cli/v2/internal/ghrepo"
11+
"github.com/cli/cli/v2/pkg/httpmock"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestFetchRefSHA(t *testing.T) {
17+
tests := []struct {
18+
name string
19+
tagName string
20+
responseStatus int
21+
responseBody string
22+
responseMessage string
23+
expectedSHA string
24+
errorMessage string
25+
}{
26+
{
27+
name: "match (200)",
28+
tagName: "v1.2.3",
29+
responseStatus: 200,
30+
responseBody: `{"object": {"sha": "1234567890abcdef1234567890abcdef12345678"}}`,
31+
expectedSHA: "1234567890abcdef1234567890abcdef12345678",
32+
},
33+
{
34+
name: "non-match (404)",
35+
tagName: "v1.2.3",
36+
responseStatus: 404,
37+
responseMessage: `Not found`,
38+
errorMessage: "release not found",
39+
},
40+
{
41+
name: "server error (500)",
42+
tagName: "v1.2.3",
43+
responseStatus: 500,
44+
responseMessage: `arbitrary error"`,
45+
errorMessage: "HTTP 500: arbitrary error\" (https://api.github.com/repos/owner/repo/git/ref/tags/v1.2.3)",
46+
},
47+
{
48+
name: "malformed JSON with 200",
49+
tagName: "v1.2.3",
50+
responseStatus: 200,
51+
responseBody: `{"object": {"sha":`,
52+
errorMessage: "failed to parse ref response: unexpected EOF",
53+
},
54+
}
55+
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
fakeHTTP := &httpmock.Registry{}
59+
defer fakeHTTP.Verify(t)
60+
61+
repo, err := ghrepo.FromFullName("owner/repo")
62+
require.NoError(t, err)
63+
64+
path := "repos/owner/repo/git/ref/tags/" + tt.tagName
65+
if tt.responseStatus == 404 || tt.responseStatus == 500 {
66+
fakeHTTP.Register(
67+
httpmock.REST("GET", path),
68+
httpmock.JSONErrorResponse(tt.responseStatus, api.HTTPError{
69+
StatusCode: tt.responseStatus,
70+
Message: tt.responseMessage,
71+
}),
72+
)
73+
} else {
74+
fakeHTTP.Register(
75+
httpmock.REST("GET", path),
76+
httpmock.StatusStringResponse(tt.responseStatus, tt.responseBody),
77+
)
78+
}
79+
80+
httpClient := &http.Client{Transport: fakeHTTP}
81+
ctx := context.Background()
82+
83+
sha, err := FetchRefSHA(ctx, httpClient, repo, tt.tagName)
84+
85+
if tt.errorMessage != "" {
86+
assert.Contains(t, err.Error(), tt.errorMessage)
87+
assert.Empty(t, sha)
88+
} else {
89+
require.NoError(t, err)
90+
assert.Equal(t, tt.expectedSHA, sha)
91+
}
92+
})
93+
}
94+
}

0 commit comments

Comments
 (0)