Commit 5a9a8d21 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'rs-remote-mirrors-update-api' into 'master'

Add API for updating a remote mirror's attributes

Closes #194105

See merge request gitlab-org/gitlab!22433
parents c9a80dc0 8d9191fd
......@@ -7,6 +7,8 @@ module 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 @@ module 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
......@@ -5,14 +5,13 @@ require 'spec_helper'
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 @@ describe API::RemoteMirrors do
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 @@ describe API::RemoteMirrors do
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
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