Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Add support for eager loading on active endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuhei Kitagawa committed May 2, 2019
1 parent 060d87e commit 4b2e6c4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
30 changes: 18 additions & 12 deletions active.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
8 changes: 6 additions & 2 deletions active_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 4b2e6c4

Please sign in to comment.