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 <guanxiatao@corp.netease.com>
This commit is contained in:
Ted Guan 2020-03-11 18:06:58 +08:00 committed by GitHub
parent 8452100148
commit 4ac31c6d46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 0 deletions

View File

@ -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'

View File

@ -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);

View File

@ -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 := &notificationPolicyForUI{
PolicyName: policy.Name,
EventType: t,
Enabled: policy.Enabled,
CreationTime: &policy.CreationTime,

View File

@ -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")