Remove the label from artifacts when deleting a label

Fixes #12112, remove the label from artifacts when deleting a label

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2020-06-03 11:40:57 +08:00
parent f5d482854b
commit 06f7b7f763
4 changed files with 31 additions and 0 deletions

View File

@ -24,6 +24,8 @@ import (
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/rbac"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/pkg/label"
)
// LabelAPI handles requests for label management
@ -293,6 +295,12 @@ func (l *LabelAPI) Delete() {
l.SendInternalServerError(fmt.Errorf("failed to delete resource label mappings of label %d: %v", id, err))
return
}
if err := label.Mgr.RemoveFromAllArtifacts(orm.Context(), id); err != nil {
l.SendInternalServerError(fmt.Errorf("failed to remove the label %d from all artifacts: %v", id, err))
return
}
if err := dao.DeleteLabel(id); err != nil {
l.SendInternalServerError(fmt.Errorf("failed to delete label %d: %v", id, err))
return

View File

@ -37,6 +37,8 @@ type Manager interface {
RemoveFrom(ctx context.Context, labelID int64, artifactID int64) (err error)
// Remove all labels added to the artifact specified by the ID
RemoveAllFrom(ctx context.Context, artifactID int64) (err error)
// RemoveFromAllArtifacts removes the label specified by the ID from all artifacts
RemoveFromAllArtifacts(ctx context.Context, labelID int64) (err error)
}
// New creates an instance of the default label manager
@ -92,3 +94,12 @@ func (m *manager) RemoveAllFrom(ctx context.Context, artifactID int64) error {
})
return err
}
func (m *manager) RemoveFromAllArtifacts(ctx context.Context, labelID int64) error {
_, err := m.dao.DeleteReferences(ctx, &q.Query{
Keywords: map[string]interface{}{
"LabelID": labelID,
},
})
return err
}

View File

@ -118,6 +118,12 @@ func (m *managerTestSuite) TestRemoveAllFrom() {
m.Require().Nil(err)
}
func (m *managerTestSuite) TestRemoveFromAllArtifacts() {
m.dao.On("DeleteReferences").Return(2, nil)
err := m.mgr.RemoveFromAllArtifacts(nil, 1)
m.Require().Nil(err)
}
func TestManager(t *testing.T) {
suite.Run(t, &managerTestSuite{})
}

View File

@ -62,3 +62,9 @@ func (f *FakeManager) RemoveAllFrom(ctx context.Context, artifactID int64) error
args := f.Called()
return args.Error(0)
}
// RemoveFromAllArtifacts ...
func (f *FakeManager) RemoveFromAllArtifacts(ctx context.Context, labelID int64) error {
args := f.Called()
return args.Error(0)
}