MCPcopy
hub / github.com/kubernetes/test-infra / FetchGoVersion

Function FetchGoVersion

releng/prepare-release-branch/internal/run/run.go:116–143  ·  view source on GitHub ↗

FetchGoVersion fetches the Go version string from the given URL. Returns ErrBranchNotFound if the release branch does not exist (HTTP 404).

(ctx context.Context, url string)

Source from the content-addressed store, hash-verified

114// FetchGoVersion fetches the Go version string from the given URL.
115// Returns ErrBranchNotFound if the release branch does not exist (HTTP 404).
116func FetchGoVersion(ctx context.Context, url string) (string, error) {
117 req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
118 if err != nil {
119 return "", fmt.Errorf("creating request: %w", err)
120 }
121
122 resp, err := http.DefaultClient.Do(req)
123 if err != nil {
124 return "", fmt.Errorf("fetching Go version: %w", err)
125 }
126
127 defer func() { _ = resp.Body.Close() }()
128
129 if resp.StatusCode == http.StatusNotFound {
130 return "", fmt.Errorf("%w: %s", ErrBranchNotFound, url)
131 }
132
133 if resp.StatusCode != http.StatusOK {
134 return "", fmt.Errorf("%w: %d", ErrHTTPStatus, resp.StatusCode)
135 }
136
137 body, err := io.ReadAll(resp.Body)
138 if err != nil {
139 return "", fmt.Errorf("reading Go version response: %w", err)
140 }
141
142 return strings.TrimSpace(string(body)), nil
143}

Callers 5

executeFunction · 0.92
TestFetchGoVersionFunction · 0.92

Calls 1

CloseMethod · 0.45

Tested by 4

TestFetchGoVersionFunction · 0.74