This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shuhei Kitagawa
committed
Apr 21, 2019
1 parent
fc7481b
commit 985e5d3
Showing
5 changed files
with
139 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters