Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple files in generic packages #20661

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions docs/content/doc/packages/generic.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ To authenticate to the Package Registry, you need to provide [custom HTTP header
## Publish a package

To publish a generic package perform a HTTP PUT operation with the package content in the request body.
You cannot publish a package if a package of the same name and version already exists. You must delete the existing package first.
You cannot publish a file with the same name twice to a package. You must delete the existing package version first.

```
PUT https://gitea.example.com/api/packages/{owner}/generic/{package_name}/{package_version}/{file_name}
Expand Down Expand Up @@ -55,7 +55,7 @@ The server reponds with the following HTTP Status codes.
| HTTP Status Code | Meaning |
| ----------------- | ------- |
| `201 Created` | The package has been published. |
| `400 Bad Request` | The package name and/or version are invalid or a package with the same name and version already exist. |
| `400 Bad Request` | The package name and/or version and/or file name are invalid or a file with the same name exist. |

## Download a package

Expand All @@ -80,3 +80,38 @@ Example request using HTTP Basic authentication:
curl --user your_username:your_token_or_password \
https://gitea.example.com/api/packages/testuser/generic/test_package/1.0.0/file.bin
```

The server reponds with the following HTTP Status codes.

| HTTP Status Code | Meaning |
| ----------------- | ------- |
| `200 OK` | Success |
| `404 Not Found` | The package or file was not found. |

## Delete a package

To delete a generic package perform a HTTP DELETE operation. This will delete all files of this version.

```
DELETE https://gitea.example.com/api/packages/{owner}/generic/{package_name}/{package_version}
```

| Parameter | Description |
| ----------------- | ----------- |
| `owner` | The owner of the package. |
| `package_name` | The package name. |
| `package_version` | The package version. |

Example request using HTTP Basic authentication:

```shell
curl --user your_username:your_token_or_password -X DELETE \
https://gitea.example.com/api/packages/testuser/generic/test_package/1.0.0
```

The server reponds with the following HTTP Status codes.

| HTTP Status Code | Meaning |
| ----------------- | ------- |
| `200 OK` | Success |
| `404 Not Found` | The package was not found. |
78 changes: 53 additions & 25 deletions integrations/api_packages_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func TestPackageGeneric(t *testing.T) {
filename := "fi-le_na.me"
content := []byte{1, 2, 3}

url := fmt.Sprintf("/api/packages/%s/generic/%s/%s/%s", user.Name, packageName, packageVersion, filename)
url := fmt.Sprintf("/api/packages/%s/generic/%s/%s", user.Name, packageName, packageVersion)

t.Run("Upload", func(t *testing.T) {
defer PrintCurrentTest(t)()

req := NewRequestWithBody(t, "PUT", url, bytes.NewReader(content))
req := NewRequestWithBody(t, "PUT", url+"/"+filename, bytes.NewReader(content))
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusCreated)

Expand All @@ -55,28 +55,60 @@ func TestPackageGeneric(t *testing.T) {
pb, err := packages.GetBlobByID(db.DefaultContext, pfs[0].BlobID)
assert.NoError(t, err)
assert.Equal(t, int64(len(content)), pb.Size)
})

t.Run("UploadExists", func(t *testing.T) {
defer PrintCurrentTest(t)()
t.Run("Exists", func(t *testing.T) {
defer PrintCurrentTest(t)()

req := NewRequestWithBody(t, "PUT", url, bytes.NewReader(content))
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusBadRequest)
req := NewRequestWithBody(t, "PUT", url+"/"+filename, bytes.NewReader(content))
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusBadRequest)
})

t.Run("Additional", func(t *testing.T) {
defer PrintCurrentTest(t)()

req := NewRequestWithBody(t, "PUT", url+"/dummy.bin", bytes.NewReader(content))
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusCreated)

// Check deduplication
pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID)
assert.NoError(t, err)
assert.Len(t, pfs, 2)
assert.Equal(t, pfs[0].BlobID, pfs[1].BlobID)
})
})

t.Run("Download", func(t *testing.T) {
defer PrintCurrentTest(t)()

req := NewRequest(t, "GET", url)
checkDownloadCount := func(count int64) {
pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeGeneric)
assert.NoError(t, err)
assert.Len(t, pvs, 1)
assert.Equal(t, count, pvs[0].DownloadCount)
}

checkDownloadCount(0)

req := NewRequest(t, "GET", url+"/"+filename)
resp := MakeRequest(t, req, http.StatusOK)

assert.Equal(t, content, resp.Body.Bytes())

pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeGeneric)
assert.NoError(t, err)
assert.Len(t, pvs, 1)
assert.Equal(t, int64(1), pvs[0].DownloadCount)
checkDownloadCount(1)

req = NewRequest(t, "GET", url+"/dummy.bin")
MakeRequest(t, req, http.StatusOK)

checkDownloadCount(2)

t.Run("NotExists", func(t *testing.T) {
defer PrintCurrentTest(t)()

req := NewRequest(t, "GET", url+"/not.found")
MakeRequest(t, req, http.StatusNotFound)
})
})

t.Run("Delete", func(t *testing.T) {
Expand All @@ -89,20 +121,16 @@ func TestPackageGeneric(t *testing.T) {
pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeGeneric)
assert.NoError(t, err)
assert.Empty(t, pvs)
})

t.Run("DownloadNotExists", func(t *testing.T) {
defer PrintCurrentTest(t)()
t.Run("NotExists", func(t *testing.T) {
defer PrintCurrentTest(t)()

req := NewRequest(t, "GET", url)
MakeRequest(t, req, http.StatusNotFound)
})

t.Run("DeleteNotExists", func(t *testing.T) {
defer PrintCurrentTest(t)()
req := NewRequest(t, "GET", url+"/"+filename)
MakeRequest(t, req, http.StatusNotFound)

req := NewRequest(t, "DELETE", url)
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusNotFound)
req = NewRequest(t, "DELETE", url)
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusNotFound)
})
})
}
12 changes: 6 additions & 6 deletions routers/api/packages/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ func Routes() *web.Route {
})
})
r.Group("/generic", func() {
r.Group("/{packagename}/{packageversion}/{filename}", func() {
r.Get("", generic.DownloadPackageFile)
r.Group("", func() {
r.Put("", generic.UploadPackage)
r.Delete("", generic.DeletePackage)
}, reqPackageAccess(perm.AccessModeWrite))
r.Group("/{packagename}/{packageversion}", func() {
r.Delete("", reqPackageAccess(perm.AccessModeWrite), generic.DeletePackage)
r.Group("/{filename}", func() {
r.Get("", generic.DownloadPackageFile)
r.Put("", reqPackageAccess(perm.AccessModeWrite), generic.UploadPackage)
})
})
})
r.Group("/helm", func() {
Expand Down
58 changes: 19 additions & 39 deletions routers/api/packages/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,16 @@ func apiError(ctx *context.Context, status int, obj interface{}) {

// DownloadPackageFile serves the specific generic package.
func DownloadPackageFile(ctx *context.Context) {
packageName, packageVersion, filename, err := sanitizeParameters(ctx)
if err != nil {
apiError(ctx, http.StatusBadRequest, err)
return
}

s, pf, err := packages_service.GetFileStreamByPackageNameAndVersion(
ctx,
&packages_service.PackageInfo{
Owner: ctx.Package.Owner,
PackageType: packages_model.TypeGeneric,
Name: packageName,
Version: packageVersion,
Name: ctx.Params("packagename"),
Version: ctx.Params("packageversion"),
},
&packages_service.PackageFileInfo{
Filename: filename,
Filename: ctx.Params("filename"),
},
)
if err != nil {
Expand All @@ -65,9 +59,17 @@ func DownloadPackageFile(ctx *context.Context) {
// UploadPackage uploads the specific generic package.
// Duplicated packages get rejected.
func UploadPackage(ctx *context.Context) {
packageName, packageVersion, filename, err := sanitizeParameters(ctx)
if err != nil {
apiError(ctx, http.StatusBadRequest, err)
packageName := ctx.Params("packagename")
filename := ctx.Params("filename")

if !packageNameRegex.MatchString(packageName) || !filenameRegex.MatchString(filename) {
apiError(ctx, http.StatusBadRequest, errors.New("Invalid package name or filename"))
return
}

packageVersion := strings.TrimSpace(ctx.Params("packageversion"))
if packageVersion == "" {
apiError(ctx, http.StatusBadRequest, errors.New("Invalid package version"))
return
}

Expand All @@ -88,7 +90,7 @@ func UploadPackage(ctx *context.Context) {
}
defer buf.Close()

_, _, err = packages_service.CreatePackageAndAddFile(
_, _, err = packages_service.CreatePackageOrAddFileToExisting(
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
Owner: ctx.Package.Owner,
Expand All @@ -107,7 +109,7 @@ func UploadPackage(ctx *context.Context) {
},
)
if err != nil {
if err == packages_model.ErrDuplicatePackageVersion {
if err == packages_model.ErrDuplicatePackageFile {
KN4CK3R marked this conversation as resolved.
Show resolved Hide resolved
apiError(ctx, http.StatusBadRequest, err)
return
}
Expand All @@ -120,19 +122,13 @@ func UploadPackage(ctx *context.Context) {

// DeletePackage deletes the specific generic package.
func DeletePackage(ctx *context.Context) {
packageName, packageVersion, _, err := sanitizeParameters(ctx)
if err != nil {
apiError(ctx, http.StatusBadRequest, err)
return
}

err = packages_service.RemovePackageVersionByNameAndVersion(
err := packages_service.RemovePackageVersionByNameAndVersion(
ctx.Doer,
&packages_service.PackageInfo{
Owner: ctx.Package.Owner,
PackageType: packages_model.TypeGeneric,
Name: packageName,
Version: packageVersion,
Name: ctx.Params("packagename"),
Version: ctx.Params("packageversion"),
},
)
if err != nil {
Expand All @@ -146,19 +142,3 @@ func DeletePackage(ctx *context.Context) {

ctx.Status(http.StatusOK)
}

func sanitizeParameters(ctx *context.Context) (string, string, string, error) {
packageName := ctx.Params("packagename")
filename := ctx.Params("filename")

if !packageNameRegex.MatchString(packageName) || !filenameRegex.MatchString(filename) {
return "", "", "", errors.New("Invalid package name or filename")
}

packageVersion := strings.TrimSpace(ctx.Params("packageversion"))
if packageVersion == "" {
return "", "", "", errors.New("Invalid package version")
}

return packageName, packageVersion, filename, nil
}