Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/release/v1.17' into codeberg-1.17
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Aug 9, 2022
2 parents 90cf86e + 113d13a commit 7b04535
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
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
19 changes: 9 additions & 10 deletions models/migrations/v220.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ import (
"xorm.io/xorm/schemas"
)

func addContainerRepositoryProperty(x *xorm.Engine) error {
func addContainerRepositoryProperty(x *xorm.Engine) (err error) {
switch x.Dialect().URI().DBType {
case schemas.SQLITE:
_, err := x.Exec("INSERT INTO package_property (ref_type, ref_id, name, value) SELECT ?, p.id, ?, u.lower_name || '/' || p.lower_name FROM package p JOIN `user` u ON p.owner_id = u.id WHERE p.type = ?", packages_model.PropertyTypePackage, container_module.PropertyRepository, packages_model.TypeContainer)
if err != nil {
return err
}
_, err = x.Exec("INSERT INTO package_property (ref_type, ref_id, name, value) SELECT ?, p.id, ?, u.lower_name || '/' || p.lower_name FROM package p JOIN `user` u ON p.owner_id = u.id WHERE p.type = ?",
packages_model.PropertyTypePackage, container_module.PropertyRepository, packages_model.TypeContainer)
case schemas.MSSQL:
_, err = x.Exec("INSERT INTO package_property (ref_type, ref_id, name, value) SELECT ?, p.id, ?, u.lower_name + '/' + p.lower_name FROM package p JOIN `user` u ON p.owner_id = u.id WHERE p.type = ?",
packages_model.PropertyTypePackage, container_module.PropertyRepository, packages_model.TypeContainer)
default:
_, err := x.Exec("INSERT INTO package_property (ref_type, ref_id, name, value) SELECT ?, p.id, ?, CONCAT(u.lower_name, '/', p.lower_name) FROM package p JOIN `user` u ON p.owner_id = u.id WHERE p.type = ?", packages_model.PropertyTypePackage, container_module.PropertyRepository, packages_model.TypeContainer)
if err != nil {
return err
}
_, err = x.Exec("INSERT INTO package_property (ref_type, ref_id, name, value) SELECT ?, p.id, ?, CONCAT(u.lower_name, '/', p.lower_name) FROM package p JOIN `user` u ON p.owner_id = u.id WHERE p.type = ?",
packages_model.PropertyTypePackage, container_module.PropertyRepository, packages_model.TypeContainer)
}
return nil
return err
}
24 changes: 20 additions & 4 deletions modules/util/sec_to_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@ import (
// 1563418 -> 2 weeks 4 days
// 3937125s -> 1 month 2 weeks
// 45677465s -> 1 year 6 months
//
// Magic numbers:
// 3600 = 60 * 60 (amount of seconds in a hour)
// 86400 = 60 * 60 * 24 (amount of seconds in a day)
func SecToTime(duration int64) string {
formattedTime := ""
years := duration / (3600 * 24 * 7 * 4 * 12)
months := (duration / (3600 * 24 * 30)) % 12
weeks := (duration / (3600 * 24 * 7)) % 4
days := (duration / (3600 * 24)) % 7

// The following four variables are calculated by taking
// into account the previously calculated variables, this avoids
// pitfalls when using remainders. As that could lead to incorrect
// results when the calculated number equals the quotient number.
remainingDays := duration / (60 * 60 * 24)
years := remainingDays / 365
remainingDays -= years * 365
months := remainingDays * 12 / 365
remainingDays -= months * 365 / 12
weeks := remainingDays / 7
remainingDays -= weeks * 7
days := remainingDays

// The following three variables are calculated without depending
// on the previous calculated variables.
hours := (duration / 3600) % 24
minutes := (duration / 60) % 60
seconds := duration % 60
Expand Down
23 changes: 17 additions & 6 deletions modules/util/sec_to_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ import (
)

func TestSecToTime(t *testing.T) {
assert.Equal(t, SecToTime(66), "1 minute 6 seconds")
assert.Equal(t, SecToTime(52410), "14 hours 33 minutes")
assert.Equal(t, SecToTime(563418), "6 days 12 hours")
assert.Equal(t, SecToTime(1563418), "2 weeks 4 days")
assert.Equal(t, SecToTime(3937125), "1 month 2 weeks")
assert.Equal(t, SecToTime(45677465), "1 year 5 months")
second := int64(1)
minute := 60 * second
hour := 60 * minute
day := 24 * hour
year := 365 * day

assert.Equal(t, "1 minute 6 seconds", SecToTime(minute+6*second))
assert.Equal(t, "1 hour", SecToTime(hour))
assert.Equal(t, "1 hour", SecToTime(hour+second))
assert.Equal(t, "14 hours 33 minutes", SecToTime(14*hour+33*minute+30*second))
assert.Equal(t, "6 days 12 hours", SecToTime(6*day+12*hour+30*minute+18*second))
assert.Equal(t, "2 weeks 4 days", SecToTime((2*7+4)*day+2*hour+16*minute+58*second))
assert.Equal(t, "4 weeks", SecToTime(4*7*day))
assert.Equal(t, "4 weeks 1 day", SecToTime((4*7+1)*day))
assert.Equal(t, "1 month 2 weeks", SecToTime((6*7+3)*day+13*hour+38*minute+45*second))
assert.Equal(t, "11 months", SecToTime(year-25*day))
assert.Equal(t, "1 year 5 months", SecToTime(year+163*day+10*hour+11*minute+5*second))
}
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 7b04535

Please sign in to comment.