Skip to content

Commit a96de36

Browse files
committed
Allow empty repository name.
Change delete endpoint.
1 parent 52ee571 commit a96de36

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

routers/api/packages/api.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,24 +155,25 @@ func CommonRoutes() *web.Router {
155155
pathFields := strings.Split(path, "/")
156156
pathFieldsLen := len(pathFields)
157157

158-
if pathFieldsLen >= 2 {
158+
if (ctx.Req.Method == "HEAD" || ctx.Req.Method == "GET") && pathFieldsLen >= 2 {
159159
ctx.SetPathParam("repository", strings.Join(pathFields[:pathFieldsLen-2], "/"))
160160
ctx.SetPathParam("architecture", pathFields[pathFieldsLen-2])
161161
ctx.SetPathParam("filename", pathFields[pathFieldsLen-1])
162+
arch.GetPackageOrRepositoryFile(ctx)
163+
return
164+
}
162165

163-
if ctx.Req.Method == "HEAD" || ctx.Req.Method == "GET" {
164-
arch.GetPackageOrRepositoryFile(ctx)
165-
return
166-
}
167-
168-
if ctx.Req.Method == "DELETE" {
169-
reqPackageAccess(perm.AccessModeWrite)(ctx)
170-
if ctx.Written() {
171-
return
172-
}
173-
arch.DeletePackageFile(ctx)
166+
if ctx.Req.Method == "DELETE" && pathFieldsLen >= 3 {
167+
reqPackageAccess(perm.AccessModeWrite)(ctx)
168+
if ctx.Written() {
174169
return
175170
}
171+
ctx.SetPathParam("repository", strings.Join(pathFields[:pathFieldsLen-3], "/"))
172+
ctx.SetPathParam("architecture", pathFields[pathFieldsLen-3])
173+
ctx.SetPathParam("name", pathFields[pathFieldsLen-2])
174+
ctx.SetPathParam("version", pathFields[pathFieldsLen-1])
175+
arch.DeletePackageVersion(ctx)
176+
return
176177
}
177178

178179
ctx.Status(http.StatusNotFound)

routers/api/packages/arch/arch.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ func GetRepositoryKey(ctx *context.Context) {
4343

4444
func UploadPackageFile(ctx *context.Context) {
4545
repository := strings.TrimSpace(ctx.PathParam("repository"))
46-
if repository == "" {
47-
apiError(ctx, http.StatusBadRequest, "invalid repository")
48-
return
49-
}
5046

5147
upload, needToClose, err := ctx.UploadStream()
5248
if err != nil {
@@ -256,8 +252,11 @@ func GetPackageOrRepositoryFile(ctx *context.Context) {
256252
helper.ServePackageFile(ctx, s, u, pf)
257253
}
258254

259-
func DeletePackageFile(ctx *context.Context) {
260-
repository, architecture := ctx.PathParam("repository"), ctx.PathParam("architecture")
255+
func DeletePackageVersion(ctx *context.Context) {
256+
repository := ctx.PathParam("repository")
257+
architecture := ctx.PathParam("architecture")
258+
name := ctx.PathParam("name")
259+
version := ctx.PathParam("version")
261260

262261
release, err := arch_service.AquireRegistryLock(ctx, ctx.Package.Owner.ID)
263262
if err != nil {
@@ -266,10 +265,18 @@ func DeletePackageFile(ctx *context.Context) {
266265
}
267266
defer release()
268267

268+
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeArch, name, version)
269+
if err != nil {
270+
if errors.Is(err, util.ErrNotExist) {
271+
apiError(ctx, http.StatusNotFound, err)
272+
} else {
273+
apiError(ctx, http.StatusInternalServerError, err)
274+
}
275+
return
276+
}
277+
269278
pfs, _, err := packages_model.SearchFiles(ctx, &packages_model.PackageFileSearchOptions{
270-
OwnerID: ctx.Package.Owner.ID,
271-
PackageType: packages_model.TypeArch,
272-
Query: ctx.PathParam("filename"),
279+
VersionID: pv.ID,
273280
CompositeKey: fmt.Sprintf("%s|%s", repository, architecture),
274281
})
275282
if err != nil {

tests/integration/api_packages_arch_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ license = MIT`)
8181
}
8282

8383
compressions := []string{"gz", "xz", "zst"}
84-
repositories := []string{"main", "testing", "with/slash"}
84+
repositories := []string{"main", "testing", "with/slash", ""}
8585

8686
rootURL := fmt.Sprintf("/api/packages/%s/arch", user.Name)
8787

@@ -277,18 +277,18 @@ license = MIT`)
277277
MakeRequest(t, req, http.StatusOK)
278278
}
279279

280-
req = NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/any/%s-%s-any.pkg.tar.%s", rootURL, repository, packageName+"_"+arch_module.AnyArch, packageVersion, compression)).
280+
req = NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/any/%s/%s", rootURL, repository, packageName+"_"+arch_module.AnyArch, packageVersion)).
281281
AddBasicAuth(user.Name)
282282
MakeRequest(t, req, http.StatusNoContent)
283283
})
284284

285285
t.Run("Delete", func(t *testing.T) {
286286
defer tests.PrintCurrentTest(t)()
287287

288-
req := NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/aarch64/%s-%s-aarch64.pkg.tar.%s", rootURL, repository, packageName, packageVersion, compression))
288+
req := NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/aarch64/%s/%s", rootURL, repository, packageName, packageVersion))
289289
MakeRequest(t, req, http.StatusUnauthorized)
290290

291-
req = NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/aarch64/%s-%s-aarch64.pkg.tar.%s", rootURL, repository, packageName, packageVersion, compression)).
291+
req = NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/aarch64/%s/%s", rootURL, repository, packageName, packageVersion)).
292292
AddBasicAuth(user.Name)
293293
MakeRequest(t, req, http.StatusNoContent)
294294

0 commit comments

Comments
 (0)