debian-mirror-gitlab/app/controllers/repositories/git_http_controller.rb

127 lines
3.3 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Repositories
class GitHttpController < Repositories::GitHttpClientController
include WorkhorseRequest
before_action :access_check
prepend_before_action :deny_head_requests, only: [:info_refs]
2020-04-08 14:13:33 +05:30
rescue_from Gitlab::GitAccess::ForbiddenError, with: :render_403_with_exception
2020-03-13 15:44:24 +05:30
rescue_from Gitlab::GitAccess::NotFoundError, with: :render_404_with_exception
2020-06-23 00:09:42 +05:30
rescue_from Gitlab::GitAccessProject::CreationError, with: :render_422_with_exception
2020-03-13 15:44:24 +05:30
rescue_from Gitlab::GitAccess::TimeoutError, with: :render_503_with_exception
# GET /foo/bar.git/info/refs?service=git-upload-pack (git pull)
# GET /foo/bar.git/info/refs?service=git-receive-pack (git push)
def info_refs
log_user_activity if upload_pack?
render_ok
end
# POST /foo/bar.git/git-upload-pack (git pull)
def git_upload_pack
2020-04-22 19:07:51 +05:30
update_fetch_statistics
2020-03-13 15:44:24 +05:30
render_ok
end
# POST /foo/bar.git/git-receive-pack" (git push)
def git_receive_pack
render_ok
end
private
def deny_head_requests
head :forbidden if request.head?
end
def download_request?
upload_pack?
end
def upload_pack?
git_command == 'git-upload-pack'
end
def git_command
if action_name == 'info_refs'
params[:service]
else
action_name.dasherize
end
end
def render_ok
set_workhorse_internal_api_content_type
render json: Gitlab::Workhorse.git_http_ok(repository, repo_type, user, action_name)
end
def render_403_with_exception(exception)
render plain: exception.message, status: :forbidden
end
def render_404_with_exception(exception)
render plain: exception.message, status: :not_found
end
def render_422_with_exception(exception)
render plain: exception.message, status: :unprocessable_entity
end
def render_503_with_exception(exception)
render plain: exception.message, status: :service_unavailable
end
2020-04-22 19:07:51 +05:30
def update_fetch_statistics
return unless project
2020-03-13 15:44:24 +05:30
return if Gitlab::Database.read_only?
return unless repo_type.project?
2020-04-22 19:07:51 +05:30
if Feature.enabled?(:project_statistics_sync, project, default_enabled: true)
Projects::FetchStatisticsIncrementService.new(project).execute
else
ProjectDailyStatisticsWorker.perform_async(project.id) # rubocop:disable CodeReuse/Worker
end
2020-03-13 15:44:24 +05:30
end
def access
2020-04-08 14:13:33 +05:30
@access ||= access_klass.new(access_actor, container, 'http',
2020-03-13 15:44:24 +05:30
authentication_abilities: authentication_abilities,
namespace_path: params[:namespace_id],
2020-04-08 14:13:33 +05:30
repository_path: repository_path,
2020-03-13 15:44:24 +05:30
redirected_path: redirected_path,
auth_result_type: auth_result_type)
end
def access_actor
return user if user
return :ci if ci?
end
def access_check
access.check(git_command, Gitlab::GitAccess::ANY)
2020-04-08 14:13:33 +05:30
if repo_type.project? && !container
@project = @container = access.project
end
2020-03-13 15:44:24 +05:30
end
def access_klass
@access_klass ||= repo_type.access_checker_class
end
2020-04-08 14:13:33 +05:30
def repository_path
@repository_path ||= params[:repository_id].sub(/\.git$/, '')
2020-03-13 15:44:24 +05:30
end
def log_user_activity
Users::ActivityService.new(user).execute
end
end
end
Repositories::GitHttpController.prepend_if_ee('EE::Repositories::GitHttpController')