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

Commit c14da45

Browse files
author
Noah Hanjun Lee
authored
Fix to sync 'created_at' of the deployment status (#174)
* Add SyncDeploymentStatus method to store * Fix to sync 'created_at', and 'updated_at'
1 parent 7d3887f commit c14da45

File tree

7 files changed

+89
-47
lines changed

7 files changed

+89
-47
lines changed

internal/interactor/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type (
5555
UpdateDeployment(ctx context.Context, d *ent.Deployment) (*ent.Deployment, error)
5656

5757
CreateDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
58+
SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
5859

5960
FindDeploymentStatisticsOfRepoByEnv(ctx context.Context, r *ent.Repo, env string) (*ent.DeploymentStatistics, error)
6061
CreateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error)

internal/interactor/mock/pkg.go

Lines changed: 15 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ func (s *Store) CreateDeploymentStatus(ctx context.Context, ds *ent.DeploymentSt
1515
SetDeploymentID(ds.DeploymentID).
1616
Save(ctx)
1717
}
18+
19+
func (s *Store) SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error) {
20+
return s.c.DeploymentStatus.
21+
Create().
22+
SetStatus(ds.Status).
23+
SetDescription(ds.Description).
24+
SetLogURL(ds.LogURL).
25+
SetDeploymentID(ds.DeploymentID).
26+
SetCreatedAt(ds.CreatedAt).
27+
SetUpdatedAt(ds.UpdatedAt).
28+
Save(ctx)
29+
}

internal/server/hooks/hook.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (h *Hooks) handleGithubHook(c *gin.Context) {
9898
}
9999

100100
ds.DeploymentID = d.ID
101-
if ds, err = h.i.CreateDeploymentStatus(ctx, ds); err != nil {
101+
if ds, err = h.i.SyncDeploymentStatus(ctx, ds); err != nil {
102102
h.log.Error("It has failed to create a new the deployment status.", zap.Error(err))
103103
gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to create a new the deployment status.")
104104
return
@@ -139,26 +139,26 @@ func isGithubDeploymentStatusEvent(c *gin.Context) bool {
139139
return c.GetHeader(headerGtihubEvent) == "deployment_status"
140140
}
141141

142-
func mapGithubDeploymentStatus(gds *github.DeploymentStatusEvent) *ent.DeploymentStatus {
142+
func mapGithubDeploymentStatus(e *github.DeploymentStatusEvent) *ent.DeploymentStatus {
143143
var (
144-
state = *gds.DeploymentStatus.State
145-
description = *gds.DeploymentStatus.Description
146-
logURL string
144+
logURL string
147145
)
148146

149147
// target_url is deprecated.
150-
if gds.DeploymentStatus.TargetURL != nil {
151-
logURL = *gds.DeploymentStatus.TargetURL
148+
if e.DeploymentStatus.TargetURL != nil {
149+
logURL = *e.DeploymentStatus.TargetURL
152150
}
153151

154-
if gds.DeploymentStatus.LogURL != nil {
155-
logURL = *gds.DeploymentStatus.LogURL
152+
if e.DeploymentStatus.LogURL != nil {
153+
logURL = *e.DeploymentStatus.LogURL
156154
}
157155

158156
ds := &ent.DeploymentStatus{
159-
Status: state,
160-
Description: description,
157+
Status: *e.DeploymentStatus.State,
158+
Description: *e.DeploymentStatus.Description,
161159
LogURL: logURL,
160+
CreatedAt: e.DeploymentStatus.CreatedAt.Time.UTC(),
161+
UpdatedAt: e.DeploymentStatus.UpdatedAt.Time.UTC(),
162162
}
163163

164164
return ds

internal/server/hooks/hook_test.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package hooks
22

33
import (
4+
"encoding/json"
5+
"io/ioutil"
46
"net/http"
57
"net/http/httptest"
68
"os"
79
"testing"
810

911
"github.com/gin-gonic/gin"
1012
"github.com/golang/mock/gomock"
13+
"github.com/google/go-github/v32/github"
1114

1215
"github.com/gitploy-io/gitploy/ent"
16+
"github.com/gitploy-io/gitploy/ent/deployment"
1317
"github.com/gitploy-io/gitploy/internal/server/hooks/mock"
1418
)
1519

@@ -18,45 +22,55 @@ func init() {
1822
}
1923

2024
func TestHook_HandleHook(t *testing.T) {
21-
ctrl := gomock.NewController(t)
22-
defer ctrl.Finish()
25+
t.Run("Listen the deployment event.", func(t *testing.T) {
26+
e := &github.DeploymentStatusEvent{}
27+
bytes, _ := ioutil.ReadFile("./testdata/github.hook.json")
28+
if err := json.Unmarshal(bytes, &e); err != nil {
29+
t.Fatalf("It has failed to unmarshal: %s", err)
30+
}
31+
32+
ctrl := gomock.NewController(t)
33+
defer ctrl.Finish()
2334

24-
m := mock.NewMockInteractor(ctrl)
35+
m := mock.NewMockInteractor(ctrl)
2536

26-
m.
27-
EXPECT().
28-
FindDeploymentByUID(gomock.Any(), gomock.Eq(int64(145988746))).
29-
Return(&ent.Deployment{
30-
ID: 1,
31-
UID: 145988746,
32-
}, nil)
37+
m.
38+
EXPECT().
39+
FindDeploymentByUID(gomock.Any(), gomock.Eq(int64(*e.Deployment.ID))).
40+
Return(&ent.Deployment{
41+
ID: 1,
42+
UID: *e.Deployment.ID,
43+
}, nil)
3344

34-
t.Run("", func(t *testing.T) {
3545
m.
3646
EXPECT().
37-
CreateDeploymentStatus(gomock.Any(), gomock.Eq(&ent.DeploymentStatus{
38-
Status: "success",
39-
Description: "Deployed successfully.",
47+
SyncDeploymentStatus(gomock.Any(), gomock.Eq(&ent.DeploymentStatus{
48+
Status: *e.DeploymentStatus.State,
49+
Description: *e.DeploymentStatus.Description,
50+
CreatedAt: e.DeploymentStatus.CreatedAt.Time.UTC(),
51+
UpdatedAt: e.DeploymentStatus.UpdatedAt.Time.UTC(),
4052
DeploymentID: 1,
4153
})).
4254
Return(&ent.DeploymentStatus{
4355
ID: 1,
44-
Status: "success",
45-
Description: "Deployed successfully.",
56+
Status: *e.DeploymentStatus.State,
57+
Description: *e.DeploymentStatus.Description,
58+
CreatedAt: e.DeploymentStatus.CreatedAt.Time.UTC(),
59+
UpdatedAt: e.DeploymentStatus.UpdatedAt.Time.UTC(),
4660
DeploymentID: 1,
4761
}, nil)
4862

4963
m.
5064
EXPECT().
5165
UpdateDeployment(gomock.Any(), gomock.Eq(&ent.Deployment{
5266
ID: 1,
53-
UID: 145988746,
54-
Status: "success",
67+
UID: *e.Deployment.ID,
68+
Status: deployment.StatusSuccess,
5569
})).
5670
Return(&ent.Deployment{
5771
ID: 1,
58-
UID: 145988746,
59-
Status: "success",
72+
UID: *e.Deployment.ID,
73+
Status: deployment.StatusSuccess,
6074
}, nil)
6175

6276
m.

internal/server/hooks/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
type (
1212
Interactor interface {
1313
FindDeploymentByUID(ctx context.Context, uid int64) (*ent.Deployment, error)
14-
CreateDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
14+
SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
1515
UpdateDeployment(ctx context.Context, d *ent.Deployment) (*ent.Deployment, error)
1616
ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment) (*ent.DeploymentStatistics, error)
1717
CreateEvent(ctx context.Context, e *ent.Event) (*ent.Event, error)

internal/server/hooks/mock/interactor.go

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

0 commit comments

Comments
 (0)