debian-mirror-gitlab/lib/api/helpers/internal_helpers.rb

139 lines
3.8 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module API
module Helpers
module InternalHelpers
2018-03-17 18:26:18 +05:30
attr_reader :redirected_path
2017-09-10 17:25:29 +05:30
2019-05-18 00:54:41 +05:30
delegate :wiki?, to: :repo_type
def repo_type
set_project unless defined?(@repo_type) # rubocop:disable Gitlab/ModuleWithInstanceVariables
@repo_type # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
end
def project
2018-03-17 18:26:18 +05:30
set_project unless defined?(@project) # rubocop:disable Gitlab/ModuleWithInstanceVariables
@project # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
def ssh_authentication_abilities
[
:read_project,
:download_code,
:push_code
]
end
def parse_env
return {} if params[:env].blank?
JSON.parse(params[:env])
rescue JSON::ParserError
{}
end
def log_user_activity(actor)
commands = Gitlab::GitAccess::DOWNLOAD_COMMANDS
::Users::ActivityService.new(actor, 'Git SSH').execute if commands.include?(params[:action])
end
2018-03-17 18:26:18 +05:30
def merge_request_urls
::MergeRequests::GetUrlsService.new(project).execute(params[:changes])
end
2019-05-18 00:54:41 +05:30
def process_mr_push_options(push_options, project, user, changes)
output = {}
service = ::MergeRequests::PushOptionsHandlerService.new(
project,
user,
changes,
push_options
).execute
if service.errors.present?
output[:warnings] = push_options_warning(service.errors.join("\n\n"))
end
output
end
def push_options_warning(warning)
options = Array.wrap(params[:push_options]).map { |p| "'#{p}'" }.join(' ')
"Error encountered with push options #{options}: #{warning}"
end
2018-03-17 18:26:18 +05:30
def redis_ping
result = Gitlab::Redis::SharedState.with { |redis| redis.ping }
result == 'PONG'
rescue => e
Rails.logger.warn("GitLab: An unexpected error occurred in pinging to Redis: #{e}")
false
end
def project_path
project&.path || project_path_match[:project_path]
end
def namespace_path
project&.namespace&.full_path || project_path_match[:namespace_path]
end
2017-08-17 22:00:37 +05:30
private
2018-03-17 18:26:18 +05:30
def project_path_match
@project_path_match ||= params[:project].match(Gitlab::PathRegex.full_project_git_path_regex) || {}
end
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
def set_project
if params[:gl_repository]
2019-05-18 00:54:41 +05:30
@project, @repo_type = Gitlab::GlRepository.parse(params[:gl_repository])
2017-09-10 17:25:29 +05:30
@redirected_path = nil
else
2019-05-18 00:54:41 +05:30
@project, @repo_type, @redirected_path = Gitlab::RepoPath.parse(params[:project])
2017-09-10 17:25:29 +05:30
end
end
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-09-10 17:25:29 +05:30
# Project id to pass between components that don't share/don't have
# access to the same filesystem mounts
def gl_repository
2019-05-18 00:54:41 +05:30
repo_type.identifier_for_subject(project)
end
def gl_project_path
if wiki?
project.wiki.full_path
else
project.full_path
end
2017-09-10 17:25:29 +05:30
end
# Return the repository depending on whether we want the wiki or the
# regular repository
def repository
2019-05-18 00:54:41 +05:30
if repo_type.wiki?
2017-09-10 17:25:29 +05:30
project.wiki.repository
2017-08-17 22:00:37 +05:30
else
2017-09-10 17:25:29 +05:30
project.repository
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
# Return the Gitaly Address if it is enabled
def gitaly_payload(action)
2018-05-09 12:01:36 +05:30
return unless %w[git-receive-pack git-upload-pack git-upload-archive].include?(action)
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
{
repository: repository.gitaly_repository,
address: Gitlab::GitalyClient.address(project.repository_storage),
token: Gitlab::GitalyClient.token(project.repository_storage)
}
end
2017-08-17 22:00:37 +05:30
end
end
end