Commit 18148d1d authored by Aditya Tiwari's avatar Aditya Tiwari Committed by Steve Abrams

Add response in Publish a package file endpoint

Add package file as a response for Publish a package file endpoint.
A new optional attribute select is introduced.
Select can take package_file as an input.

Changelog: changed
parent 8bf564c4
...@@ -48,6 +48,7 @@ PUT /projects/:id/packages/generic/:package_name/:package_version/:file_name?sta ...@@ -48,6 +48,7 @@ PUT /projects/:id/packages/generic/:package_name/:package_version/:file_name?sta
| `package_version` | string | yes | The package version. The following regex validates this: `\A(\.?[\w\+-]+\.?)+\z`. You can test your version strings on [Rubular](https://rubular.com/r/aNCV0wG5K14uq8). | `package_version` | string | yes | The package version. The following regex validates this: `\A(\.?[\w\+-]+\.?)+\z`. You can test your version strings on [Rubular](https://rubular.com/r/aNCV0wG5K14uq8).
| `file_name` | string | yes | The filename. It can contain only lowercase letters (`a-z`), uppercase letter (`A-Z`), numbers (`0-9`), dots (`.`), hyphens (`-`), or underscores (`_`). | `file_name` | string | yes | The filename. It can contain only lowercase letters (`a-z`), uppercase letter (`A-Z`), numbers (`0-9`), dots (`.`), hyphens (`-`), or underscores (`_`).
| `status` | string | no | The package status. It can be `default` (default) or `hidden`. Hidden packages do not appear in the UI or [package API list endpoints](../../../api/packages.md). | `status` | string | no | The package status. It can be `default` (default) or `hidden`. Hidden packages do not appear in the UI or [package API list endpoints](../../../api/packages.md).
| `select` | string | no | The response payload. By default, the response is empty. Valid values are: `package_file`. `package_file` returns details of the package file record created by this request.
Provide the file context in the request body. Provide the file context in the request body.
...@@ -59,7 +60,7 @@ curl --header "PRIVATE-TOKEN: <your_access_token>" \ ...@@ -59,7 +60,7 @@ curl --header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package/0.0.1/file.txt?status=hidden" "https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package/0.0.1/file.txt?status=hidden"
``` ```
Example response: Example response without attribute `select`:
```json ```json
{ {
...@@ -67,6 +68,34 @@ Example response: ...@@ -67,6 +68,34 @@ Example response:
} }
``` ```
Example response with attribute `select = package_file`:
```json
{
"id": 1,
"package_id": 1,
"created_at": "2021-10-12T12:05:23.387Z",
"updated_at": "2021-10-12T12:05:23.387Z",
"size": 0,
"file_store": 1,
"file_md5": null,
"file_sha1": null,
"file_name": "file.txt",
"file": {
"url": "/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b/packages/26/files/36/file.txt"
},
"file_sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"verification_retry_at": null,
"verified_at": null,
"verification_failure": null,
"verification_retry_count": null,
"verification_checksum": null,
"verification_state": 0,
"verification_started_at": null,
"new_file_path": null
}
```
### Publishing a package with the same name or version ### Publishing a package with the same name or version
When you publish a package with the same name and version as an existing package, the new package When you publish a package with the same name and version as an existing package, the new package
......
...@@ -54,6 +54,7 @@ module API ...@@ -54,6 +54,7 @@ module API
requires :file_name, type: String, desc: 'Package file name', regexp: Gitlab::Regex.generic_package_file_name_regex, file_path: true requires :file_name, type: String, desc: 'Package file name', regexp: Gitlab::Regex.generic_package_file_name_regex, file_path: true
optional :status, type: String, values: ALLOWED_STATUSES, desc: 'Package status' optional :status, type: String, values: ALLOWED_STATUSES, desc: 'Package status'
requires :file, type: ::API::Validations::Types::WorkhorseFile, desc: 'The package file to be published (generated by Multipart middleware)' requires :file, type: ::API::Validations::Types::WorkhorseFile, desc: 'The package file to be published (generated by Multipart middleware)'
optional :select, type: String, values: %w[package_file]
end end
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
...@@ -65,11 +66,15 @@ module API ...@@ -65,11 +66,15 @@ module API
track_package_event('push_package', :generic, project: project, user: current_user, namespace: project.namespace) track_package_event('push_package', :generic, project: project, user: current_user, namespace: project.namespace)
create_package_file_params = declared_params.merge(build: current_authenticated_job) create_package_file_params = declared_params.merge(build: current_authenticated_job)
::Packages::Generic::CreatePackageFileService package_file = ::Packages::Generic::CreatePackageFileService
.new(project, current_user, create_package_file_params) .new(project, current_user, create_package_file_params)
.execute .execute
if params[:select] == 'package_file'
present package_file
else
created! created!
end
rescue ObjectStorage::RemoteStoreError => e rescue ObjectStorage::RemoteStoreError => e
Gitlab::ErrorTracking.track_exception(e, extra: { file_name: params[:file_name], project_id: project.id }) Gitlab::ErrorTracking.track_exception(e, extra: { file_name: params[:file_name], project_id: project.id })
......
...@@ -297,6 +297,37 @@ RSpec.describe API::GenericPackages do ...@@ -297,6 +297,37 @@ RSpec.describe API::GenericPackages do
end end
end end
context 'with select' do
context 'with a valid value' do
context 'package_file' do
let(:params) { super().merge(select: 'package_file') }
it 'returns a package file' do
headers = workhorse_headers.merge(auth_header)
upload_file(params, headers)
aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to have_key('id')
end
end
end
end
context 'with an invalid value' do
let(:params) { super().merge(select: 'invalid_value') }
it 'returns a package file' do
headers = workhorse_headers.merge(auth_header)
upload_file(params, headers)
expect(response).to have_gitlab_http_status(:bad_request)
end
end
end
context 'with a status' do context 'with a status' do
context 'valid status' do context 'valid status' do
let(:params) { super().merge(status: 'hidden') } let(:params) { super().merge(status: 'hidden') }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment