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
|
# frozen_string_literal: true
class Projects::MirrorsController < Projects::ApplicationController
include RepositorySettingsRedirect
# Authorize
before_action :remote_mirror, only: [:update]
before_action :check_mirror_available!
before_action :authorize_admin_project!
layout "project_settings"
feature_category :source_code_management
def show
redirect_to_repository_settings(project, anchor: 'js-push-remote-settings')
end
def update
if push_mirror_create_or_destroy?
result = execute_push_mirror_service
if result.success?
flash[:notice] = notice_message
else
flash[:alert] = alert_error(result.message)
end
respond_to do |format|
format.html { redirect_to_repository_settings(project, anchor: 'js-push-remote-settings') }
format.json do
if result.error?
render json: result.message, status: :unprocessable_entity
else
render json: ProjectMirrorSerializer.new.represent(project)
end
end
end
else
flash[:alert] = alert_error('Invalid mirror update request')
respond_to do |format|
format.html { redirect_to_repository_settings(project, anchor: 'js-push-remote-settings') }
format.json do
render json: { error: flash[:alert] }, status: :bad_request
end
end
end
end
def update_now
if params[:sync_remote]
project.update_remote_mirrors
flash[:notice] = _("The remote repository is being updated...")
end
redirect_to_repository_settings(project, anchor: 'js-push-remote-settings')
end
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 => e
render json: { message: e.message }, status: :bad_request
end
private
def push_mirror_create_or_destroy?
push_mirror_create? || push_mirror_destroy?
end
def push_mirror_create?
push_mirror_attributes.present?
end
def push_mirror_destroy?
::Gitlab::Utils.to_boolean(mirror_params.dig(:remote_mirrors_attributes, '_destroy'))
end
def push_mirror_attributes
mirror_params.dig(:remote_mirrors_attributes, '0')
end
def execute_push_mirror_service
if push_mirror_create?
return ::RemoteMirrors::CreateService.new(project, current_user, push_mirror_attributes).execute
end
return unless push_mirror_destroy?
::RemoteMirrors::DestroyService.new(project, current_user).execute(push_mirror_to_destroy)
end
def safe_mirror_params
mirror_params
end
def notice_message
_('Mirroring settings were successfully updated.')
end
def push_mirror_to_destroy
push_mirror_to_destroy_id = safe_mirror_params.dig(:remote_mirrors_attributes, 'id')
project.remote_mirrors.find(push_mirror_to_destroy_id)
end
def remote_mirror
@remote_mirror = project.remote_mirrors.first_or_initialize
end
def check_mirror_available!
render_404 unless can?(current_user, :admin_remote_mirror, project)
end
def mirror_params_attributes
[
remote_mirrors_attributes: %i[
url
id
enabled
only_protected_branches
keep_divergent_refs
auth_method
user
password
ssh_known_hosts
regenerate_ssh_private_key
_destroy
]
]
end
def mirror_params
params.require(:project).permit(mirror_params_attributes)
end
def alert_error(error)
return error.full_messages.to_sentence if error.respond_to?(:full_messages)
error
end
end
Projects::MirrorsController.prepend_mod_with('Projects::MirrorsController')
|