|
|
|
@ -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) {
|
|
|
|
|