summaryrefslogtreecommitdiffstats
path: root/models/packages
diff options
context:
space:
mode:
authorRobert Wolff <mahlzahn@posteo.de>2024-07-31 12:40:24 +0200
committerRobert Wolff <mahlzahn@posteo.de>2024-07-31 12:40:24 +0200
commit994bd93e69056943571384a2c47ca51bdefc702f (patch)
tree070f2b61336e3f4865526c43c9c4eba1fd181e80 /models/packages
parentMerge pull request '[UI] Fix admin layout' (#4754) from gusted/forgejo-fix-ad... (diff)
downloadforgejo-994bd93e69056943571384a2c47ca51bdefc702f.tar.xz
forgejo-994bd93e69056943571384a2c47ca51bdefc702f.zip
feat(UI): add package counter to repo/user/org overview pages
- add package counter to repo/user/org overview pages - add go unit tests for repo/user has/count packages - add many more unit tests for packages model - fix error for non-existing packages in DeletePackageByID and SetRepositoryLink
Diffstat (limited to 'models/packages')
-rw-r--r--models/packages/package.go54
-rw-r--r--models/packages/package_test.go269
2 files changed, 304 insertions, 19 deletions
diff --git a/models/packages/package.go b/models/packages/package.go
index 50717c6951..84e2fa7ee7 100644
--- a/models/packages/package.go
+++ b/models/packages/package.go
@@ -1,4 +1,5 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
+// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package packages
@@ -12,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
+ "xorm.io/xorm"
)
func init() {
@@ -212,13 +214,19 @@ func TryInsertPackage(ctx context.Context, p *Package) (*Package, error) {
// DeletePackageByID deletes a package by id
func DeletePackageByID(ctx context.Context, packageID int64) error {
- _, err := db.GetEngine(ctx).ID(packageID).Delete(&Package{})
+ n, err := db.GetEngine(ctx).ID(packageID).Delete(&Package{})
+ if n == 0 && err == nil {
+ return ErrPackageNotExist
+ }
return err
}
// SetRepositoryLink sets the linked repository
func SetRepositoryLink(ctx context.Context, packageID, repoID int64) error {
- _, err := db.GetEngine(ctx).ID(packageID).Cols("repo_id").Update(&Package{RepoID: repoID})
+ n, err := db.GetEngine(ctx).ID(packageID).Cols("repo_id").Update(&Package{RepoID: repoID})
+ if n == 0 && err == nil {
+ return ErrPackageNotExist
+ }
return err
}
@@ -293,19 +301,45 @@ func FindUnreferencedPackages(ctx context.Context) ([]int64, error) {
return pIDs, nil
}
-// HasOwnerPackages tests if a user/org has accessible packages
-func HasOwnerPackages(ctx context.Context, ownerID int64) (bool, error) {
+func getPackages(ctx context.Context) *xorm.Session {
return db.GetEngine(ctx).
Table("package_version").
Join("INNER", "package", "package.id = package_version.package_id").
- Where(builder.Eq{
- "package_version.is_internal": false,
- "package.owner_id": ownerID,
- }).
- Exist(&PackageVersion{})
+ Where("package_version.is_internal = ?", false)
+}
+
+func getOwnerPackages(ctx context.Context, ownerID int64) *xorm.Session {
+ return getPackages(ctx).
+ Where("package.owner_id = ?", ownerID)
+}
+
+// HasOwnerPackages tests if a user/org has accessible packages
+func HasOwnerPackages(ctx context.Context, ownerID int64) (bool, error) {
+ return getOwnerPackages(ctx, ownerID).
+ Exist(&Package{})
+}
+
+// CountOwnerPackages counts user/org accessible packages
+func CountOwnerPackages(ctx context.Context, ownerID int64) (int64, error) {
+ return getOwnerPackages(ctx, ownerID).
+ Distinct("package.id").
+ Count(&Package{})
+}
+
+func getRepositoryPackages(ctx context.Context, repositoryID int64) *xorm.Session {
+ return getPackages(ctx).
+ Where("package.repo_id = ?", repositoryID)
}
// HasRepositoryPackages tests if a repository has packages
func HasRepositoryPackages(ctx context.Context, repositoryID int64) (bool, error) {
- return db.GetEngine(ctx).Where("repo_id = ?", repositoryID).Exist(&Package{})
+ return getRepositoryPackages(ctx, repositoryID).
+ Exist(&PackageVersion{})
+}
+
+// CountRepositoryPackages counts packages of a repository
+func CountRepositoryPackages(ctx context.Context, repositoryID int64) (int64, error) {
+ return getRepositoryPackages(ctx, repositoryID).
+ Distinct("package.id").
+ Count(&Package{})
}
diff --git a/models/packages/package_test.go b/models/packages/package_test.go
index 8ab7d31e00..1c96e08f0c 100644
--- a/models/packages/package_test.go
+++ b/models/packages/package_test.go
@@ -1,4 +1,5 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
+// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package packages_test
@@ -8,6 +9,7 @@ import (
"code.gitea.io/gitea/models/db"
packages_model "code.gitea.io/gitea/models/packages"
+ repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
@@ -15,7 +17,6 @@ import (
_ "code.gitea.io/gitea/models/actions"
_ "code.gitea.io/gitea/models/activities"
- "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -23,21 +24,185 @@ func TestMain(m *testing.M) {
unittest.MainTest(m)
}
-func TestHasOwnerPackages(t *testing.T) {
+func prepareExamplePackage(t *testing.T) *packages_model.Package {
+ require.NoError(t, unittest.PrepareTestDatabase())
+
+ owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
+
+ p0 := &packages_model.Package{
+ OwnerID: owner.ID,
+ RepoID: repo.ID,
+ LowerName: "package",
+ Type: packages_model.TypeGeneric,
+ }
+
+ p, err := packages_model.TryInsertPackage(db.DefaultContext, p0)
+ require.NotNil(t, p)
+ require.NoError(t, err)
+ require.Equal(t, *p0, *p)
+ return p
+}
+
+func deletePackage(t *testing.T, p *packages_model.Package) {
+ err := packages_model.DeletePackageByID(db.DefaultContext, p.ID)
+ require.NoError(t, err)
+}
+
+func TestTryInsertPackage(t *testing.T) {
+ require.NoError(t, unittest.PrepareTestDatabase())
+
+ owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
+
+ p0 := &packages_model.Package{
+ OwnerID: owner.ID,
+ LowerName: "package",
+ }
+
+ // Insert package should return the package and yield no error
+ p, err := packages_model.TryInsertPackage(db.DefaultContext, p0)
+ require.NotNil(t, p)
+ require.NoError(t, err)
+ require.Equal(t, *p0, *p)
+
+ // Insert same package again should return the same package and yield ErrDuplicatePackage
+ p, err = packages_model.TryInsertPackage(db.DefaultContext, p0)
+ require.NotNil(t, p)
+ require.IsType(t, packages_model.ErrDuplicatePackage, err)
+ require.Equal(t, *p0, *p)
+
+ err = packages_model.DeletePackageByID(db.DefaultContext, p0.ID)
+ require.NoError(t, err)
+}
+
+func TestGetPackageByID(t *testing.T) {
+ p0 := prepareExamplePackage(t)
+
+ // Get package should return package and yield no error
+ p, err := packages_model.GetPackageByID(db.DefaultContext, p0.ID)
+ require.NotNil(t, p)
+ require.Equal(t, *p0, *p)
+ require.NoError(t, err)
+
+ // Get package with non-existng ID should yield ErrPackageNotExist
+ p, err = packages_model.GetPackageByID(db.DefaultContext, 999)
+ require.Nil(t, p)
+ require.Error(t, err)
+ require.IsType(t, packages_model.ErrPackageNotExist, err)
+
+ deletePackage(t, p0)
+}
+
+func TestDeletePackageByID(t *testing.T) {
+ p0 := prepareExamplePackage(t)
+
+ // Delete existing package should yield no error
+ err := packages_model.DeletePackageByID(db.DefaultContext, p0.ID)
+ require.NoError(t, err)
+
+ // Delete (now) non-existing package should yield ErrPackageNotExist
+ err = packages_model.DeletePackageByID(db.DefaultContext, p0.ID)
+ require.Error(t, err)
+ require.IsType(t, packages_model.ErrPackageNotExist, err)
+}
+
+func TestSetRepositoryLink(t *testing.T) {
+ p0 := prepareExamplePackage(t)
+
+ // Set repository link to package should yield no error and package RepoID should be updated
+ err := packages_model.SetRepositoryLink(db.DefaultContext, p0.ID, 5)
+ require.NoError(t, err)
+
+ p, err := packages_model.GetPackageByID(db.DefaultContext, p0.ID)
+ require.NoError(t, err)
+ require.EqualValues(t, 5, p.RepoID)
+
+ // Set repository link to non-existing package should yied ErrPackageNotExist
+ err = packages_model.SetRepositoryLink(db.DefaultContext, 999, 5)
+ require.Error(t, err)
+ require.IsType(t, packages_model.ErrPackageNotExist, err)
+
+ deletePackage(t, p0)
+}
+
+func TestUnlinkRepositoryFromAllPackages(t *testing.T) {
+ p0 := prepareExamplePackage(t)
+
+ // Unlink repository from all packages should yield no error and package with p0.ID should have RepoID 0
+ err := packages_model.UnlinkRepositoryFromAllPackages(db.DefaultContext, p0.RepoID)
+ require.NoError(t, err)
+
+ p, err := packages_model.GetPackageByID(db.DefaultContext, p0.ID)
+ require.NoError(t, err)
+ require.EqualValues(t, 0, p.RepoID)
+
+ // Unlink repository again from all packages should also yield no error
+ err = packages_model.UnlinkRepositoryFromAllPackages(db.DefaultContext, p0.RepoID)
+ require.NoError(t, err)
+
+ deletePackage(t, p0)
+}
+
+func TestGetPackageByName(t *testing.T) {
+ p0 := prepareExamplePackage(t)
+
+ // Get package should return package and yield no error
+ p, err := packages_model.GetPackageByName(db.DefaultContext, p0.OwnerID, p0.Type, p0.LowerName)
+ require.NotNil(t, p)
+ require.Equal(t, *p0, *p)
+ require.NoError(t, err)
+
+ // Get package with uppercase name should return package and yield no error
+ p, err = packages_model.GetPackageByName(db.DefaultContext, p0.OwnerID, p0.Type, "Package")
+ require.NotNil(t, p)
+ require.Equal(t, *p0, *p)
+ require.NoError(t, err)
+
+ // Get package with wrong owner ID, type or name should return no package and yield ErrPackageNotExist
+ p, err = packages_model.GetPackageByName(db.DefaultContext, 999, p0.Type, p0.LowerName)
+ require.Nil(t, p)
+ require.Error(t, err)
+ require.IsType(t, packages_model.ErrPackageNotExist, err)
+ p, err = packages_model.GetPackageByName(db.DefaultContext, p0.OwnerID, packages_model.TypeDebian, p0.LowerName)
+ require.Nil(t, p)
+ require.Error(t, err)
+ require.IsType(t, packages_model.ErrPackageNotExist, err)
+ p, err = packages_model.GetPackageByName(db.DefaultContext, p0.OwnerID, p0.Type, "package1")
+ require.Nil(t, p)
+ require.Error(t, err)
+ require.IsType(t, packages_model.ErrPackageNotExist, err)
+
+ deletePackage(t, p0)
+}
+
+func TestHasCountPackages(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
p, err := packages_model.TryInsertPackage(db.DefaultContext, &packages_model.Package{
OwnerID: owner.ID,
+ RepoID: repo.ID,
LowerName: "package",
})
- assert.NotNil(t, p)
+ require.NotNil(t, p)
require.NoError(t, err)
- // A package without package versions gets automatically cleaned up and should return false
+ // A package without package versions gets automatically cleaned up and should return false for owner
has, err := packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
- assert.False(t, has)
+ require.False(t, has)
+ require.NoError(t, err)
+ count, err := packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
+ require.EqualValues(t, 0, count)
+ require.NoError(t, err)
+
+ // A package without package versions gets automatically cleaned up and should return false for repository
+ has, err = packages_model.HasRepositoryPackages(db.DefaultContext, repo.ID)
+ require.False(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountRepositoryPackages(db.DefaultContext, repo.ID)
+ require.EqualValues(t, 0, count)
require.NoError(t, err)
pv, err := packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
@@ -45,12 +210,21 @@ func TestHasOwnerPackages(t *testing.T) {
LowerVersion: "internal",
IsInternal: true,
})
- assert.NotNil(t, pv)
+ require.NotNil(t, pv)
require.NoError(t, err)
// A package with an internal package version gets automatically cleaned up and should return false
has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
- assert.False(t, has)
+ require.False(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
+ require.EqualValues(t, 0, count)
+ require.NoError(t, err)
+ has, err = packages_model.HasRepositoryPackages(db.DefaultContext, repo.ID)
+ require.False(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountRepositoryPackages(db.DefaultContext, repo.ID)
+ require.EqualValues(t, 0, count)
require.NoError(t, err)
pv, err = packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
@@ -58,11 +232,88 @@ func TestHasOwnerPackages(t *testing.T) {
LowerVersion: "normal",
IsInternal: false,
})
- assert.NotNil(t, pv)
+ require.NotNil(t, pv)
require.NoError(t, err)
// A package with a normal package version should return true
has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
- assert.True(t, has)
+ require.True(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
+ require.EqualValues(t, 1, count)
+ require.NoError(t, err)
+ has, err = packages_model.HasRepositoryPackages(db.DefaultContext, repo.ID)
+ require.True(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountRepositoryPackages(db.DefaultContext, repo.ID)
+ require.EqualValues(t, 1, count)
+ require.NoError(t, err)
+
+ pv2, err := packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
+ PackageID: p.ID,
+ LowerVersion: "normal2",
+ IsInternal: false,
+ })
+ require.NotNil(t, pv2)
+ require.NoError(t, err)
+
+ // A package withmultiple package versions should be counted only once
+ has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
+ require.True(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
+ require.EqualValues(t, 1, count)
+ require.NoError(t, err)
+ has, err = packages_model.HasRepositoryPackages(db.DefaultContext, repo.ID)
+ require.True(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountRepositoryPackages(db.DefaultContext, repo.ID)
+ require.EqualValues(t, 1, count)
+ require.NoError(t, err)
+
+ // For owner ID 0 there should be no packages
+ has, err = packages_model.HasOwnerPackages(db.DefaultContext, 0)
+ require.False(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountOwnerPackages(db.DefaultContext, 0)
+ require.EqualValues(t, 0, count)
+ require.NoError(t, err)
+
+ // For repo ID 0 there should be no packages
+ has, err = packages_model.HasRepositoryPackages(db.DefaultContext, 0)
+ require.False(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountRepositoryPackages(db.DefaultContext, 0)
+ require.EqualValues(t, 0, count)
+ require.NoError(t, err)
+
+ p1, err := packages_model.TryInsertPackage(db.DefaultContext, &packages_model.Package{
+ OwnerID: owner.ID,
+ LowerName: "package0",
+ })
+ require.NotNil(t, p1)
+ require.NoError(t, err)
+ p1v, err := packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
+ PackageID: p1.ID,
+ LowerVersion: "normal",
+ IsInternal: false,
+ })
+ require.NotNil(t, p1v)
+ require.NoError(t, err)
+
+ // Owner owner.ID should have two packages now
+ has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
+ require.True(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountOwnerPackages(db.DefaultContext, owner.ID)
+ require.EqualValues(t, 2, count)
+ require.NoError(t, err)
+
+ // For repo ID 0 there should be now one package, because p1 is not assigned to a repo
+ has, err = packages_model.HasRepositoryPackages(db.DefaultContext, 0)
+ require.True(t, has)
+ require.NoError(t, err)
+ count, err = packages_model.CountRepositoryPackages(db.DefaultContext, 0)
+ require.EqualValues(t, 1, count)
require.NoError(t, err)
}