diff --git a/planetscale/deploy_requests.go b/planetscale/deploy_requests.go index 999f6fb..e959160 100644 --- a/planetscale/deploy_requests.go +++ b/planetscale/deploy_requests.go @@ -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) } @@ -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 { @@ -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) } diff --git a/planetscale/deploy_requests_test.go b/planetscale/deploy_requests_test.go index c6b7798..5106a63 100644 --- a/planetscale/deploy_requests_test.go +++ b/planetscale/deploy_requests_test.go @@ -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) +}