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

141 lines
4.2 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
2020-04-22 19:07:51 +05:30
attr_reader :redirected_path
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
delegate :wiki?, to: :repo_type
2020-01-01 13:55:28 +05:30
def actor
@actor ||= Support::GitAccessActor.from_params(params)
end
2020-04-22 19:07:51 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2019-07-07 11:18:12 +05:30
def repo_type
2020-04-22 19:07:51 +05:30
parse_repo_path unless defined?(@repo_type)
@repo_type
2017-08-17 22:00:37 +05:30
end
def project
2020-04-22 19:07:51 +05:30
parse_repo_path unless defined?(@project)
@project
end
def container
parse_repo_path unless defined?(@container)
@container
2017-09-10 17:25:29 +05:30
end
2020-04-22 19:07:51 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-09-10 17:25:29 +05:30
2019-12-04 20:38:33 +05:30
def access_checker_for(actor, protocol)
2020-04-08 14:13:33 +05:30
access_checker_klass.new(actor.key_or_user, container, protocol,
2019-12-04 20:38:33 +05:30
authentication_abilities: ssh_authentication_abilities,
2021-02-22 17:27:13 +05:30
repository_path: repository_path,
2019-12-04 20:38:33 +05:30
redirected_path: redirected_path)
end
def access_checker_klass
repo_type.access_checker_class
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?
2020-05-24 23:13:21 +05:30
Gitlab::Json.parse(params[:env])
2017-08-17 22:00:37 +05:30
rescue JSON::ParserError
{}
end
def log_user_activity(actor)
commands = Gitlab::GitAccess::DOWNLOAD_COMMANDS
2023-05-27 22:25:52 +05:30
commands += Gitlab::GitAccess::PUSH_COMMANDS if Feature.enabled?(:log_user_git_push_activity)
2017-08-17 22:00:37 +05:30
2023-04-23 21:23:45 +05:30
return unless commands.include?(params[:action])
::Users::ActivityService.new(author: actor, namespace: project&.namespace, project: project).execute
2019-07-07 11:18:12 +05:30
end
2018-03-17 18:26:18 +05:30
def redis_ping
result = Gitlab::Redis::SharedState.with { |redis| redis.ping }
result == 'PONG'
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2020-11-24 15:15:51 +05:30
Gitlab::AppLogger.warn("GitLab: An unexpected error occurred in pinging to Redis: #{e}")
2018-03-17 18:26:18 +05:30
false
end
2017-08-17 22:00:37 +05:30
private
2021-02-22 17:27:13 +05:30
def repository_path
if container
"#{container.full_path}.git"
elsif params[:project]
# When the project doesn't exist, we still need to pass on the path
# to support auto-creation in `GitAccessProject`.
#
# For consistency with the Git HTTP controllers, we normalize the path
# to remove a leading slash and ensure a trailing `.git`.
#
# NOTE: For GitLab Shell, `params[:project]` is the full repository path
# from the SSH command, with an optional trailing `.git`.
"#{params[:project].delete_prefix('/').delete_suffix('.git')}.git"
end
2018-03-17 18:26:18 +05:30
end
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2020-04-22 19:07:51 +05:30
def parse_repo_path
2020-04-08 14:13:33 +05:30
@container, @project, @repo_type, @redirected_path =
2020-03-13 15:44:24 +05:30
if params[:gl_repository]
Gitlab::GlRepository.parse(params[:gl_repository])
elsif params[:project]
Gitlab::RepoPath.parse(params[:project])
end
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-09-10 17:25:29 +05:30
2021-02-22 17:27:13 +05:30
# Repository id to pass between components that don't share/don't have
2017-09-10 17:25:29 +05:30
# access to the same filesystem mounts
def gl_repository
2020-04-08 14:13:33 +05:30
repo_type.identifier_for_container(container)
2019-07-07 11:18:12 +05:30
end
2020-04-08 14:13:33 +05:30
def gl_repository_path
2020-03-13 15:44:24 +05:30
repository.full_path
2017-09-10 17:25:29 +05:30
end
2021-02-22 17:27:13 +05:30
# Return the repository for the detected type and container
#
# @returns [Repository]
2017-09-10 17:25:29 +05:30
def repository
2020-04-08 14:13:33 +05:30
@repository ||= repo_type.repository_for(container)
2017-08-17 22:00:37 +05:30
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
{
2020-10-24 23:57:45 +05:30
repository: repository.gitaly_repository.to_h,
2020-07-28 23:09:34 +05:30
address: Gitlab::GitalyClient.address(repository.shard),
token: Gitlab::GitalyClient.token(repository.shard),
2023-01-13 00:05:48 +05:30
features: Feature::Gitaly.server_feature_flags(
user: ::Feature::Gitaly.user_actor(actor.user),
repository: repository,
project: ::Feature::Gitaly.project_actor(repository.container),
group: ::Feature::Gitaly.group_actor(repository.container)
)
2017-09-10 17:25:29 +05:30
}
end
2017-08-17 22:00:37 +05:30
end
end
end