2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
class Projects::MirrorsController < Projects::ApplicationController
|
|
|
|
include RepositorySettingsRedirect
|
|
|
|
|
|
|
|
# Authorize
|
|
|
|
before_action :remote_mirror, only: [:update]
|
2018-05-03 10:19:21 -04:00
|
|
|
before_action :check_mirror_available!
|
|
|
|
before_action :authorize_admin_project!
|
2018-05-03 08:55:14 -04:00
|
|
|
|
|
|
|
layout "project_settings"
|
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :source_code_management
|
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
def show
|
2018-08-08 02:17:43 -04:00
|
|
|
redirect_to_repository_settings(project, anchor: 'js-push-remote-settings')
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2018-07-27 11:24:43 -04:00
|
|
|
result = ::Projects::UpdateService.new(project, current_user, mirror_params).execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
2019-03-27 12:52:52 -04:00
|
|
|
flash[:notice] = _('Mirroring settings were successfully updated.')
|
2018-05-03 08:55:14 -04:00
|
|
|
else
|
|
|
|
flash[:alert] = project.errors.full_messages.join(', ').html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
2018-08-08 02:17:43 -04:00
|
|
|
format.html { redirect_to_repository_settings(project, anchor: 'js-push-remote-settings') }
|
2018-05-03 08:55:14 -04: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 12:52:52 -04:00
|
|
|
flash[:notice] = _("The remote repository is being updated...")
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
|
2018-08-08 02:17:43 -04:00
|
|
|
redirect_to_repository_settings(project, anchor: 'js-push-remote-settings')
|
2018-05-03 08:55:14 -04:00
|
|
|
end
|
|
|
|
|
2018-10-24 11:03:00 -04: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 08:55:14 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def remote_mirror
|
|
|
|
@remote_mirror = project.remote_mirrors.first_or_initialize
|
|
|
|
end
|
|
|
|
|
2018-05-03 10:19:21 -04:00
|
|
|
def check_mirror_available!
|
2020-03-26 14:08:03 -04:00
|
|
|
render_404 unless can?(current_user, :admin_remote_mirror, project)
|
2018-05-03 10:19:21 -04:00
|
|
|
end
|
|
|
|
|
2018-05-03 08:55:14 -04:00
|
|
|
def mirror_params_attributes
|
|
|
|
[
|
|
|
|
remote_mirrors_attributes: %i[
|
|
|
|
url
|
|
|
|
id
|
|
|
|
enabled
|
|
|
|
only_protected_branches
|
2020-03-30 11:07:51 -04:00
|
|
|
keep_divergent_refs
|
2018-11-12 05:52:48 -05:00
|
|
|
auth_method
|
|
|
|
password
|
|
|
|
ssh_known_hosts
|
|
|
|
regenerate_ssh_private_key
|
2019-05-06 09:15:03 -04:00
|
|
|
_destroy
|
2018-05-03 08:55:14 -04:00
|
|
|
]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def mirror_params
|
|
|
|
params.require(:project).permit(mirror_params_attributes)
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Projects::MirrorsController.prepend_if_ee('EE::Projects::MirrorsController')
|