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 2 commits
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
68 changes: 66 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,8 @@ 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. |
| `409 Conflict` | A file with the same name exist already in the package. |

## Download a package

Expand All @@ -80,3 +81,66 @@ 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 |
| ----------------- | ------- |
| `204 No Content` | Success |
| `404 Not Found` | The package was not found. |

## Delete a package file

To delete a file of a generic package perform a HTTP DELETE operation. This will delete the package version too if there is no file left.
KN4CK3R marked this conversation as resolved.
Show resolved Hide resolved
```
DELETE https://gitea.example.com/api/packages/{owner}/generic/{package_name}/{package_version}/{filename}
```

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

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/file.bin
```

The server reponds with the following HTTP Status codes.

| HTTP Status Code | Meaning |
| ----------------- | ------- |
| `204 No Content` | Success |
| `404 Not Found` | The package or file was not found. |
131 changes: 100 additions & 31 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,54 +55,123 @@ 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.StatusConflict)
})

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)
})

t.Run("Delete", func(t *testing.T) {
defer PrintCurrentTest(t)()
checkDownloadCount(1)

req := NewRequest(t, "DELETE", url)
AddBasicAuthHeader(req, user.Name)
req = NewRequest(t, "GET", url+"/dummy.bin")
MakeRequest(t, req, http.StatusOK)

pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeGeneric)
assert.NoError(t, err)
assert.Empty(t, pvs)
})
checkDownloadCount(2)

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)
req := NewRequest(t, "GET", url+"/not.found")
MakeRequest(t, req, http.StatusNotFound)
})
})

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

req := NewRequest(t, "DELETE", url)
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusNotFound)
t.Run("File", func(t *testing.T) {
defer PrintCurrentTest(t)()

req := NewRequest(t, "DELETE", url+"/"+filename)
MakeRequest(t, req, http.StatusUnauthorized)

req = NewRequest(t, "DELETE", url+"/"+filename)
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusNoContent)

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

req = NewRequest(t, "DELETE", url+"/"+filename)
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusNotFound)

pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeGeneric)
assert.NoError(t, err)
assert.Len(t, pvs, 1)

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

req = NewRequest(t, "DELETE", url+"/dummy.bin")
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusNoContent)

pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeGeneric)
assert.NoError(t, err)
assert.Empty(t, pvs)
})
})

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

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

req = NewRequest(t, "DELETE", url)
MakeRequest(t, req, http.StatusUnauthorized)

req = NewRequest(t, "DELETE", url)
AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusNoContent)

pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeGeneric)
assert.NoError(t, err)
assert.Empty(t, pvs)

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)
})
})
}
15 changes: 9 additions & 6 deletions routers/api/packages/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,15 @@ 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.Group("", func() {
r.Put("", generic.UploadPackage)
r.Delete("", generic.DeletePackageFile)
}, reqPackageAccess(perm.AccessModeWrite))
})
})
})
r.Group("/helm", func() {
Expand Down
Loading