From a1b1d4a6f80452c347d7955888801cd1ed8dcd93 Mon Sep 17 00:00:00 2001 From: barnettZQG Date: Thu, 30 Dec 2021 14:37:40 +0800 Subject: [PATCH] Fix: trigger webbook bug (#3024) Signed-off-by: barnettZQG --- pkg/apiserver/model/application.go | 2 +- pkg/apiserver/rest/apis/v1/types.go | 11 +- pkg/apiserver/rest/usecase/application.go | 67 ++-- .../rest/usecase/application_test.go | 10 +- .../rest/usecase/testdata/ui-schema.yaml | 298 +++++++++--------- pkg/apiserver/rest/usecase/webhook.go | 16 +- pkg/apiserver/rest/usecase/webhook_test.go | 19 +- .../rest/utils/bcode/001_application.go | 3 + pkg/apiserver/rest/webservice/application.go | 10 +- pkg/apiserver/rest/webservice/webhook.go | 7 +- 10 files changed, 240 insertions(+), 203 deletions(-) diff --git a/pkg/apiserver/model/application.go b/pkg/apiserver/model/application.go index 11df2dae7..be91993c2 100644 --- a/pkg/apiserver/model/application.go +++ b/pkg/apiserver/model/application.go @@ -212,7 +212,7 @@ type ApplicationRevision struct { // EnvName is the env name of this application revision EnvName string `json:"envName"` // CodeInfo is the code info of this application revision - CodeInfo *CodeInfo `json:"gitInfo,omitempty"` + CodeInfo *CodeInfo `json:"codeInfo,omitempty"` } // CodeInfo is the code info for webhook request diff --git a/pkg/apiserver/rest/apis/v1/types.go b/pkg/apiserver/rest/apis/v1/types.go index 7fb5705a1..6e21921d4 100644 --- a/pkg/apiserver/rest/apis/v1/types.go +++ b/pkg/apiserver/rest/apis/v1/types.go @@ -362,6 +362,11 @@ type ApplicationTriggerBase struct { UpdateTime time.Time `json:"updateTime"` } +// ListApplicationTriggerResponse list application triggers response body +type ListApplicationTriggerResponse struct { + Triggers []*ApplicationTriggerBase `json:"triggers"` +} + // HandleApplicationWebhookRequest handles application webhook request type HandleApplicationWebhookRequest struct { Upgrade map[string]*model.JSONStruct `json:"upgrade,omitempty"` @@ -859,12 +864,14 @@ type ApplicationRevisionBase struct { CreateTime time.Time `json:"createTime"` Version string `json:"version"` Status string `json:"status"` - Reason string `json:"reason"` - DeployUser string `json:"deployUser"` + Reason string `json:"reason,omitempty"` + DeployUser string `json:"deployUser,omitempty"` Note string `json:"note"` EnvName string `json:"envName"` // SourceType the event trigger source, Web or API or Webhook TriggerType string `json:"triggerType"` + // CodeInfo is the code info of this application revision + CodeInfo *model.CodeInfo `json:"codeInfo,omitempty"` } // ListRevisionsResponse list application revisions diff --git a/pkg/apiserver/rest/usecase/application.go b/pkg/apiserver/rest/usecase/application.go index a54e0ad44..19976bb6f 100644 --- a/pkg/apiserver/rest/usecase/application.go +++ b/pkg/apiserver/rest/usecase/application.go @@ -91,8 +91,8 @@ type ApplicationUsecase interface { DetailRevision(ctx context.Context, appName, revisionName string) (*apisv1.DetailRevisionResponse, error) Statistics(ctx context.Context, app *model.Application) (*apisv1.ApplicationStatisticsResponse, error) ListRecords(ctx context.Context, appName string) (*apisv1.ListWorkflowRecordsResponse, error) - CreateApplicationTrigger(ctx context.Context, appName string, req apisv1.CreateApplicationTriggerRequest) (*apisv1.ApplicationTriggerBase, error) - ListApplicationTriggers(ctx context.Context, appName string) ([]*apisv1.ApplicationTriggerBase, error) + CreateApplicationTrigger(ctx context.Context, app *model.Application, req apisv1.CreateApplicationTriggerRequest) (*apisv1.ApplicationTriggerBase, error) + ListApplicationTriggers(ctx context.Context, app *model.Application) ([]*apisv1.ApplicationTriggerBase, error) } type applicationUsecaseImpl struct { @@ -346,10 +346,11 @@ func (c *applicationUsecaseImpl) CreateApplication(ctx context.Context, req apis if err != nil { return nil, err } - if _, err := c.CreateApplicationTrigger(ctx, application.PrimaryKey(), apisv1.CreateApplicationTriggerRequest{ - Name: fmt.Sprintf("%s-%s", application.Name, "default"), - PayloadType: model.PayloadTypeCustom, - Type: apisv1.TriggerTypeWebhook, + if _, err := c.CreateApplicationTrigger(ctx, &application, apisv1.CreateApplicationTriggerRequest{ + Name: fmt.Sprintf("%s-%s", application.Name, "default"), + PayloadType: model.PayloadTypeCustom, + Type: apisv1.TriggerTypeWebhook, + WorkflowName: convertWorkflowName(req.EnvBinding[0].Name), }); err != nil { return nil, err } @@ -367,9 +368,9 @@ func (c *applicationUsecaseImpl) CreateApplication(ctx context.Context, req apis } // CreateApplicationTrigger create application trigger -func (c *applicationUsecaseImpl) CreateApplicationTrigger(ctx context.Context, appName string, req apisv1.CreateApplicationTriggerRequest) (*apisv1.ApplicationTriggerBase, error) { +func (c *applicationUsecaseImpl) CreateApplicationTrigger(ctx context.Context, app *model.Application, req apisv1.CreateApplicationTriggerRequest) (*apisv1.ApplicationTriggerBase, error) { trigger := &model.ApplicationTrigger{ - AppPrimaryKey: appName, + AppPrimaryKey: app.Name, WorkflowName: req.WorkflowName, Name: req.Name, Alias: req.Alias, @@ -395,9 +396,9 @@ func (c *applicationUsecaseImpl) CreateApplicationTrigger(ctx context.Context, a } // ListApplicationTrigger list application triggers -func (c *applicationUsecaseImpl) ListApplicationTriggers(ctx context.Context, appName string) ([]*apisv1.ApplicationTriggerBase, error) { +func (c *applicationUsecaseImpl) ListApplicationTriggers(ctx context.Context, app *model.Application) ([]*apisv1.ApplicationTriggerBase, error) { trigger := &model.ApplicationTrigger{ - AppPrimaryKey: appName, + AppPrimaryKey: app.Name, } triggers, err := c.ds.List(ctx, trigger, &datastore.ListOptions{ SortBy: []datastore.SortOption{{Key: "createTime", Order: datastore.SortOrderDescending}}}, @@ -417,6 +418,7 @@ func (c *applicationUsecaseImpl) ListApplicationTriggers(ctx context.Context, ap Alias: trigger.Alias, Description: trigger.Description, Type: trigger.Type, + PayloadType: trigger.PayloadType, Token: trigger.Token, UpdateTime: trigger.UpdateTime, CreateTime: trigger.CreateTime, @@ -723,14 +725,7 @@ func (c *applicationUsecaseImpl) Deploy(ctx context.Context, app *model.Applicat } return &apisv1.ApplicationDeployResponse{ - ApplicationRevisionBase: apisv1.ApplicationRevisionBase{ - Version: appRevision.Version, - Status: appRevision.Status, - Reason: appRevision.Reason, - DeployUser: appRevision.DeployUser, - Note: appRevision.Note, - TriggerType: appRevision.TriggerType, - }, + ApplicationRevisionBase: c.converRevisionModelToBase(appRevision), }, nil } @@ -897,6 +892,20 @@ func (c *applicationUsecaseImpl) converAppModelToBase(ctx context.Context, app * return appBase } +func (c *applicationUsecaseImpl) converRevisionModelToBase(revision *model.ApplicationRevision) apisv1.ApplicationRevisionBase { + return apisv1.ApplicationRevisionBase{ + Version: revision.Version, + Status: revision.Status, + Reason: revision.Reason, + DeployUser: revision.DeployUser, + Note: revision.Note, + TriggerType: revision.TriggerType, + CreateTime: revision.CreateTime, + EnvName: revision.EnvName, + CodeInfo: revision.CodeInfo, + } +} + // DeleteApplication delete application func (c *applicationUsecaseImpl) DeleteApplication(ctx context.Context, app *model.Application) error { // TODO: check app can be deleted @@ -926,6 +935,11 @@ func (c *applicationUsecaseImpl) DeleteApplication(ctx context.Context, app *mod return err } + triggers, err := c.ListApplicationTriggers(ctx, app) + if err != nil { + return err + } + // delete workflow if err := c.workflowUsecase.DeleteWorkflowByApp(ctx, app); err != nil && !errors.Is(err, bcode.ErrWorkflowNotExist) { log.Logger.Errorf("delete workflow %s failure %s", app.Name, err.Error()) @@ -952,6 +966,12 @@ func (c *applicationUsecaseImpl) DeleteApplication(ctx context.Context, app *mod } } + for _, trigger := range triggers { + if err := c.ds.Delete(ctx, &model.ApplicationTrigger{AppPrimaryKey: app.PrimaryKey(), Name: trigger.Name, Token: trigger.Token}); err != nil { + log.Logger.Errorf("delete trigger %s in app %s failure %s", trigger.Name, app.Name, err.Error()) + } + } + if err := c.envBindingUsecase.BatchDeleteEnvBinding(ctx, app); err != nil { log.Logger.Errorf("delete envbindings in app %s failure %s", app.Name, err.Error()) } @@ -1259,16 +1279,7 @@ func (c *applicationUsecaseImpl) ListRevisions(ctx context.Context, appName, env for _, raw := range revisions { r, ok := raw.(*model.ApplicationRevision) if ok { - resp.Revisions = append(resp.Revisions, apisv1.ApplicationRevisionBase{ - CreateTime: r.CreateTime, - Version: r.Version, - Status: r.Status, - Reason: r.Reason, - DeployUser: r.DeployUser, - Note: r.Note, - EnvName: r.EnvName, - TriggerType: r.TriggerType, - }) + resp.Revisions = append(resp.Revisions, c.converRevisionModelToBase(r)) } } count, err := c.ds.Count(ctx, &revision, nil) diff --git a/pkg/apiserver/rest/usecase/application_test.go b/pkg/apiserver/rest/usecase/application_test.go index ef9f3ed90..f3f185c2d 100644 --- a/pkg/apiserver/rest/usecase/application_test.go +++ b/pkg/apiserver/rest/usecase/application_test.go @@ -118,7 +118,7 @@ var _ = Describe("Test application usecase function", func() { Expect(err).Should(BeNil()) Expect(cmp.Diff(base.Description, req.Description)).Should(BeEmpty()) - triggers, err := appUsecase.ListApplicationTriggers(context.TODO(), testApp) + triggers, err := appUsecase.ListApplicationTriggers(context.TODO(), &model.Application{Name: testApp}) Expect(err).Should(BeNil()) Expect(len(triggers)).Should(Equal(1)) }) @@ -148,14 +148,18 @@ var _ = Describe("Test application usecase function", func() { }) It("Test CreateTrigger function", func() { - _, err := appUsecase.CreateApplicationTrigger(context.TODO(), testApp, v1.CreateApplicationTriggerRequest{ + appModel, err := appUsecase.GetApplication(context.TODO(), testApp) + Expect(err).Should(BeNil()) + _, err = appUsecase.CreateApplicationTrigger(context.TODO(), appModel, v1.CreateApplicationTriggerRequest{ Name: "trigger-name", }) Expect(err).Should(BeNil()) }) It("Test ListTriggers function", func() { - triggers, err := appUsecase.ListApplicationTriggers(context.TODO(), testApp) + appModel, err := appUsecase.GetApplication(context.TODO(), testApp) + Expect(err).Should(BeNil()) + triggers, err := appUsecase.ListApplicationTriggers(context.TODO(), appModel) Expect(err).Should(BeNil()) Expect(len(triggers)).Should(Equal(2)) }) diff --git a/pkg/apiserver/rest/usecase/testdata/ui-schema.yaml b/pkg/apiserver/rest/usecase/testdata/ui-schema.yaml index a3173488b..e861fa779 100755 --- a/pkg/apiserver/rest/usecase/testdata/ui-schema.yaml +++ b/pkg/apiserver/rest/usecase/testdata/ui-schema.yaml @@ -51,6 +51,19 @@ - valueFrom label: Add By Secret subParameters: + - description: Environment variable name + jsonKey: name + label: Name + sort: 100 + uiType: Input + validate: + required: true + - description: The value of the environment variable + jsonKey: value + label: Value + sort: 100 + uiType: Input + validate: {} - description: Specifies a source the value of this var should come from jsonKey: valueFrom label: Secret Selector @@ -81,19 +94,6 @@ required: true uiType: InnerGroup validate: {} - - description: Environment variable name - jsonKey: name - label: Name - sort: 100 - uiType: Input - validate: - required: true - - description: The value of the environment variable - jsonKey: value - label: Value - sort: 100 - uiType: Input - validate: {} uiType: Structs validate: {} - description: Instructions for assessing whether the container is in a suitable state @@ -102,88 +102,6 @@ label: ReadinessProbe sort: 13 subParameters: - - description: Number of consecutive failures required to determine the container - is not alive (liveness probe) or not ready (readiness probe). - jsonKey: failureThreshold - label: FailureThreshold - sort: 100 - uiType: Number - validate: - defaultValue: 3 - required: true - - description: Instructions for assessing container health by executing an HTTP - GET request. Either this attribute or the exec attribute or the tcpSocket attribute - MUST be specified. This attribute is mutually exclusive with both the exec attribute - and the tcpSocket attribute. - jsonKey: httpGet - label: HttpGet - sort: 100 - subParameters: - - description: The endpoint, relative to the port, to which the HTTP GET request - should be directed. - jsonKey: path - label: Path - sort: 100 - uiType: Input - validate: - required: true - - description: The TCP socket within the container to which the HTTP GET request - should be directed. - jsonKey: port - label: Port - sort: 100 - uiType: Number - validate: - required: true - - description: "" - jsonKey: httpHeaders - label: HttpHeaders - sort: 100 - subParameters: - - description: "" - jsonKey: name - label: Name - sort: 100 - uiType: Input - validate: - required: true - - description: "" - jsonKey: value - label: Value - sort: 100 - uiType: Input - validate: - required: true - uiType: Structs - validate: {} - uiType: Group - validate: {} - - description: Number of seconds after the container is started before the first - probe is initiated. - jsonKey: initialDelaySeconds - label: InitialDelaySeconds - sort: 100 - uiType: Number - validate: - defaultValue: 0 - required: true - - description: How often, in seconds, to execute the probe. - jsonKey: periodSeconds - label: PeriodSeconds - sort: 100 - uiType: Number - validate: - defaultValue: 10 - required: true - - description: Minimum consecutive successes for the probe to be considered successful - after having failed. - jsonKey: successThreshold - label: SuccessThreshold - sort: 100 - uiType: Number - validate: - defaultValue: 1 - required: true - description: Instructions for assessing container health by probing a TCP socket. Either this attribute or the exec attribute or the httpGet attribute MUST be specified. This attribute is mutually exclusive with both the exec attribute @@ -230,6 +148,88 @@ required: true uiType: Group validate: {} + - description: Number of consecutive failures required to determine the container + is not alive (liveness probe) or not ready (readiness probe). + jsonKey: failureThreshold + label: FailureThreshold + sort: 100 + uiType: Number + validate: + defaultValue: 3 + required: true + - description: Instructions for assessing container health by executing an HTTP + GET request. Either this attribute or the exec attribute or the tcpSocket attribute + MUST be specified. This attribute is mutually exclusive with both the exec attribute + and the tcpSocket attribute. + jsonKey: httpGet + label: HttpGet + sort: 100 + subParameters: + - description: "" + jsonKey: httpHeaders + label: HttpHeaders + sort: 100 + subParameters: + - description: "" + jsonKey: name + label: Name + sort: 100 + uiType: Input + validate: + required: true + - description: "" + jsonKey: value + label: Value + sort: 100 + uiType: Input + validate: + required: true + uiType: Structs + validate: {} + - description: The endpoint, relative to the port, to which the HTTP GET request + should be directed. + jsonKey: path + label: Path + sort: 100 + uiType: Input + validate: + required: true + - description: The TCP socket within the container to which the HTTP GET request + should be directed. + jsonKey: port + label: Port + sort: 100 + uiType: Number + validate: + required: true + uiType: Group + validate: {} + - description: Number of seconds after the container is started before the first + probe is initiated. + jsonKey: initialDelaySeconds + label: InitialDelaySeconds + sort: 100 + uiType: Number + validate: + defaultValue: 0 + required: true + - description: How often, in seconds, to execute the probe. + jsonKey: periodSeconds + label: PeriodSeconds + sort: 100 + uiType: Number + validate: + defaultValue: 10 + required: true + - description: Minimum consecutive successes for the probe to be considered successful + after having failed. + jsonKey: successThreshold + label: SuccessThreshold + sort: 100 + uiType: Number + validate: + defaultValue: 1 + required: true uiType: Group validate: {} - description: Instructions for assessing whether the container is alive. @@ -237,33 +237,6 @@ label: LivenessProbe sort: 15 subParameters: - - description: Minimum consecutive successes for the probe to be considered successful - after having failed. - jsonKey: successThreshold - label: SuccessThreshold - sort: 100 - uiType: Number - validate: - defaultValue: 1 - required: true - - description: Instructions for assessing container health by probing a TCP socket. - Either this attribute or the exec attribute or the httpGet attribute MUST be - specified. This attribute is mutually exclusive with both the exec attribute - and the httpGet attribute. - jsonKey: tcpSocket - label: TcpSocket - sort: 100 - subParameters: - - description: The TCP socket within the container that should be probed to assess - container health. - jsonKey: port - label: Port - sort: 100 - uiType: Number - validate: - required: true - uiType: Group - validate: {} - description: Number of seconds after which the probe times out. jsonKey: timeoutSeconds label: TimeoutSeconds @@ -309,27 +282,6 @@ label: HttpGet sort: 100 subParameters: - - description: "" - jsonKey: httpHeaders - label: HttpHeaders - sort: 100 - subParameters: - - description: "" - jsonKey: value - label: Value - sort: 100 - uiType: Input - validate: - required: true - - description: "" - jsonKey: name - label: Name - sort: 100 - uiType: Input - validate: - required: true - uiType: Structs - validate: {} - description: The endpoint, relative to the port, to which the HTTP GET request should be directed. jsonKey: path @@ -346,6 +298,27 @@ uiType: Number validate: required: true + - description: "" + jsonKey: httpHeaders + label: HttpHeaders + sort: 100 + subParameters: + - description: "" + jsonKey: name + label: Name + sort: 100 + uiType: Input + validate: + required: true + - description: "" + jsonKey: value + label: Value + sort: 100 + uiType: Input + validate: + required: true + uiType: Structs + validate: {} uiType: Group validate: {} - description: Number of seconds after the container is started before the first @@ -365,8 +338,41 @@ validate: defaultValue: 10 required: true + - description: Minimum consecutive successes for the probe to be considered successful + after having failed. + jsonKey: successThreshold + label: SuccessThreshold + sort: 100 + uiType: Number + validate: + defaultValue: 1 + required: true + - description: Instructions for assessing container health by probing a TCP socket. + Either this attribute or the exec attribute or the httpGet attribute MUST be + specified. This attribute is mutually exclusive with both the exec attribute + and the httpGet attribute. + jsonKey: tcpSocket + label: TcpSocket + sort: 100 + subParameters: + - description: The TCP socket within the container that should be probed to assess + container health. + jsonKey: port + label: Port + sort: 100 + uiType: Number + validate: + required: true + uiType: Group + validate: {} uiType: Group validate: {} +- description: Specify image pull secrets for your service + jsonKey: imagePullSecrets + label: ImagePullSecrets + sort: 100 + uiType: Strings + validate: {} - description: Which port do you want customer traffic sent to disable: true jsonKey: port @@ -424,9 +430,3 @@ validate: defaultValue: false required: true -- description: Specify image pull secrets for your service - jsonKey: imagePullSecrets - label: ImagePullSecrets - sort: 100 - uiType: Strings - validate: {} diff --git a/pkg/apiserver/rest/usecase/webhook.go b/pkg/apiserver/rest/usecase/webhook.go index 6148dba1f..a89fcf7bc 100644 --- a/pkg/apiserver/rest/usecase/webhook.go +++ b/pkg/apiserver/rest/usecase/webhook.go @@ -20,6 +20,8 @@ import ( "context" "errors" + "github.com/emicklei/go-restful/v3" + "github.com/oam-dev/kubevela/pkg/apiserver/datastore" "github.com/oam-dev/kubevela/pkg/apiserver/model" apisv1 "github.com/oam-dev/kubevela/pkg/apiserver/rest/apis/v1" @@ -29,7 +31,7 @@ import ( // WebhookUsecase webhook usecase type WebhookUsecase interface { - HandleApplicationWebhook(ctx context.Context, token string, req apisv1.HandleApplicationWebhookRequest) (*apisv1.ApplicationDeployResponse, error) + HandleApplicationWebhook(ctx context.Context, token string, req *restful.Request) (*apisv1.ApplicationDeployResponse, error) } type webhookUsecaseImpl struct { @@ -47,7 +49,7 @@ func NewWebhookUsecase(ds datastore.DataStore, } } -func (c *webhookUsecaseImpl) HandleApplicationWebhook(ctx context.Context, token string, req apisv1.HandleApplicationWebhookRequest) (*apisv1.ApplicationDeployResponse, error) { +func (c *webhookUsecaseImpl) HandleApplicationWebhook(ctx context.Context, token string, req *restful.Request) (*apisv1.ApplicationDeployResponse, error) { webhookTrigger := &model.ApplicationTrigger{ Token: token, } @@ -68,7 +70,11 @@ func (c *webhookUsecaseImpl) HandleApplicationWebhook(ctx context.Context, token } switch webhookTrigger.PayloadType { case model.PayloadTypeCustom: - for comp, properties := range req.Upgrade { + var webhookReq apisv1.HandleApplicationWebhookRequest + if err := req.ReadEntity(&webhookReq); err != nil { + return nil, bcode.ErrInvalidWebhookPayloadBody + } + for comp, properties := range webhookReq.Upgrade { component := &model.ApplicationComponent{ AppPrimaryKey: webhookTrigger.AppPrimaryKey, Name: comp, @@ -79,7 +85,7 @@ func (c *webhookUsecaseImpl) HandleApplicationWebhook(ctx context.Context, token } return nil, err } - merge, err := envbinding.MergeRawExtension(properties.RawExtension(), component.Properties.RawExtension()) + merge, err := envbinding.MergeRawExtension(component.Properties.RawExtension(), properties.RawExtension()) if err != nil { return nil, err } @@ -97,7 +103,7 @@ func (c *webhookUsecaseImpl) HandleApplicationWebhook(ctx context.Context, token Note: "triggered by webhook", TriggerType: apisv1.TriggerTypeWebhook, Force: true, - CodeInfo: req.CodeInfo, + CodeInfo: webhookReq.CodeInfo, }) default: return nil, bcode.ErrInvalidWebhookPayloadType diff --git a/pkg/apiserver/rest/usecase/webhook_test.go b/pkg/apiserver/rest/usecase/webhook_test.go index f8219dcc6..e8438b82d 100644 --- a/pkg/apiserver/rest/usecase/webhook_test.go +++ b/pkg/apiserver/rest/usecase/webhook_test.go @@ -17,8 +17,12 @@ limitations under the License. package usecase import ( + "bytes" "context" + "encoding/json" + "net/http" + "github.com/emicklei/go-restful/v3" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -97,13 +101,12 @@ var _ = Describe("Test application usecase function", func() { appModel, err := appUsecase.GetApplication(context.TODO(), "test-app-webhook") Expect(err).Should(BeNil()) - _, err = webhookUsecase.HandleApplicationWebhook(context.TODO(), "invalid-token", apisv1.HandleApplicationWebhookRequest{}) + _, err = webhookUsecase.HandleApplicationWebhook(context.TODO(), "invalid-token", nil) Expect(err).Should(Equal(bcode.ErrInvalidWebhookToken)) - triggers, err := appUsecase.ListApplicationTriggers(context.TODO(), "test-app-webhook") + triggers, err := appUsecase.ListApplicationTriggers(context.TODO(), appModel) Expect(err).Should(BeNil()) - - res, err := webhookUsecase.HandleApplicationWebhook(context.TODO(), triggers[0].Token, apisv1.HandleApplicationWebhookRequest{ + reqBody := apisv1.HandleApplicationWebhookRequest{ Upgrade: map[string]*model.JSONStruct{ "component-name-webhook": { "image": "test-image", @@ -117,7 +120,13 @@ var _ = Describe("Test application usecase function", func() { Branch: "test-branch", User: "test-user", }, - }) + } + body, err := json.Marshal(reqBody) + Expect(err).Should(BeNil()) + httpreq, err := http.NewRequest("post", "/", bytes.NewBuffer(body)) + httpreq.Header.Add(restful.HEADER_ContentType, "application/json") + Expect(err).Should(BeNil()) + res, err := webhookUsecase.HandleApplicationWebhook(context.TODO(), triggers[0].Token, restful.NewRequest(httpreq)) Expect(err).Should(BeNil()) comp, err := appUsecase.GetApplicationComponent(context.TODO(), appModel, "component-name-webhook") Expect(err).Should(BeNil()) diff --git a/pkg/apiserver/rest/utils/bcode/001_application.go b/pkg/apiserver/rest/utils/bcode/001_application.go index c1a641f27..1be6367d3 100644 --- a/pkg/apiserver/rest/utils/bcode/001_application.go +++ b/pkg/apiserver/rest/utils/bcode/001_application.go @@ -84,3 +84,6 @@ var ErrInvalidWebhookToken = NewBcode(400, 10021, "Invalid webhook token") // ErrInvalidWebhookPayloadType means the webhook payload type is invalid var ErrInvalidWebhookPayloadType = NewBcode(400, 10022, "Invalid webhook payload type") + +// ErrInvalidWebhookPayloadBody means the webhook payload body is invalid +var ErrInvalidWebhookPayloadBody = NewBcode(400, 10023, "Invalid webhook payload body") diff --git a/pkg/apiserver/rest/webservice/application.go b/pkg/apiserver/rest/webservice/application.go index 520e819e1..f0d71bad9 100644 --- a/pkg/apiserver/rest/webservice/application.go +++ b/pkg/apiserver/rest/webservice/application.go @@ -138,7 +138,7 @@ func (c *applicationWebService) GetWebService() *restful.WebService { Metadata(restfulspec.KeyOpenAPITags, tags). Filter(c.appCheckFilter). Param(ws.PathParameter("name", "identifier of the application ").DataType("string")). - Returns(200, "", []*apis.ApplicationTriggerBase{}). + Returns(200, "", apis.ListApplicationTriggerResponse{}). Returns(400, "", bcode.Bcode{}). Writes([]*apis.ApplicationTriggerBase{})) @@ -559,7 +559,8 @@ func (c *applicationWebService) createApplicationTrigger(req *restful.Request, r bcode.ReturnError(req, res, err) return } - base, err := c.applicationUsecase.CreateApplicationTrigger(req.Request.Context(), req.PathParameter("name"), createReq) + app := req.Request.Context().Value(&apis.CtxKeyApplication).(*model.Application) + base, err := c.applicationUsecase.CreateApplicationTrigger(req.Request.Context(), app, createReq) if err != nil { bcode.ReturnError(req, res, err) return @@ -571,12 +572,13 @@ func (c *applicationWebService) createApplicationTrigger(req *restful.Request, r } func (c *applicationWebService) listApplicationTriggers(req *restful.Request, res *restful.Response) { - triggers, err := c.applicationUsecase.ListApplicationTriggers(req.Request.Context(), req.PathParameter("name")) + app := req.Request.Context().Value(&apis.CtxKeyApplication).(*model.Application) + triggers, err := c.applicationUsecase.ListApplicationTriggers(req.Request.Context(), app) if err != nil { bcode.ReturnError(req, res, err) return } - if err := res.WriteEntity(triggers); err != nil { + if err := res.WriteEntity(apis.ListApplicationTriggerResponse{Triggers: triggers}); err != nil { bcode.ReturnError(req, res, err) return } diff --git a/pkg/apiserver/rest/webservice/webhook.go b/pkg/apiserver/rest/webservice/webhook.go index 0e65d1622..fdc2a54dc 100644 --- a/pkg/apiserver/rest/webservice/webhook.go +++ b/pkg/apiserver/rest/webservice/webhook.go @@ -59,12 +59,7 @@ func (c *webhookWebService) GetWebService() *restful.WebService { } func (c *webhookWebService) handleApplicationWebhook(req *restful.Request, res *restful.Response) { - var webhookReq apis.HandleApplicationWebhookRequest - if err := req.ReadEntity(&webhookReq); err != nil { - bcode.ReturnError(req, res, err) - return - } - base, err := c.webhookUsecase.HandleApplicationWebhook(req.Request.Context(), req.PathParameter("token"), webhookReq) + base, err := c.webhookUsecase.HandleApplicationWebhook(req.Request.Context(), req.PathParameter("token"), req) if err != nil { bcode.ReturnError(req, res, err) return