Skip to content

Commit 7b04535

Browse files
committed
Merge remote-tracking branch 'upstream/release/v1.17' into codeberg-1.17
2 parents 90cf86e + 113d13a commit 7b04535

File tree

5 files changed

+62
-21
lines changed

5 files changed

+62
-21
lines changed

integrations/api_packages_container_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,23 @@ func TestPackageContainer(t *testing.T) {
275275
}
276276
}
277277

278-
// Overwrite existing tag
278+
req = NewRequest(t, "GET", fmt.Sprintf("%s/manifests/%s", url, tag))
279+
addTokenAuthHeader(req, userToken)
280+
MakeRequest(t, req, http.StatusOK)
281+
282+
pv, err = packages_model.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, image, tag)
283+
assert.NoError(t, err)
284+
assert.EqualValues(t, 1, pv.DownloadCount)
285+
286+
// Overwrite existing tag should keep the download count
279287
req = NewRequestWithBody(t, "PUT", fmt.Sprintf("%s/manifests/%s", url, tag), strings.NewReader(manifestContent))
280288
addTokenAuthHeader(req, userToken)
281289
req.Header.Set("Content-Type", oci.MediaTypeDockerManifest)
282290
MakeRequest(t, req, http.StatusCreated)
291+
292+
pv, err = packages_model.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, image, tag)
293+
assert.NoError(t, err)
294+
assert.EqualValues(t, 1, pv.DownloadCount)
283295
})
284296

285297
t.Run("HeadManifest", func(t *testing.T) {

models/migrations/v220.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ import (
1212
"xorm.io/xorm/schemas"
1313
)
1414

15-
func addContainerRepositoryProperty(x *xorm.Engine) error {
15+
func addContainerRepositoryProperty(x *xorm.Engine) (err error) {
1616
switch x.Dialect().URI().DBType {
1717
case schemas.SQLITE:
18-
_, 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)
19-
if err != nil {
20-
return err
21-
}
18+
_, 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 = ?",
19+
packages_model.PropertyTypePackage, container_module.PropertyRepository, packages_model.TypeContainer)
20+
case schemas.MSSQL:
21+
_, 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 = ?",
22+
packages_model.PropertyTypePackage, container_module.PropertyRepository, packages_model.TypeContainer)
2223
default:
23-
_, 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)
24-
if err != nil {
25-
return err
26-
}
24+
_, 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 = ?",
25+
packages_model.PropertyTypePackage, container_module.PropertyRepository, packages_model.TypeContainer)
2726
}
28-
return nil
27+
return err
2928
}

modules/util/sec_to_time.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@ import (
1616
// 1563418 -> 2 weeks 4 days
1717
// 3937125s -> 1 month 2 weeks
1818
// 45677465s -> 1 year 6 months
19+
//
20+
// Magic numbers:
21+
// 3600 = 60 * 60 (amount of seconds in a hour)
22+
// 86400 = 60 * 60 * 24 (amount of seconds in a day)
1923
func SecToTime(duration int64) string {
2024
formattedTime := ""
21-
years := duration / (3600 * 24 * 7 * 4 * 12)
22-
months := (duration / (3600 * 24 * 30)) % 12
23-
weeks := (duration / (3600 * 24 * 7)) % 4
24-
days := (duration / (3600 * 24)) % 7
25+
26+
// The following four variables are calculated by taking
27+
// into account the previously calculated variables, this avoids
28+
// pitfalls when using remainders. As that could lead to incorrect
29+
// results when the calculated number equals the quotient number.
30+
remainingDays := duration / (60 * 60 * 24)
31+
years := remainingDays / 365
32+
remainingDays -= years * 365
33+
months := remainingDays * 12 / 365
34+
remainingDays -= months * 365 / 12
35+
weeks := remainingDays / 7
36+
remainingDays -= weeks * 7
37+
days := remainingDays
38+
39+
// The following three variables are calculated without depending
40+
// on the previous calculated variables.
2541
hours := (duration / 3600) % 24
2642
minutes := (duration / 60) % 60
2743
seconds := duration % 60

modules/util/sec_to_time_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,21 @@ import (
1111
)
1212

1313
func TestSecToTime(t *testing.T) {
14-
assert.Equal(t, SecToTime(66), "1 minute 6 seconds")
15-
assert.Equal(t, SecToTime(52410), "14 hours 33 minutes")
16-
assert.Equal(t, SecToTime(563418), "6 days 12 hours")
17-
assert.Equal(t, SecToTime(1563418), "2 weeks 4 days")
18-
assert.Equal(t, SecToTime(3937125), "1 month 2 weeks")
19-
assert.Equal(t, SecToTime(45677465), "1 year 5 months")
14+
second := int64(1)
15+
minute := 60 * second
16+
hour := 60 * minute
17+
day := 24 * hour
18+
year := 365 * day
19+
20+
assert.Equal(t, "1 minute 6 seconds", SecToTime(minute+6*second))
21+
assert.Equal(t, "1 hour", SecToTime(hour))
22+
assert.Equal(t, "1 hour", SecToTime(hour+second))
23+
assert.Equal(t, "14 hours 33 minutes", SecToTime(14*hour+33*minute+30*second))
24+
assert.Equal(t, "6 days 12 hours", SecToTime(6*day+12*hour+30*minute+18*second))
25+
assert.Equal(t, "2 weeks 4 days", SecToTime((2*7+4)*day+2*hour+16*minute+58*second))
26+
assert.Equal(t, "4 weeks", SecToTime(4*7*day))
27+
assert.Equal(t, "4 weeks 1 day", SecToTime((4*7+1)*day))
28+
assert.Equal(t, "1 month 2 weeks", SecToTime((6*7+3)*day+13*hour+38*minute+45*second))
29+
assert.Equal(t, "11 months", SecToTime(year-25*day))
30+
assert.Equal(t, "1 year 5 months", SecToTime(year+163*day+10*hour+11*minute+5*second))
2031
}

routers/api/packages/container/manifest.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ func createPackageAndVersion(ctx context.Context, mci *manifestCreationInfo, met
312312
return nil, err
313313
}
314314

315+
// keep download count on overwrite
316+
_pv.DownloadCount = pv.DownloadCount
317+
315318
if pv, err = packages_model.GetOrInsertVersion(ctx, _pv); err != nil {
316319
log.Error("Error inserting package: %v", err)
317320
return nil, err

0 commit comments

Comments
 (0)