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 the stages endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuhei Kitagawa committed Apr 21, 2019
1 parent fc7481b commit 985e5d3
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 27 deletions.
27 changes: 0 additions & 27 deletions stage.go

This file was deleted.

72 changes: 72 additions & 0 deletions stages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package travis

import (
"context"
"fmt"
"net/http"
)

// StagesService handles communication with the stage
// related methods of the Travis CI API.
type StagesService struct {
client *Client
}

// Stage is a standard representation of an individual stage
//
// Travis CI API docs: https://developer.travis-ci.com/resource/stage#standard-representation
type Stage struct {
// Value uniquely identifying the stage
Id uint `json:"id,omitempty"`
// Incremental number for a stage
Number uint `json:"number,omitempty"`
// The name of the stage
Name string `json:"name,omitempty"`
// Current state of the stage
State string `json:"state,omitempty"`
// When the stage started
StartedAt string `json:"started_at,omitempty"`
// When the stage finished
FinishedAt string `json:"finished_at,omitempty"`
// The jobs of a stage.
Jobs []*Job `json:"jobs,omitempty"`
*Metadata
}

// StagesOption is query parameters to one can specify to list stages
type StagesOption struct {
// List of attributes to eager load
Include []string `url:"include,omitempty,comma"`
}

type stagesResponse struct {
Stages []*Stage `json:"stages"`
}

// ListByBuild fetches stages of the build
//
// Travis CI API docs: https://developer.travis-ci.com/resource/stages#find
func (ss *StagesService) ListByBuild(ctx context.Context, buildId uint, opt *StagesOption) ([]*Stage, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/build/%d/stages", buildId), opt)
if err != nil {
return nil, nil, err
}

req, err := ss.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}

var sr stagesResponse
resp, err := ss.client.Do(ctx, req, &sr)
if err != nil {
return nil, resp, err
}

return sr.Stages, resp, err
}
27 changes: 27 additions & 0 deletions stages_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build integration

package travis

import (
"context"
"net/http"
"testing"
)

func TestStagesService_Integration_ListByBuild(t *testing.T) {
opt := StagesOption{Include: []string{"stage.jobs"}}
_, res, err := integrationClient.Stages.ListByBuild(context.TODO(), integrationBuildId, &opt)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("invalid http status: %s", res.Status)
}
}
38 changes: 38 additions & 0 deletions stages_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package travis

import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
)

func TestStagesService_ListByBuild(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

var buildId uint = 10
mux.HandleFunc(fmt.Sprintf("/build/%d/stages", buildId), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testFormValues(t, r, values{"include": "stage.jobs"})
fmt.Fprint(w, `{"stages": [{"id":1,"number":2,"name":"Test"}]}`)
})

opt := StagesOption{Include: []string{"stage.jobs"}}
stages, _, err := client.Stages.ListByBuild(context.Background(), buildId, &opt)

if err != nil {
t.Errorf("Repository.List returned error: %v", err)
}

want := &Stage{Id: 1, Number: 2, Name: "Test"}
if !reflect.DeepEqual(stages[0], want) {
t.Errorf("Stages.ListByBuild returned %+v, want %+v", stages[0], want)
}
}
2 changes: 2 additions & 0 deletions travis.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type Client struct {
Repositories *RepositoriesService
Requests *RequestsService
Settings *SettingsService
Stages *StagesService
User *UserService
}

Expand Down Expand Up @@ -113,6 +114,7 @@ func NewClient(baseUrl string, travisToken string) *Client {
c.Repositories = &RepositoriesService{client: c}
c.Requests = &RequestsService{client: c}
c.Settings = &SettingsService{client: c}
c.Stages = &StagesService{client: c}
c.User = &UserService{client: c}

if travisToken != "" {
Expand Down

0 comments on commit 985e5d3

Please sign in to comment.