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

Commit

Permalink
Fix to sync 'created_at' of the deployment status (#174)
Browse files Browse the repository at this point in the history
* Add SyncDeploymentStatus method to store

* Fix to sync 'created_at', and 'updated_at'
  • Loading branch information
Noah Hanjun Lee authored Oct 18, 2021
1 parent 7d3887f commit c14da45
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 47 deletions.
1 change: 1 addition & 0 deletions internal/interactor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions internal/interactor/mock/pkg.go

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

12 changes: 12 additions & 0 deletions internal/pkg/store/deploymentstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
22 changes: 11 additions & 11 deletions internal/server/hooks/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
54 changes: 34 additions & 20 deletions internal/server/hooks/hook_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package hooks

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

"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"
)

Expand All @@ -18,45 +22,55 @@ 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)

m.
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.
Expand Down
2 changes: 1 addition & 1 deletion internal/server/hooks/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions internal/server/hooks/mock/interactor.go

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

0 comments on commit c14da45

Please sign in to comment.