gitlab-org--gitlab-foss/app/controllers/projects/git_http_controller.rb

113 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-07-20 12:41:26 -04:00
class Projects::GitHttpController < Projects::GitHttpClientController
include WorkhorseRequest
before_action :access_check
prepend_before_action :deny_head_requests, only: [:info_refs]
rescue_from Gitlab::GitAccess::UnauthorizedError, with: :render_403
rescue_from Gitlab::GitAccess::NotFoundError, with: :render_404
rescue_from Gitlab::GitAccess::ProjectCreationError, with: :render_422
rescue_from Gitlab::GitAccess::TimeoutError, with: :render_503
# 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
2016-03-23 13:34:16 -04:00
end
2016-04-15 06:40:43 -04:00
# POST /foo/bar.git/git-upload-pack (git pull)
def git_upload_pack
enqueue_fetch_statistics_update
render_ok
end
# POST /foo/bar.git/git-receive-pack" (git push)
def git_receive_pack
render_ok
2016-03-23 13:34:16 -04:00
end
private
def deny_head_requests
head :forbidden if request.head?
end
2016-07-20 12:41:26 -04:00
def download_request?
upload_pack?
2016-03-23 13:34:16 -04:00
end
def upload_pack?
git_command == 'git-upload-pack'
2016-03-24 13:58:29 -04:00
end
def git_command
2016-03-23 13:34:16 -04:00
if action_name == 'info_refs'
2016-03-24 13:58:29 -04:00
params[:service]
2016-03-23 13:34:16 -04:00
else
2016-06-03 08:57:34 -04:00
action_name.dasherize
2016-03-23 13:34:16 -04:00
end
end
2016-04-15 06:40:43 -04:00
2016-03-23 13:34:16 -04:00
def render_ok
set_workhorse_internal_api_content_type
render json: Gitlab::Workhorse.git_http_ok(repository, repo_type, user, action_name)
2016-03-23 13:34:16 -04:00
end
2016-04-15 06:40:43 -04:00
def render_403(exception)
render plain: exception.message, status: :forbidden
end
def render_404(exception)
render plain: exception.message, status: :not_found
2016-03-23 13:34:16 -04:00
end
2016-03-24 13:58:29 -04:00
def render_422(exception)
render plain: exception.message, status: :unprocessable_entity
end
def render_503(exception)
render plain: exception.message, status: :service_unavailable
end
def enqueue_fetch_statistics_update
return if wiki?
return unless project.daily_statistics_enabled?
ProjectDailyStatisticsWorker.perform_async(project.id)
end
def access
@access ||= access_klass.new(access_actor, project,
'http', authentication_abilities: authentication_abilities,
namespace_path: params[:namespace_id], project_path: project_path,
redirected_path: redirected_path, auth_result_type: auth_result_type)
end
2017-05-16 15:58:46 -04:00
def access_actor
return user if user
return :ci if ci?
end
def access_check
2018-12-20 11:25:48 -05:00
access.check(git_command, Gitlab::GitAccess::ANY)
@project ||= access.project
2016-03-24 13:58:29 -04:00
end
def access_klass
@access_klass ||= repo_type.access_checker_class
end
def project_path
@project_path ||= params[:project_id].sub(/\.git$/, '')
end
def log_user_activity
Users::ActivityService.new(user, 'pull').execute
end
2016-03-23 13:34:16 -04:00
end