Browse Source

Pin GitHub API version in requests (#4647)

Signed-off-by: utafrali <tafraliugur@gmail.com>
pull/4640/merge
Uğur Tafralı 14 hours ago committed by GitHub
parent
commit
0f9b7eba77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      connector/github/github.go
  2. 15
      connector/github/github_test.go

4
connector/github/github.go

@ -28,6 +28,8 @@ const (
// GitHub requires this scope to access '/user/teams' and '/orgs' API endpoints // GitHub requires this scope to access '/user/teams' and '/orgs' API endpoints
// which are used when a client includes the 'groups' scope. // which are used when a client includes the 'groups' scope.
scopeOrgs = "read:org" scopeOrgs = "read:org"
// githubAPIVersion pins the GitHub REST API version used in requests.
githubAPIVersion = "2022-11-28"
) )
// Pagination URL patterns // Pagination URL patterns
@ -462,6 +464,7 @@ func get(ctx context.Context, client *http.Client, apiURL string, v interface{})
return "", fmt.Errorf("github: new req: %v", err) return "", fmt.Errorf("github: new req: %v", err)
} }
req = req.WithContext(ctx) req = req.WithContext(ctx)
req.Header.Set("X-GitHub-Api-Version", githubAPIVersion)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return "", fmt.Errorf("github: get URL %v", err) return "", fmt.Errorf("github: get URL %v", err)
@ -659,6 +662,7 @@ func (c *githubConnector) userInOrg(ctx context.Context, client *http.Client, us
return false, fmt.Errorf("github: new req: %v", err) return false, fmt.Errorf("github: new req: %v", err)
} }
req = req.WithContext(ctx) req = req.WithContext(ctx)
req.Header.Set("X-GitHub-Api-Version", githubAPIVersion)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return false, fmt.Errorf("github: get teams: %v", err) return false, fmt.Errorf("github: get teams: %v", err)

15
connector/github/github_test.go

@ -485,6 +485,21 @@ func Test_Open_PreferredDomainConfig(t *testing.T) {
} }
} }
func TestGetSendsAPIVersionHeader(t *testing.T) {
var gotHeader string
s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotHeader = r.Header.Get("X-GitHub-Api-Version")
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode([]org{})
}))
defer s.Close()
var result []org
_, err := get(context.Background(), newClient(), s.URL+"/user/orgs", &result)
expectNil(t, err)
expectEquals(t, gotHeader, githubAPIVersion)
}
func newTestServer(responses map[string]testResponse) *httptest.Server { func newTestServer(responses map[string]testResponse) *httptest.Server {
var s *httptest.Server var s *httptest.Server
s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Loading…
Cancel
Save