diff --git a/internal/interactor/interface.go b/internal/interactor/interface.go index 2077f45c..8ba57b4d 100644 --- a/internal/interactor/interface.go +++ b/internal/interactor/interface.go @@ -55,6 +55,7 @@ type ( UpdateDeployment(ctx context.Context, d *ent.Deployment) (*ent.Deployment, error) CreateDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error) + SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) FindDeploymentStatisticsOfRepoByEnv(ctx context.Context, r *ent.Repo, env string) (*ent.DeploymentStatistics, error) CreateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error) diff --git a/internal/interactor/mock/pkg.go b/internal/interactor/mock/pkg.go index 342cab43..2395c4c2 100644 --- a/internal/interactor/mock/pkg.go +++ b/internal/interactor/mock/pkg.go @@ -784,6 +784,21 @@ func (mr *MockStoreMockRecorder) ListUsers(ctx, login, page, perPage interface{} return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListUsers", reflect.TypeOf((*MockStore)(nil).ListUsers), ctx, login, page, perPage) } +// SyncDeploymentStatus mocks base method. +func (m *MockStore) SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SyncDeploymentStatus", ctx, ds) + ret0, _ := ret[0].(*ent.DeploymentStatus) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SyncDeploymentStatus indicates an expected call of SyncDeploymentStatus. +func (mr *MockStoreMockRecorder) SyncDeploymentStatus(ctx, ds interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SyncDeploymentStatus", reflect.TypeOf((*MockStore)(nil).SyncDeploymentStatus), ctx, ds) +} + // SearchApprovals mocks base method. func (m *MockStore) SearchApprovals(ctx context.Context, u *ent.User, s []approval.Status, from, to time.Time, page, perPage int) ([]*ent.Approval, error) { m.ctrl.T.Helper() diff --git a/internal/pkg/store/deploymentstatus.go b/internal/pkg/store/deploymentstatus.go index e5a7e387..ce986869 100644 --- a/internal/pkg/store/deploymentstatus.go +++ b/internal/pkg/store/deploymentstatus.go @@ -15,3 +15,15 @@ func (s *Store) CreateDeploymentStatus(ctx context.Context, ds *ent.DeploymentSt SetDeploymentID(ds.DeploymentID). Save(ctx) } + +func (s *Store) SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) { + return s.c.DeploymentStatus. + Create(). + SetStatus(ds.Status). + SetDescription(ds.Description). + SetLogURL(ds.LogURL). + SetDeploymentID(ds.DeploymentID). + SetCreatedAt(ds.CreatedAt). + SetUpdatedAt(ds.UpdatedAt). + Save(ctx) +} diff --git a/internal/server/hooks/hook.go b/internal/server/hooks/hook.go index 9d4ca315..c28ef2d0 100644 --- a/internal/server/hooks/hook.go +++ b/internal/server/hooks/hook.go @@ -98,7 +98,7 @@ func (h *Hooks) handleGithubHook(c *gin.Context) { } ds.DeploymentID = d.ID - if ds, err = h.i.CreateDeploymentStatus(ctx, ds); err != nil { + if ds, err = h.i.SyncDeploymentStatus(ctx, ds); err != nil { h.log.Error("It has failed to create a new the deployment status.", zap.Error(err)) gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to create a new the deployment status.") return @@ -139,26 +139,26 @@ func isGithubDeploymentStatusEvent(c *gin.Context) bool { return c.GetHeader(headerGtihubEvent) == "deployment_status" } -func mapGithubDeploymentStatus(gds *github.DeploymentStatusEvent) *ent.DeploymentStatus { +func mapGithubDeploymentStatus(e *github.DeploymentStatusEvent) *ent.DeploymentStatus { var ( - state = *gds.DeploymentStatus.State - description = *gds.DeploymentStatus.Description - logURL string + logURL string ) // target_url is deprecated. - if gds.DeploymentStatus.TargetURL != nil { - logURL = *gds.DeploymentStatus.TargetURL + if e.DeploymentStatus.TargetURL != nil { + logURL = *e.DeploymentStatus.TargetURL } - if gds.DeploymentStatus.LogURL != nil { - logURL = *gds.DeploymentStatus.LogURL + if e.DeploymentStatus.LogURL != nil { + logURL = *e.DeploymentStatus.LogURL } ds := &ent.DeploymentStatus{ - Status: state, - Description: description, + Status: *e.DeploymentStatus.State, + Description: *e.DeploymentStatus.Description, LogURL: logURL, + CreatedAt: e.DeploymentStatus.CreatedAt.Time.UTC(), + UpdatedAt: e.DeploymentStatus.UpdatedAt.Time.UTC(), } return ds diff --git a/internal/server/hooks/hook_test.go b/internal/server/hooks/hook_test.go index ad50755c..a85e9f30 100644 --- a/internal/server/hooks/hook_test.go +++ b/internal/server/hooks/hook_test.go @@ -1,6 +1,8 @@ package hooks import ( + "encoding/json" + "io/ioutil" "net/http" "net/http/httptest" "os" @@ -8,8 +10,10 @@ import ( "github.com/gin-gonic/gin" "github.com/golang/mock/gomock" + "github.com/google/go-github/v32/github" "github.com/gitploy-io/gitploy/ent" + "github.com/gitploy-io/gitploy/ent/deployment" "github.com/gitploy-io/gitploy/internal/server/hooks/mock" ) @@ -18,31 +22,41 @@ func init() { } func TestHook_HandleHook(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() + t.Run("Listen the deployment event.", func(t *testing.T) { + e := &github.DeploymentStatusEvent{} + bytes, _ := ioutil.ReadFile("./testdata/github.hook.json") + if err := json.Unmarshal(bytes, &e); err != nil { + t.Fatalf("It has failed to unmarshal: %s", err) + } + + ctrl := gomock.NewController(t) + defer ctrl.Finish() - m := mock.NewMockInteractor(ctrl) + m := mock.NewMockInteractor(ctrl) - m. - EXPECT(). - FindDeploymentByUID(gomock.Any(), gomock.Eq(int64(145988746))). - Return(&ent.Deployment{ - ID: 1, - UID: 145988746, - }, nil) + m. + EXPECT(). + FindDeploymentByUID(gomock.Any(), gomock.Eq(int64(*e.Deployment.ID))). + Return(&ent.Deployment{ + ID: 1, + UID: *e.Deployment.ID, + }, nil) - t.Run("", func(t *testing.T) { m. EXPECT(). - CreateDeploymentStatus(gomock.Any(), gomock.Eq(&ent.DeploymentStatus{ - Status: "success", - Description: "Deployed successfully.", + SyncDeploymentStatus(gomock.Any(), gomock.Eq(&ent.DeploymentStatus{ + Status: *e.DeploymentStatus.State, + Description: *e.DeploymentStatus.Description, + CreatedAt: e.DeploymentStatus.CreatedAt.Time.UTC(), + UpdatedAt: e.DeploymentStatus.UpdatedAt.Time.UTC(), DeploymentID: 1, })). Return(&ent.DeploymentStatus{ ID: 1, - Status: "success", - Description: "Deployed successfully.", + Status: *e.DeploymentStatus.State, + Description: *e.DeploymentStatus.Description, + CreatedAt: e.DeploymentStatus.CreatedAt.Time.UTC(), + UpdatedAt: e.DeploymentStatus.UpdatedAt.Time.UTC(), DeploymentID: 1, }, nil) @@ -50,13 +64,13 @@ func TestHook_HandleHook(t *testing.T) { EXPECT(). UpdateDeployment(gomock.Any(), gomock.Eq(&ent.Deployment{ ID: 1, - UID: 145988746, - Status: "success", + UID: *e.Deployment.ID, + Status: deployment.StatusSuccess, })). Return(&ent.Deployment{ ID: 1, - UID: 145988746, - Status: "success", + UID: *e.Deployment.ID, + Status: deployment.StatusSuccess, }, nil) m. diff --git a/internal/server/hooks/interface.go b/internal/server/hooks/interface.go index 5cf0d58b..a41f5427 100644 --- a/internal/server/hooks/interface.go +++ b/internal/server/hooks/interface.go @@ -11,7 +11,7 @@ import ( type ( Interactor interface { FindDeploymentByUID(ctx context.Context, uid int64) (*ent.Deployment, error) - CreateDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error) + SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) UpdateDeployment(ctx context.Context, d *ent.Deployment) (*ent.Deployment, error) ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment) (*ent.DeploymentStatistics, error) CreateEvent(ctx context.Context, e *ent.Event) (*ent.Event, error) diff --git a/internal/server/hooks/mock/interactor.go b/internal/server/hooks/mock/interactor.go index c4bc4a2b..8fcbf8c5 100644 --- a/internal/server/hooks/mock/interactor.go +++ b/internal/server/hooks/mock/interactor.go @@ -35,21 +35,6 @@ func (m *MockInteractor) EXPECT() *MockInteractorMockRecorder { return m.recorder } -// CreateDeploymentStatus mocks base method. -func (m *MockInteractor) CreateDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateDeploymentStatus", ctx, s) - ret0, _ := ret[0].(*ent.DeploymentStatus) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// CreateDeploymentStatus indicates an expected call of CreateDeploymentStatus. -func (mr *MockInteractorMockRecorder) CreateDeploymentStatus(ctx, s interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateDeploymentStatus", reflect.TypeOf((*MockInteractor)(nil).CreateDeploymentStatus), ctx, s) -} - // CreateEvent mocks base method. func (m *MockInteractor) CreateEvent(ctx context.Context, e *ent.Event) (*ent.Event, error) { m.ctrl.T.Helper() @@ -80,6 +65,21 @@ func (mr *MockInteractorMockRecorder) FindDeploymentByUID(ctx, uid interface{}) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindDeploymentByUID", reflect.TypeOf((*MockInteractor)(nil).FindDeploymentByUID), ctx, uid) } +// SyncDeploymentStatus mocks base method. +func (m *MockInteractor) SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SyncDeploymentStatus", ctx, ds) + ret0, _ := ret[0].(*ent.DeploymentStatus) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SyncDeploymentStatus indicates an expected call of SyncDeploymentStatus. +func (mr *MockInteractorMockRecorder) SyncDeploymentStatus(ctx, ds interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SyncDeploymentStatus", reflect.TypeOf((*MockInteractor)(nil).SyncDeploymentStatus), ctx, ds) +} + // ProduceDeploymentStatisticsOfRepo mocks base method. func (m *MockInteractor) ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment) (*ent.DeploymentStatistics, error) { m.ctrl.T.Helper()