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.
Merge pull request #47 from shuheiktgw/support_beta_migration
Support beta_migration_requests endpoint
- Loading branch information
Showing
4 changed files
with
234 additions
and
23 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,101 @@ | ||
// 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" | ||
) | ||
|
||
// BetaMigrationRequestsService handles communication with the | ||
// beta migration requests related methods of the Travis CI API. | ||
type BetaMigrationRequestsService struct { | ||
client *Client | ||
} | ||
|
||
// BetaMigrationRequest is a standard representation of an individual beta migration request | ||
// | ||
// Travis CI API docs: https://developer.travis-ci.com/resource/beta_migration_request#attributes | ||
type BetaMigrationRequest struct { | ||
// The beta_migration_request's id | ||
Id uint `json:"id,omitempty"` | ||
// The beta_migration_request's owner_id | ||
OwnerId uint `json:"owner_id,omitempty"` | ||
// The beta_migration_request's owner_name | ||
OwnerName string `json:"owner_name,omitempty"` | ||
// Longer description of the feature | ||
OwnerType string `json:"owner_type,omitempty"` | ||
// The beta_migration_request's accepted_at | ||
AcceptedAt string `json:"accepted_at,omitempty"` | ||
// The beta_migration_request's organizations | ||
Organizations []*Organization `json:"organizations,omitempty"` | ||
*Metadata | ||
} | ||
|
||
// BetaMigrationRequestOption specifies options for | ||
// finding a beta migration requests. | ||
type BetaMigrationRequestsOption struct { | ||
// List of attributes to eager load | ||
Include []string `url:"include,omitempty,comma"` | ||
} | ||
|
||
// BetaMigrationRequestBody specifies body for | ||
// creating a beta migration request. | ||
type BetaMigrationRequestBody struct { | ||
// The beta_migration_request's organizations | ||
OrganizationIds []uint `json:"beta_migration_request.organizations"` | ||
} | ||
|
||
type betaMigrationRequestsResponse struct { | ||
BetaMigrationRequests []*BetaMigrationRequest `json:"beta_migration_requests,omitempty"` | ||
} | ||
|
||
// List fetches a list of beta migration requests created by the user | ||
// | ||
// Travis CI API docs: https://developer.travis-ci.com/resource/beta_migration_requests#find | ||
func (bs *BetaMigrationRequestsService) List(ctx context.Context, userId uint, opt *BetaMigrationRequestsOption) ([]*BetaMigrationRequest, *http.Response, error) { | ||
u, err := urlWithOptions(fmt.Sprintf("/user/%d/beta_migration_requests", userId), opt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
req, err := bs.client.NewRequest(http.MethodGet, u, nil, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var bmrr betaMigrationRequestsResponse | ||
resp, err := bs.client.Do(ctx, req, &bmrr) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return bmrr.BetaMigrationRequests, resp, err | ||
} | ||
|
||
// Create creates a beta migration request | ||
// | ||
// Travis CI API docs: https://developer.travis-ci.com/resource/beta_migration_request#create | ||
func (bs *BetaMigrationRequestsService) Create(ctx context.Context, userId uint, request *BetaMigrationRequestBody) (*BetaMigrationRequest, *http.Response, error) { | ||
u, err := urlWithOptions(fmt.Sprintf("/user/%d/beta_migration_request", userId), nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
req, err := bs.client.NewRequest(http.MethodPost, u, request, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var bmr BetaMigrationRequest | ||
resp, err := bs.client.Do(ctx, req, &bmr) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return &bmr, 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,48 @@ | ||
// 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 TestBetaMigrationRequestsService_Integration_List(t *testing.T) { | ||
opt := BetaMigrationRequestsOption{Include: []string{"beta_migration_request.organizations"}} | ||
requests, res, err := integrationClient.BetaMigrationRequests.List(context.TODO(), integrationUserId, &opt) | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error occured: %s", err) | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
t.Fatalf("invalid http status: %s", res.Status) | ||
} | ||
|
||
if len(requests) == 0 { | ||
t.Fatal("requests cannot be empty") | ||
} | ||
} | ||
|
||
func TestBetaMigrationRequestsService_Integration_Create(t *testing.T) { | ||
r := BetaMigrationRequestBody{OrganizationIds: []uint{integrationOrgId}} | ||
request, res, err := integrationClient.BetaMigrationRequests.Create(context.TODO(), integrationUserId, &r) | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error occured: %s", err) | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
t.Fatalf("invalid http status: %s", res.Status) | ||
} | ||
|
||
if got, want := request.OwnerName, integrationGitHubOwner; got != want { | ||
t.Fatalf("invalid owner: got: %s, want: %s", got, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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 TestBetaMigrationRequestsService_List(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc(fmt.Sprintf("/user/%d/beta_migration_requests", testUserId), func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodGet) | ||
testFormValues(t, r, values{"include": "beta_migration_requests.organizations"}) | ||
fmt.Fprint(w, `{"beta_migration_requests":[{"id":1,"owner_id":2,"owner_name":"test","owner_type":"User"}]}`) | ||
}) | ||
|
||
opt := BetaMigrationRequestsOption{Include: []string{"beta_migration_requests.organizations"}} | ||
requests, _, err := client.BetaMigrationRequests.List(context.Background(), testUserId, &opt) | ||
|
||
if err != nil { | ||
t.Errorf("BetaMigrationRequest.List returned error: %v", err) | ||
} | ||
|
||
want := []*BetaMigrationRequest{{Id: 1, OwnerId: 2, OwnerName: "test", OwnerType: "User"}} | ||
if !reflect.DeepEqual(requests, want) { | ||
t.Errorf("BetaMigrationRequest.List returned %+v, want %+v", requests, want) | ||
} | ||
} | ||
|
||
func TestBetaMigrationRequestsService_Create(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc(fmt.Sprintf("/user/%d/beta_migration_request", testUserId), func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodPost) | ||
testBody(t, r, `{"beta_migration_request.organizations":[1]}`+"\n") | ||
fmt.Fprint(w, `{"id":1,"owner_id":2,"owner_name":"test","owner_type":"User"}`) | ||
}) | ||
|
||
rb := BetaMigrationRequestBody{OrganizationIds: []uint{1}} | ||
request, _, err := client.BetaMigrationRequests.Create(context.Background(), testUserId, &rb) | ||
|
||
if err != nil { | ||
t.Errorf("BetaMigrationRequests.Create returned error: %v", err) | ||
} | ||
|
||
want := &BetaMigrationRequest{Id: 1, OwnerId: 2, OwnerName: "test", OwnerType: "User"} | ||
if !reflect.DeepEqual(request, want) { | ||
t.Errorf("BetaMigrationRequests.Create returned %+v, want %+v", request, 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