mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-20 23:57:42 +01:00
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:
parent
f5d482854b
commit
06f7b7f763
@ -24,6 +24,8 @@ import (
|
|||||||
"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/common/rbac"
|
"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
|
// 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))
|
l.SendInternalServerError(fmt.Errorf("failed to delete resource label mappings of label %d: %v", id, err))
|
||||||
return
|
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 {
|
if err := dao.DeleteLabel(id); err != nil {
|
||||||
l.SendInternalServerError(fmt.Errorf("failed to delete label %d: %v", id, err))
|
l.SendInternalServerError(fmt.Errorf("failed to delete label %d: %v", id, err))
|
||||||
return
|
return
|
||||||
|
@ -37,6 +37,8 @@ type Manager interface {
|
|||||||
RemoveFrom(ctx context.Context, labelID int64, artifactID int64) (err error)
|
RemoveFrom(ctx context.Context, labelID int64, artifactID int64) (err error)
|
||||||
// Remove all labels added to the artifact specified by the ID
|
// Remove all labels added to the artifact specified by the ID
|
||||||
RemoveAllFrom(ctx context.Context, artifactID int64) (err error)
|
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
|
// 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
|
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
|
||||||
|
}
|
||||||
|
@ -118,6 +118,12 @@ func (m *managerTestSuite) TestRemoveAllFrom() {
|
|||||||
m.Require().Nil(err)
|
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) {
|
func TestManager(t *testing.T) {
|
||||||
suite.Run(t, &managerTestSuite{})
|
suite.Run(t, &managerTestSuite{})
|
||||||
}
|
}
|
||||||
|
@ -62,3 +62,9 @@ func (f *FakeManager) RemoveAllFrom(ctx context.Context, artifactID int64) error
|
|||||||
args := f.Called()
|
args := f.Called()
|
||||||
return args.Error(0)
|
return args.Error(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveFromAllArtifacts ...
|
||||||
|
func (f *FakeManager) RemoveFromAllArtifacts(ctx context.Context, labelID int64) error {
|
||||||
|
args := f.Called()
|
||||||
|
return args.Error(0)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user