1
1
mirror of https://github.com/go-gitea/gitea synced 2025-01-08 17:04:25 +00:00

Refactor package (routes and error handling, npm peer dependency) (#33111)

This commit is contained in:
wxiaoguang 2025-01-06 22:45:20 +08:00 committed by GitHub
parent ef736b7e27
commit 80e4f4c4eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
28 changed files with 153 additions and 244 deletions

View File

@ -81,6 +81,7 @@ type PackageMetadataVersion struct {
BundleDependencies []string `json:"bundleDependencies,omitempty"` BundleDependencies []string `json:"bundleDependencies,omitempty"`
DevDependencies map[string]string `json:"devDependencies,omitempty"` DevDependencies map[string]string `json:"devDependencies,omitempty"`
PeerDependencies map[string]string `json:"peerDependencies,omitempty"` PeerDependencies map[string]string `json:"peerDependencies,omitempty"`
PeerDependenciesMeta map[string]any `json:"peerDependenciesMeta,omitempty"`
Bin map[string]string `json:"bin,omitempty"` Bin map[string]string `json:"bin,omitempty"`
OptionalDependencies map[string]string `json:"optionalDependencies,omitempty"` OptionalDependencies map[string]string `json:"optionalDependencies,omitempty"`
Readme string `json:"readme,omitempty"` Readme string `json:"readme,omitempty"`
@ -222,6 +223,7 @@ func ParsePackage(r io.Reader) (*Package, error) {
BundleDependencies: meta.BundleDependencies, BundleDependencies: meta.BundleDependencies,
DevelopmentDependencies: meta.DevDependencies, DevelopmentDependencies: meta.DevDependencies,
PeerDependencies: meta.PeerDependencies, PeerDependencies: meta.PeerDependencies,
PeerDependenciesMeta: meta.PeerDependenciesMeta,
OptionalDependencies: meta.OptionalDependencies, OptionalDependencies: meta.OptionalDependencies,
Bin: meta.Bin, Bin: meta.Bin,
Readme: meta.Readme, Readme: meta.Readme,

View File

@ -19,6 +19,7 @@ type Metadata struct {
BundleDependencies []string `json:"bundleDependencies,omitempty"` BundleDependencies []string `json:"bundleDependencies,omitempty"`
DevelopmentDependencies map[string]string `json:"development_dependencies,omitempty"` DevelopmentDependencies map[string]string `json:"development_dependencies,omitempty"`
PeerDependencies map[string]string `json:"peer_dependencies,omitempty"` PeerDependencies map[string]string `json:"peer_dependencies,omitempty"`
PeerDependenciesMeta map[string]any `json:"peer_dependencies_meta,omitempty"`
OptionalDependencies map[string]string `json:"optional_dependencies,omitempty"` OptionalDependencies map[string]string `json:"optional_dependencies,omitempty"`
Bin map[string]string `json:"bin,omitempty"` Bin map[string]string `json:"bin,omitempty"`
Readme string `json:"readme,omitempty"` Readme string `json:"readme,omitempty"`

View File

@ -89,11 +89,23 @@ func (p *routerPathMatcher) matchPath(chiCtx *chi.Context, path string) bool {
return true return true
} }
func isValidMethod(name string) bool {
switch name {
case http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodHead, http.MethodOptions, http.MethodConnect, http.MethodTrace:
return true
}
return false
}
func newRouterPathMatcher(methods, pattern string, h ...any) *routerPathMatcher { func newRouterPathMatcher(methods, pattern string, h ...any) *routerPathMatcher {
middlewares, handlerFunc := wrapMiddlewareAndHandler(nil, h) middlewares, handlerFunc := wrapMiddlewareAndHandler(nil, h)
p := &routerPathMatcher{methods: make(container.Set[string]), middlewares: middlewares, handlerFunc: handlerFunc} p := &routerPathMatcher{methods: make(container.Set[string]), middlewares: middlewares, handlerFunc: handlerFunc}
for _, method := range strings.Split(methods, ",") { for _, method := range strings.Split(methods, ",") {
p.methods.Add(strings.TrimSpace(method)) method = strings.TrimSpace(method)
if !isValidMethod(method) {
panic(fmt.Sprintf("invalid HTTP method: %s", method))
}
p.methods.Add(method)
} }
re := []byte{'^'} re := []byte{'^'}
lastEnd := 0 lastEnd := 0

View File

@ -114,7 +114,7 @@ func UploadPackageFile(ctx *context.Context) {
pck, err := alpine_module.ParsePackage(buf) pck, err := alpine_module.ParsePackage(buf)
if err != nil { if err != nil {
if errors.Is(err, util.ErrInvalidArgument) || err == io.EOF { if errors.Is(err, util.ErrInvalidArgument) || errors.Is(err, io.EOF) {
apiError(ctx, http.StatusBadRequest, err) apiError(ctx, http.StatusBadRequest, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)

View File

@ -138,7 +138,8 @@ func CommonRoutes() *web.Router {
}, reqPackageAccess(perm.AccessModeRead)) }, reqPackageAccess(perm.AccessModeRead))
r.Group("/arch", func() { r.Group("/arch", func() {
r.Methods("HEAD,GET", "/repository.key", arch.GetRepositoryKey) r.Methods("HEAD,GET", "/repository.key", arch.GetRepositoryKey)
r.PathGroup("*", func(g *web.RouterPathGroup) { r.Methods("PUT", "" /* no repository */, reqPackageAccess(perm.AccessModeWrite), arch.UploadPackageFile)
r.PathGroup("/*", func(g *web.RouterPathGroup) {
g.MatchPath("PUT", "/<repository:*>", reqPackageAccess(perm.AccessModeWrite), arch.UploadPackageFile) g.MatchPath("PUT", "/<repository:*>", reqPackageAccess(perm.AccessModeWrite), arch.UploadPackageFile)
g.MatchPath("HEAD,GET", "/<repository:*>/<architecture>/<filename>", arch.GetPackageOrRepositoryFile) g.MatchPath("HEAD,GET", "/<repository:*>/<architecture>/<filename>", arch.GetPackageOrRepositoryFile)
g.MatchPath("DELETE", "/<repository:*>/<name>/<version>/<architecture>", reqPackageAccess(perm.AccessModeWrite), arch.DeletePackageVersion) g.MatchPath("DELETE", "/<repository:*>/<name>/<version>/<architecture>", reqPackageAccess(perm.AccessModeWrite), arch.DeletePackageVersion)
@ -698,150 +699,28 @@ func ContainerRoutes() *web.Router {
}) })
r.Get("/_catalog", container.ReqContainerAccess, container.GetRepositoryList) r.Get("/_catalog", container.ReqContainerAccess, container.GetRepositoryList)
r.Group("/{username}", func() { r.Group("/{username}", func() {
r.Group("/{image}", func() { r.PathGroup("/*", func(g *web.RouterPathGroup) {
r.Group("/blobs/uploads", func() { g.MatchPath("POST", "/<image:*>/blobs/uploads", reqPackageAccess(perm.AccessModeWrite), container.VerifyImageName, container.InitiateUploadBlob)
r.Post("", container.InitiateUploadBlob) g.MatchPath("GET", "/<image:*>/tags/list", container.VerifyImageName, container.GetTagList)
r.Group("/{uuid}", func() { g.MatchPath("GET,PATCH,PUT,DELETE", `/<image:*>/blobs/uploads/<uuid:[-.=\w]+>`, reqPackageAccess(perm.AccessModeWrite), container.VerifyImageName, func(ctx *context.Context) {
r.Get("", container.GetUploadBlob) if ctx.Req.Method == http.MethodGet {
r.Patch("", container.UploadBlob)
r.Put("", container.EndUploadBlob)
r.Delete("", container.CancelUploadBlob)
})
}, reqPackageAccess(perm.AccessModeWrite))
r.Group("/blobs/{digest}", func() {
r.Head("", container.HeadBlob)
r.Get("", container.GetBlob)
r.Delete("", reqPackageAccess(perm.AccessModeWrite), container.DeleteBlob)
})
r.Group("/manifests/{reference}", func() {
r.Put("", reqPackageAccess(perm.AccessModeWrite), container.UploadManifest)
r.Head("", container.HeadManifest)
r.Get("", container.GetManifest)
r.Delete("", reqPackageAccess(perm.AccessModeWrite), container.DeleteManifest)
})
r.Get("/tags/list", container.GetTagList)
}, container.VerifyImageName)
var (
blobsUploadsPattern = regexp.MustCompile(`\A(.+)/blobs/uploads/([a-zA-Z0-9-_.=]+)\z`)
blobsPattern = regexp.MustCompile(`\A(.+)/blobs/([^/]+)\z`)
manifestsPattern = regexp.MustCompile(`\A(.+)/manifests/([^/]+)\z`)
)
// Manual mapping of routes because {image} can contain slashes which chi does not support
r.Methods("HEAD,GET,POST,PUT,PATCH,DELETE", "/*", func(ctx *context.Context) {
path := ctx.PathParam("*")
isHead := ctx.Req.Method == "HEAD"
isGet := ctx.Req.Method == "GET"
isPost := ctx.Req.Method == "POST"
isPut := ctx.Req.Method == "PUT"
isPatch := ctx.Req.Method == "PATCH"
isDelete := ctx.Req.Method == "DELETE"
if isPost && strings.HasSuffix(path, "/blobs/uploads") {
reqPackageAccess(perm.AccessModeWrite)(ctx)
if ctx.Written() {
return
}
ctx.SetPathParam("image", path[:len(path)-14])
container.VerifyImageName(ctx)
if ctx.Written() {
return
}
container.InitiateUploadBlob(ctx)
return
}
if isGet && strings.HasSuffix(path, "/tags/list") {
ctx.SetPathParam("image", path[:len(path)-10])
container.VerifyImageName(ctx)
if ctx.Written() {
return
}
container.GetTagList(ctx)
return
}
m := blobsUploadsPattern.FindStringSubmatch(path)
if len(m) == 3 && (isGet || isPut || isPatch || isDelete) {
reqPackageAccess(perm.AccessModeWrite)(ctx)
if ctx.Written() {
return
}
ctx.SetPathParam("image", m[1])
container.VerifyImageName(ctx)
if ctx.Written() {
return
}
ctx.SetPathParam("uuid", m[2])
if isGet {
container.GetUploadBlob(ctx) container.GetUploadBlob(ctx)
} else if isPatch { } else if ctx.Req.Method == http.MethodPatch {
container.UploadBlob(ctx) container.UploadBlob(ctx)
} else if isPut { } else if ctx.Req.Method == http.MethodPut {
container.EndUploadBlob(ctx) container.EndUploadBlob(ctx)
} else { } else /* DELETE */ {
container.CancelUploadBlob(ctx) container.CancelUploadBlob(ctx)
} }
return })
} g.MatchPath("HEAD", `/<image:*>/blobs/<digest>`, container.VerifyImageName, container.HeadBlob)
m = blobsPattern.FindStringSubmatch(path) g.MatchPath("GET", `/<image:*>/blobs/<digest>`, container.VerifyImageName, container.GetBlob)
if len(m) == 3 && (isHead || isGet || isDelete) { g.MatchPath("DELETE", `/<image:*>/blobs/<digest>`, container.VerifyImageName, reqPackageAccess(perm.AccessModeWrite), container.DeleteBlob)
ctx.SetPathParam("image", m[1])
container.VerifyImageName(ctx)
if ctx.Written() {
return
}
ctx.SetPathParam("digest", m[2]) g.MatchPath("HEAD", `/<image:*>/manifests/<reference>`, container.VerifyImageName, container.HeadManifest)
g.MatchPath("GET", `/<image:*>/manifests/<reference>`, container.VerifyImageName, container.GetManifest)
if isHead { g.MatchPath("PUT", `/<image:*>/manifests/<reference>`, container.VerifyImageName, reqPackageAccess(perm.AccessModeWrite), container.UploadManifest)
container.HeadBlob(ctx) g.MatchPath("DELETE", `/<image:*>/manifests/<reference>`, container.VerifyImageName, reqPackageAccess(perm.AccessModeWrite), container.DeleteManifest)
} else if isGet {
container.GetBlob(ctx)
} else {
reqPackageAccess(perm.AccessModeWrite)(ctx)
if ctx.Written() {
return
}
container.DeleteBlob(ctx)
}
return
}
m = manifestsPattern.FindStringSubmatch(path)
if len(m) == 3 && (isHead || isGet || isPut || isDelete) {
ctx.SetPathParam("image", m[1])
container.VerifyImageName(ctx)
if ctx.Written() {
return
}
ctx.SetPathParam("reference", m[2])
if isHead {
container.HeadManifest(ctx)
} else if isGet {
container.GetManifest(ctx)
} else {
reqPackageAccess(perm.AccessModeWrite)(ctx)
if ctx.Written() {
return
}
if isPut {
container.UploadManifest(ctx)
} else {
container.DeleteManifest(ctx)
}
}
return
}
ctx.Status(http.StatusNotFound)
}) })
}, container.ReqContainerAccess, context.UserAssignmentWeb(), context.PackageAssignment(), reqPackageAccess(perm.AccessModeRead)) }, container.ReqContainerAccess, context.UserAssignmentWeb(), context.PackageAssignment(), reqPackageAccess(perm.AccessModeRead))

View File

@ -62,7 +62,7 @@ func UploadPackageFile(ctx *context.Context) {
pck, err := arch_module.ParsePackage(buf) pck, err := arch_module.ParsePackage(buf)
if err != nil { if err != nil {
if errors.Is(err, util.ErrInvalidArgument) || err == io.EOF { if errors.Is(err, util.ErrInvalidArgument) || errors.Is(err, io.EOF) {
apiError(ctx, http.StatusBadRequest, err) apiError(ctx, http.StatusBadRequest, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)

View File

@ -181,7 +181,7 @@ func DownloadPackageFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -276,7 +276,7 @@ func UnyankPackage(ctx *context.Context) {
func yankPackage(ctx *context.Context, yank bool) { func yankPackage(ctx *context.Context, yank bool) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeCargo, ctx.PathParam("package"), ctx.PathParam("version")) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeCargo, ctx.PathParam("package"), ctx.PathParam("version"))
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -216,7 +216,7 @@ func PackageVersionMetadata(ctx *context.Context) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeChef, packageName, packageVersion) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeChef, packageName, packageVersion)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -327,7 +327,7 @@ func UploadPackage(ctx *context.Context) {
func DownloadPackage(ctx *context.Context) { func DownloadPackage(ctx *context.Context) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeChef, ctx.PathParam("name"), ctx.PathParam("version")) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeChef, ctx.PathParam("name"), ctx.PathParam("version"))
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -368,7 +368,7 @@ func DeletePackageVersion(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)

View File

@ -176,7 +176,7 @@ func DownloadPackageFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -5,6 +5,7 @@ package conan
import ( import (
std_ctx "context" std_ctx "context"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -183,7 +184,7 @@ func serveSnapshot(ctx *context.Context, fileKey string) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -244,7 +245,7 @@ func serveDownloadURLs(ctx *context.Context, fileKey, downloadURL string) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -493,7 +494,7 @@ func downloadFile(ctx *context.Context, fileFilter container.Set[string], fileKe
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -509,7 +510,7 @@ func DeleteRecipeV1(ctx *context.Context) {
rref := ctx.Data[recipeReferenceKey].(*conan_module.RecipeReference) rref := ctx.Data[recipeReferenceKey].(*conan_module.RecipeReference)
if err := deleteRecipeOrPackage(ctx, rref, true, nil, false); err != nil { if err := deleteRecipeOrPackage(ctx, rref, true, nil, false); err != nil {
if err == packages_model.ErrPackageNotExist || err == conan_model.ErrPackageReferenceNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, conan_model.ErrPackageReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -524,7 +525,7 @@ func DeleteRecipeV2(ctx *context.Context) {
rref := ctx.Data[recipeReferenceKey].(*conan_module.RecipeReference) rref := ctx.Data[recipeReferenceKey].(*conan_module.RecipeReference)
if err := deleteRecipeOrPackage(ctx, rref, rref.Revision == "", nil, false); err != nil { if err := deleteRecipeOrPackage(ctx, rref, rref.Revision == "", nil, false); err != nil {
if err == packages_model.ErrPackageNotExist || err == conan_model.ErrPackageReferenceNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, conan_model.ErrPackageReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -571,7 +572,7 @@ func DeletePackageV1(ctx *context.Context) {
for _, reference := range references { for _, reference := range references {
pref, _ := conan_module.NewPackageReference(currentRref, reference.Value, conan_module.DefaultRevision) pref, _ := conan_module.NewPackageReference(currentRref, reference.Value, conan_module.DefaultRevision)
if err := deleteRecipeOrPackage(ctx, currentRref, true, pref, true); err != nil { if err := deleteRecipeOrPackage(ctx, currentRref, true, pref, true); err != nil {
if err == packages_model.ErrPackageNotExist || err == conan_model.ErrPackageReferenceNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, conan_model.ErrPackageReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -590,7 +591,7 @@ func DeletePackageV2(ctx *context.Context) {
if pref != nil { // has package reference if pref != nil { // has package reference
if err := deleteRecipeOrPackage(ctx, rref, false, pref, pref.Revision == ""); err != nil { if err := deleteRecipeOrPackage(ctx, rref, false, pref, pref.Revision == ""); err != nil {
if err == packages_model.ErrPackageNotExist || err == conan_model.ErrPackageReferenceNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, conan_model.ErrPackageReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -615,7 +616,7 @@ func DeletePackageV2(ctx *context.Context) {
pref, _ := conan_module.NewPackageReference(rref, reference.Value, conan_module.DefaultRevision) pref, _ := conan_module.NewPackageReference(rref, reference.Value, conan_module.DefaultRevision)
if err := deleteRecipeOrPackage(ctx, rref, false, pref, true); err != nil { if err := deleteRecipeOrPackage(ctx, rref, false, pref, true); err != nil {
if err == packages_model.ErrPackageNotExist || err == conan_model.ErrPackageReferenceNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, conan_model.ErrPackageReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -749,7 +750,7 @@ func LatestRecipeRevision(ctx *context.Context) {
revision, err := conan_model.GetLastRecipeRevision(ctx, ctx.Package.Owner.ID, rref) revision, err := conan_model.GetLastRecipeRevision(ctx, ctx.Package.Owner.ID, rref)
if err != nil { if err != nil {
if err == conan_model.ErrRecipeReferenceNotExist || err == conan_model.ErrPackageReferenceNotExist { if errors.Is(err, conan_model.ErrRecipeReferenceNotExist) || errors.Is(err, conan_model.ErrPackageReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -766,7 +767,7 @@ func LatestPackageRevision(ctx *context.Context) {
revision, err := conan_model.GetLastPackageRevision(ctx, ctx.Package.Owner.ID, pref) revision, err := conan_model.GetLastPackageRevision(ctx, ctx.Package.Owner.ID, pref)
if err != nil { if err != nil {
if err == conan_model.ErrRecipeReferenceNotExist || err == conan_model.ErrPackageReferenceNotExist { if errors.Is(err, conan_model.ErrRecipeReferenceNotExist) || errors.Is(err, conan_model.ErrPackageReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -796,7 +797,7 @@ func listRevisionFiles(ctx *context.Context, fileKey string) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)

View File

@ -4,6 +4,7 @@
package conan package conan
import ( import (
"errors"
"net/http" "net/http"
"strings" "strings"
@ -76,7 +77,7 @@ func searchPackages(ctx *context.Context, searchAllRevisions bool) {
if !searchAllRevisions && rref.Revision == "" { if !searchAllRevisions && rref.Revision == "" {
lastRevision, err := conan_model.GetLastRecipeRevision(ctx, ctx.Package.Owner.ID, rref) lastRevision, err := conan_model.GetLastRecipeRevision(ctx, ctx.Package.Owner.ID, rref)
if err != nil { if err != nil {
if err == conan_model.ErrRecipeReferenceNotExist { if errors.Is(err, conan_model.ErrRecipeReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -87,7 +88,7 @@ func searchPackages(ctx *context.Context, searchAllRevisions bool) {
} else { } else {
has, err := conan_model.RecipeExists(ctx, ctx.Package.Owner.ID, rref) has, err := conan_model.RecipeExists(ctx, ctx.Package.Owner.ID, rref)
if err != nil { if err != nil {
if err == conan_model.ErrRecipeReferenceNotExist { if errors.Is(err, conan_model.ErrRecipeReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -119,7 +120,7 @@ func searchPackages(ctx *context.Context, searchAllRevisions bool) {
} }
packageReferences, err := conan_model.GetPackageReferences(ctx, ctx.Package.Owner.ID, currentRef) packageReferences, err := conan_model.GetPackageReferences(ctx, ctx.Package.Owner.ID, currentRef)
if err != nil { if err != nil {
if err == conan_model.ErrRecipeReferenceNotExist { if errors.Is(err, conan_model.ErrRecipeReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -133,7 +134,7 @@ func searchPackages(ctx *context.Context, searchAllRevisions bool) {
pref, _ := conan_module.NewPackageReference(currentRef, packageReference.Value, "") pref, _ := conan_module.NewPackageReference(currentRef, packageReference.Value, "")
lastPackageRevision, err := conan_model.GetLastPackageRevision(ctx, ctx.Package.Owner.ID, pref) lastPackageRevision, err := conan_model.GetLastPackageRevision(ctx, ctx.Package.Owner.ID, pref)
if err != nil { if err != nil {
if err == conan_model.ErrPackageReferenceNotExist { if errors.Is(err, conan_model.ErrPackageReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -143,7 +144,7 @@ func searchPackages(ctx *context.Context, searchAllRevisions bool) {
pref = pref.WithRevision(lastPackageRevision.Value) pref = pref.WithRevision(lastPackageRevision.Value)
infoRaw, err := conan_model.GetPackageInfo(ctx, ctx.Package.Owner.ID, pref) infoRaw, err := conan_model.GetPackageInfo(ctx, ctx.Package.Owner.ID, pref)
if err != nil { if err != nil {
if err == conan_model.ErrPackageReferenceNotExist { if errors.Is(err, conan_model.ErrPackageReferenceNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)

View File

@ -111,12 +111,11 @@ func getOrCreateUploadVersion(ctx context.Context, pi *packages_service.PackageI
} }
var err error var err error
if p, err = packages_model.TryInsertPackage(ctx, p); err != nil { if p, err = packages_model.TryInsertPackage(ctx, p); err != nil {
if err == packages_model.ErrDuplicatePackage { if !errors.Is(err, packages_model.ErrDuplicatePackage) {
created = false
} else {
log.Error("Error inserting package: %v", err) log.Error("Error inserting package: %v", err)
return err return err
} }
created = false
} }
if created { if created {
@ -135,7 +134,7 @@ func getOrCreateUploadVersion(ctx context.Context, pi *packages_service.PackageI
MetadataJSON: "null", MetadataJSON: "null",
} }
if pv, err = packages_model.GetOrInsertVersion(ctx, pv); err != nil { if pv, err = packages_model.GetOrInsertVersion(ctx, pv); err != nil {
if err != packages_model.ErrDuplicatePackageVersion { if !errors.Is(err, packages_model.ErrDuplicatePackageVersion) {
log.Error("Error inserting package: %v", err) log.Error("Error inserting package: %v", err)
return err return err
} }
@ -161,7 +160,7 @@ func createFileForBlob(ctx context.Context, pv *packages_model.PackageVersion, p
} }
var err error var err error
if pf, err = packages_model.TryInsertFile(ctx, pf); err != nil { if pf, err = packages_model.TryInsertFile(ctx, pf); err != nil {
if err == packages_model.ErrDuplicatePackageFile { if errors.Is(err, packages_model.ErrDuplicatePackageFile) {
return nil return nil
} }
log.Error("Error inserting package file: %v", err) log.Error("Error inserting package file: %v", err)

View File

@ -324,7 +324,7 @@ func GetUploadBlob(ctx *context.Context) {
upload, err := packages_model.GetBlobUploadByID(ctx, uuid) upload, err := packages_model.GetBlobUploadByID(ctx, uuid)
if err != nil { if err != nil {
if err == packages_model.ErrPackageBlobUploadNotExist { if errors.Is(err, packages_model.ErrPackageBlobUploadNotExist) {
apiErrorDefined(ctx, errBlobUploadUnknown) apiErrorDefined(ctx, errBlobUploadUnknown)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -345,7 +345,7 @@ func UploadBlob(ctx *context.Context) {
uploader, err := container_service.NewBlobUploader(ctx, ctx.PathParam("uuid")) uploader, err := container_service.NewBlobUploader(ctx, ctx.PathParam("uuid"))
if err != nil { if err != nil {
if err == packages_model.ErrPackageBlobUploadNotExist { if errors.Is(err, packages_model.ErrPackageBlobUploadNotExist) {
apiErrorDefined(ctx, errBlobUploadUnknown) apiErrorDefined(ctx, errBlobUploadUnknown)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -396,7 +396,7 @@ func EndUploadBlob(ctx *context.Context) {
uploader, err := container_service.NewBlobUploader(ctx, ctx.PathParam("uuid")) uploader, err := container_service.NewBlobUploader(ctx, ctx.PathParam("uuid"))
if err != nil { if err != nil {
if err == packages_model.ErrPackageBlobUploadNotExist { if errors.Is(err, packages_model.ErrPackageBlobUploadNotExist) {
apiErrorDefined(ctx, errBlobUploadUnknown) apiErrorDefined(ctx, errBlobUploadUnknown)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -465,7 +465,7 @@ func CancelUploadBlob(ctx *context.Context) {
_, err := packages_model.GetBlobUploadByID(ctx, uuid) _, err := packages_model.GetBlobUploadByID(ctx, uuid)
if err != nil { if err != nil {
if err == packages_model.ErrPackageBlobUploadNotExist { if errors.Is(err, packages_model.ErrPackageBlobUploadNotExist) {
apiErrorDefined(ctx, errBlobUploadUnknown) apiErrorDefined(ctx, errBlobUploadUnknown)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -501,7 +501,7 @@ func getBlobFromContext(ctx *context.Context) (*packages_model.PackageFileDescri
func HeadBlob(ctx *context.Context) { func HeadBlob(ctx *context.Context) {
blob, err := getBlobFromContext(ctx) blob, err := getBlobFromContext(ctx)
if err != nil { if err != nil {
if err == container_model.ErrContainerBlobNotExist { if errors.Is(err, container_model.ErrContainerBlobNotExist) {
apiErrorDefined(ctx, errBlobUnknown) apiErrorDefined(ctx, errBlobUnknown)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -520,7 +520,7 @@ func HeadBlob(ctx *context.Context) {
func GetBlob(ctx *context.Context) { func GetBlob(ctx *context.Context) {
blob, err := getBlobFromContext(ctx) blob, err := getBlobFromContext(ctx)
if err != nil { if err != nil {
if err == container_model.ErrContainerBlobNotExist { if errors.Is(err, container_model.ErrContainerBlobNotExist) {
apiErrorDefined(ctx, errBlobUnknown) apiErrorDefined(ctx, errBlobUnknown)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -639,7 +639,7 @@ func getManifestFromContext(ctx *context.Context) (*packages_model.PackageFileDe
func HeadManifest(ctx *context.Context) { func HeadManifest(ctx *context.Context) {
manifest, err := getManifestFromContext(ctx) manifest, err := getManifestFromContext(ctx)
if err != nil { if err != nil {
if err == container_model.ErrContainerBlobNotExist { if errors.Is(err, container_model.ErrContainerBlobNotExist) {
apiErrorDefined(ctx, errManifestUnknown) apiErrorDefined(ctx, errManifestUnknown)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -659,7 +659,7 @@ func HeadManifest(ctx *context.Context) {
func GetManifest(ctx *context.Context) { func GetManifest(ctx *context.Context) {
manifest, err := getManifestFromContext(ctx) manifest, err := getManifestFromContext(ctx)
if err != nil { if err != nil {
if err == container_model.ErrContainerBlobNotExist { if errors.Is(err, container_model.ErrContainerBlobNotExist) {
apiErrorDefined(ctx, errManifestUnknown) apiErrorDefined(ctx, errManifestUnknown)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
@ -739,7 +739,7 @@ func GetTagList(ctx *context.Context) {
image := ctx.PathParam("image") image := ctx.PathParam("image")
if _, err := packages_model.GetPackageByName(ctx, ctx.Package.Owner.ID, packages_model.TypeContainer, image); err != nil { if _, err := packages_model.GetPackageByName(ctx, ctx.Package.Owner.ID, packages_model.TypeContainer, image); err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiErrorDefined(ctx, errNameUnknown) apiErrorDefined(ctx, errNameUnknown)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)

View File

@ -240,7 +240,7 @@ func processImageManifestIndex(ctx context.Context, mci *manifestCreationInfo, b
IsManifest: true, IsManifest: true,
}) })
if err != nil { if err != nil {
if err == container_model.ErrContainerBlobNotExist { if errors.Is(err, container_model.ErrContainerBlobNotExist) {
return errManifestBlobUnknown return errManifestBlobUnknown
} }
return err return err
@ -321,12 +321,11 @@ func createPackageAndVersion(ctx context.Context, mci *manifestCreationInfo, met
} }
var err error var err error
if p, err = packages_model.TryInsertPackage(ctx, p); err != nil { if p, err = packages_model.TryInsertPackage(ctx, p); err != nil {
if err == packages_model.ErrDuplicatePackage { if !errors.Is(err, packages_model.ErrDuplicatePackage) {
created = false
} else {
log.Error("Error inserting package: %v", err) log.Error("Error inserting package: %v", err)
return nil, err return nil, err
} }
created = false
} }
if created { if created {
@ -352,21 +351,23 @@ func createPackageAndVersion(ctx context.Context, mci *manifestCreationInfo, met
} }
var pv *packages_model.PackageVersion var pv *packages_model.PackageVersion
if pv, err = packages_model.GetOrInsertVersion(ctx, _pv); err != nil { if pv, err = packages_model.GetOrInsertVersion(ctx, _pv); err != nil {
if err == packages_model.ErrDuplicatePackageVersion { if !errors.Is(err, packages_model.ErrDuplicatePackageVersion) {
if err := packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil { log.Error("Error inserting package: %v", err)
return nil, err return nil, err
} }
// keep download count on overwrite if err = packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil {
_pv.DownloadCount = pv.DownloadCount return nil, err
}
if pv, err = packages_model.GetOrInsertVersion(ctx, _pv); err != nil { // keep download count on overwrite
_pv.DownloadCount = pv.DownloadCount
if pv, err = packages_model.GetOrInsertVersion(ctx, _pv); err != nil {
if !errors.Is(err, packages_model.ErrDuplicatePackageVersion) {
log.Error("Error inserting package: %v", err) log.Error("Error inserting package: %v", err)
return nil, err return nil, err
} }
} else {
log.Error("Error inserting package: %v", err)
return nil, err
} }
} }
@ -417,7 +418,7 @@ func createFileFromBlobReference(ctx context.Context, pv, uploadVersion *package
} }
var err error var err error
if pf, err = packages_model.TryInsertFile(ctx, pf); err != nil { if pf, err = packages_model.TryInsertFile(ctx, pf); err != nil {
if err == packages_model.ErrDuplicatePackageFile { if errors.Is(err, packages_model.ErrDuplicatePackageFile) {
// Skip this blob because the manifest contains the same filesystem layer multiple times. // Skip this blob because the manifest contains the same filesystem layer multiple times.
return nil return nil
} }

View File

@ -68,7 +68,7 @@ func GetRepositoryFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
} else { } else {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)

View File

@ -44,7 +44,7 @@ func DownloadPackageFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -155,7 +155,7 @@ func DeletePackage(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -182,7 +182,7 @@ func DeletePackageFile(ctx *context.Context) {
return pv, pf, nil return pv, pf, nil
}() }()
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -130,7 +130,7 @@ func DownloadPackageFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -67,6 +67,7 @@ func createPackageMetadataVersion(registryURL string, pd *packages_model.Package
BundleDependencies: metadata.BundleDependencies, BundleDependencies: metadata.BundleDependencies,
DevDependencies: metadata.DevelopmentDependencies, DevDependencies: metadata.DevelopmentDependencies,
PeerDependencies: metadata.PeerDependencies, PeerDependencies: metadata.PeerDependencies,
PeerDependenciesMeta: metadata.PeerDependenciesMeta,
OptionalDependencies: metadata.OptionalDependencies, OptionalDependencies: metadata.OptionalDependencies,
Readme: metadata.Readme, Readme: metadata.Readme,
Bin: metadata.Bin, Bin: metadata.Bin,

View File

@ -98,7 +98,7 @@ func DownloadPackageFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -140,7 +140,7 @@ func DownloadPackageFileByName(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -267,7 +267,7 @@ func DeletePackageVersion(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -341,7 +341,7 @@ func AddPackageTag(ctx *context.Context) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeNpm, packageName, version) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeNpm, packageName, version)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -259,7 +259,7 @@ func RegistrationLeafV2(ctx *context.Context) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeNuGet, packageName, packageVersion) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeNuGet, packageName, packageVersion)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -288,7 +288,7 @@ func RegistrationLeafV3(ctx *context.Context) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeNuGet, packageName, packageVersion) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeNuGet, packageName, packageVersion)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -418,7 +418,7 @@ func DownloadPackageFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -671,7 +671,7 @@ func DownloadSymbolFile(ctx *context.Context) {
s, u, pf, err := packages_service.GetPackageFileStream(ctx, pfs[0]) s, u, pf, err := packages_service.GetPackageFileStream(ctx, pfs[0])
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -699,7 +699,7 @@ func DeletePackage(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -124,7 +124,7 @@ func PackageVersionMetadata(ctx *context.Context) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypePub, packageName, packageVersion) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypePub, packageName, packageVersion)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -233,7 +233,7 @@ func FinalizePackage(ctx *context.Context) {
_, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypePub, packageName, packageVersion) _, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypePub, packageName, packageVersion)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -258,7 +258,7 @@ func DownloadPackageFile(ctx *context.Context) {
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypePub, packageName, packageVersion) pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypePub, packageName, packageVersion)
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -5,6 +5,7 @@ package pypi
import ( import (
"encoding/hex" "encoding/hex"
"errors"
"io" "io"
"net/http" "net/http"
"regexp" "regexp"
@ -94,7 +95,7 @@ func DownloadPackageFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -185,7 +185,7 @@ func DownloadPackageFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }
@ -288,7 +288,7 @@ func DeletePackage(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -4,6 +4,7 @@
package vagrant package vagrant
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -230,7 +231,7 @@ func DownloadPackageFile(ctx *context.Context) {
}, },
) )
if err != nil { if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist { if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
apiError(ctx, http.StatusNotFound, err) apiError(ctx, http.StatusNotFound, err)
return return
} }

View File

@ -188,7 +188,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
meta, err := git_model.GetLFSMetaObjectByOid(ctx, ctx.Repo.Repository.ID, pointer.Oid) meta, err := git_model.GetLFSMetaObjectByOid(ctx, ctx.Repo.Repository.ID, pointer.Oid)
// If there isn't one, just serve the data directly // If there isn't one, just serve the data directly
if err == git_model.ErrLFSObjectNotExist { if errors.Is(err, git_model.ErrLFSObjectNotExist) {
// Handle caching for the blob SHA (not the LFS object OID) // Handle caching for the blob SHA (not the LFS object OID)
if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) { if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
return return

View File

@ -4,6 +4,7 @@
package repo package repo
import ( import (
"errors"
"net/http" "net/http"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -82,7 +83,7 @@ func GetGitHook(ctx *context.APIContext) {
hookID := ctx.PathParam("id") hookID := ctx.PathParam("id")
hook, err := ctx.Repo.GitRepo.GetHook(hookID) hook, err := ctx.Repo.GitRepo.GetHook(hookID)
if err != nil { if err != nil {
if err == git.ErrNotValidHook { if errors.Is(err, git.ErrNotValidHook) {
ctx.NotFound() ctx.NotFound()
} else { } else {
ctx.Error(http.StatusInternalServerError, "GetHook", err) ctx.Error(http.StatusInternalServerError, "GetHook", err)
@ -129,7 +130,7 @@ func EditGitHook(ctx *context.APIContext) {
hookID := ctx.PathParam("id") hookID := ctx.PathParam("id")
hook, err := ctx.Repo.GitRepo.GetHook(hookID) hook, err := ctx.Repo.GitRepo.GetHook(hookID)
if err != nil { if err != nil {
if err == git.ErrNotValidHook { if errors.Is(err, git.ErrNotValidHook) {
ctx.NotFound() ctx.NotFound()
} else { } else {
ctx.Error(http.StatusInternalServerError, "GetHook", err) ctx.Error(http.StatusInternalServerError, "GetHook", err)
@ -178,7 +179,7 @@ func DeleteGitHook(ctx *context.APIContext) {
hookID := ctx.PathParam("id") hookID := ctx.PathParam("id")
hook, err := ctx.Repo.GitRepo.GetHook(hookID) hook, err := ctx.Repo.GitRepo.GetHook(hookID)
if err != nil { if err != nil {
if err == git.ErrNotValidHook { if errors.Is(err, git.ErrNotValidHook) {
ctx.NotFound() ctx.NotFound()
} else { } else {
ctx.Error(http.StatusInternalServerError, "GetHook", err) ctx.Error(http.StatusInternalServerError, "GetHook", err)

View File

@ -132,12 +132,11 @@ func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, all
} }
var err error var err error
if p, err = packages_model.TryInsertPackage(ctx, p); err != nil { if p, err = packages_model.TryInsertPackage(ctx, p); err != nil {
if err == packages_model.ErrDuplicatePackage { if !errors.Is(err, packages_model.ErrDuplicatePackage) {
packageCreated = false
} else {
log.Error("Error inserting package: %v", err) log.Error("Error inserting package: %v", err)
return nil, false, err return nil, false, err
} }
packageCreated = false
} }
if packageCreated { if packageCreated {
@ -163,11 +162,10 @@ func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, all
MetadataJSON: string(metadataJSON), MetadataJSON: string(metadataJSON),
} }
if pv, err = packages_model.GetOrInsertVersion(ctx, pv); err != nil { if pv, err = packages_model.GetOrInsertVersion(ctx, pv); err != nil {
if err == packages_model.ErrDuplicatePackageVersion { if errors.Is(err, packages_model.ErrDuplicatePackageVersion) && allowDuplicate {
versionCreated = false versionCreated = false
} } else {
if err != packages_model.ErrDuplicatePackageVersion || !allowDuplicate { log.Error("Error inserting package: %v", err) // other error, or disallowing duplicates
log.Error("Error inserting package: %v", err)
return nil, false, err return nil, false, err
} }
} }
@ -433,7 +431,7 @@ func GetOrCreateInternalPackageVersion(ctx context.Context, ownerID int64, packa
} }
var err error var err error
if p, err = packages_model.TryInsertPackage(ctx, p); err != nil { if p, err = packages_model.TryInsertPackage(ctx, p); err != nil {
if err != packages_model.ErrDuplicatePackage { if !errors.Is(err, packages_model.ErrDuplicatePackage) {
log.Error("Error inserting package: %v", err) log.Error("Error inserting package: %v", err)
return err return err
} }

View File

@ -52,24 +52,33 @@ func TestPackageNpm(t *testing.T) {
"` + packageTag + `": "` + version + `" "` + packageTag + `": "` + version + `"
}, },
"versions": { "versions": {
"` + version + `": { "` + version + `": {
"name": "` + packageName + `", "name": "` + packageName + `",
"version": "` + version + `", "version": "` + version + `",
"description": "` + packageDescription + `", "description": "` + packageDescription + `",
"author": { "author": {
"name": "` + packageAuthor + `" "name": "` + packageAuthor + `"
}, },
"bin": { "bin": {
"` + packageBinName + `": "` + packageBinPath + `" "` + packageBinName + `": "` + packageBinPath + `"
}, },
"dist": { "dist": {
"integrity": "sha512-yA4FJsVhetynGfOC1jFf79BuS+jrHbm0fhh+aHzCQkOaOBXKf9oBnC4a6DnLLnEsHQDRLYd00cwj8sCXpC+wIg==", "integrity": "sha512-yA4FJsVhetynGfOC1jFf79BuS+jrHbm0fhh+aHzCQkOaOBXKf9oBnC4a6DnLLnEsHQDRLYd00cwj8sCXpC+wIg==",
"shasum": "aaa7eaf852a948b0aa05afeda35b1badca155d90" "shasum": "aaa7eaf852a948b0aa05afeda35b1badca155d90"
}, },
"repository": { "repository": {
"type": "` + repoType + `", "type": "` + repoType + `",
"url": "` + repoURL + `" "url": "` + repoURL + `"
} },
"peerDependencies": {
"tea": "2.x",
"soy-milk": "1.2"
},
"peerDependenciesMeta": {
"soy-milk": {
"optional": true
}
}
} }
}, },
"_attachments": { "_attachments": {
@ -178,6 +187,8 @@ func TestPackageNpm(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%s%s/-/%s/%s", setting.AppURL, root[1:], packageVersion, filename), pmv.Dist.Tarball) assert.Equal(t, fmt.Sprintf("%s%s/-/%s/%s", setting.AppURL, root[1:], packageVersion, filename), pmv.Dist.Tarball)
assert.Equal(t, repoType, result.Repository.Type) assert.Equal(t, repoType, result.Repository.Type)
assert.Equal(t, repoURL, result.Repository.URL) assert.Equal(t, repoURL, result.Repository.URL)
assert.Equal(t, map[string]string{"tea": "2.x", "soy-milk": "1.2"}, pmv.PeerDependencies)
assert.Equal(t, map[string]any{"soy-milk": map[string]any{"optional": true}}, pmv.PeerDependenciesMeta)
}) })
t.Run("AddTag", func(t *testing.T) { t.Run("AddTag", func(t *testing.T) {