Fix bugs by comments for webhook

Signed-off-by: peimingming <peimingming@corp.netease.com>
This commit is contained in:
peimingming 2019-08-21 14:57:09 +08:00
parent 222c47142a
commit 599d12a04d
8 changed files with 64 additions and 48 deletions

View File

@ -126,12 +126,13 @@ func modifyResponse(res *http.Response) error {
Operator: e.Resource.ExtendedInfo["operator"].(string), Operator: e.Resource.ExtendedInfo["operator"].(string),
}, },
} }
if err := event.Build(metaData); err != nil { if err := event.Build(metaData); err == nil {
hlog.Errorf("failed to build chart upload event metadata: %v", err)
}
if err := event.Publish(); err != nil { if err := event.Publish(); err != nil {
hlog.Errorf("failed to publish chart upload event: %v", err) hlog.Errorf("failed to publish chart upload event: %v", err)
} }
} else {
hlog.Errorf("failed to build chart upload event metadata: %v", err)
}
} }
} }
} }
@ -143,12 +144,13 @@ func modifyResponse(res *http.Response) error {
if ok && eventMetaData != nil { if ok && eventMetaData != nil {
// Trigger harbor webhook // Trigger harbor webhook
event := &n_event.Event{} event := &n_event.Event{}
if err := event.Build(eventMetaData); err != nil { if err := event.Build(eventMetaData); err == nil {
hlog.Errorf("failed to build chart download event metadata: %v", err)
}
if err := event.Publish(); err != nil { if err := event.Publish(); err != nil {
hlog.Errorf("failed to publish chart download event: %v", err) hlog.Errorf("failed to publish chart download event: %v", err)
} }
} else {
hlog.Errorf("failed to build chart download event metadata: %v", err)
}
} }
} }

View File

@ -301,12 +301,13 @@ func (cra *ChartRepositoryAPI) DeleteChartVersion() {
Operator: cra.SecurityCtx.GetUsername(), Operator: cra.SecurityCtx.GetUsername(),
}, },
} }
if err := event.Build(metaData); err != nil { if err := event.Build(metaData); err == nil {
hlog.Errorf("failed to build chart delete event metadata: %v", err)
}
if err := event.Publish(); err != nil { if err := event.Publish(); err != nil {
hlog.Errorf("failed to publish chart delete event: %v", err) hlog.Errorf("failed to publish chart delete event: %v", err)
} }
} else {
hlog.Errorf("failed to build chart delete event metadata: %v", err)
}
} }
// UploadChartVersion handles POST /api/:repo/charts // UploadChartVersion handles POST /api/:repo/charts
@ -393,6 +394,11 @@ func (cra *ChartRepositoryAPI) DeleteChart() {
} }
} }
if err := chartController.DeleteChart(cra.namespace, chartName); err != nil {
cra.SendInternalServerError(err)
return
}
event := &n_event.Event{} event := &n_event.Event{}
metaData := &n_event.ChartDeleteMetaData{ metaData := &n_event.ChartDeleteMetaData{
ChartMetaData: n_event.ChartMetaData{ ChartMetaData: n_event.ChartMetaData{
@ -403,16 +409,12 @@ func (cra *ChartRepositoryAPI) DeleteChart() {
Operator: cra.SecurityCtx.GetUsername(), Operator: cra.SecurityCtx.GetUsername(),
}, },
} }
if err := event.Build(metaData); err != nil { if err := event.Build(metaData); err == nil {
hlog.Errorf("failed to build chart delete event metadata: %v", err)
}
if err := event.Publish(); err != nil { if err := event.Publish(); err != nil {
hlog.Errorf("failed to publish chart delete event: %v", err) hlog.Errorf("failed to publish chart delete event: %v", err)
} }
} else {
if err := chartController.DeleteChart(cra.namespace, chartName); err != nil { hlog.Errorf("failed to build chart delete event metadata: %v", err)
cra.SendInternalServerError(err)
return
} }
} }

View File

@ -349,14 +349,15 @@ func (ra *RepositoryAPI) Delete() {
OccurAt: time.Now(), OccurAt: time.Now(),
Operator: ra.SecurityCtx.GetUsername(), Operator: ra.SecurityCtx.GetUsername(),
} }
if err := evt.Build(imgDelMetadata); err != nil { if err := evt.Build(imgDelMetadata); err == nil {
// do not return when building event metadata failed
log.Errorf("failed to build image delete event metadata: %v", err)
}
if err := evt.Publish(); err != nil { if err := evt.Publish(); err != nil {
// do not return when publishing event failed // do not return when publishing event failed
log.Errorf("failed to publish image delete event: %v", err) log.Errorf("failed to publish image delete event: %v", err)
} }
} else {
// do not return when building event metadata failed
log.Errorf("failed to build image delete event metadata: %v", err)
}
exist, err := repositoryExist(repoName, rc) exist, err := repositoryExist(repoName, rc)
if err != nil { if err != nil {

View File

@ -98,6 +98,7 @@ func constructImagePayload(event *notifyModel.ImageEvent) (*notifyModel.Payload,
// send hook by publishing topic of specified target type(notify type) // send hook by publishing topic of specified target type(notify type)
func sendHookWithPolicies(policies []*models.NotificationPolicy, payload *notifyModel.Payload, eventType string) error { func sendHookWithPolicies(policies []*models.NotificationPolicy, payload *notifyModel.Payload, eventType string) error {
errRet := false
for _, ply := range policies { for _, ply := range policies {
targets := ply.Targets targets := ply.Targets
for _, target := range targets { for _, target := range targets {
@ -108,18 +109,22 @@ func sendHookWithPolicies(policies []*models.NotificationPolicy, payload *notify
Payload: payload, Payload: payload,
Target: &target, Target: &target,
} }
if err := evt.Build(hookMetadata); err != nil { // It should never affect evaluating other policies when one is failed, but error should return
log.Errorf("failed to build hook notify event metadata: %v", err) if err := evt.Build(hookMetadata); err == nil {
return err
}
if err := evt.Publish(); err != nil { if err := evt.Publish(); err != nil {
errRet = true
log.Errorf("failed to publish hook notify event: %v", err) log.Errorf("failed to publish hook notify event: %v", err)
return err
} }
} else {
errRet = true
log.Errorf("failed to build hook notify event metadata: %v", err)
}
log.Debugf("published image event %s by topic %s", payload.Type, target.Type) log.Debugf("published image event %s by topic %s", payload.Type, target.Type)
} }
} }
if errRet {
return errors.New("failed to trigger some of the events")
}
return nil return nil
} }

View File

@ -52,6 +52,9 @@ func (si *ScanImagePreprocessHandler) Handle(value interface{}) error {
log.Errorf("failed to find project[%s] for scan image event: %v", projectName, err) log.Errorf("failed to find project[%s] for scan image event: %v", projectName, err)
return err return err
} }
if project == nil {
return fmt.Errorf("project[%s] not found", projectName)
}
policies, err := notification.PolicyMgr.GetRelatedPolices(project.ProjectID, e.EventType) policies, err := notification.PolicyMgr.GetRelatedPolices(project.ProjectID, e.EventType)
if err != nil { if err != nil {
log.Errorf("failed to find policy for %s event: %v", e.EventType, err) log.Errorf("failed to find policy for %s event: %v", e.EventType, err)

View File

@ -94,7 +94,7 @@ func TestScanImagePreprocessHandler_Handle(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
err := handler.Handle(tt.args.data) err := handler.Handle(tt.args.data)
if tt.wantErr { if tt.wantErr {
require.NotNil(t, err, "Error: %s", err) require.NotNil(t, err, "Error: %v", err)
return return
} }
assert.Nil(t, err) assert.Nil(t, err)

View File

@ -89,12 +89,13 @@ func (h *Handler) HandleScan() {
JobID: h.id, JobID: h.id,
Status: h.status, Status: h.status,
} }
if err := e.Build(metaData); err != nil { if err := e.Build(metaData); err == nil {
log.Errorf("failed to build image scanning event metadata: %v", err)
}
if err := e.Publish(); err != nil { if err := e.Publish(); err != nil {
log.Errorf("failed to publish image scanning event: %v", err) log.Errorf("failed to publish image scanning event: %v", err)
} }
} else {
log.Errorf("failed to build image scanning event metadata: %v", err)
}
} }
if err := dao.UpdateScanJobStatus(h.id, h.status); err != nil { if err := dao.UpdateScanJobStatus(h.id, h.status); err != nil {

View File

@ -127,14 +127,15 @@ func (n *NotificationHandler) Post() {
OccurAt: time.Now(), OccurAt: time.Now(),
Operator: event.Actor.Name, Operator: event.Actor.Name,
} }
if err := evt.Build(imgPushMetadata); err != nil { if err := evt.Build(imgPushMetadata); err == nil {
// do not return when building event metadata failed
log.Errorf("failed to build image push event metadata: %v", err)
}
if err := evt.Publish(); err != nil { if err := evt.Publish(); err != nil {
// do not return when publishing event failed // do not return when publishing event failed
log.Errorf("failed to publish image push event: %v", err) log.Errorf("failed to publish image push event: %v", err)
} }
} else {
// do not return when building event metadata failed
log.Errorf("failed to build image push event metadata: %v", err)
}
// TODO: handle image delete event and chart event // TODO: handle image delete event and chart event
go func() { go func() {
@ -178,14 +179,15 @@ func (n *NotificationHandler) Post() {
OccurAt: time.Now(), OccurAt: time.Now(),
Operator: event.Actor.Name, Operator: event.Actor.Name,
} }
if err := evt.Build(imgPullMetadata); err != nil { if err := evt.Build(imgPullMetadata); err == nil {
// do not return when building event metadata failed
log.Errorf("failed to build image push event metadata: %v", err)
}
if err := evt.Publish(); err != nil { if err := evt.Publish(); err != nil {
// do not return when publishing event failed // do not return when publishing event failed
log.Errorf("failed to publish image pull event: %v", err) log.Errorf("failed to publish image pull event: %v", err)
} }
} else {
// do not return when building event metadata failed
log.Errorf("failed to build image push event metadata: %v", err)
}
go func() { go func() {
log.Debugf("Increase the repository %s pull count.", repository) log.Debugf("Increase the repository %s pull count.", repository)