Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ require (
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7 h1:M1gcVrIb2lSn2FIL19DG0+/b8nNVKJ7W7b4WcAGZAYM=
golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
50 changes: 33 additions & 17 deletions internal/interactor/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,15 @@ func (i *DeploymentInteractor) Deploy(ctx context.Context, u *ent.User, r *ent.R
i.log.Error("Failed to request a review.", zap.Errors("errs", errs))
}

i.log.Debug("Dispatch a event.")
if _, err := i.store.CreateEvent(ctx, &ent.Event{
Kind: event.KindDeployment,
Type: event.TypeCreated,
if _, err := i.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
Status: string(deployment.StatusWaiting),
Description: "Gitploy waits the reviews.",
DeploymentID: d.ID,
RepoID: r.ID,
}); err != nil {
i.log.Error("Failed to create the event.", zap.Error(err))
i.log.Error("Failed to create a deployment status.", zap.Error(err))
}

return d, nil
}

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

i.store.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
if _, err := i.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
Status: string(deployment.StatusCreated),
Description: "Gitploy starts to deploy.",
DeploymentID: d.ID,
})

i.log.Debug("Dispatch a event.")
if _, err := i.store.CreateEvent(ctx, &ent.Event{
Kind: event.KindDeployment,
Type: event.TypeCreated,
DeploymentID: d.ID,
RepoID: r.ID,
}); err != nil {
i.log.Error("Failed to create the event.", zap.Error(err))
i.log.Error("Failed to create a deployment status.", zap.Error(err))
}

return d, nil
}

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

i.store.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
i.CreateDeploymentStatus(ctx, &ent.DeploymentStatus{
Status: string(deployment.StatusCreated),
Description: "Gitploy creates a new deployment.",
Description: "Gitploy start to deploy.",
DeploymentID: d.ID,
RepoID: r.ID,
})

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

// CreateDeploymentStatus create a DeploymentStatus and dispatch the event.
func (i *DeploymentInteractor) CreateDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
ds, err := i.store.CreateEntDeploymentStatus(ctx, ds)
if err != nil {
return nil, err
}

if _, err := i.store.CreateEvent(ctx, &ent.Event{
Kind: event.KindDeploymentStatus,
Type: event.TypeCreated,
DeploymentStatusID: ds.ID,
}); err != nil {
i.log.Error("Failed to dispatch the event.", zap.Error(err))
}

return ds, nil
}

Comment on lines +259 to +275
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add CreateDeploymentStatus method and create a event internally.

func (i *DeploymentInteractor) runClosingInactiveDeployment(stop <-chan struct{}) {
ctx := context.Background()

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

if _, err := i.store.CreateDeploymentStatus(ctx, s); err != nil {
if _, err := i.CreateDeploymentStatus(ctx, s); err != nil {
i.log.Error("It has failed to create a new deployment status.", zap.Error(err))
continue
}
Expand Down
20 changes: 16 additions & 4 deletions internal/interactor/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ func TestInteractor_Deploy(t *testing.T) {

store.
EXPECT().
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
Return(&ent.Event{}, nil)
CreateEntDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{})).
Return(&ent.DeploymentStatus{}, nil)

store.
EXPECT().
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
Return(&ent.Event{}, nil)

it := i.NewInteractor(&i.InteractorConfig{
Store: store,
Expand Down Expand Up @@ -99,6 +100,11 @@ func TestInteractor_Deploy(t *testing.T) {
return d, nil
})

store.
EXPECT().
CreateEntDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{})).
Return(&ent.DeploymentStatus{}, nil)

store.
EXPECT().
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
Expand Down Expand Up @@ -160,7 +166,13 @@ func TestInteractor_DeployToRemote(t *testing.T) {

store.
EXPECT().
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
CreateEntDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{})).
Return(&ent.DeploymentStatus{}, nil)

store.
EXPECT().
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
Return(&ent.Event{}, nil)

it := i.NewInteractor(&i.InteractorConfig{
Store: store,
Expand Down
6 changes: 4 additions & 2 deletions internal/interactor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ type (
// PermStore defines operations for working with deployment_statuses.
DeploymentStatusStore interface {
ListDeploymentStatuses(ctx context.Context, d *ent.Deployment) ([]*ent.DeploymentStatus, error)
CreateDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
FindDeploymentStatusByID(ctx context.Context, id int) (*ent.DeploymentStatus, error)
// CreateEntDeploymentStatus create a DeploymentStatus entity to the store.
// Ent is appended to distinguish it from the interactor.
CreateEntDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
}

SCM interface {
Expand Down
82 changes: 41 additions & 41 deletions internal/interactor/mock/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 24 additions & 23 deletions internal/pkg/store/deploymentstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,41 @@ func (s *Store) ListDeploymentStatuses(ctx context.Context, d *ent.Deployment) (
return dss, nil
}

func (s *Store) CreateDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
ret, err := s.c.DeploymentStatus.
Create().
SetStatus(ds.Status).
SetDescription(ds.Description).
SetLogURL(ds.LogURL).
SetDeploymentID(ds.DeploymentID).
Save(ctx)
if ent.IsConstraintError(err) {
return nil, e.NewErrorWithMessage(
e.ErrorCodeEntityUnprocessable,
fmt.Sprintf("Failed to create a deployment status. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
err)
} else if err != nil {
return nil, e.NewError(e.ErrorCodeInternalError, err)
func (s *Store) FindDeploymentStatusByID(ctx context.Context, id int) (*ent.DeploymentStatus, error) {
ds, err := s.c.DeploymentStatus.Query().
Where(deploymentstatus.IDEQ(id)).
WithDeployment().
WithRepo().
Only(ctx)
if ent.IsNotFound(err) {
return nil, e.NewError(e.ErrorCodeEntityNotFound, err)
}

return ret, nil
return ds, nil
}

func (s *Store) SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
ret, err := s.c.DeploymentStatus.
Create().
func (s *Store) CreateEntDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
// Build the query creating a deployment status.
qry := s.c.DeploymentStatus.Create().
SetStatus(ds.Status).
SetDescription(ds.Description).
SetLogURL(ds.LogURL).
SetDeploymentID(ds.DeploymentID).
SetCreatedAt(ds.CreatedAt).
SetUpdatedAt(ds.UpdatedAt).
Save(ctx)
SetRepoID(ds.RepoID)

if !ds.CreatedAt.IsZero() {
qry.SetCreatedAt(ds.CreatedAt.UTC())
}

if !ds.UpdatedAt.IsZero() {
qry.SetUpdatedAt(ds.UpdatedAt.UTC())
}

ret, err := qry.Save(ctx)
if ent.IsConstraintError(err) {
return nil, e.NewErrorWithMessage(
e.ErrorCodeEntityUnprocessable,
fmt.Sprintf("Failed to sync the deployment status. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
fmt.Sprintf("Failed to create a deployment status. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
err)
} else if err != nil {
return nil, e.NewError(e.ErrorCodeInternalError, err)
Expand Down
Loading