Skip to content

Commit

Permalink
Merge pull request #223 from faun/faun/deploy-operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mscoutermarsh authored Oct 16, 2024
2 parents 9ff8cd1 + 9716d32 commit d247c37
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
27 changes: 27 additions & 0 deletions planetscale/deploy_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type DeployRequestsService interface {
Diff(ctx context.Context, diffReq *DiffRequest) ([]*Diff, error)
Get(context.Context, *GetDeployRequestRequest) (*DeployRequest, error)
List(context.Context, *ListDeployRequestsRequest) ([]*DeployRequest, error)
GetDeployOperations(context.Context, *GetDeployOperationsRequest) ([]*DeployOperation, error)
SkipRevertDeploy(context.Context, *SkipRevertDeployRequestRequest) (*DeployRequest, error)
RevertDeploy(context.Context, *RevertDeployRequestRequest) (*DeployRequest, error)
}
Expand Down Expand Up @@ -71,6 +72,14 @@ type ListDeployRequestsRequest struct {
IntoBranch string
}

// GetDeployOperationsRequest encapsulates the request for getting a deploy
// operation for a deploy request.
type GetDeployOperationsRequest struct {
Organization string `json:"-"`
Database string `json:"-"`
Number uint64 `json:"-"`
}

// DeployOperation encapsulates a deploy operation within a deployment from the
// PlanetScale API.
type DeployOperation struct {
Expand Down Expand Up @@ -505,6 +514,24 @@ func (d *deployRequestsService) CreateReview(ctx context.Context, reviewReq *Rev
return drr, nil
}

type deployOperationResponse struct {
Ops []*DeployOperation `json:"data"`
}

func (d *deployRequestsService) GetDeployOperations(ctx context.Context, getReq *GetDeployOperationsRequest) ([]*DeployOperation, error) {
req, err := d.client.newRequest(http.MethodGet, deployRequestActionAPIPath(getReq.Organization, getReq.Database, getReq.Number, "operations"), nil)
if err != nil {
return nil, errors.Wrap(err, "error creating http request")
}

resp := &deployOperationResponse{}
if err := d.client.do(ctx, req, &resp); err != nil {
return nil, err
}

return resp.Ops, nil
}

func deployRequestsAPIPath(org, db string) string {
return fmt.Sprintf("%s/%s/deploy-requests", databasesAPIPath(org), db)
}
Expand Down
53 changes: 53 additions & 0 deletions planetscale/deploy_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,56 @@ func TestDeployRequests_RevertDeploy(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(dr, qt.DeepEquals, want)
}

func TestDeployRequests_DeployOperations(t *testing.T) {
c := qt.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
out := `{
"type":"list",
"current_page":1,
"data":[
{
"id":"test-operation-id",
"type":"DeployOperation",
"state":"pending",
"keyspace_name":"treats",
"table_name":"ice_creams",
"operation_name":"CREATE",
"created_at":"2021-01-14T10:19:23.000Z",
"updated_at":"2021-01-14T10:19:23.000Z"
}
]
}`
_, err := w.Write([]byte(out))
c.Assert(err, qt.IsNil)
}))

client, err := NewClient(WithBaseURL(ts.URL))
c.Assert(err, qt.IsNil)

ctx := context.Background()

do, err := client.DeployRequests.GetDeployOperations(ctx, &GetDeployOperationsRequest{
Organization: "test-organization",
Database: "test-database",
Number: 1337,
})

testTime := time.Date(2021, time.January, 14, 10, 19, 23, 0, time.UTC)

want := []*DeployOperation{{
ID: "test-operation-id",
State: "pending",
Table: "ice_creams",
Keyspace: "treats",
Operation: "CREATE",
ETASeconds: 0,
ProgressPercentage: 0,
CreatedAt: testTime,
UpdatedAt: testTime,
}}
c.Assert(err, qt.IsNil)
c.Assert(do, qt.DeepEquals, want)
}

0 comments on commit d247c37

Please sign in to comment.