debian-mirror-gitlab/app/controllers/projects/mirrors_controller.rb

92 lines
2.4 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-10-15 14:42:47 +05:30
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"
def show
2018-11-20 20:47:30 +05:30
redirect_to_repository_settings(project, anchor: 'js-push-remote-settings')
2018-10-15 14:42:47 +05:30
end
def update
2018-11-18 11:00:15 +05:30
result = ::Projects::UpdateService.new(project, current_user, mirror_params).execute
if result[:status] == :success
2018-10-15 14:42:47 +05:30
flash[:notice] = 'Mirroring settings were successfully updated.'
else
flash[:alert] = project.errors.full_messages.join(', ').html_safe
end
respond_to do |format|
2018-11-20 20:47:30 +05:30
format.html { redirect_to_repository_settings(project, anchor: 'js-push-remote-settings') }
2018-10-15 14:42:47 +05:30
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
flash[:notice] = "The remote repository is being updated..."
end
2018-11-20 20:47:30 +05:30
redirect_to_repository_settings(project, anchor: 'js-push-remote-settings')
2018-10-15 14:42:47 +05:30
end
2018-12-13 13:39:08 +05:30
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-10-15 14:42:47 +05:30
private
def remote_mirror
@remote_mirror = project.remote_mirrors.first_or_initialize
end
def check_mirror_available!
Gitlab::CurrentSettings.current_application_settings.mirror_available || current_user&.admin?
end
def mirror_params_attributes
[
remote_mirrors_attributes: %i[
url
id
enabled
only_protected_branches
2018-12-23 12:14:25 +05:30
auth_method
password
ssh_known_hosts
regenerate_ssh_private_key
2018-10-15 14:42:47 +05:30
]
]
end
def mirror_params
params.require(:project).permit(mirror_params_attributes)
end
end