Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit be0132d

Browse files
author
Noah Lee
authored
Enhance the notification message (#414)
* Add the `deployment_status_id` field to event schema * Move `CreateEvent` into `CreateDeploymentStatus` * Change the slack message format * Fix the stream API to support DeploymentStatus * Add the RepoID field to DeploymentStatus schema * Fix to add `RepoID` when creating a status * Subscribe the deployment status event in UI * Deprecate the deployment event
1 parent 132fb84 commit be0132d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2064
-1030
lines changed

Diff for: go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ require (
8080
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
8181
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
8282
golang.org/x/text v0.3.7 // indirect
83+
golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7 // indirect
84+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
8385
google.golang.org/appengine v1.6.7 // indirect
8486
google.golang.org/protobuf v1.26.0 // indirect
8587
gopkg.in/yaml.v2 v2.4.0 // indirect

Diff for: go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
882882
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
883883
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
884884
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
885+
golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7 h1:M1gcVrIb2lSn2FIL19DG0+/b8nNVKJ7W7b4WcAGZAYM=
885886
golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
886887
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
887888
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

Diff for: internal/interactor/deployment.go

+33-17
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,15 @@ func (i *DeploymentInteractor) Deploy(ctx context.Context, u *ent.User, r *ent.R
121121
i.log.Error("Failed to request a review.", zap.Errors("errs", errs))
122122
}
123123

124-
i.log.Debug("Dispatch a event.")
125-
if _, err := i.store.CreateEvent(ctx, &ent.Event{
126-
Kind: event.KindDeployment,
127-
Type: event.TypeCreated,
124+
if _, err := i.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
125+
Status: string(deployment.StatusWaiting),
126+
Description: "Gitploy waits the reviews.",
128127
DeploymentID: d.ID,
128+
RepoID: r.ID,
129129
}); err != nil {
130-
i.log.Error("Failed to create the event.", zap.Error(err))
130+
i.log.Error("Failed to create a deployment status.", zap.Error(err))
131131
}
132+
132133
return d, nil
133134
}
134135

@@ -149,20 +150,15 @@ func (i *DeploymentInteractor) Deploy(ctx context.Context, u *ent.User, r *ent.R
149150
return nil, fmt.Errorf("It failed to save a new deployment.: %w", err)
150151
}
151152

152-
i.store.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
153+
if _, err := i.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
153154
Status: string(deployment.StatusCreated),
154155
Description: "Gitploy starts to deploy.",
155156
DeploymentID: d.ID,
156-
})
157-
158-
i.log.Debug("Dispatch a event.")
159-
if _, err := i.store.CreateEvent(ctx, &ent.Event{
160-
Kind: event.KindDeployment,
161-
Type: event.TypeCreated,
162-
DeploymentID: d.ID,
157+
RepoID: r.ID,
163158
}); err != nil {
164-
i.log.Error("Failed to create the event.", zap.Error(err))
159+
i.log.Error("Failed to create a deployment status.", zap.Error(err))
165160
}
161+
166162
return d, nil
167163
}
168164

@@ -237,10 +233,11 @@ func (i *DeploymentInteractor) DeployToRemote(ctx context.Context, u *ent.User,
237233
return nil, err
238234
}
239235

240-
i.store.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
236+
i.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
241237
Status: string(deployment.StatusCreated),
242-
Description: "Gitploy creates a new deployment.",
238+
Description: "Gitploy start to deploy.",
243239
DeploymentID: d.ID,
240+
RepoID: r.ID,
244241
})
245242

246243
return d, nil
@@ -258,6 +255,24 @@ func (i *DeploymentInteractor) createRemoteDeployment(ctx context.Context, u *en
258255
return i.scm.CreateRemoteDeployment(ctx, u, r, d, env)
259256
}
260257

258+
// CreateDeploymentStatus create a DeploymentStatus and dispatch the event.
259+
func (i *DeploymentInteractor) CreateDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
260+
ds, err := i.store.CreateEntDeploymentStatus(ctx, ds)
261+
if err != nil {
262+
return nil, err
263+
}
264+
265+
if _, err := i.store.CreateEvent(ctx, &ent.Event{
266+
Kind: event.KindDeploymentStatus,
267+
Type: event.TypeCreated,
268+
DeploymentStatusID: ds.ID,
269+
}); err != nil {
270+
i.log.Error("Failed to dispatch the event.", zap.Error(err))
271+
}
272+
273+
return ds, nil
274+
}
275+
261276
func (i *DeploymentInteractor) runClosingInactiveDeployment(stop <-chan struct{}) {
262277
ctx := context.Background()
263278

@@ -289,13 +304,14 @@ L:
289304
Status: "canceled",
290305
Description: "Gitploy cancels the inactive deployment.",
291306
DeploymentID: d.ID,
307+
RepoID: d.RepoID,
292308
}
293309
if err := i.scm.CancelDeployment(ctx, d.Edges.User, d.Edges.Repo, d, s); err != nil {
294310
i.log.Error("It has failed to cancel the remote deployment.", zap.Error(err))
295311
continue
296312
}
297313

298-
if _, err := i.store.CreateDeploymentStatus(ctx, s); err != nil {
314+
if _, err := i.CreateDeploymentStatus(ctx, s); err != nil {
299315
i.log.Error("It has failed to create a new deployment status.", zap.Error(err))
300316
continue
301317
}

Diff for: internal/interactor/deployment_test.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ func TestInteractor_Deploy(t *testing.T) {
5252

5353
store.
5454
EXPECT().
55-
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
56-
Return(&ent.Event{}, nil)
55+
CreateEntDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{})).
56+
Return(&ent.DeploymentStatus{}, nil)
5757

5858
store.
5959
EXPECT().
60-
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
60+
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
61+
Return(&ent.Event{}, nil)
6162

6263
it := i.NewInteractor(&i.InteractorConfig{
6364
Store: store,
@@ -99,6 +100,11 @@ func TestInteractor_Deploy(t *testing.T) {
99100
return d, nil
100101
})
101102

103+
store.
104+
EXPECT().
105+
CreateEntDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{})).
106+
Return(&ent.DeploymentStatus{}, nil)
107+
102108
store.
103109
EXPECT().
104110
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
@@ -160,7 +166,13 @@ func TestInteractor_DeployToRemote(t *testing.T) {
160166

161167
store.
162168
EXPECT().
163-
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
169+
CreateEntDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{})).
170+
Return(&ent.DeploymentStatus{}, nil)
171+
172+
store.
173+
EXPECT().
174+
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
175+
Return(&ent.Event{}, nil)
164176

165177
it := i.NewInteractor(&i.InteractorConfig{
166178
Store: store,

Diff for: internal/interactor/interface.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ type (
4343
// PermStore defines operations for working with deployment_statuses.
4444
DeploymentStatusStore interface {
4545
ListDeploymentStatuses(ctx context.Context, d *ent.Deployment) ([]*ent.DeploymentStatus, error)
46-
CreateDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
47-
SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
46+
FindDeploymentStatusByID(ctx context.Context, id int) (*ent.DeploymentStatus, error)
47+
// CreateEntDeploymentStatus create a DeploymentStatus entity to the store.
48+
// Ent is appended to distinguish it from the interactor.
49+
CreateEntDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
4850
}
4951

5052
SCM interface {

Diff for: internal/interactor/mock/pkg.go

+41-41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: internal/pkg/store/deploymentstatus.go

+24-23
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,41 @@ func (s *Store) ListDeploymentStatuses(ctx context.Context, d *ent.Deployment) (
2121
return dss, nil
2222
}
2323

24-
func (s *Store) CreateDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
25-
ret, err := s.c.DeploymentStatus.
26-
Create().
27-
SetStatus(ds.Status).
28-
SetDescription(ds.Description).
29-
SetLogURL(ds.LogURL).
30-
SetDeploymentID(ds.DeploymentID).
31-
Save(ctx)
32-
if ent.IsConstraintError(err) {
33-
return nil, e.NewErrorWithMessage(
34-
e.ErrorCodeEntityUnprocessable,
35-
fmt.Sprintf("Failed to create a deployment status. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
36-
err)
37-
} else if err != nil {
38-
return nil, e.NewError(e.ErrorCodeInternalError, err)
24+
func (s *Store) FindDeploymentStatusByID(ctx context.Context, id int) (*ent.DeploymentStatus, error) {
25+
ds, err := s.c.DeploymentStatus.Query().
26+
Where(deploymentstatus.IDEQ(id)).
27+
WithDeployment().
28+
WithRepo().
29+
Only(ctx)
30+
if ent.IsNotFound(err) {
31+
return nil, e.NewError(e.ErrorCodeEntityNotFound, err)
3932
}
4033

41-
return ret, nil
34+
return ds, nil
4235
}
4336

44-
func (s *Store) SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
45-
ret, err := s.c.DeploymentStatus.
46-
Create().
37+
func (s *Store) CreateEntDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
38+
// Build the query creating a deployment status.
39+
qry := s.c.DeploymentStatus.Create().
4740
SetStatus(ds.Status).
4841
SetDescription(ds.Description).
4942
SetLogURL(ds.LogURL).
5043
SetDeploymentID(ds.DeploymentID).
51-
SetCreatedAt(ds.CreatedAt).
52-
SetUpdatedAt(ds.UpdatedAt).
53-
Save(ctx)
44+
SetRepoID(ds.RepoID)
45+
46+
if !ds.CreatedAt.IsZero() {
47+
qry.SetCreatedAt(ds.CreatedAt.UTC())
48+
}
49+
50+
if !ds.UpdatedAt.IsZero() {
51+
qry.SetUpdatedAt(ds.UpdatedAt.UTC())
52+
}
53+
54+
ret, err := qry.Save(ctx)
5455
if ent.IsConstraintError(err) {
5556
return nil, e.NewErrorWithMessage(
5657
e.ErrorCodeEntityUnprocessable,
57-
fmt.Sprintf("Failed to sync the deployment status. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
58+
fmt.Sprintf("Failed to create a deployment status. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
5859
err)
5960
} else if err != nil {
6061
return nil, e.NewError(e.ErrorCodeInternalError, err)

0 commit comments

Comments
 (0)