From 4ac31c6d46ac5f73cfb7e6a4403308caf0b7420a Mon Sep 17 00:00:00 2001 From: Ted Guan Date: Wed, 11 Mar 2020 18:06:58 +0800 Subject: [PATCH] Add API for query supported event types and notify types; Return policy name in last trigger info; Remove project_id unique constraint in table notification_policy (#11029) Signed-off-by: guanxiatao --- api/v2.0/swagger.yaml | 43 +++++++++++++++++++ .../postgresql/0030_2.0.0_schema.up.sql | 6 +++ src/core/api/notification_policy.go | 25 +++++++++++ src/server/v2.0/route/legacy.go | 1 + 4 files changed, 75 insertions(+) diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index b714de257..7ec83d786 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -565,6 +565,23 @@ paths: $ref: '#/responses/401' '500': $ref: '#/responses/500' + /projects/{project_id}/webhook/events: + get: + summary: Get supported event types and notify types. + description: Get supportted event types and notify types. + tags: + - notification + parameters: + - $ref: '#/parameters/projectId' + responses: + '200': + description: Success + schema: + $ref: '#/definitions/SupportedWebhookEventTypes' + '401': + $ref: '#/responses/401' + '403': + $ref: '#/responses/403' parameters: query: name: q @@ -585,6 +602,12 @@ parameters: description: The name of the project required: true type: string + projectId: + name: project_id + in: path + description: The ID of the project + required: true + type: string repositoryName: name: repository_name in: path @@ -1012,3 +1035,23 @@ definitions: op_time: type: string description: The time when this operation is triggered. + SupportedWebhookEventTypes: + type: object + description: Supportted webhook event types and notify types. + properties: + event_type: + type: array + items: + $ref: '#/definitions/EventType' + notify_type: + type: array + items: + $ref: '#/definitions/NotifyType' + EventType: + type: string + description: Webhook supportted event type. + example: 'pullImage' + NotifyType: + type: string + description: Webhook supportted notify type. + example: 'http' diff --git a/make/migrations/postgresql/0030_2.0.0_schema.up.sql b/make/migrations/postgresql/0030_2.0.0_schema.up.sql index c634944e0..c89b006d5 100644 --- a/make/migrations/postgresql/0030_2.0.0_schema.up.sql +++ b/make/migrations/postgresql/0030_2.0.0_schema.up.sql @@ -208,3 +208,9 @@ BEGIN END IF; END LOOP; END $$; + +/*remove the constraint for project_id in table 'notification_policy'*/ +ALTER TABLE notification_policy DROP CONSTRAINT unique_project_id; + +/*add the unique constraint for name in table 'notification_policy'*/ +ALTER TABLE notification_policy ADD UNIQUE (name); diff --git a/src/core/api/notification_policy.go b/src/core/api/notification_policy.go index 324227d15..67eceaec9 100755 --- a/src/core/api/notification_policy.go +++ b/src/core/api/notification_policy.go @@ -23,12 +23,18 @@ type NotificationPolicyAPI struct { // notificationPolicyForUI defines the structure of notification policy info display in UI type notificationPolicyForUI struct { + PolicyName string `json:"policy_name"` EventType string `json:"event_type"` Enabled bool `json:"enabled"` CreationTime *time.Time `json:"creation_time"` LastTriggerTime *time.Time `json:"last_trigger_time,omitempty"` } +type notificationSupportedEventTypes struct { + EventType []string `json:"event_type"` + NotifyType []string `json:"notify_type"` +} + // Prepare ... func (w *NotificationPolicyAPI) Prepare() { w.BaseController.Prepare() @@ -256,6 +262,24 @@ func (w *NotificationPolicyAPI) Delete() { } } +// GetSupportedEventTypes get supported trigger event types and notify types in module notification +func (w *NotificationPolicyAPI) GetSupportedEventTypes() { + projectID := w.project.ProjectID + if !w.validateRBAC(rbac.ActionList, projectID) { + return + } + + var notificationTypes = notificationSupportedEventTypes{} + for key := range notification.SupportedNotifyTypes { + notificationTypes.NotifyType = append(notificationTypes.NotifyType, key) + } + + for key := range notification.SupportedEventTypes { + notificationTypes.EventType = append(notificationTypes.EventType, key) + } + w.WriteJSONData(notificationTypes) +} + // Test ... func (w *NotificationPolicyAPI) Test() { projectID := w.project.ProjectID @@ -353,6 +377,7 @@ func constructPolicyWithTriggerTime(policies []*models.NotificationPolicy) ([]*n for _, policy := range policies { for _, t := range policy.EventTypes { ply := ¬ificationPolicyForUI{ + PolicyName: policy.Name, EventType: t, Enabled: policy.Enabled, CreationTime: &policy.CreationTime, diff --git a/src/server/v2.0/route/legacy.go b/src/server/v2.0/route/legacy.go index c841e2b88..64a05a7b4 100755 --- a/src/server/v2.0/route/legacy.go +++ b/src/server/v2.0/route/legacy.go @@ -77,6 +77,7 @@ func registerLegacyRoutes() { beego.Router("/api/"+version+"/projects/:pid([0-9]+)/webhook/policies/:id([0-9]+)", &api.NotificationPolicyAPI{}) beego.Router("/api/"+version+"/projects/:pid([0-9]+)/webhook/policies/test", &api.NotificationPolicyAPI{}, "post:Test") beego.Router("/api/"+version+"/projects/:pid([0-9]+)/webhook/lasttrigger", &api.NotificationPolicyAPI{}, "get:ListGroupByEventType") + beego.Router("/api/"+version+"/projects/:pid([0-9]+)/webhook/events", &api.NotificationPolicyAPI{}, "get:GetSupportedEventTypes") beego.Router("/api/"+version+"/projects/:pid([0-9]+)/webhook/jobs/", &api.NotificationJobAPI{}, "get:List") beego.Router("/api/"+version+"/projects/:pid([0-9]+)/immutabletagrules", &api.ImmutableTagRuleAPI{}, "get:List;post:Post")