Commit 8dd127e1 authored by Valery Sizov's avatar Valery Sizov Committed by charlie ablett

Add repository storage data to REST API

parent 4e3b102b
...@@ -2597,3 +2597,28 @@ GET /projects/:id/snapshot ...@@ -2597,3 +2597,28 @@ GET /projects/:id/snapshot
|-----------|----------------|------------------------|-------------| |-----------|----------------|------------------------|-------------|
| `id` | integer/string | **{check-circle}** Yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding). | | `id` | integer/string | **{check-circle}** Yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding). |
| `wiki` | boolean | **{dotted-circle}** No | Whether to download the wiki, rather than project, repository. | | `wiki` | boolean | **{dotted-circle}** No | Whether to download the wiki, rather than project, repository. |
## Get the path to repository storage
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/29861) in GitLab 14.0.
Get the path to repository storage for specified project. Available for administrators only.
```plaintext
GET /projects/:id/storage
```
| Attribute | Type | Required | Description |
|--------------|----------------|------------------------|-------------|
| `id` | integer/string | **{check-circle}** Yes | ID or [URL-encoded path of the project](README.md#namespaced-path-encoding). |
```json
[
{
"project_id": 1,
"disk_path": "@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
"created_at": "2012-10-12T17:04:47Z",
"repository_storage": "default"
}
]
```
# frozen_string_literal: true
module API
module Entities
class ProjectRepositoryStorage < Grape::Entity
include Gitlab::Routing
expose :disk_path do |project|
project.repository.disk_path
end
expose :id, as: :project_id
expose :repository_storage, :created_at
end
end
end
...@@ -660,6 +660,18 @@ module API ...@@ -660,6 +660,18 @@ module API
render_api_error!("Failed to transfer project #{user_project.errors.messages}", 400) render_api_error!("Failed to transfer project #{user_project.errors.messages}", 400)
end end
end end
desc 'Show the storage information' do
success Entities::ProjectRepositoryStorage
end
params do
requires :id, type: String, desc: 'ID of a project'
end
get ':id/storage', feature_category: :projects do
authenticated_as_admin!
present user_project, with: Entities::ProjectRepositoryStorage, current_user: current_user
end
end end
end end
end end
......
...@@ -3864,6 +3864,48 @@ RSpec.describe API::Projects do ...@@ -3864,6 +3864,48 @@ RSpec.describe API::Projects do
end end
end end
describe 'GET /projects/:id/storage' do
context 'when unauthenticated' do
it 'does not return project storage data' do
get api("/projects/#{project.id}/storage")
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
it 'returns project storage data when user is admin' do
get api("/projects/#{project.id}/storage", create(:admin))
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['project_id']).to eq(project.id)
expect(json_response['disk_path']).to eq(project.repository.disk_path)
expect(json_response['created_at']).to be_present
expect(json_response['repository_storage']).to eq(project.repository_storage)
end
it 'does not return project storage data when user is not admin' do
get api("/projects/#{project.id}/storage", user3)
expect(response).to have_gitlab_http_status(:forbidden)
end
it 'responds with a 401 for unauthenticated users trying to access a non-existent project id' do
expect(Project.find_by(id: non_existing_record_id)).to be_nil
get api("/projects/#{non_existing_record_id}/storage")
expect(response).to have_gitlab_http_status(:unauthorized)
end
it 'responds with a 403 for non-admin users trying to access a non-existent project id' do
expect(Project.find_by(id: non_existing_record_id)).to be_nil
get api("/projects/#{non_existing_record_id}/storage", user3)
expect(response).to have_gitlab_http_status(:forbidden)
end
end
it_behaves_like 'custom attributes endpoints', 'projects' do it_behaves_like 'custom attributes endpoints', 'projects' do
let(:attributable) { project } let(:attributable) { project }
let(:other_attributable) { project2 } let(:other_attributable) { project2 }
......
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