fix: check the existence of the tag before updating pull time

Signed-off-by: chlins <chenyuzh@vmware.com>
This commit is contained in:
chlins 2022-03-11 09:40:53 +08:00
parent 6673841526
commit 4f2cd939ee
2 changed files with 28 additions and 13 deletions

View File

@ -19,10 +19,11 @@ import (
"context"
stderrors "errors"
"fmt"
accessorymodel "github.com/goharbor/harbor/src/pkg/accessory/model"
"strings"
"time"
accessorymodel "github.com/goharbor/harbor/src/pkg/accessory/model"
"github.com/goharbor/harbor/src/controller/artifact/processor/chart"
"github.com/goharbor/harbor/src/controller/artifact/processor/cnab"
"github.com/goharbor/harbor/src/controller/artifact/processor/image"
@ -529,19 +530,24 @@ func (c *controller) UpdatePullTime(ctx context.Context, artifactID int64, tagID
if err := c.artMgr.UpdatePullTime(ctx, artifactID, time); err != nil {
return err
}
tg, err := c.tagCtl.Get(ctx, tagID, nil)
if err != nil {
return err
// update tag pull time if artifact has tag
if tagID != 0 {
tg, err := c.tagCtl.Get(ctx, tagID, nil)
if err != nil {
return err
}
if tg.ArtifactID != artifactID {
return fmt.Errorf("tag %d isn't attached to artifact %d", tagID, artifactID)
}
return c.tagCtl.Update(ctx, &tag.Tag{
Tag: model_tag.Tag{
ID: tg.ID,
PullTime: time,
},
}, "PullTime")
}
if tg.ArtifactID != artifactID {
return fmt.Errorf("tag %d isn't attached to artifact %d", tagID, artifactID)
}
return c.tagCtl.Update(ctx, &tag.Tag{
Tag: model_tag.Tag{
ID: tg.ID,
PullTime: time,
},
}, "PullTime")
return nil
}
func (c *controller) GetAddition(ctx context.Context, artifactID int64, addition string) (*processor.Addition, error) {

View File

@ -598,6 +598,15 @@ func (c *controllerTestSuite) TestUpdatePullTime() {
c.Require().NotNil(err)
c.tagCtl.AssertExpectations(c.T())
// if no tag, should not update tag
c.SetupTest()
c.tagCtl.On("Update").Return(nil)
c.artMgr.On("UpdatePullTime", mock.Anything, mock.Anything, mock.Anything).Return(nil)
err = c.ctl.UpdatePullTime(nil, 1, 0, time.Now())
c.Require().Nil(err)
c.artMgr.AssertExpectations(c.T())
// should not call tag Update
c.tagCtl.AssertNotCalled(c.T(), "Update")
}
func (c *controllerTestSuite) TestGetAddition() {