1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
# frozen_string_literal: true
module API
class RemoteMirrors < ::API::Base
include PaginationParams
helpers Helpers::RemoteMirrorsHelpers
feature_category :source_code_management
before do
unauthorized! unless can?(current_user, :admin_remote_mirror, user_project)
end
helpers do
def find_remote_mirror
user_project.remote_mirrors.find(params[:mirror_id])
end
end
params do
requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of the project'
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc "List the project's remote mirrors" do
success code: 200, model: Entities::RemoteMirror
is_array true
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' }
]
tags %w[remote_mirrors]
end
params do
use :pagination
end
get ':id/remote_mirrors' do
present paginate(user_project.remote_mirrors),
with: Entities::RemoteMirror
end
desc 'Get a single remote mirror' do
success code: 200, model: Entities::RemoteMirror
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' }
]
tags %w[remote_mirrors]
end
params do
requires :mirror_id, type: String, desc: 'The ID of a remote mirror'
end
get ':id/remote_mirrors/:mirror_id' do
mirror = find_remote_mirror
present mirror, with: Entities::RemoteMirror
end
desc 'Triggers a push mirror operation' do
success code: 204
failure [
{ code: 400, message: 'Bad request' },
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' }
]
tags %w[remote_mirrors]
end
params do
requires :mirror_id, type: String, desc: 'The ID of a remote mirror'
end
post ':id/remote_mirrors/:mirror_id/sync' do
mirror = find_remote_mirror
result = ::RemoteMirrors::SyncService.new(user_project, current_user).execute(mirror)
if result.success?
status :no_content
else
render_api_error!(result[:message], 400)
end
end
desc 'Create remote mirror for a project' do
success code: 201, model: Entities::RemoteMirror
failure [
{ code: 400, message: 'Bad request' },
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' }
]
tags %w[remote_mirrors]
end
params do
requires :url, type: String, desc: 'The URL for a remote mirror', documentation: { example: 'https://*****:*****@example.com/gitlab/example.git' }
optional :enabled, type: Boolean, desc: 'Determines if the mirror is enabled', documentation: { example: false }
optional :auth_method, type: String, desc: 'Determines the mirror authentication method',
values: %w[ssh_public_key password]
optional :keep_divergent_refs, type: Boolean, desc: 'Determines if divergent refs are kept on the target',
documentation: { example: false }
use :mirror_branches_setting
end
post ':id/remote_mirrors' do
service = ::RemoteMirrors::CreateService.new(
user_project,
current_user,
declared_params(include_missing: false)
)
result = service.execute
if result.success?
present result.payload[:remote_mirror], with: Entities::RemoteMirror
else
render_api_error!(result.message, 400)
end
end
desc 'Update the attributes of a single remote mirror' do
success code: 200, model: Entities::RemoteMirror
failure [
{ code: 400, message: 'Bad request' },
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' }
]
tags %w[remote_mirrors]
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', documentation: { example: true }
optional :auth_method, type: String, desc: 'Determines the mirror authentication method'
optional :keep_divergent_refs, type: Boolean, desc: 'Determines if divergent refs are kept on the target',
documentation: { example: false }
use :mirror_branches_setting
end
put ':id/remote_mirrors/:mirror_id' do
mirror = find_remote_mirror
service = ::RemoteMirrors::UpdateService.new(
user_project,
current_user,
declared_params(include_missing: false)
)
result = service.execute(mirror)
render_api_error!(result.message, 400) if result.error?
present result.payload[:remote_mirror], with: Entities::RemoteMirror
end
desc 'Delete a single remote mirror' do
detail 'This feature was introduced in GitLab 14.10'
success code: 204
failure [
{ code: 400, message: 'Bad request' },
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' }
]
tags %w[remote_mirrors]
end
params do
requires :mirror_id, type: String, desc: 'The ID of a remote mirror'
end
delete ':id/remote_mirrors/:mirror_id' do
mirror = find_remote_mirror
destroy_conditionally!(mirror) do
result = ::RemoteMirrors::DestroyService.new(user_project, current_user).execute(mirror)
render_api_error!(result.message, 400) if result.error?
end
end
end
end
end
|