aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api/packages
diff options
context:
space:
mode:
authordelvh <dev.lh@web.de>2023-07-24 05:47:27 +0200
committerGitHub <noreply@github.com>2023-07-23 23:47:27 -0400
commitf3d41c61eb0e66aacc269508d27716425673509b (patch)
tree37990d8fbe3c7e8bac6208cde513e5bed5126232 /routers/api/packages
parent24c3bb95acfd906dc752f867615dafd170176502 (diff)
downloadgitea-f3d41c61eb0e66aacc269508d27716425673509b.tar.gz
gitea-f3d41c61eb0e66aacc269508d27716425673509b.zip
Remove `db.DefaultContext` in `routers/` and `cmd/` (#26076)
Now, the only remaining usages of `models.db.DefaultContext` are in - `modules` - `models` - `services`
Diffstat (limited to 'routers/api/packages')
-rw-r--r--routers/api/packages/composer/composer.go2
-rw-r--r--routers/api/packages/conan/conan.go96
-rw-r--r--routers/api/packages/container/blob.go20
-rw-r--r--routers/api/packages/container/container.go10
-rw-r--r--routers/api/packages/container/manifest.go24
-rw-r--r--routers/api/packages/npm/npm.go68
6 files changed, 106 insertions, 114 deletions
diff --git a/routers/api/packages/composer/composer.go b/routers/api/packages/composer/composer.go
index bf5bda743f..75bbfdf4d3 100644
--- a/routers/api/packages/composer/composer.go
+++ b/routers/api/packages/composer/composer.go
@@ -114,7 +114,7 @@ func SearchPackages(ctx *context.Context) {
// EnumeratePackages lists all package names
// https://packagist.org/apidoc#list-packages
func EnumeratePackages(ctx *context.Context) {
- ps, err := packages_model.GetPackagesByType(db.DefaultContext, ctx.Package.Owner.ID, packages_model.TypeComposer)
+ ps, err := packages_model.GetPackagesByType(ctx, ctx.Package.Owner.ID, packages_model.TypeComposer)
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
diff --git a/routers/api/packages/conan/conan.go b/routers/api/packages/conan/conan.go
index 6e1727fd76..d7349a84b2 100644
--- a/routers/api/packages/conan/conan.go
+++ b/routers/api/packages/conan/conan.go
@@ -4,6 +4,7 @@
package conan
import (
+ std_ctx "context"
"fmt"
"io"
"net/http"
@@ -602,67 +603,62 @@ func DeletePackageV2(ctx *context.Context) {
}
func deleteRecipeOrPackage(apictx *context.Context, rref *conan_module.RecipeReference, ignoreRecipeRevision bool, pref *conan_module.PackageReference, ignorePackageRevision bool) error {
- ctx, committer, err := db.TxContext(db.DefaultContext)
- if err != nil {
- return err
- }
- defer committer.Close()
-
- pv, err := packages_model.GetVersionByNameAndVersion(ctx, apictx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version)
- if err != nil {
- return err
- }
+ var pd *packages_model.PackageDescriptor
+ versionDeleted := false
- pd, err := packages_model.GetPackageDescriptor(ctx, pv)
- if err != nil {
- return err
- }
+ err := db.WithTx(apictx, func(ctx std_ctx.Context) error {
+ pv, err := packages_model.GetVersionByNameAndVersion(ctx, apictx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version)
+ if err != nil {
+ return err
+ }
- filter := map[string]string{
- conan_module.PropertyRecipeUser: rref.User,
- conan_module.PropertyRecipeChannel: rref.Channel,
- }
- if !ignoreRecipeRevision {
- filter[conan_module.PropertyRecipeRevision] = rref.RevisionOrDefault()
- }
- if pref != nil {
- filter[conan_module.PropertyPackageReference] = pref.Reference
- if !ignorePackageRevision {
- filter[conan_module.PropertyPackageRevision] = pref.RevisionOrDefault()
+ pd, err = packages_model.GetPackageDescriptor(ctx, pv)
+ if err != nil {
+ return err
}
- }
- pfs, _, err := packages_model.SearchFiles(ctx, &packages_model.PackageFileSearchOptions{
- VersionID: pv.ID,
- Properties: filter,
- })
- if err != nil {
- return err
- }
- if len(pfs) == 0 {
- return conan_model.ErrPackageReferenceNotExist
- }
+ filter := map[string]string{
+ conan_module.PropertyRecipeUser: rref.User,
+ conan_module.PropertyRecipeChannel: rref.Channel,
+ }
+ if !ignoreRecipeRevision {
+ filter[conan_module.PropertyRecipeRevision] = rref.RevisionOrDefault()
+ }
+ if pref != nil {
+ filter[conan_module.PropertyPackageReference] = pref.Reference
+ if !ignorePackageRevision {
+ filter[conan_module.PropertyPackageRevision] = pref.RevisionOrDefault()
+ }
+ }
- for _, pf := range pfs {
- if err := packages_service.DeletePackageFile(ctx, pf); err != nil {
+ pfs, _, err := packages_model.SearchFiles(ctx, &packages_model.PackageFileSearchOptions{
+ VersionID: pv.ID,
+ Properties: filter,
+ })
+ if err != nil {
return err
}
- }
-
- versionDeleted := false
- has, err := packages_model.HasVersionFileReferences(ctx, pv.ID)
- if err != nil {
- return err
- }
- if !has {
- versionDeleted = true
+ if len(pfs) == 0 {
+ return conan_model.ErrPackageReferenceNotExist
+ }
- if err := packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil {
+ for _, pf := range pfs {
+ if err := packages_service.DeletePackageFile(ctx, pf); err != nil {
+ return err
+ }
+ }
+ has, err := packages_model.HasVersionFileReferences(ctx, pv.ID)
+ if err != nil {
return err
}
- }
+ if !has {
+ versionDeleted = true
- if err := committer.Commit(); err != nil {
+ return packages_service.DeletePackageVersionAndReferences(ctx, pv)
+ }
+ return nil
+ })
+ if err != nil {
return err
}
diff --git a/routers/api/packages/container/blob.go b/routers/api/packages/container/blob.go
index c8e8dd0545..f2d63297c1 100644
--- a/routers/api/packages/container/blob.go
+++ b/routers/api/packages/container/blob.go
@@ -26,19 +26,19 @@ var uploadVersionMutex sync.Mutex
// saveAsPackageBlob creates a package blob from an upload
// The uploaded blob gets stored in a special upload version to link them to the package/image
-func saveAsPackageBlob(hsr packages_module.HashedSizeReader, pci *packages_service.PackageCreationInfo) (*packages_model.PackageBlob, error) {
+func saveAsPackageBlob(ctx context.Context, hsr packages_module.HashedSizeReader, pci *packages_service.PackageCreationInfo) (*packages_model.PackageBlob, error) {
pb := packages_service.NewPackageBlob(hsr)
exists := false
contentStore := packages_module.NewContentStore()
- uploadVersion, err := getOrCreateUploadVersion(&pci.PackageInfo)
+ uploadVersion, err := getOrCreateUploadVersion(ctx, &pci.PackageInfo)
if err != nil {
return nil, err
}
- err = db.WithTx(db.DefaultContext, func(ctx context.Context) error {
+ err = db.WithTx(ctx, func(ctx context.Context) error {
if err := packages_service.CheckSizeQuotaExceeded(ctx, pci.Creator, pci.Owner, packages_model.TypeContainer, hsr.Size()); err != nil {
return err
}
@@ -79,24 +79,24 @@ func saveAsPackageBlob(hsr packages_module.HashedSizeReader, pci *packages_servi
}
// mountBlob mounts the specific blob to a different package
-func mountBlob(pi *packages_service.PackageInfo, pb *packages_model.PackageBlob) error {
- uploadVersion, err := getOrCreateUploadVersion(pi)
+func mountBlob(ctx context.Context, pi *packages_service.PackageInfo, pb *packages_model.PackageBlob) error {
+ uploadVersion, err := getOrCreateUploadVersion(ctx, pi)
if err != nil {
return err
}
- return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
+ return db.WithTx(ctx, func(ctx context.Context) error {
return createFileForBlob(ctx, uploadVersion, pb)
})
}
-func getOrCreateUploadVersion(pi *packages_service.PackageInfo) (*packages_model.PackageVersion, error) {
+func getOrCreateUploadVersion(ctx context.Context, pi *packages_service.PackageInfo) (*packages_model.PackageVersion, error) {
var uploadVersion *packages_model.PackageVersion
// FIXME: Replace usage of mutex with database transaction
// https://github.com/go-gitea/gitea/pull/21862
uploadVersionMutex.Lock()
- err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
+ err := db.WithTx(ctx, func(ctx context.Context) error {
created := true
p := &packages_model.Package{
OwnerID: pi.Owner.ID,
@@ -172,8 +172,8 @@ func createFileForBlob(ctx context.Context, pv *packages_model.PackageVersion, p
return nil
}
-func deleteBlob(ownerID int64, image, digest string) error {
- return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
+func deleteBlob(ctx context.Context, ownerID int64, image, digest string) error {
+ return db.WithTx(ctx, func(ctx context.Context) error {
pfds, err := container_model.GetContainerBlobs(ctx, &container_model.BlobSearchOptions{
OwnerID: ownerID,
Image: image,
diff --git a/routers/api/packages/container/container.go b/routers/api/packages/container/container.go
index 8f79805cc8..7bd5cadaaf 100644
--- a/routers/api/packages/container/container.go
+++ b/routers/api/packages/container/container.go
@@ -210,7 +210,7 @@ func InitiateUploadBlob(ctx *context.Context) {
}
if accessible {
- if err := mountBlob(&packages_service.PackageInfo{Owner: ctx.Package.Owner, Name: image}, blob.Blob); err != nil {
+ if err := mountBlob(ctx, &packages_service.PackageInfo{Owner: ctx.Package.Owner, Name: image}, blob.Blob); err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
}
@@ -239,7 +239,7 @@ func InitiateUploadBlob(ctx *context.Context) {
return
}
- if _, err := saveAsPackageBlob(
+ if _, err := saveAsPackageBlob(ctx,
buf,
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
@@ -384,7 +384,7 @@ func EndUploadBlob(ctx *context.Context) {
return
}
- if _, err := saveAsPackageBlob(
+ if _, err := saveAsPackageBlob(ctx,
uploader,
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
@@ -502,7 +502,7 @@ func DeleteBlob(ctx *context.Context) {
return
}
- if err := deleteBlob(ctx.Package.Owner.ID, ctx.Params("image"), d); err != nil {
+ if err := deleteBlob(ctx, ctx.Package.Owner.ID, ctx.Params("image"), d); err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
}
@@ -543,7 +543,7 @@ func UploadManifest(ctx *context.Context) {
return
}
- digest, err := processManifest(mci, buf)
+ digest, err := processManifest(ctx, mci, buf)
if err != nil {
var namedError *namedError
if errors.As(err, &namedError) {
diff --git a/routers/api/packages/container/manifest.go b/routers/api/packages/container/manifest.go
index 1dbd058d6b..6678ed20bc 100644
--- a/routers/api/packages/container/manifest.go
+++ b/routers/api/packages/container/manifest.go
@@ -50,7 +50,7 @@ type manifestCreationInfo struct {
Properties map[string]string
}
-func processManifest(mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) {
+func processManifest(ctx context.Context, mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) {
var index oci.Index
if err := json.NewDecoder(buf).Decode(&index); err != nil {
return "", err
@@ -72,14 +72,14 @@ func processManifest(mci *manifestCreationInfo, buf *packages_module.HashedBuffe
}
if isImageManifestMediaType(mci.MediaType) {
- return processImageManifest(mci, buf)
+ return processImageManifest(ctx, mci, buf)
} else if isImageIndexMediaType(mci.MediaType) {
- return processImageManifestIndex(mci, buf)
+ return processImageManifestIndex(ctx, mci, buf)
}
return "", errManifestInvalid
}
-func processImageManifest(mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) {
+func processImageManifest(ctx context.Context, mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) {
manifestDigest := ""
err := func() error {
@@ -92,7 +92,7 @@ func processImageManifest(mci *manifestCreationInfo, buf *packages_module.Hashed
return err
}
- ctx, committer, err := db.TxContext(db.DefaultContext)
+ ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
}
@@ -181,7 +181,7 @@ func processImageManifest(mci *manifestCreationInfo, buf *packages_module.Hashed
return err
}
- if err := notifyPackageCreate(mci.Creator, pv); err != nil {
+ if err := notifyPackageCreate(ctx, mci.Creator, pv); err != nil {
return err
}
@@ -196,7 +196,7 @@ func processImageManifest(mci *manifestCreationInfo, buf *packages_module.Hashed
return manifestDigest, nil
}
-func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) {
+func processImageManifestIndex(ctx context.Context, mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) {
manifestDigest := ""
err := func() error {
@@ -209,7 +209,7 @@ func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.H
return err
}
- ctx, committer, err := db.TxContext(db.DefaultContext)
+ ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
}
@@ -285,7 +285,7 @@ func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.H
return err
}
- if err := notifyPackageCreate(mci.Creator, pv); err != nil {
+ if err := notifyPackageCreate(ctx, mci.Creator, pv); err != nil {
return err
}
@@ -300,13 +300,13 @@ func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.H
return manifestDigest, nil
}
-func notifyPackageCreate(doer *user_model.User, pv *packages_model.PackageVersion) error {
- pd, err := packages_model.GetPackageDescriptor(db.DefaultContext, pv)
+func notifyPackageCreate(ctx context.Context, doer *user_model.User, pv *packages_model.PackageVersion) error {
+ pd, err := packages_model.GetPackageDescriptor(ctx, pv)
if err != nil {
return err
}
- notification.NotifyPackageCreate(db.DefaultContext, doer, pd)
+ notification.NotifyPackageCreate(ctx, doer, pd)
return nil
}
diff --git a/routers/api/packages/npm/npm.go b/routers/api/packages/npm/npm.go
index 3616211d61..d1e271f23f 100644
--- a/routers/api/packages/npm/npm.go
+++ b/routers/api/packages/npm/npm.go
@@ -5,6 +5,7 @@ package npm
import (
"bytes"
+ std_ctx "context"
"errors"
"fmt"
"io"
@@ -222,7 +223,7 @@ func UploadPackage(ctx *context.Context) {
}
for _, tag := range npmPackage.DistTags {
- if err := setPackageTag(tag, pv, false); err != nil {
+ if err := setPackageTag(ctx, tag, pv, false); err != nil {
if err == errInvalidTagName {
apiError(ctx, http.StatusBadRequest, err)
return
@@ -345,7 +346,7 @@ func AddPackageTag(ctx *context.Context) {
return
}
- if err := setPackageTag(ctx.Params("tag"), pv, false); err != nil {
+ if err := setPackageTag(ctx, ctx.Params("tag"), pv, false); err != nil {
if err == errInvalidTagName {
apiError(ctx, http.StatusBadRequest, err)
return
@@ -366,7 +367,7 @@ func DeletePackageTag(ctx *context.Context) {
}
if len(pvs) != 0 {
- if err := setPackageTag(ctx.Params("tag"), pvs[0], true); err != nil {
+ if err := setPackageTag(ctx, ctx.Params("tag"), pvs[0], true); err != nil {
if err == errInvalidTagName {
apiError(ctx, http.StatusBadRequest, err)
return
@@ -377,7 +378,7 @@ func DeletePackageTag(ctx *context.Context) {
}
}
-func setPackageTag(tag string, pv *packages_model.PackageVersion, deleteOnly bool) error {
+func setPackageTag(ctx std_ctx.Context, tag string, pv *packages_model.PackageVersion, deleteOnly bool) error {
if tag == "" {
return errInvalidTagName
}
@@ -386,47 +387,42 @@ func setPackageTag(tag string, pv *packages_model.PackageVersion, deleteOnly boo
return errInvalidTagName
}
- ctx, committer, err := db.TxContext(db.DefaultContext)
- if err != nil {
- return err
- }
- defer committer.Close()
-
- pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
- PackageID: pv.PackageID,
- Properties: map[string]string{
- npm_module.TagProperty: tag,
- },
- IsInternal: util.OptionalBoolFalse,
- })
- if err != nil {
- return err
- }
-
- if len(pvs) == 1 {
- pvps, err := packages_model.GetPropertiesByName(ctx, packages_model.PropertyTypeVersion, pvs[0].ID, npm_module.TagProperty)
+ return db.WithTx(ctx, func(ctx std_ctx.Context) error {
+ pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
+ PackageID: pv.PackageID,
+ Properties: map[string]string{
+ npm_module.TagProperty: tag,
+ },
+ IsInternal: util.OptionalBoolFalse,
+ })
if err != nil {
return err
}
- for _, pvp := range pvps {
- if pvp.Value == tag {
- if err := packages_model.DeletePropertyByID(ctx, pvp.ID); err != nil {
- return err
+ if len(pvs) == 1 {
+ pvps, err := packages_model.GetPropertiesByName(ctx, packages_model.PropertyTypeVersion, pvs[0].ID, npm_module.TagProperty)
+ if err != nil {
+ return err
+ }
+
+ for _, pvp := range pvps {
+ if pvp.Value == tag {
+ if err := packages_model.DeletePropertyByID(ctx, pvp.ID); err != nil {
+ return err
+ }
+ break
}
- break
}
}
- }
- if !deleteOnly {
- _, err = packages_model.InsertProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, npm_module.TagProperty, tag)
- if err != nil {
- return err
+ if !deleteOnly {
+ _, err = packages_model.InsertProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, npm_module.TagProperty, tag)
+ if err != nil {
+ return err
+ }
}
- }
-
- return committer.Commit()
+ return nil
+ })
}
func PackageSearch(ctx *context.Context) {