From 519cf46c124e8d207aa1f91c595511c98da6e03d Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 12 Nov 2019 17:56:49 -0600 Subject: [PATCH] Add a `remote_mirrors` project API This API can be used to gather information on a project's push mirrors and their status. --- lib/api/api.rb | 1 + lib/api/entities.rb | 12 ++++++ lib/api/remote_mirrors.rb | 30 ++++++++++++++ spec/fixtures/api/schemas/remote_mirror.json | 26 ++++++++++++ spec/fixtures/api/schemas/remote_mirrors.json | 4 ++ spec/requests/api/remote_mirrors_spec.rb | 41 +++++++++++++++++++ 6 files changed, 114 insertions(+) create mode 100644 lib/api/remote_mirrors.rb create mode 100644 spec/fixtures/api/schemas/remote_mirror.json create mode 100644 spec/fixtures/api/schemas/remote_mirrors.json create mode 100644 spec/requests/api/remote_mirrors_spec.rb diff --git a/lib/api/api.rb b/lib/api/api.rb index a2bdb76b8345dd..6949cfa8e4940a 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 ead2ea34227c32..64a13df5b30a5d 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 00000000000000..8a085517ce9034 --- /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 00000000000000..416b0f080d9124 --- /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 00000000000000..3c4600c6caa75f --- /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 00000000000000..c5ba9bd223e199 --- /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 -- GitLab