Skip to content

Commit

Permalink
Keep download count on Container tag overwrite (go-gitea#20728)
Browse files Browse the repository at this point in the history
Co-authored-by: 6543 <6543@obermui.de>
  • Loading branch information
KN4CK3R and 6543 committed Aug 9, 2022
1 parent 9ec1c88 commit b590869
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 13 additions & 1 deletion integrations/api_packages_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,23 @@ func TestPackageContainer(t *testing.T) {
}
}

// Overwrite existing tag
req = NewRequest(t, "GET", fmt.Sprintf("%s/manifests/%s", url, tag))
addTokenAuthHeader(req, userToken)
MakeRequest(t, req, http.StatusOK)

pv, err = packages_model.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, image, tag)
assert.NoError(t, err)
assert.EqualValues(t, 1, pv.DownloadCount)

// Overwrite existing tag should keep the download count
req = NewRequestWithBody(t, "PUT", fmt.Sprintf("%s/manifests/%s", url, tag), strings.NewReader(manifestContent))
addTokenAuthHeader(req, userToken)
req.Header.Set("Content-Type", oci.MediaTypeDockerManifest)
MakeRequest(t, req, http.StatusCreated)

pv, err = packages_model.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, image, tag)
assert.NoError(t, err)
assert.EqualValues(t, 1, pv.DownloadCount)
})

t.Run("HeadManifest", func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions routers/api/packages/container/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ func createPackageAndVersion(ctx context.Context, mci *manifestCreationInfo, met
return nil, err
}

// keep download count on overwrite
_pv.DownloadCount = pv.DownloadCount

if pv, err = packages_model.GetOrInsertVersion(ctx, _pv); err != nil {
log.Error("Error inserting package: %v", err)
return nil, err
Expand Down

0 comments on commit b590869

Please sign in to comment.