mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-27 16:17:34 +00:00
Feat: add the component name field for the trigger (#3884)
Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
This commit is contained in:
@@ -379,6 +379,7 @@ type ApplicationTrigger struct {
|
||||
Token string `json:"token"`
|
||||
Type string `json:"type"`
|
||||
PayloadType string `json:"payloadType"`
|
||||
ComponentName string `json:"componentName"`
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -403,6 +403,7 @@ func (c *applicationUsecaseImpl) CreateApplication(ctx context.Context, req apis
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// For the custom payload, no need assign the component name
|
||||
if _, err := c.CreateApplicationTrigger(ctx, &application, apisv1.CreateApplicationTriggerRequest{
|
||||
Name: fmt.Sprintf("%s-%s", application.Name, "default"),
|
||||
PayloadType: model.PayloadTypeCustom,
|
||||
@@ -434,6 +435,7 @@ func (c *applicationUsecaseImpl) CreateApplicationTrigger(ctx context.Context, a
|
||||
Description: req.Description,
|
||||
Type: req.Type,
|
||||
PayloadType: req.PayloadType,
|
||||
ComponentName: req.ComponentName,
|
||||
Token: genWebhookToken(),
|
||||
}
|
||||
if err := c.ds.Add(ctx, trigger); err != nil {
|
||||
@@ -449,7 +451,7 @@ func (c *applicationUsecaseImpl) CreateApplicationTrigger(ctx context.Context, a
|
||||
Type: req.Type,
|
||||
PayloadType: req.PayloadType,
|
||||
Token: trigger.Token,
|
||||
ComponentName: req.ComponentName,
|
||||
ComponentName: trigger.ComponentName,
|
||||
CreateTime: trigger.CreateTime,
|
||||
UpdateTime: trigger.UpdateTime,
|
||||
}, nil
|
||||
@@ -489,15 +491,16 @@ func (c *applicationUsecaseImpl) ListApplicationTriggers(ctx context.Context, ap
|
||||
trigger, ok := raw.(*model.ApplicationTrigger)
|
||||
if ok {
|
||||
resp = append(resp, &apisv1.ApplicationTriggerBase{
|
||||
WorkflowName: trigger.WorkflowName,
|
||||
Name: trigger.Name,
|
||||
Alias: trigger.Alias,
|
||||
Description: trigger.Description,
|
||||
Type: trigger.Type,
|
||||
PayloadType: trigger.PayloadType,
|
||||
Token: trigger.Token,
|
||||
UpdateTime: trigger.UpdateTime,
|
||||
CreateTime: trigger.CreateTime,
|
||||
WorkflowName: trigger.WorkflowName,
|
||||
Name: trigger.Name,
|
||||
Alias: trigger.Alias,
|
||||
Description: trigger.Description,
|
||||
Type: trigger.Type,
|
||||
PayloadType: trigger.PayloadType,
|
||||
Token: trigger.Token,
|
||||
UpdateTime: trigger.UpdateTime,
|
||||
CreateTime: trigger.CreateTime,
|
||||
ComponentName: trigger.ComponentName,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,6 +208,12 @@ var _ = Describe("Test application usecase function", func() {
|
||||
Name: "trigger-name",
|
||||
})
|
||||
Expect(err).Should(BeNil())
|
||||
base, err := appUsecase.CreateApplicationTrigger(context.TODO(), appModel, v1.CreateApplicationTriggerRequest{
|
||||
Name: "trigger-name-2",
|
||||
ComponentName: "trigger-component",
|
||||
})
|
||||
Expect(err).Should(BeNil())
|
||||
Expect(base.ComponentName).Should(Equal("trigger-component"))
|
||||
})
|
||||
|
||||
It("Test ListTriggers function", func() {
|
||||
@@ -215,7 +221,7 @@ var _ = Describe("Test application usecase function", func() {
|
||||
Expect(err).Should(BeNil())
|
||||
triggers, err := appUsecase.ListApplicationTriggers(context.TODO(), appModel)
|
||||
Expect(err).Should(BeNil())
|
||||
Expect(len(triggers)).Should(Equal(2))
|
||||
Expect(len(triggers)).Should(Equal(3))
|
||||
})
|
||||
|
||||
It("Test DeleteTrigger function", func() {
|
||||
@@ -223,7 +229,7 @@ var _ = Describe("Test application usecase function", func() {
|
||||
Expect(err).Should(BeNil())
|
||||
triggers, err := appUsecase.ListApplicationTriggers(context.TODO(), appModel)
|
||||
Expect(err).Should(BeNil())
|
||||
Expect(len(triggers)).Should(Equal(2))
|
||||
Expect(len(triggers)).Should(Equal(3))
|
||||
var trigger *v1.ApplicationTriggerBase
|
||||
for _, t := range triggers {
|
||||
if t.Name == "trigger-name" {
|
||||
@@ -235,7 +241,7 @@ var _ = Describe("Test application usecase function", func() {
|
||||
Expect(appUsecase.DeleteApplicationTrigger(context.TODO(), appModel, trigger.Token)).Should(BeNil())
|
||||
triggers, err = appUsecase.ListApplicationTriggers(context.TODO(), appModel)
|
||||
Expect(err).Should(BeNil())
|
||||
Expect(len(triggers)).Should(Equal(1))
|
||||
Expect(len(triggers)).Should(Equal(2))
|
||||
trigger = nil
|
||||
for _, t := range triggers {
|
||||
if t.Name == "trigger-name" {
|
||||
|
||||
@@ -219,19 +219,11 @@ func (c *customHandlerImpl) install() {
|
||||
}
|
||||
|
||||
func (c *acrHandlerImpl) handle(ctx context.Context, webhookTrigger *model.ApplicationTrigger, app *model.Application) (interface{}, error) {
|
||||
comp := &model.ApplicationComponent{
|
||||
AppPrimaryKey: webhookTrigger.AppPrimaryKey,
|
||||
}
|
||||
comps, err := c.w.ds.List(ctx, comp, &datastore.ListOptions{})
|
||||
|
||||
component, err := getComponent(ctx, c.w.ds, webhookTrigger)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(comps) == 0 {
|
||||
return nil, bcode.ErrApplicationComponentNotExist
|
||||
}
|
||||
|
||||
// use the first component as the target component
|
||||
component := comps[0].(*model.ApplicationComponent)
|
||||
acrReq := c.req
|
||||
image := fmt.Sprintf("registry.%s.aliyuncs.com/%s:%s", acrReq.Repository.Region, acrReq.Repository.RepoFullName, acrReq.PushData.Tag)
|
||||
if err := c.w.patchComponentProperties(ctx, component, &runtime.RawExtension{
|
||||
@@ -278,20 +270,10 @@ func (c dockerHubHandlerImpl) handle(ctx context.Context, trigger *model.Applica
|
||||
Description: "not create event",
|
||||
}, nil
|
||||
}
|
||||
|
||||
comp := &model.ApplicationComponent{
|
||||
AppPrimaryKey: trigger.AppPrimaryKey,
|
||||
}
|
||||
comps, err := c.w.ds.List(ctx, comp, &datastore.ListOptions{})
|
||||
component, err := getComponent(ctx, c.w.ds, trigger)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(comps) == 0 {
|
||||
return nil, bcode.ErrApplicationComponentNotExist
|
||||
}
|
||||
|
||||
// use the first component as the target component
|
||||
component := comps[0].(*model.ApplicationComponent)
|
||||
image := fmt.Sprintf("docker.io/%s:%s", dockerHubReq.Repository.RepoName, dockerHubReq.PushData.Tag)
|
||||
if err := c.w.patchComponentProperties(ctx, component, &runtime.RawExtension{
|
||||
Raw: []byte(fmt.Sprintf(`{"image": "%s"}`, image)),
|
||||
@@ -387,19 +369,10 @@ func (c *harborHandlerImpl) handle(ctx context.Context, webhookTrigger *model.Ap
|
||||
imageURL := resources[0].ResourceURL
|
||||
digest := resources[0].Digest
|
||||
tag := resources[0].Tag
|
||||
comp := &model.ApplicationComponent{
|
||||
AppPrimaryKey: webhookTrigger.AppPrimaryKey,
|
||||
}
|
||||
comps, err := c.w.ds.List(ctx, comp, &datastore.ListOptions{})
|
||||
component, err := getComponent(ctx, c.w.ds, webhookTrigger)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(comps) == 0 {
|
||||
return nil, bcode.ErrApplicationComponentNotExist
|
||||
}
|
||||
|
||||
// use the first component as the target component
|
||||
component := comps[0].(*model.ApplicationComponent)
|
||||
harborReq := c.req
|
||||
if err := c.w.patchComponentProperties(ctx, component, &runtime.RawExtension{
|
||||
Raw: []byte(fmt.Sprintf(`{"image": "%s"}`, imageURL)),
|
||||
@@ -453,19 +426,10 @@ func (c *webhookUsecaseImpl) newJFrogHandler(req *restful.Request) (webhookHandl
|
||||
|
||||
func (j *jfrogHandlerImpl) handle(ctx context.Context, webhookTrigger *model.ApplicationTrigger, app *model.Application) (interface{}, error) {
|
||||
jfrogReq := j.req
|
||||
comp := &model.ApplicationComponent{
|
||||
AppPrimaryKey: webhookTrigger.AppPrimaryKey,
|
||||
}
|
||||
comps, err := j.w.ds.List(ctx, comp, &datastore.ListOptions{})
|
||||
component, err := getComponent(ctx, j.w.ds, webhookTrigger)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(comps) == 0 {
|
||||
return nil, bcode.ErrApplicationComponentNotExist
|
||||
}
|
||||
|
||||
// use the first component as the target component
|
||||
component := comps[0].(*model.ApplicationComponent)
|
||||
image := fmt.Sprintf("%s/%s:%s", jfrogReq.Data.RepoKey, jfrogReq.Data.ImageName, jfrogReq.Data.Tag)
|
||||
if jfrogReq.Data.URL != "" {
|
||||
image = fmt.Sprintf("%s/%s", jfrogReq.Data.URL, image)
|
||||
@@ -500,3 +464,28 @@ func (j *jfrogHandlerImpl) handle(ctx context.Context, webhookTrigger *model.App
|
||||
func (j *jfrogHandlerImpl) install() {
|
||||
WebhookHandlers = append(WebhookHandlers, model.PayloadTypeJFrog)
|
||||
}
|
||||
|
||||
func getComponent(ctx context.Context, ds datastore.DataStore, webhookTrigger *model.ApplicationTrigger) (*model.ApplicationComponent, error) {
|
||||
if webhookTrigger.ComponentName != "" {
|
||||
comp := &model.ApplicationComponent{
|
||||
AppPrimaryKey: webhookTrigger.AppPrimaryKey,
|
||||
Name: webhookTrigger.ComponentName,
|
||||
}
|
||||
err := ds.Get(ctx, comp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return comp, nil
|
||||
}
|
||||
comp := &model.ApplicationComponent{
|
||||
AppPrimaryKey: webhookTrigger.AppPrimaryKey,
|
||||
}
|
||||
comps, err := ds.List(ctx, comp, &datastore.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(comps) == 0 {
|
||||
return nil, bcode.ErrApplicationComponentNotExist
|
||||
}
|
||||
return comps[0].(*model.ApplicationComponent), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user