Fix for duplicate webhook policy name (#12729)

Signed-off-by: guanxiatao <guanxiatao@corp.netease.com>
This commit is contained in:
Ted Guan 2020-08-20 18:02:13 +08:00 committed by GitHub
parent 29f3ced3ff
commit 645dea36a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 3 deletions

View File

@ -116,6 +116,14 @@ ALTER TABLE schedule DROP COLUMN IF EXISTS status;
UPDATE registry SET type = 'quay' WHERE type = 'quay-io'; UPDATE registry SET type = 'quay' WHERE type = 'quay-io';
ALTER TABLE artifact ADD COLUMN icon varchar(255);
/*remove the constraint for name in table 'notification_policy'*/
ALTER TABLE notification_policy DROP CONSTRAINT notification_policy_name_key;
/*add union unique constraint for name and project_id in table 'notification_policy'*/
ALTER TABLE notification_policy ADD UNIQUE(name,project_id);
CREATE TABLE IF NOT EXISTS data_migrations ( CREATE TABLE IF NOT EXISTS data_migrations (
version int version int
); );
@ -142,3 +150,4 @@ CASE
ELSE ELSE
'sha256:da834479c923584f4cbcdecc0dac61f32bef1d51e8aae598cf16bd154efab49f' 'sha256:da834479c923584f4cbcdecc0dac61f32bef1d51e8aae598cf16bd154efab49f'
END); END);

View File

@ -1,10 +1,13 @@
package notification package notification
import ( import (
"fmt"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/common/dao" "github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models" "github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/errors"
lib_orm "github.com/goharbor/harbor/src/lib/orm"
) )
// GetNotificationPolicy return notification policy by id // GetNotificationPolicy return notification policy by id
@ -48,7 +51,16 @@ func AddNotificationPolicy(policy *models.NotificationPolicy) (int64, error) {
return 0, errors.New("nil policy") return 0, errors.New("nil policy")
} }
o := dao.GetOrmer() o := dao.GetOrmer()
return o.Insert(policy) id, err := o.Insert(policy)
if err != nil {
if e := lib_orm.AsConflictError(err, "notification policy named %s already exists", policy.Name); e != nil {
err = e
return id, err
}
err = fmt.Errorf("failed to create the notification policy: %v", err)
return id, err
}
return id, err
} }
// UpdateNotificationPolicy update t specified notification policy // UpdateNotificationPolicy update t specified notification policy
@ -58,6 +70,13 @@ func UpdateNotificationPolicy(policy *models.NotificationPolicy) error {
} }
o := dao.GetOrmer() o := dao.GetOrmer()
_, err := o.Update(policy) _, err := o.Update(policy)
if err != nil {
if e := lib_orm.AsConflictError(err, "notification policy named %s already exists", policy.Name); e != nil {
return e
}
err = fmt.Errorf("failed to update the notification policy: %v", err)
return err
}
return err return err
} }

View File

@ -128,7 +128,7 @@ func (w *NotificationPolicyAPI) Post() {
id, err := notification.PolicyMgr.Create(policy) id, err := notification.PolicyMgr.Create(policy)
if err != nil { if err != nil {
w.SendInternalServerError(fmt.Errorf("failed to create the notification policy: %v", err)) w.SendError(err)
return return
} }
w.Redirect(http.StatusCreated, strconv.FormatInt(id, 10)) w.Redirect(http.StatusCreated, strconv.FormatInt(id, 10))
@ -180,7 +180,7 @@ func (w *NotificationPolicyAPI) Put() {
policy.ProjectID = w.project.ProjectID policy.ProjectID = w.project.ProjectID
if err = notification.PolicyMgr.Update(policy); err != nil { if err = notification.PolicyMgr.Update(policy); err != nil {
w.SendInternalServerError(fmt.Errorf("failed to update the notification policy: %v", err)) w.SendError(err)
return return
} }
} }