From 4b2e6c4af0dfac75e36722578196eba9aa7a2051 Mon Sep 17 00:00:00 2001 From: Shuhei Kitagawa Date: Thu, 2 May 2019 15:41:57 +0900 Subject: [PATCH] Add support for eager loading on active endpoint --- active.go | 30 ++++++++++++++++++------------ active_integration_test.go | 8 ++++++-- active_test.go | 8 ++++++-- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/active.go b/active.go index 555e080..d5dfe8b 100644 --- a/active.go +++ b/active.go @@ -17,17 +17,23 @@ type ActiveService struct { client *Client } -// getActiveResponse represents the response of a call +// activeResponse represents the response of a call // to the Travis CI active endpoint. -type getActiveResponse struct { +type activeResponse struct { Builds []*Build `json:"builds"` } +// ActiveOption specifies the optional parameters for active endpoint +type ActiveOption struct { + // List of attributes to eager load + Include []string `url:"include,omitempty,comma"` +} + // FindByOwner fetches active builds based on the owner's name // // Travis CI API docs: https://developer.travis-ci.com/resource/active#for_owner -func (as *ActiveService) FindByOwner(ctx context.Context, owner string) ([]*Build, *http.Response, error) { - u, err := urlWithOptions(fmt.Sprintf("/owner/%s/active", owner), nil) +func (as *ActiveService) FindByOwner(ctx context.Context, owner string, opt *ActiveOption) ([]*Build, *http.Response, error) { + u, err := urlWithOptions(fmt.Sprintf("/owner/%s/active", owner), opt) if err != nil { return nil, nil, err } @@ -37,20 +43,20 @@ func (as *ActiveService) FindByOwner(ctx context.Context, owner string) ([]*Buil return nil, nil, err } - var getActiveResponse getActiveResponse - resp, err := as.client.Do(ctx, req, &getActiveResponse) + var ar activeResponse + resp, err := as.client.Do(ctx, req, &ar) if err != nil { return nil, resp, err } - return getActiveResponse.Builds, resp, err + return ar.Builds, resp, err } // FindByGitHubId fetches active builds based on the owner's GitHub id // // Travis CI API docs: https://developer.travis-ci.com/resource/active#for_owner -func (as *ActiveService) FindByGitHubId(ctx context.Context, githubId uint) ([]*Build, *http.Response, error) { - u, err := urlWithOptions(fmt.Sprintf("/owner/github_id/%d/active", githubId), nil) +func (as *ActiveService) FindByGitHubId(ctx context.Context, githubId uint, opt *ActiveOption) ([]*Build, *http.Response, error) { + u, err := urlWithOptions(fmt.Sprintf("/owner/github_id/%d/active", githubId), opt) if err != nil { return nil, nil, err } @@ -60,11 +66,11 @@ func (as *ActiveService) FindByGitHubId(ctx context.Context, githubId uint) ([]* return nil, nil, err } - var getActiveResponse getActiveResponse - resp, err := as.client.Do(ctx, req, &getActiveResponse) + var ar activeResponse + resp, err := as.client.Do(ctx, req, &ar) if err != nil { return nil, resp, err } - return getActiveResponse.Builds, resp, err + return ar.Builds, resp, err } diff --git a/active_integration_test.go b/active_integration_test.go index 5d9d0c9..392fdd7 100644 --- a/active_integration_test.go +++ b/active_integration_test.go @@ -14,7 +14,9 @@ import ( ) func TestActiveService_Integration_FindByOwner(t *testing.T) { - _, res, err := integrationClient.Active.FindByOwner(context.TODO(), integrationGitHubOwner) + opt := ActiveOption{Include: []string{"active.builds"}} + + _, res, err := integrationClient.Active.FindByOwner(context.TODO(), integrationGitHubOwner, &opt) if err != nil { t.Fatalf("unexpected error occured: %s", err) @@ -26,7 +28,9 @@ func TestActiveService_Integration_FindByOwner(t *testing.T) { } func TestActiveService_Integration_FindByGitHubId(t *testing.T) { - _, res, err := integrationClient.Active.FindByGitHubId(context.TODO(), integrationGitHubOwnerId) + opt := ActiveOption{Include: []string{"active.builds"}} + + _, res, err := integrationClient.Active.FindByGitHubId(context.TODO(), integrationGitHubOwnerId, &opt) if err != nil { t.Fatalf("unexpected error occured: %s", err) diff --git a/active_test.go b/active_test.go index de9487d..6c2cd6e 100644 --- a/active_test.go +++ b/active_test.go @@ -22,10 +22,12 @@ func TestActiveService_FindByOwner(t *testing.T) { mux.HandleFunc(fmt.Sprintf("/owner/%s/active", testOwner), func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) + testFormValues(t, r, values{"include": "active.builds"}) fmt.Fprint(w, `{"builds": [{"id":1,"number":"1","state":"created","duration":10}]}`) }) - builds, _, err := client.Active.FindByOwner(context.Background(), testOwner) + opt := ActiveOption{Include: []string{"active.builds"}} + builds, _, err := client.Active.FindByOwner(context.Background(), testOwner, &opt) if err != nil { t.Errorf("Active.FindByOwner returned error: %v", err) @@ -43,10 +45,12 @@ func TestActiveService_FindByGitHubId(t *testing.T) { mux.HandleFunc(fmt.Sprintf("/owner/github_id/%d/active", testGitHubId), func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) + testFormValues(t, r, values{"include": "active.builds"}) fmt.Fprint(w, `{"builds": [{"id":1,"number":"1","state":"created","duration":10}]}`) }) - builds, _, err := client.Active.FindByGitHubId(context.Background(), testGitHubId) + opt := ActiveOption{Include: []string{"active.builds"}} + builds, _, err := client.Active.FindByGitHubId(context.Background(), testGitHubId, &opt) if err != nil { t.Errorf("Active.FindByGitHubId returned error: %v", err)