Merge pull request #9825 from stonezdj/bug_9681

Avoid to create duplicated immutable tag rules in the same project
This commit is contained in:
Wang Yan 2019-11-18 17:26:22 +08:00 committed by GitHub
commit eab974419c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 4 deletions

View File

@ -44,7 +44,8 @@ CREATE TABLE immutable_tag_rule
project_id int NOT NULL, project_id int NOT NULL,
tag_filter text, tag_filter text,
disabled BOOLEAN NOT NULL DEFAULT FALSE, disabled BOOLEAN NOT NULL DEFAULT FALSE,
creation_time timestamp default CURRENT_TIMESTAMP creation_time timestamp default CURRENT_TIMESTAMP,
UNIQUE(project_id, tag_filter)
); );
ALTER TABLE robot ADD COLUMN visible boolean DEFAULT true NOT NULL; ALTER TABLE robot ADD COLUMN visible boolean DEFAULT true NOT NULL;

View File

@ -101,6 +101,10 @@ func (itr *ImmutableTagRuleAPI) Post() {
} }
ir.ProjectID = itr.projectID ir.ProjectID = itr.projectID
id, err := itr.ctr.CreateImmutableRule(ir) id, err := itr.ctr.CreateImmutableRule(ir)
if err != nil && strings.Contains(err.Error(), "duplicate key") {
itr.RenderError(http.StatusConflict, "immutable tag rule duplicated")
return
}
if err != nil { if err != nil {
itr.SendInternalServerError(err) itr.SendInternalServerError(err)
return return

View File

@ -152,7 +152,7 @@ func TestImmutableTagRuleAPI_Post(t *testing.T) {
}, },
code: http.StatusCreated, code: http.StatusCreated,
}, },
// 201 // 409
{ {
request: &testingRequest{ request: &testingRequest{
method: http.MethodPost, method: http.MethodPost,
@ -160,7 +160,7 @@ func TestImmutableTagRuleAPI_Post(t *testing.T) {
credential: projAdmin, credential: projAdmin,
bodyJSON: metadata, bodyJSON: metadata,
}, },
code: http.StatusCreated, code: http.StatusConflict,
}, },
// 403 // 403
{ {

View File

@ -1,12 +1,14 @@
package dao package dao
import ( import (
"strings"
"testing"
"github.com/goharbor/harbor/src/common/dao" "github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/pkg/immutabletag/dao/model" "github.com/goharbor/harbor/src/pkg/immutabletag/dao/model"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"testing"
) )
type immutableRuleDaoTestSuite struct { type immutableRuleDaoTestSuite struct {
@ -29,6 +31,13 @@ func (t *immutableRuleDaoTestSuite) TestCreateImmutableRule() {
id, err := t.dao.CreateImmutableRule(ir) id, err := t.dao.CreateImmutableRule(ir)
t.require.Nil(err) t.require.Nil(err)
t.require.True(id > 0, "Can not create immutable tag rule") t.require.True(id > 0, "Can not create immutable tag rule")
// insert duplicate rows
ir2 := &model.ImmutableRule{TagFilter: "**", ProjectID: 1}
id2, err := t.dao.CreateImmutableRule(ir2)
t.require.True(strings.Contains(err.Error(), "duplicate key"))
t.require.Equal(int64(0), id2)
_, err = t.dao.DeleteImmutableRule(id) _, err = t.dao.DeleteImmutableRule(id)
t.require.Nil(err) t.require.Nil(err)
} }