| 1 |
package github |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
const ( |
| 14 |
githubAPIHost = "api.github.com" |
| 15 |
// Successful API responses are limited to 1 MiB. |
| 16 |
maxResponseBodySize = 1 << 20 |
| 17 |
// Error responses are retained only as a 4 KiB diagnostic excerpt. |
| 18 |
maxErrorResponseBodySize = 4 << 10 |
| 19 |
) |
| 20 |
|
| 21 |
// get performs an authenticated GitHub API request and decodes the JSON response. |
| 22 |
func get[T any](ctx context.Context, httpClient *http.Client, accessToken, apiURL string, data *T) (string, error) { |
| 23 |
if err := validateAPIURL(apiURL); err != nil { |
| 24 |
return "", err |
| 25 |
} |
| 26 |
|
| 27 |
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil) |
| 28 |
if err != nil { |
| 29 |
return "", fmt.Errorf("error creating request context with URL %s: %w", apiURL, err) |
| 30 |
} |
| 31 |
|
| 32 |
req.Header.Add("Authorization", "Bearer "+accessToken) |
| 33 |
req.Header.Add("Accept", "application/json") |
| 34 |
|
| 35 |
resp, err := httpClient.Do(req) |
| 36 |
if err != nil { |
| 37 |
return "", fmt.Errorf("error calling GitHub API %s: %w", apiURL, err) |
| 38 |
} |
| 39 |
|
| 40 |
defer resp.Body.Close() |
| 41 |
|
| 42 |
bodyLimit := maxResponseBodySize |
| 43 |
if resp.StatusCode != http.StatusOK { |
| 44 |
bodyLimit = maxErrorResponseBodySize |
| 45 |
} |
| 46 |
|
| 47 |
respBody, truncated, err := readResponseBody(resp.Body, bodyLimit) |
| 48 |
if err != nil { |
| 49 |
return "", fmt.Errorf("unable to read body from GitHub API %s: http status code: %d; error: %w", apiURL, resp.StatusCode, err) |
| 50 |
} |
| 51 |
|
| 52 |
if resp.StatusCode != http.StatusOK { |
| 53 |
truncationMarker := "" |
| 54 |
if truncated { |
| 55 |
truncationMarker = " [truncated]" |
| 56 |
} |
| 57 |
|
| 58 |
return "", fmt.Errorf( |
| 59 |
"error from GitHub API %s: http status code: %d; message: %s%s", |
| 60 |
apiURL, |
| 61 |
resp.StatusCode, |
| 62 |
respBody, |
| 63 |
truncationMarker, |
| 64 |
) |
| 65 |
} |
| 66 |
|
| 67 |
if truncated { |
| 68 |
return "", fmt.Errorf("response body from GitHub API %s exceeds the %d byte limit", apiURL, maxResponseBodySize) |
| 69 |
} |
| 70 |
|
| 71 |
if err = json.Unmarshal(respBody, data); err != nil { |
| 72 |
return "", fmt.Errorf("unable to decode JSON from GitHub API %s: '%s': %w", apiURL, respBody, err) |
| 73 |
} |
| 74 |
|
| 75 |
return getPagination(apiURL, resp) |
| 76 |
} |
| 77 |
|
| 78 |
func validateAPIURL(apiURL string) error { |
| 79 |
parsedURL, err := url.Parse(apiURL) |
| 80 |
if err != nil { |
| 81 |
return fmt.Errorf("unable to parse GitHub API URL %q: %w", apiURL, err) |
| 82 |
} |
| 83 |
|
| 84 |
if parsedURL.Scheme != "https" || parsedURL.Host != githubAPIHost { |
| 85 |
return fmt.Errorf("untrusted GitHub API URL %q", apiURL) |
| 86 |
} |
| 87 |
|
| 88 |
return nil |
| 89 |
} |
| 90 |
|
| 91 |
func readResponseBody(body io.Reader, limit int) ([]byte, bool, error) { |
| 92 |
responseBody, err := io.ReadAll(io.LimitReader(body, int64(limit+1))) |
| 93 |
if err != nil { |
| 94 |
return nil, false, fmt.Errorf("read limited response body: %w", err) |
| 95 |
} |
| 96 |
|
| 97 |
if len(responseBody) <= limit { |
| 98 |
return responseBody, false, nil |
| 99 |
} |
| 100 |
|
| 101 |
return responseBody[:limit], true, nil |
| 102 |
} |
| 103 |
|
| 104 |
// getPagination returns the next GitHub pagination URL when more pages are available. |
| 105 |
func getPagination(apiURL string, resp *http.Response) (string, error) { |
| 106 |
if resp == nil { |
| 107 |
return "", nil |
| 108 |
} |
| 109 |
|
| 110 |
links := resp.Header.Get("Link") |
| 111 |
|
| 112 |
nextPageURL, lastPageURL := parsePaginationLinks(links) |
| 113 |
if lastPageURL == "" { |
| 114 |
return "", nil |
| 115 |
} |
| 116 |
|
| 117 |
for _, paginationURL := range []string{nextPageURL, lastPageURL} { |
| 118 |
if paginationURL == "" { |
| 119 |
continue |
| 120 |
} |
| 121 |
|
| 122 |
if err := validateAPIURL(paginationURL); err != nil { |
| 123 |
return "", fmt.Errorf("invalid GitHub pagination URL: %w", err) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if apiURL == lastPageURL { |
| 128 |
return "", nil |
| 129 |
} |
| 130 |
|
| 131 |
return nextPageURL, nil |
| 132 |
} |
| 133 |
|
| 134 |
func parsePaginationLinks(links string) (string, string) { |
| 135 |
var nextPageURL, lastPageURL string |
| 136 |
|
| 137 |
for links != "" { |
| 138 |
links = strings.TrimSpace(links) |
| 139 |
|
| 140 |
if !strings.HasPrefix(links, "<") { |
| 141 |
return nextPageURL, lastPageURL |
| 142 |
} |
| 143 |
|
| 144 |
targetEnd := strings.IndexByte(links, '>') |
| 145 |
if targetEnd < 0 { |
| 146 |
return nextPageURL, lastPageURL |
| 147 |
} |
| 148 |
|
| 149 |
linkURL := links[1:targetEnd] |
| 150 |
attrs, remaining := splitPaginationLinkAttrs(links[targetEnd+1:]) |
| 151 |
|
| 152 |
for attr := range strings.SplitSeq(attrs, ";") { |
| 153 |
switch strings.TrimSpace(attr) { |
| 154 |
case `rel="next"`: |
| 155 |
nextPageURL = linkURL |
| 156 |
case `rel="last"`: |
| 157 |
lastPageURL = linkURL |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
links = remaining |
| 162 |
} |
| 163 |
|
| 164 |
return nextPageURL, lastPageURL |
| 165 |
} |
| 166 |
|
| 167 |
func splitPaginationLinkAttrs(attrs string) (string, string) { |
| 168 |
for i := range attrs { |
| 169 |
if attrs[i] != ',' { |
| 170 |
continue |
| 171 |
} |
| 172 |
|
| 173 |
remaining := strings.TrimSpace(attrs[i+1:]) |
| 174 |
if strings.HasPrefix(remaining, "<") { |
| 175 |
return attrs[:i], remaining |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return attrs, "" |
| 180 |
} |