2018-09-26 03:45:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-03 12:55:14 +00:00
|
|
|
class Projects::MirrorsController < Projects::ApplicationController
|
|
|
|
include RepositorySettingsRedirect
|
|
|
|
|
|
|
|
# Authorize
|
|
|
|
before_action :remote_mirror, only: [:update]
|
2018-05-03 14:19:21 +00:00
|
|
|
before_action :check_mirror_available!
|
|
|
|
before_action :authorize_admin_project!
|
2018-05-03 12:55:14 +00:00
|
|
|
|
|
|
|
layout "project_settings"
|
|
|
|
|
|
|
|
def show
|
2018-08-08 06:17:43 +00:00
|
|
|
redirect_to_repository_settings(project, anchor: 'js-push-remote-settings')
|
2018-05-03 12:55:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2018-07-27 15:24:43 +00:00
|
|
|
result = ::Projects::UpdateService.new(project, current_user, mirror_params).execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
2019-03-27 16:52:52 +00:00
|
|
|
flash[:notice] = _('Mirroring settings were successfully updated.')
|
2018-05-03 12:55:14 +00:00
|
|
|
else
|
|
|
|
flash[:alert] = project.errors.full_messages.join(', ').html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
2018-08-08 06:17:43 +00:00
|
|
|
format.html { redirect_to_repository_settings(project, anchor: 'js-push-remote-settings') }
|
2018-05-03 12:55:14 +00:00
|
|
|
format.json do
|
|
|
|
if project.errors.present?
|
|
|
|
render json: project.errors, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
render json: ProjectMirrorSerializer.new.represent(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_now
|
|
|
|
if params[:sync_remote]
|
|
|
|
project.update_remote_mirrors
|
2019-03-27 16:52:52 +00:00
|
|
|
flash[:notice] = _("The remote repository is being updated...")
|
2018-05-03 12:55:14 +00:00
|
|
|
end
|
|
|
|
|
2018-08-08 06:17:43 +00:00
|
|
|
redirect_to_repository_settings(project, anchor: 'js-push-remote-settings')
|
2018-05-03 12:55:14 +00:00
|
|
|
end
|
|
|
|
|
2018-10-24 15:03:00 +00:00
|
|
|
def ssh_host_keys
|
|
|
|
lookup = SshHostKey.new(project: project, url: params[:ssh_url], compare_host_keys: params[:compare_host_keys])
|
|
|
|
|
|
|
|
if lookup.error.present?
|
|
|
|
# Failed to read keys
|
|
|
|
render json: { message: lookup.error }, status: :bad_request
|
|
|
|
elsif lookup.known_hosts.nil?
|
|
|
|
# Still working, come back later
|
|
|
|
render body: nil, status: :no_content
|
|
|
|
else
|
|
|
|
render json: lookup
|
|
|
|
end
|
|
|
|
rescue ArgumentError => err
|
|
|
|
render json: { message: err.message }, status: :bad_request
|
|
|
|
end
|
|
|
|
|
2018-05-03 12:55:14 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def remote_mirror
|
|
|
|
@remote_mirror = project.remote_mirrors.first_or_initialize
|
|
|
|
end
|
|
|
|
|
2018-05-03 14:19:21 +00:00
|
|
|
def check_mirror_available!
|
2020-03-26 18:08:03 +00:00
|
|
|
render_404 unless can?(current_user, :admin_remote_mirror, project)
|
2018-05-03 14:19:21 +00:00
|
|
|
end
|
|
|
|
|
2018-05-03 12:55:14 +00:00
|
|
|
def mirror_params_attributes
|
|
|
|
[
|
|
|
|
remote_mirrors_attributes: %i[
|
|
|
|
url
|
|
|
|
id
|
|
|
|
enabled
|
|
|
|
only_protected_branches
|
2020-03-30 15:07:51 +00:00
|
|
|
keep_divergent_refs
|
2018-11-12 10:52:48 +00:00
|
|
|
auth_method
|
|
|
|
password
|
|
|
|
ssh_known_hosts
|
|
|
|
regenerate_ssh_private_key
|
2019-05-06 13:15:03 +00:00
|
|
|
_destroy
|
2018-05-03 12:55:14 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def mirror_params
|
|
|
|
params.require(:project).permit(mirror_params_attributes)
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Projects::MirrorsController.prepend_if_ee('EE::Projects::MirrorsController')
|