Skip to content

Commit

Permalink
Merge pull request #205 from buildkite/teams-api-update
Browse files Browse the repository at this point in the history
Support Team Suites and Team Pipelines API
  • Loading branch information
lizrabuya authored Nov 6, 2024
2 parents e382f5b + 4bd3e18 commit 8be22bb
Show file tree
Hide file tree
Showing 5 changed files with 670 additions and 0 deletions.
4 changes: 4 additions & 0 deletions buildkite.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Client struct {
User *UserService
Teams *TeamsService
TeamMember *TeamMemberService
TeamPipelines *TeamPipelinesService
TeamSuites *TeamSuitesService
Tests *TestsService
TestRuns *TestRunsService
TestSuites *TestSuitesService
Expand Down Expand Up @@ -162,6 +164,8 @@ func (c *Client) populateDefaultServices() {
c.User = &UserService{c}
c.Teams = &TeamsService{c}
c.TeamMember = &TeamMemberService{c}
c.TeamPipelines = &TeamPipelinesService{c}
c.TeamSuites = &TeamSuitesService{c}
c.Tests = &TestsService{c}
c.TestRuns = &TestRunsService{c}
c.TestSuites = &TestSuitesService{c}
Expand Down
123 changes: 123 additions & 0 deletions team_pipelines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package buildkite

import (
"context"
"fmt"
)

// TeamPipelinesService handles communication with the team pipelines related
// methods of the buildkite API.
//
// Buildkite API docs: https://buildkite.com/docs/apis/rest-api/teams/pipelines

type TeamPipelinesService struct {
client *Client
}

type TeamPipeline struct {
ID string `json:"pipeline_id,omitempty"`
URL string `json:"pipeline_url,omitempty"`
AccessLevel string `json:"access_level,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
}

type CreateTeamPipelines struct {
PipelineID string `json:"pipeline_id,omitempty"`
AccessLevel string `json:"access_level,omitempty"`
}

type UpdateTeamPipelines struct {
AccessLevel string `json:"access_level,omitempty"`
}

type TeamPipelinesListOptions struct {
ListOptions
}

func (tps *TeamPipelinesService) List(ctx context.Context, org string, id string, opt *TeamPipelinesListOptions) ([]TeamPipeline, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/teams/%s/pipelines", org, id)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}

req, err := tps.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}

var teamPipelines []TeamPipeline
resp, err := tps.client.Do(req, &teamPipelines)
if err != nil {
return nil, resp, err
}

return teamPipelines, resp, err
}

func (tps *TeamPipelinesService) Get(ctx context.Context, org string, teamID string, pipelineID string) (TeamPipeline, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/teams/%s/pipelines/%s", org, teamID, pipelineID)

req, err := tps.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return TeamPipeline{}, nil, err
}

var tp TeamPipeline
resp, err := tps.client.Do(req, &tp)
if err != nil {
return TeamPipeline{}, resp, err
}

return tp, resp, err
}

func (tps *TeamPipelinesService) Create(ctx context.Context, org string, teamID string, ctp CreateTeamPipelines) (TeamPipeline, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/teams/%s/pipelines", org, teamID)

req, err := tps.client.NewRequest(ctx, "POST", u, ctp)
if err != nil {
return TeamPipeline{}, nil, err
}

var tp TeamPipeline
resp, err := tps.client.Do(req, &tp)
if err != nil {
return tp, resp, err
}

return tp, resp, err
}

func (tps *TeamPipelinesService) Update(ctx context.Context, org string, teamID string, pipelineID string, utp UpdateTeamPipelines) (TeamPipeline, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/teams/%s/pipelines/%s", org, teamID, pipelineID)

req, err := tps.client.NewRequest(ctx, "PATCH", u, utp)
if err != nil {
return TeamPipeline{}, nil, err
}

var tp TeamPipeline
resp, err := tps.client.Do(req, &tp)
if err != nil {
return TeamPipeline{}, resp, err
}

return tp, resp, err
}

func (tps *TeamPipelinesService) Delete(ctx context.Context, org string, teamID string, pipelineID string) (*Response, error) {
u := fmt.Sprintf("v2/organizations/%s/teams/%s/pipelines/%s", org, teamID, pipelineID)

req, err := tps.client.NewRequest(ctx, "DELETE", u, nil)
if err != nil {
return nil, err
}

resp, err := tps.client.Do(req, nil)
if err != nil {
return resp, err
}

return resp, err
}
212 changes: 212 additions & 0 deletions team_pipelines_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package buildkite

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

func TestTeamPipelinesService_List(t *testing.T) {
t.Parallel()

server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)

server.HandleFunc("/v2/organizations/my-org/teams/c6fa9b07-efeb-4aea-b5ad-c4aa01e91038/pipelines", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w,
`
[{
"access_level": "manage_build_and_read",
"created_at": "2023-08-10T05:24:08.651Z",
"pipeline_id": "1239d7f9-394a-4d99-badf-7c3d8577a8ff",
"pipeline_url": "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-1"
},
{
"access_level": "manage_build_and_read",
"created_at": "2023-08-10T05:24:08.663Z",
"pipeline_id": "4569ddb1-1697-4fad-a46b-372f7318432d",
"pipeline_url": "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-2"
}]
`)
})

got, _, err := client.TeamPipelines.List(context.Background(), "my-org", "c6fa9b07-efeb-4aea-b5ad-c4aa01e91038", nil)
if err != nil {
t.Errorf("TeamPipelinesService.List returned error: %v", err)
}

pipeline1CreatedAt := must(time.Parse(BuildKiteDateFormat, "2023-08-10T05:24:08.651Z"))
pipeline2CreatedAt := must(time.Parse(BuildKiteDateFormat, "2023-08-10T05:24:08.663Z"))

want := []TeamPipeline{
{
ID: "1239d7f9-394a-4d99-badf-7c3d8577a8ff",
URL: "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-1",
AccessLevel: "manage_build_and_read",
CreatedAt: NewTimestamp(pipeline1CreatedAt),
},
{
ID: "4569ddb1-1697-4fad-a46b-372f7318432d",
URL: "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-2",
AccessLevel: "manage_build_and_read",
CreatedAt: NewTimestamp(pipeline2CreatedAt),
},
}

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("TeamPipelinesService.List diff: (-got +want)\n%s", diff)
}
}

func TestTeamPipelinesService_Get(t *testing.T) {
t.Parallel()

server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)

server.HandleFunc("/v2/organizations/my-org/teams/c6fa9b07-efeb-4aea-b5ad-c4aa01e91038/pipelines/1239d7f9-394a-4d99-badf-7c3d8577a8ff", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w,
`
{
"access_level": "manage_build_and_read",
"created_at": "2023-08-10T05:24:08.651Z",
"pipeline_id": "1239d7f9-394a-4d99-badf-7c3d8577a8ff",
"pipeline_url": "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-1"
}`)
})

got, _, err := client.TeamPipelines.Get(context.Background(), "my-org", "c6fa9b07-efeb-4aea-b5ad-c4aa01e91038", "1239d7f9-394a-4d99-badf-7c3d8577a8ff")
if err != nil {
t.Errorf("TeamPipelinesService.Get returned error: %v", err)
}

pipelineCreatedAt := must(time.Parse(BuildKiteDateFormat, "2023-08-10T05:24:08.651Z"))
want := TeamPipeline{
ID: "1239d7f9-394a-4d99-badf-7c3d8577a8ff",
URL: "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-1",
AccessLevel: "manage_build_and_read",
CreatedAt: NewTimestamp(pipelineCreatedAt),
}

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("TeamPipelinesService.Get diff: (-got +want)\n%s", diff)
}

}

func TestTeamPipelinesService_Create(t *testing.T) {
t.Parallel()

server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)

input := CreateTeamPipelines{
PipelineID: "1239d7f9-394a-4d99-badf-7c3d8577a8ff",
AccessLevel: "manage_build_and_read",
}

server.HandleFunc("/v2/organizations/my-org/teams/c6fa9b07-efeb-4aea-b5ad-c4aa01e91038/pipelines", func(w http.ResponseWriter, r *http.Request) {
var v CreateTeamPipelines
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
t.Errorf("Error parsing json body: %v", err)
}

testMethod(t, r, "POST")

if diff := cmp.Diff(v, input); diff != "" {
t.Errorf("create Team Pipelines input diff: (-got +want)\n%s", diff)
}

fmt.Fprint(w,
`
{
"access_level": "manage_build_and_read",
"created_at": "2023-08-10T05:24:08.651Z",
"pipeline_id": "1239d7f9-394a-4d99-badf-7c3d8577a8ff",
"pipeline_url": "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-1"
}`)
})

got, _, err := client.TeamPipelines.Create(context.Background(), "my-org", "c6fa9b07-efeb-4aea-b5ad-c4aa01e91038", input)
if err != nil {
t.Errorf("TeamPipelinesService.Create returned error: %v", err)
}

pipelineCreatedAt := must(time.Parse(BuildKiteDateFormat, "2023-08-10T05:24:08.651Z"))
want := TeamPipeline{
ID: "1239d7f9-394a-4d99-badf-7c3d8577a8ff",
URL: "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-1",
AccessLevel: "manage_build_and_read",
CreatedAt: NewTimestamp(pipelineCreatedAt),
}

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("TeamPipelinesService.Create diff: (-got +want)\n%s", diff)
}
}

func TestTeamPipelinesService_Update(t *testing.T) {
t.Parallel()

server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)

server.HandleFunc("/v2/organizations/my-org/teams/c6fa9b07-efeb-4aea-b5ad-c4aa01e91038/pipelines/1239d7f9-394a-4d99-badf-7c3d8577a8ff", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PATCH")
fmt.Fprint(w,
`
{
"access_level": "build_and_read",
"created_at": "2023-08-10T05:24:08.651Z",
"pipeline_id": "1239d7f9-394a-4d99-badf-7c3d8577a8ff",
"pipeline_url": "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-1"
}`)
})

wantUpdate := UpdateTeamPipelines{
AccessLevel: "build_and_read",
}

got, _, err := client.TeamPipelines.Update(context.Background(), "my-org", "c6fa9b07-efeb-4aea-b5ad-c4aa01e91038", "1239d7f9-394a-4d99-badf-7c3d8577a8ff", wantUpdate)
if err != nil {
t.Errorf("TeamPipelinesService.Get returned error: %v", err)
}

pipelineCreatedAt := must(time.Parse(BuildKiteDateFormat, "2023-08-10T05:24:08.651Z"))
want := TeamPipeline{
ID: "1239d7f9-394a-4d99-badf-7c3d8577a8ff",
URL: "https://api.buildkite.com/v2/organizations/my-org/pipelines/pipeline-1",
AccessLevel: "build_and_read",
CreatedAt: NewTimestamp(pipelineCreatedAt),
}

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("TeamPipelinesService.Get diff: (-got +want)\n%s", diff)
}
}

func TestTeamPipelinesService_Delete(t *testing.T) {
t.Parallel()

server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)

server.HandleFunc("/v2/organizations/my-org/teams/c6fa9b07-efeb-4aea-b5ad-c4aa01e91038/pipelines/1239d7f9-394a-4d99-badf-7c3d8577a8ff", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})

_, err := client.TeamPipelines.Delete(context.Background(), "my-org", "c6fa9b07-efeb-4aea-b5ad-c4aa01e91038", "1239d7f9-394a-4d99-badf-7c3d8577a8ff")

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

}
Loading

0 comments on commit 8be22bb

Please sign in to comment.