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

Commit 7c1cdbd

Browse files
author
Noah Lee
authored
Provide the dynamic_payload field (#389)
* Add `DynamicPayload` field to env * Add `DynamicPayload` to the ent * Add `DynamicPayloadValidator` into the `interactor`
1 parent 1b00790 commit 7c1cdbd

File tree

20 files changed

+456
-65
lines changed

20 files changed

+456
-65
lines changed

Diff for: go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ require (
8080
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
8181
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
8282
golang.org/x/text v0.3.7 // indirect
83+
golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7 // indirect
84+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
8385
google.golang.org/appengine v1.6.7 // indirect
8486
google.golang.org/protobuf v1.26.0 // indirect
8587
gopkg.in/yaml.v2 v2.4.0 // indirect

Diff for: go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
881881
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
882882
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
883883
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
884+
golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7 h1:M1gcVrIb2lSn2FIL19DG0+/b8nNVKJ7W7b4WcAGZAYM=
884885
golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
885886
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
886887
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

Diff for: internal/interactor/deployment.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,37 @@ type (
7171
// Deploy posts a new deployment to SCM with the payload.
7272
// But if it requires a review, it saves the payload on the store and waits until reviewed.
7373
// It returns an error for a undeployable payload.
74-
func (i *DeploymentInteractor) Deploy(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, env *extent.Env) (*ent.Deployment, error) {
74+
func (i *DeploymentInteractor) Deploy(ctx context.Context, u *ent.User, r *ent.Repo, t *ent.Deployment, env *extent.Env) (*ent.Deployment, error) {
7575
number, err := i.store.GetNextDeploymentNumberOfRepo(ctx, r)
7676
if err != nil {
7777
return nil, e.NewError(e.ErrorCodeInternalError, err)
7878
}
7979

8080
i.log.Debug("Get the next number, and build the deployment.")
81-
d = &ent.Deployment{
81+
d := &ent.Deployment{
8282
Number: number,
83-
Type: d.Type,
84-
Env: d.Env,
85-
Ref: d.Ref,
83+
Type: t.Type,
84+
Env: t.Env,
85+
Ref: t.Ref,
8686
Status: deployment.DefaultStatus,
8787
ProductionEnvironment: env.IsProductionEnvironment(),
88-
IsRollback: d.IsRollback,
88+
IsRollback: t.IsRollback,
8989
UserID: u.ID,
9090
RepoID: r.ID,
9191
}
9292

93+
if env.IsDynamicPayloadEnabled() {
94+
i.log.Debug("Set the dynamic payload.")
95+
d.DynamicPayload = t.DynamicPayload
96+
}
97+
9398
i.log.Debug("Validate the deployment before a request.")
9499
v := NewDeploymentValidator([]Validator{
95100
&RefValidator{Env: env},
96101
&FrozenWindowValidator{Env: env},
97102
&LockValidator{Repo: r, Store: i.store},
98103
&SerializationValidator{Env: env, Store: i.store},
104+
&DynamicPayloadValidator{Env: env},
99105
})
100106
if err := v.Validate(d); err != nil {
101107
return nil, err

Diff for: internal/interactor/validator.go

+14
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,17 @@ func (v *SerializationValidator) Validate(d *ent.Deployment) error {
168168

169169
return err
170170
}
171+
172+
// DynamicPayloadValidator validate the payload with
173+
// the specifications defined in the configuration file.
174+
type DynamicPayloadValidator struct {
175+
Env *extent.Env
176+
}
177+
178+
func (v *DynamicPayloadValidator) Validate(d *ent.Deployment) error {
179+
if !v.Env.IsDynamicPayloadEnabled() {
180+
return nil
181+
}
182+
183+
return v.Env.ValidateDynamicPayload(d.DynamicPayload)
184+
}

Diff for: internal/pkg/github/deployment.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import (
1313
)
1414

1515
func (g *Github) CreateRemoteDeployment(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, env *extent.Env) (*extent.RemoteDeployment, error) {
16+
// If there is a dynamic payload, set it as the payload.
17+
payload := env.Payload
18+
if d.DynamicPayload != nil {
19+
payload = d.DynamicPayload
20+
}
21+
1622
gd, res, err := g.Client(ctx, u.Token).
1723
Repositories.
1824
CreateDeployment(ctx, r.Namespace, r.Name, &github.DeploymentRequest{
@@ -22,7 +28,7 @@ func (g *Github) CreateRemoteDeployment(ctx context.Context, u *ent.User, r *ent
2228
Description: env.Description,
2329
AutoMerge: env.AutoMerge,
2430
RequiredContexts: env.RequiredContexts,
25-
Payload: env.Payload,
31+
Payload: payload,
2632
ProductionEnvironment: env.ProductionEnvironment,
2733
})
2834
if res.StatusCode == http.StatusConflict {

Diff for: internal/pkg/store/deployment.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ func (s *Store) CreateDeployment(ctx context.Context, d *ent.Deployment) (*ent.D
265265
SetType(d.Type).
266266
SetRef(d.Ref).
267267
SetEnv(d.Env).
268+
SetDynamicPayload(d.DynamicPayload).
268269
SetUID(d.UID).
269270
SetSha(d.Sha).
270271
SetHTMLURL(d.HTMLURL).
@@ -277,10 +278,7 @@ func (s *Store) CreateDeployment(ctx context.Context, d *ent.Deployment) (*ent.D
277278
if ent.IsConstraintError(err) {
278279
return nil, e.NewError(e.ErrorCodeDeploymentConflict, err)
279280
} else if ent.IsValidationError(err) {
280-
return nil, e.NewErrorWithMessage(
281-
e.ErrorCodeEntityUnprocessable,
282-
fmt.Sprintf("Failed to create a deployment. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
283-
err)
281+
return nil, e.NewErrorWithMessage(e.ErrorCodeEntityUnprocessable, fmt.Sprintf("Failed to create a deployment. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name), err)
284282
} else if err != nil {
285283
return nil, e.NewError(e.ErrorCodeInternalError, err)
286284
}

Diff for: internal/server/api/v1/repos/deployment_create.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import (
1616

1717
type (
1818
DeploymentPostPayload struct {
19-
Type string `json:"type"`
20-
Ref string `json:"ref"`
21-
Env string `json:"env"`
19+
Type string `json:"type"`
20+
Ref string `json:"ref"`
21+
Env string `json:"env"`
22+
DynamicPayload map[string]interface{} `json:"dynamic_payload"`
2223
}
2324
)
2425

@@ -56,9 +57,10 @@ func (s *DeploymentAPI) Create(c *gin.Context) {
5657

5758
d, err := s.i.Deploy(ctx, u, re,
5859
&ent.Deployment{
59-
Type: deployment.Type(p.Type),
60-
Env: p.Env,
61-
Ref: p.Ref,
60+
Type: deployment.Type(p.Type),
61+
Env: p.Env,
62+
Ref: p.Ref,
63+
DynamicPayload: p.DynamicPayload,
6264
},
6365
env)
6466
if err != nil {

Diff for: model/ent/deployment.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: model/ent/deployment/deployment.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: model/ent/deployment/where.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: model/ent/deployment_create.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: model/ent/deployment_update.go

+50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)