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

Commit d05faa1

Browse files
author
noah
committed
Move CreateEvent into CreateDeploymentStatus
1 parent 35a1bd0 commit d05faa1

File tree

11 files changed

+68
-145
lines changed

11 files changed

+68
-145
lines changed

internal/interactor/deployment.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ 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,
129128
}); err != nil {
130-
i.log.Error("Failed to create the event.", zap.Error(err))
129+
i.log.Error("Failed to create a deployment status.", zap.Error(err))
131130
}
131+
132132
return d, nil
133133
}
134134

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

152-
i.store.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
152+
if _, err := i.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
153153
Status: string(deployment.StatusCreated),
154154
Description: "Gitploy starts to deploy.",
155155
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,
163156
}); err != nil {
164-
i.log.Error("Failed to create the event.", zap.Error(err))
157+
i.log.Error("Failed to create a deployment status.", zap.Error(err))
165158
}
159+
166160
return d, nil
167161
}
168162

@@ -237,9 +231,9 @@ func (i *DeploymentInteractor) DeployToRemote(ctx context.Context, u *ent.User,
237231
return nil, err
238232
}
239233

240-
i.store.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
234+
i.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
241235
Status: string(deployment.StatusCreated),
242-
Description: "Gitploy creates a new deployment.",
236+
Description: "Gitploy start to deploy.",
243237
DeploymentID: d.ID,
244238
})
245239

@@ -258,6 +252,24 @@ func (i *DeploymentInteractor) createRemoteDeployment(ctx context.Context, u *en
258252
return i.scm.CreateRemoteDeployment(ctx, u, r, d, env)
259253
}
260254

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

@@ -295,7 +307,7 @@ L:
295307
continue
296308
}
297309

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

internal/interactor/deployment_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestInteractor_Deploy(t *testing.T) {
5757

5858
store.
5959
EXPECT().
60-
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
60+
CreateEntDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
6161

6262
it := i.NewInteractor(&i.InteractorConfig{
6363
Store: store,
@@ -160,7 +160,7 @@ func TestInteractor_DeployToRemote(t *testing.T) {
160160

161161
store.
162162
EXPECT().
163-
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
163+
CreateEntDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
164164

165165
it := i.NewInteractor(&i.InteractorConfig{
166166
Store: store,

internal/interactor/interface.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ 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+
// CreateEntDeploymentStatus create a DeploymentStatus entity to the store.
47+
// Ent is appended to distinguish it from the interactor.
48+
CreateEntDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
4849
}
4950

5051
SCM interface {

internal/interactor/mock/pkg.go

Lines changed: 12 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/store/deploymentstatus.go

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,27 @@ 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().
24+
func (s *Store) CreateEntDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
25+
// Build the query creating a deployment status.
26+
qry := s.c.DeploymentStatus.Create().
2727
SetStatus(ds.Status).
2828
SetDescription(ds.Description).
2929
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)
30+
SetDeploymentID(ds.DeploymentID)
31+
32+
if !ds.CreatedAt.IsZero() {
33+
qry.SetCreatedAt(ds.CreatedAt.UTC())
3934
}
4035

41-
return ret, nil
42-
}
36+
if !ds.UpdatedAt.IsZero() {
37+
qry.SetUpdatedAt(ds.UpdatedAt.UTC())
38+
}
4339

44-
func (s *Store) SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
45-
ret, err := s.c.DeploymentStatus.
46-
Create().
47-
SetStatus(ds.Status).
48-
SetDescription(ds.Description).
49-
SetLogURL(ds.LogURL).
50-
SetDeploymentID(ds.DeploymentID).
51-
SetCreatedAt(ds.CreatedAt).
52-
SetUpdatedAt(ds.UpdatedAt).
53-
Save(ctx)
40+
ret, err := qry.Save(ctx)
5441
if ent.IsConstraintError(err) {
5542
return nil, e.NewErrorWithMessage(
5643
e.ErrorCodeEntityUnprocessable,
57-
fmt.Sprintf("Failed to sync the deployment status. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
44+
fmt.Sprintf("Failed to create a deployment status. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
5845
err)
5946
} else if err != nil {
6047
return nil, e.NewError(e.ErrorCodeInternalError, err)

internal/server/api/v1/repos/interface.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ type (
4848
UpdateLock(ctx context.Context, l *ent.Lock) (*ent.Lock, error)
4949
DeleteLock(ctx context.Context, l *ent.Lock) error
5050

51-
CreateEvent(ctx context.Context, e *ent.Event) (*ent.Event, error)
52-
5351
ListCommits(ctx context.Context, u *ent.User, r *ent.Repo, branch string, opt *i.ListOptions) ([]*extent.Commit, error)
5452
CompareCommits(ctx context.Context, u *ent.User, r *ent.Repo, base, head string, opt *i.ListOptions) ([]*extent.Commit, []*extent.CommitFile, error)
5553
GetCommit(ctx context.Context, u *ent.User, r *ent.Repo, sha string) (*extent.Commit, error)

internal/server/api/v1/repos/mock/interactor.go

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/server/hooks/hook.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
gb "github.com/gitploy-io/gitploy/internal/server/global"
1313
"github.com/gitploy-io/gitploy/model/ent"
1414
"github.com/gitploy-io/gitploy/model/ent/deployment"
15-
"github.com/gitploy-io/gitploy/model/ent/event"
1615
"github.com/gitploy-io/gitploy/model/extent"
1716
"github.com/gitploy-io/gitploy/pkg/e"
1817
)
@@ -118,7 +117,7 @@ func (h *Hooks) handleGithubDeploymentEvent(c *gin.Context) {
118117
}
119118

120119
ds.DeploymentID = d.ID
121-
if ds, err = h.i.SyncDeploymentStatus(ctx, ds); err != nil {
120+
if ds, err = h.i.CreateDeploymentStatus(ctx, ds); err != nil {
122121
h.log.Check(gb.GetZapLogLevel(err), "Failed to create a new the deployment status.").Write(zap.Error(err))
123122
gb.ResponseWithError(c, err)
124123
return
@@ -131,14 +130,6 @@ func (h *Hooks) handleGithubDeploymentEvent(c *gin.Context) {
131130
return
132131
}
133132

134-
if _, err := h.i.CreateEvent(ctx, &ent.Event{
135-
Kind: event.KindDeployment,
136-
Type: event.TypeUpdated,
137-
DeploymentID: d.ID,
138-
}); err != nil {
139-
h.log.Error("It has failed to create the event.", zap.Error(err))
140-
}
141-
142133
// Produce statistics when the deployment is success, and production environment.
143134
if d.Status == deployment.StatusSuccess &&
144135
d.ProductionEnvironment &&

internal/server/hooks/hook_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestHook_HandleHook(t *testing.T) {
4646

4747
m.
4848
EXPECT().
49-
SyncDeploymentStatus(gomock.Any(), gomock.Eq(&ent.DeploymentStatus{
49+
CreateDeploymentStatus(gomock.Any(), gomock.Eq(&ent.DeploymentStatus{
5050
Status: *e.DeploymentStatus.State,
5151
Description: *e.DeploymentStatus.Description,
5252
CreatedAt: e.DeploymentStatus.CreatedAt.Time.UTC(),
@@ -75,11 +75,6 @@ func TestHook_HandleHook(t *testing.T) {
7575
Status: deployment.StatusSuccess,
7676
}, nil)
7777

78-
m.
79-
EXPECT().
80-
CreateEvent(gomock.Any(), gomock.Any()).
81-
Return(&ent.Event{}, nil)
82-
8378
h := NewHooks(&ConfigHooks{}, m)
8479
r := gin.New()
8580
r.POST("/hooks", h.HandleHook)

internal/server/hooks/interface.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ type (
1313
Interactor interface {
1414
FindRepoByID(ctx context.Context, id int64) (*ent.Repo, error)
1515
FindDeploymentByUID(ctx context.Context, uid int64) (*ent.Deployment, error)
16-
SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
1716
Deploy(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, env *extent.Env) (*ent.Deployment, error)
1817
UpdateDeployment(ctx context.Context, d *ent.Deployment) (*ent.Deployment, error)
18+
CreateDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
1919
ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment) (*ent.DeploymentStatistics, error)
20-
CreateEvent(ctx context.Context, e *ent.Event) (*ent.Event, error)
2120
GetEvaluatedConfig(ctx context.Context, u *ent.User, r *ent.Repo, v *extent.EvalValues) (*extent.Config, error)
2221
}
2322
)

0 commit comments

Comments
 (0)