mirror of
https://github.com/netbirdio/dex.git
synced 2026-05-22 18:43:53 -07:00
Pin GitHub API version in requests (#4647)
Signed-off-by: utafrali <tafraliugur@gmail.com>
This commit is contained in:
@@ -28,6 +28,8 @@ const (
|
||||
// GitHub requires this scope to access '/user/teams' and '/orgs' API endpoints
|
||||
// which are used when a client includes the 'groups' scope.
|
||||
scopeOrgs = "read:org"
|
||||
// githubAPIVersion pins the GitHub REST API version used in requests.
|
||||
githubAPIVersion = "2022-11-28"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
req.Header.Set("X-GitHub-Api-Version", githubAPIVersion)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
req.Header.Set("X-GitHub-Api-Version", githubAPIVersion)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("github: get teams: %v", err)
|
||||
|
||||
@@ -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 {
|
||||
var s *httptest.Server
|
||||
s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user