Skip to content

Commit 3d5c085

Browse files
authored
Merge pull request #97 from planetscale/iheanyi/rework-branch-promotion-api
Rework branch promotion functions
2 parents 9ffa566 + ec83ef9 commit 3d5c085

File tree

2 files changed

+145
-22
lines changed

2 files changed

+145
-22
lines changed

Diff for: planetscale/branches.go

+45-10
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,30 @@ type RefreshSchemaRequest struct {
8989
Branch string `json:"-"`
9090
}
9191

92-
// PromoteBranchRequest encapsulates the request for promoting a branch to
92+
// PromoteRequest encapsulates the request for promoting a branch to
9393
// production.
94-
type PromoteBranchRequest struct {
94+
type PromoteRequest struct {
9595
Organization string `json:"-"`
9696
Database string `json:"-"`
97-
Branch string `json:"branch"`
97+
Branch string `json:"-"`
98+
}
99+
100+
type GetPromotionRequestRequest struct {
101+
Organization string `json:"-"`
102+
Database string `json:"-"`
103+
Branch string `json:"-"`
104+
}
105+
106+
// BranchPromotionRequest represents a promotion request for a branch.
107+
type BranchPromotionRequest struct {
108+
ID string `json:"id"`
109+
Branch string `json:"branch"`
110+
LintErrors string `json:"lint_errors"`
111+
State string `json:"state"`
112+
CreatedAt time.Time `json:"created_at"`
113+
UpdatedAt time.Time `json:"updated_at"`
114+
StartedAt *time.Time `json:"started_at"`
115+
FinishedAt *time.Time `json:"finished_at"`
98116
}
99117

100118
// DatabaseBranchesService is an interface for communicating with the PlanetScale
@@ -108,7 +126,8 @@ type DatabaseBranchesService interface {
108126
Diff(context.Context, *DiffBranchRequest) ([]*Diff, error)
109127
Schema(context.Context, *BranchSchemaRequest) ([]*Diff, error)
110128
RefreshSchema(context.Context, *RefreshSchemaRequest) error
111-
Promote(context.Context, *PromoteBranchRequest) (*DatabaseBranch, error)
129+
Promote(context.Context, *PromoteRequest) (*BranchPromotionRequest, error)
130+
GetPromotionRequest(context.Context, *GetPromotionRequestRequest) (*BranchPromotionRequest, error)
112131
}
113132

114133
type databaseBranchesService struct {
@@ -257,20 +276,36 @@ func (d *databaseBranchesService) RefreshSchema(ctx context.Context, refreshReq
257276

258277
// PromoteBranch promotes a database's branch from a development branch to a
259278
// production branch.
260-
func (d *databaseBranchesService) Promote(ctx context.Context, promoteReq *PromoteBranchRequest) (*DatabaseBranch, error) {
261-
path := fmt.Sprintf("%s/%s/promote-branch", databasesAPIPath(promoteReq.Organization), promoteReq.Database)
262-
req, err := d.client.newRequest(http.MethodPost, path, promoteReq)
279+
func (d *databaseBranchesService) Promote(ctx context.Context, promoteReq *PromoteRequest) (*BranchPromotionRequest, error) {
280+
path := fmt.Sprintf("%s/promotion-request", databaseBranchAPIPath(promoteReq.Organization, promoteReq.Database, promoteReq.Branch))
281+
req, err := d.client.newRequest(http.MethodPost, path, nil)
263282
if err != nil {
264283
return nil, errors.Wrap(err, "error creating request for branch promotion")
265284
}
266285

267-
branch := &DatabaseBranch{}
268-
err = d.client.do(ctx, req, &branch)
286+
promotionReq := &BranchPromotionRequest{}
287+
err = d.client.do(ctx, req, &promotionReq)
288+
if err != nil {
289+
return nil, err
290+
}
291+
292+
return promotionReq, nil
293+
}
294+
295+
func (d *databaseBranchesService) GetPromotionRequest(ctx context.Context, getReg *GetPromotionRequestRequest) (*BranchPromotionRequest, error) {
296+
path := fmt.Sprintf("%s/promotion-request", databaseBranchAPIPath(getReg.Organization, getReg.Database, getReg.Branch))
297+
req, err := d.client.newRequest(http.MethodGet, path, nil)
298+
if err != nil {
299+
return nil, errors.Wrap(err, "error creating request for getting branch promotion request")
300+
}
301+
302+
promotionReq := &BranchPromotionRequest{}
303+
err = d.client.do(ctx, req, &promotionReq)
269304
if err != nil {
270305
return nil, err
271306
}
272307

273-
return branch, nil
308+
return promotionReq, nil
274309
}
275310

276311
func databaseBranchesAPIPath(org, db string) string {

Diff for: planetscale/branches_test.go

+100-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package planetscale
22

33
import (
44
"context"
5-
"encoding/json"
65
"net/http"
76
"net/http/httptest"
87
"testing"
@@ -262,21 +261,105 @@ func TestBranches_RefreshSchema(t *testing.T) {
262261
}
263262

264263
func TestBranches_Promote(t *testing.T) {
264+
testTime := time.Date(2021, time.January, 14, 10, 19, 23, 000, time.UTC)
265265
c := qt.New(t)
266266

267267
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
268268
c.Assert(http.MethodPost, qt.Equals, r.Method)
269269
w.WriteHeader(200)
270270

271-
payload := make(map[string]interface{})
272-
decoder := json.NewDecoder(r.Body)
273-
err := decoder.Decode(&payload)
271+
out := `
272+
{
273+
"id": "test-promotion-branch",
274+
"type": "BranchPromotionRequest",
275+
"state": "promoted",
276+
"created_at": "2021-01-14T10:19:23.000Z",
277+
"updated_at": "2021-01-14T10:19:23.000Z",
278+
"started_at": "2021-01-14T10:19:23.000Z",
279+
"finished_at": "2021-01-14T10:19:23.000Z",
280+
"lint_errors": null,
281+
"branch": "main",
282+
"actor": {
283+
"id": "test-promotion-branch",
284+
"type": "User",
285+
"display_name": "Test User",
286+
"name": "Test User",
287+
"nickname": null,
288+
"email": "[email protected]",
289+
"avatar_url": "https://www.gravatar.com/avatar/4c97310eb2f0e43f486380f040398a02?d=https%3A%2F%2Fapp.planetscale.com%2Fgravatar-fallback.png&s=64",
290+
"created_at": "2021-08-25T21:22:20.150Z",
291+
"updated_at": "2021-08-26T20:08:14.725Z",
292+
"two_factor_auth_configured": false
293+
}
294+
295+
}`
296+
297+
_, err := w.Write([]byte(out))
274298
c.Assert(err, qt.IsNil)
299+
}))
275300

276-
c.Assert(payload["branch"], qt.Equals, "planetscale-go-test-db-branch")
301+
client, err := NewClient(WithBaseURL(ts.URL))
302+
c.Assert(err, qt.IsNil)
277303

278-
out := `{"id":"planetscale-go-test-db-branch","type":"database_branch","name":"planetscale-go-test-db-branch","created_at":"2021-01-14T10:19:23.000Z","updated_at":"2021-01-14T10:19:23.000Z"}`
279-
_, err = w.Write([]byte(out))
304+
ctx := context.Background()
305+
org := "my-org"
306+
name := "planetscale-go-test-db"
307+
308+
db, err := client.DatabaseBranches.Promote(ctx, &PromoteRequest{
309+
Organization: org,
310+
Database: name,
311+
Branch: "planetscale-go-test-db-branch",
312+
})
313+
314+
want := &BranchPromotionRequest{
315+
ID: "test-promotion-branch",
316+
State: "promoted",
317+
Branch: "main",
318+
LintErrors: "",
319+
CreatedAt: testTime,
320+
UpdatedAt: testTime,
321+
StartedAt: &testTime,
322+
FinishedAt: &testTime,
323+
}
324+
325+
c.Assert(err, qt.IsNil)
326+
c.Assert(db, qt.DeepEquals, want)
327+
}
328+
329+
func TestBranches_GetPromotionRequest(t *testing.T) {
330+
testTime := time.Date(2021, time.January, 14, 10, 19, 23, 000, time.UTC)
331+
c := qt.New(t)
332+
333+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
334+
c.Assert(http.MethodGet, qt.Equals, r.Method)
335+
w.WriteHeader(200)
336+
337+
out := `
338+
{
339+
"id": "test-promotion-branch",
340+
"type": "BranchPromotionRequest",
341+
"state": "promoted",
342+
"created_at": "2021-01-14T10:19:23.000Z",
343+
"updated_at": "2021-01-14T10:19:23.000Z",
344+
"started_at": "2021-01-14T10:19:23.000Z",
345+
"finished_at": "2021-01-14T10:19:23.000Z",
346+
"lint_errors": null,
347+
"branch": "main",
348+
"actor": {
349+
"id": "test-promotion-branch",
350+
"type": "User",
351+
"display_name": "Test User",
352+
"name": "Test User",
353+
"nickname": null,
354+
"email": "[email protected]",
355+
"avatar_url": "https://www.gravatar.com/avatar/4c97310eb2f0e43f486380f040398a02?d=https%3A%2F%2Fapp.planetscale.com%2Fgravatar-fallback.png&s=64",
356+
"created_at": "2021-08-25T21:22:20.150Z",
357+
"updated_at": "2021-08-26T20:08:14.725Z",
358+
"two_factor_auth_configured": false
359+
}
360+
}`
361+
362+
_, err := w.Write([]byte(out))
280363
c.Assert(err, qt.IsNil)
281364
}))
282365

@@ -287,16 +370,21 @@ func TestBranches_Promote(t *testing.T) {
287370
org := "my-org"
288371
name := "planetscale-go-test-db"
289372

290-
db, err := client.DatabaseBranches.Promote(ctx, &PromoteBranchRequest{
373+
db, err := client.DatabaseBranches.GetPromotionRequest(ctx, &GetPromotionRequestRequest{
291374
Organization: org,
292375
Database: name,
293376
Branch: "planetscale-go-test-db-branch",
294377
})
295378

296-
want := &DatabaseBranch{
297-
Name: testBranch,
298-
CreatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 000, time.UTC),
299-
UpdatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 000, time.UTC),
379+
want := &BranchPromotionRequest{
380+
ID: "test-promotion-branch",
381+
State: "promoted",
382+
Branch: "main",
383+
LintErrors: "",
384+
CreatedAt: testTime,
385+
UpdatedAt: testTime,
386+
StartedAt: &testTime,
387+
FinishedAt: &testTime,
300388
}
301389

302390
c.Assert(err, qt.IsNil)

0 commit comments

Comments
 (0)