From 8d9191fd64cad4b0a3d58023e616a5c9085acb92 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Fri, 3 Jan 2020 12:20:03 -0600 Subject: [PATCH] Add API for updating a remote mirror's attributes Currently only supports `enabled` and `only_protected_branches` attributes. --- lib/api/remote_mirrors.rb | 30 +++++++++++++++-- spec/requests/api/remote_mirrors_spec.rb | 43 ++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/lib/api/remote_mirrors.rb b/lib/api/remote_mirrors.rb index 8a085517ce9034..953139661333fe 100644 --- a/lib/api/remote_mirrors.rb +++ b/lib/api/remote_mirrors.rb @@ -7,6 +7,8 @@ class RemoteMirrors < Grape::API before do # TODO: Remove flag: https://gitlab.com/gitlab-org/gitlab/issues/38121 not_found! unless Feature.enabled?(:remote_mirrors_api, user_project) + + unauthorized! unless can?(current_user, :admin_remote_mirror, user_project) end params do @@ -20,11 +22,35 @@ class RemoteMirrors < Grape::API 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 + + desc 'Update the attributes of a single remote mirror' do + success Entities::RemoteMirror + end + params do + requires :mirror_id, type: String, desc: 'The ID of a remote mirror' + optional :enabled, type: Boolean, desc: 'Determines if the mirror is enabled' + optional :only_protected_branches, type: Boolean, desc: 'Determines if only protected branches are mirrored' + end + put ':id/remote_mirrors/:mirror_id' do + mirror = user_project.remote_mirrors.find(params[:mirror_id]) + + mirror_params = declared_params(include_missing: false) + mirror_params[:id] = mirror_params.delete(:mirror_id) + update_params = { remote_mirrors_attributes: mirror_params } + + result = ::Projects::UpdateService + .new(user_project, current_user, update_params) + .execute + + if result[:status] == :success + present mirror.reset, with: Entities::RemoteMirror + else + render_api_error!(result[:message], result[:http_status]) + end + end end end end diff --git a/spec/requests/api/remote_mirrors_spec.rb b/spec/requests/api/remote_mirrors_spec.rb index c5ba9bd223e199..065d9c7ca5bf4c 100644 --- a/spec/requests/api/remote_mirrors_spec.rb +++ b/spec/requests/api/remote_mirrors_spec.rb @@ -5,14 +5,13 @@ describe API::RemoteMirrors do let_it_be(:user) { create(:user) } let_it_be(:project) { create(:project, :repository, :remote_mirror) } + let_it_be(:developer) { create(:user) { |u| project.add_developer(u) } } 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) + get api(route, developer) expect(response).to have_gitlab_http_status(:unauthorized) end @@ -26,6 +25,7 @@ expect(response).to match_response_schema('remote_mirrors') end + # TODO: Remove flag: https://gitlab.com/gitlab-org/gitlab/issues/38121 context 'with the `remote_mirrors_api` feature disabled' do before do stub_feature_flags(remote_mirrors_api: false) @@ -38,4 +38,41 @@ end end end + + describe 'PUT /projects/:id/remote_mirrors/:mirror_id' do + let(:route) { ->(id) { "/projects/#{project.id}/remote_mirrors/#{id}" } } + let(:mirror) { project.remote_mirrors.first } + + it 'requires `admin_remote_mirror` permission' do + put api(route[mirror.id], developer) + + expect(response).to have_gitlab_http_status(:unauthorized) + end + + it 'updates a remote mirror' do + project.add_maintainer(user) + + put api(route[mirror.id], user), params: { + enabled: '0', + only_protected_branches: 'true' + } + + expect(response).to have_gitlab_http_status(:success) + expect(json_response['enabled']).to eq(false) + expect(json_response['only_protected_branches']).to eq(true) + end + + # TODO: Remove flag: https://gitlab.com/gitlab-org/gitlab/issues/38121 + 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 + put api(route[mirror.id], user) + + expect(response).to have_gitlab_http_status(:not_found) + end + end + end end -- GitLab