diff --git a/lib/api/api.rb b/lib/api/api.rb index a2bdb76b8345dd63685dfde94949ff531f015e07..6949cfa8e4940a39a47dd6ca0a6ffabe1f880f62 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -156,6 +156,7 @@ class API < Grape::API mount ::API::ProtectedTags mount ::API::Releases mount ::API::Release::Links + mount ::API::RemoteMirrors mount ::API::Repositories mount ::API::Runner mount ::API::Runners diff --git a/lib/api/entities.rb b/lib/api/entities.rb index ead2ea34227c3289e7b9a5a0d14fc6927a3c9092..64a13df5b30a5d11d52f5d9424d37468f2197bf2 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -166,6 +166,18 @@ class ProjectExportStatus < ProjectIdentity end end + class RemoteMirror < Grape::Entity + expose :id + expose :enabled + expose :safe_url, as: :url + expose :update_status + expose :last_update_at + expose :last_update_started_at + expose :last_successful_update_at + expose :last_error + expose :only_protected_branches + end + class ProjectImportStatus < ProjectIdentity expose :import_status diff --git a/lib/api/remote_mirrors.rb b/lib/api/remote_mirrors.rb new file mode 100644 index 0000000000000000000000000000000000000000..8a085517ce90342a348d66beb88db5828324d446 --- /dev/null +++ b/lib/api/remote_mirrors.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module API + class RemoteMirrors < Grape::API + include PaginationParams + + before do + # TODO: Remove flag: https://gitlab.com/gitlab-org/gitlab/issues/38121 + not_found! unless Feature.enabled?(:remote_mirrors_api, user_project) + end + + params do + requires :id, type: String, desc: 'The ID of a project' + end + resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do + desc "List the project's remote mirrors" do + success Entities::RemoteMirror + end + params do + use :pagination + end + get ':id/remote_mirrors' do + unauthorized! unless can?(current_user, :admin_remote_mirror, user_project) + + present paginate(user_project.remote_mirrors), + with: Entities::RemoteMirror + end + end + end +end diff --git a/spec/fixtures/api/schemas/remote_mirror.json b/spec/fixtures/api/schemas/remote_mirror.json new file mode 100644 index 0000000000000000000000000000000000000000..416b0f080d9124622c2b15ae44953c62c70de2f2 --- /dev/null +++ b/spec/fixtures/api/schemas/remote_mirror.json @@ -0,0 +1,26 @@ +{ + "type": "object", + "required": [ + "id", + "enabled", + "url", + "update_status", + "last_update_at", + "last_update_started_at", + "last_successful_update_at", + "last_error", + "only_protected_branches" + ], + "properties": { + "id": { "type": "integer" }, + "enabled": { "type": "boolean" }, + "url": { "type": "string" }, + "update_status": { "type": "string" }, + "last_update_at": { "type": ["string", "null"] }, + "last_update_started_at": { "type": ["string", "null"] }, + "last_successful_update_at": { "type": ["string", "null"] }, + "last_error": { "type": ["string", "null"] }, + "only_protected_branches": { "type": "boolean" } + }, + "additionalProperties": false +} diff --git a/spec/fixtures/api/schemas/remote_mirrors.json b/spec/fixtures/api/schemas/remote_mirrors.json new file mode 100644 index 0000000000000000000000000000000000000000..3c4600c6caa75f9f17ed0d87392f739304d04a21 --- /dev/null +++ b/spec/fixtures/api/schemas/remote_mirrors.json @@ -0,0 +1,4 @@ +{ + "type": "array", + "items": { "$ref": "remote_mirror.json" } +} diff --git a/spec/requests/api/remote_mirrors_spec.rb b/spec/requests/api/remote_mirrors_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..c5ba9bd223e199b55d7914b8f1ea82620b063480 --- /dev/null +++ b/spec/requests/api/remote_mirrors_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe API::RemoteMirrors do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project, :repository, :remote_mirror) } + + describe 'GET /projects/:id/remote_mirrors' do + let(:route) { "/projects/#{project.id}/remote_mirrors" } + + it 'requires `admin_remote_mirror` permission' do + project.add_developer(user) + + get api(route, user) + + expect(response).to have_gitlab_http_status(:unauthorized) + end + + it 'returns a list of remote mirrors' do + project.add_maintainer(user) + + get api(route, user) + + expect(response).to have_gitlab_http_status(:success) + expect(response).to match_response_schema('remote_mirrors') + end + + context 'with the `remote_mirrors_api` feature disabled' do + before do + stub_feature_flags(remote_mirrors_api: false) + end + + it 'responds with `not_found`' do + get api(route, user) + + expect(response).to have_gitlab_http_status(:not_found) + end + end + end +end