diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2022-07-28 05:59:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-28 11:59:39 +0800 |
commit | 86e5268c396bd89716b2617a4949837982c1b0c3 (patch) | |
tree | a01a04c069644694470c3159ce3daed94436ef08 /services | |
parent | 4604048010347ea946ae57628d694a631787ab17 (diff) | |
download | gitea-86e5268c396bd89716b2617a4949837982c1b0c3.tar.gz gitea-86e5268c396bd89716b2617a4949837982c1b0c3.zip |
Add Docker /v2/_catalog endpoint (#20469)
* Added properties for packages.
* Fixed authenticate header format.
* Added _catalog endpoint.
* Check owner visibility.
* Extracted condition.
* Added test for _catalog.
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'services')
-rw-r--r-- | services/packages/container/cleanup.go | 25 | ||||
-rw-r--r-- | services/packages/packages.go | 46 |
2 files changed, 59 insertions, 12 deletions
diff --git a/services/packages/container/cleanup.go b/services/packages/container/cleanup.go index 3e44f9aa1a..d23a481f27 100644 --- a/services/packages/container/cleanup.go +++ b/services/packages/container/cleanup.go @@ -6,10 +6,13 @@ package container import ( "context" + "strings" "time" packages_model "code.gitea.io/gitea/models/packages" container_model "code.gitea.io/gitea/models/packages/container" + user_model "code.gitea.io/gitea/models/user" + container_module "code.gitea.io/gitea/modules/packages/container" "code.gitea.io/gitea/modules/util" ) @@ -78,3 +81,25 @@ func cleanupExpiredUploadedBlobs(ctx context.Context, olderThan time.Duration) e return nil } + +// UpdateRepositoryNames updates the repository name property for all packages of the specific owner +func UpdateRepositoryNames(ctx context.Context, owner *user_model.User, newOwnerName string) error { + ps, err := packages_model.GetPackagesByType(ctx, owner.ID, packages_model.TypeContainer) + if err != nil { + return err + } + + newOwnerName = strings.ToLower(newOwnerName) + + for _, p := range ps { + if err := packages_model.DeletePropertyByName(ctx, packages_model.PropertyTypePackage, p.ID, container_module.PropertyRepository); err != nil { + return err + } + + if _, err := packages_model.InsertProperty(ctx, packages_model.PropertyTypePackage, p.ID, container_module.PropertyRepository, newOwnerName+"/"+p.LowerName); err != nil { + return err + } + } + + return nil +} diff --git a/services/packages/packages.go b/services/packages/packages.go index aa1796e8b3..975c5ddd35 100644 --- a/services/packages/packages.go +++ b/services/packages/packages.go @@ -34,10 +34,11 @@ type PackageInfo struct { // PackageCreationInfo describes a package to create type PackageCreationInfo struct { PackageInfo - SemverCompatible bool - Creator *user_model.User - Metadata interface{} - Properties map[string]string + SemverCompatible bool + Creator *user_model.User + Metadata interface{} + PackageProperties map[string]string + VersionProperties map[string]string } // PackageFileInfo describes a package file @@ -110,8 +111,9 @@ func createPackageAndAddFile(pvci *PackageCreationInfo, pfci *PackageFileCreatio } func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, allowDuplicate bool) (*packages_model.PackageVersion, bool, error) { - log.Trace("Creating package: %v, %v, %v, %s, %s, %+v, %v", pvci.Creator.ID, pvci.Owner.ID, pvci.PackageType, pvci.Name, pvci.Version, pvci.Properties, allowDuplicate) + log.Trace("Creating package: %v, %v, %v, %s, %s, %+v, %+v, %v", pvci.Creator.ID, pvci.Owner.ID, pvci.PackageType, pvci.Name, pvci.Version, pvci.PackageProperties, pvci.VersionProperties, allowDuplicate) + packageCreated := true p := &packages_model.Package{ OwnerID: pvci.Owner.ID, Type: pvci.PackageType, @@ -121,18 +123,29 @@ func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, all } var err error if p, err = packages_model.TryInsertPackage(ctx, p); err != nil { - if err != packages_model.ErrDuplicatePackage { + if err == packages_model.ErrDuplicatePackage { + packageCreated = false + } else { log.Error("Error inserting package: %v", err) return nil, false, err } } + if packageCreated { + for name, value := range pvci.PackageProperties { + if _, err := packages_model.InsertProperty(ctx, packages_model.PropertyTypePackage, p.ID, name, value); err != nil { + log.Error("Error setting package property: %v", err) + return nil, false, err + } + } + } + metadataJSON, err := json.Marshal(pvci.Metadata) if err != nil { return nil, false, err } - created := true + versionCreated := true pv := &packages_model.PackageVersion{ PackageID: p.ID, CreatorID: pvci.Creator.ID, @@ -142,7 +155,7 @@ func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, all } if pv, err = packages_model.GetOrInsertVersion(ctx, pv); err != nil { if err == packages_model.ErrDuplicatePackageVersion { - created = false + versionCreated = false } if err != packages_model.ErrDuplicatePackageVersion || !allowDuplicate { log.Error("Error inserting package: %v", err) @@ -150,8 +163,8 @@ func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, all } } - if created { - for name, value := range pvci.Properties { + if versionCreated { + for name, value := range pvci.VersionProperties { if _, err := packages_model.InsertProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, name, value); err != nil { log.Error("Error setting package version property: %v", err) return nil, false, err @@ -159,7 +172,7 @@ func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, all } } - return pv, created, nil + return pv, versionCreated, nil } // AddFileToExistingPackage adds a file to an existing package. If the package does not exist, ErrPackageNotExist is returned @@ -350,9 +363,18 @@ func Cleanup(unused context.Context, olderThan time.Duration) error { return err } - if err := packages_model.DeletePackagesIfUnreferenced(ctx); err != nil { + ps, err := packages_model.FindUnreferencedPackages(ctx) + if err != nil { return err } + for _, p := range ps { + if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypePackage, p.ID); err != nil { + return err + } + if err := packages_model.DeletePackageByID(ctx, p.ID); err != nil { + return err + } + } pbs, err := packages_model.FindExpiredUnreferencedBlobs(ctx, olderThan) if err != nil { |