diff options
author | Tiago Botelho <tiagonbotelho@hotmail.com> | 2018-01-25 12:26:52 +0000 |
---|---|---|
committer | Tiago Botelho <tiagonbotelho@hotmail.com> | 2018-02-06 13:35:35 +0000 |
commit | e42a548f1dac02577d0c1731fef508dab68c45a5 (patch) | |
tree | 9781b82ec0da58683ebeb0fd0ba2062a9ce10e43 /lib | |
parent | bc78ae6985ee37f9ac2ffc2dbf6f445078d16038 (diff) | |
download | gitlab-ce-e42a548f1dac02577d0c1731fef508dab68c45a5.tar.gz |
Move new project on push logic to a service
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/helpers/internal_helpers.rb | 29 | ||||
-rw-r--r-- | lib/api/internal.rb | 16 | ||||
-rw-r--r-- | lib/gitlab/checks/project_created.rb (renamed from lib/gitlab/checks/new_project.rb) | 24 | ||||
-rw-r--r-- | lib/gitlab/git_access.rb | 15 | ||||
-rw-r--r-- | lib/gitlab/git_access_wiki.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/path_regex.rb | 4 |
6 files changed, 40 insertions, 52 deletions
diff --git a/lib/api/helpers/internal_helpers.rb b/lib/api/helpers/internal_helpers.rb index fd568c5ef30..2340e962918 100644 --- a/lib/api/helpers/internal_helpers.rb +++ b/lib/api/helpers/internal_helpers.rb @@ -29,10 +29,6 @@ module API {} end - def receive_pack? - params[:action] == 'git-receive-pack' - end - def fix_git_env_repository_paths(env, repository_path) if obj_dir_relative = env['GIT_OBJECT_DIRECTORY_RELATIVE'].presence env['GIT_OBJECT_DIRECTORY'] = File.join(repository_path, obj_dir_relative) @@ -51,6 +47,10 @@ module API ::Users::ActivityService.new(actor, 'Git SSH').execute if commands.include?(params[:action]) end + def receive_pack? + params[:action] == 'git-receive-pack' + end + def merge_request_urls ::MergeRequests::GetUrlsService.new(project).execute(params[:changes]) end @@ -64,29 +64,14 @@ module API false end - def project_params - { - description: "", - path: Project.parse_project_id(project_match[:project_id]), - namespace_id: project_namespace&.id, - visibility_level: Gitlab::VisibilityLevel::PRIVATE.to_s - } + def project_namespace + @project_namespace ||= project&.namespace || Namespace.find_by_full_path(project_match[:namespace_path]) end private - def project_path_regex - @project_regex ||= /\A(?<namespace_id>#{Gitlab::PathRegex.full_namespace_route_regex})\/(?<project_id>#{Gitlab::PathRegex.project_git_route_regex})\z/.freeze - end - def project_match - @project_match ||= params[:project].match(project_path_regex) - end - - def project_namespace - return unless project_match - - @project_namespace ||= Namespace.find_by_path_or_name(project_match[:namespace_id]) + @project_match ||= params[:project].match(Gitlab::PathRegex.full_project_git_path_regex) end # rubocop:disable Gitlab/ModuleWithInstanceVariables diff --git a/lib/api/internal.rb b/lib/api/internal.rb index b7475af2044..841a34eb67f 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -51,13 +51,11 @@ module API return { status: false, message: e.message } end - if user && project.blank? && receive_pack? - @project = ::Projects::CreateService.new(user, project_params).execute - - if @project.saved? - Gitlab::Checks::NewProject.new(user, @project, protocol).add_new_project_message - else - return { status: false, message: "Could not create project" } + if receive_pack? && project.blank? + begin + @project = ::Projects::CreateFromPushService.new(user, project_match[:project_path], project_namespace, protocol).execute + rescue Gitlab::GitAccess::ProjectCreationError => e + return { status: false, message: e.message } end end @@ -218,10 +216,10 @@ module API # key could be used if user redirect_message = Gitlab::Checks::ProjectMoved.fetch_redirect_message(user.id, project.id) - new_project_message = Gitlab::Checks::NewProject.fetch_new_project_message(user.id, project.id) + project_created_message = Gitlab::Checks::ProjectCreated.fetch_project_created_message(user.id, project.id) output[:redirected_message] = redirect_message if redirect_message - output[:new_project_message] = new_project_message if new_project_message + output[:project_created_message] = project_created_message if project_created_message end output diff --git a/lib/gitlab/checks/new_project.rb b/lib/gitlab/checks/project_created.rb index 488c5c03c32..f05e8b4a7e8 100644 --- a/lib/gitlab/checks/new_project.rb +++ b/lib/gitlab/checks/project_created.rb @@ -1,7 +1,7 @@ module Gitlab module Checks - class NewProject - NEW_PROJECT = "new_project".freeze + class ProjectCreated + PROJECT_CREATED = "project_created".freeze def initialize(user, project, protocol) @user = user @@ -9,26 +9,26 @@ module Gitlab @protocol = protocol end - def self.fetch_new_project_message(user_id, project_id) - new_project_key = new_project_message_key(user_id, project_id) + def self.fetch_project_created_message(user_id, project_id) + project_created_key = project_created_message_key(user_id, project_id) Gitlab::Redis::SharedState.with do |redis| - message = redis.get(new_project_key) - redis.del(new_project_key) + message = redis.get(project_created_key) + redis.del(project_created_key) message end end - def add_new_project_message + def add_project_created_message return unless user.present? && project.present? Gitlab::Redis::SharedState.with do |redis| - key = self.class.new_project_message_key(user.id, project.id) - redis.setex(key, 5.minutes, new_project_message) + key = self.class.project_created_message_key(user.id, project.id) + redis.setex(key, 5.minutes, project_created_message) end end - def new_project_message + def project_created_message <<~MESSAGE.strip_heredoc The private project #{project.full_path} was created. @@ -46,8 +46,8 @@ module Gitlab attr_reader :project, :user, :protocol - def self.new_project_message_key(user_id, project_id) - "#{NEW_PROJECT}:#{user_id}:#{project_id}" + def self.project_created_message_key(user_id, project_id) + "#{PROJECT_CREATED}:#{user_id}:#{project_id}" end def project_url diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 38649a4fdef..32a2395a26b 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -4,6 +4,7 @@ module Gitlab class GitAccess UnauthorizedError = Class.new(StandardError) NotFoundError = Class.new(StandardError) + ProjectCreationError = Class.new(StandardError) ProjectMovedError = Class.new(NotFoundError) ERROR_MESSAGES = { @@ -13,13 +14,13 @@ module Gitlab 'This deploy key does not have write access to this project.', no_repo: 'A repository for this project does not exist yet.', project_not_found: 'The project you were looking for could not be found.', - namespace_not_found: 'The namespace you were looking for could not be found.', account_blocked: 'Your account has been blocked.', command_not_allowed: "The command you're trying to execute is not allowed.", upload_pack_disabled_over_http: 'Pulling over HTTP is not allowed.', receive_pack_disabled_over_http: 'Pushing over HTTP is not allowed.', read_only: 'The repository is temporarily read-only. Please try again later.', - cannot_push_to_read_only: "You can't push code to a read-only GitLab instance." + cannot_push_to_read_only: "You can't push code to a read-only GitLab instance.", + namespace_not_found: 'The namespace you were looking for could not be found.' }.freeze DOWNLOAD_COMMANDS = %w{ git-upload-pack git-upload-archive }.freeze @@ -52,7 +53,7 @@ module Gitlab check_download_access! when *PUSH_COMMANDS check_push_access!(cmd, changes) - check_namespace_accessibility!(cmd) + check_namespace_accessibility! end true @@ -148,7 +149,7 @@ module Gitlab end end - def check_namespace_accessibility!(cmd) + def check_namespace_accessibility! return unless project.blank? unless target_namespace @@ -248,7 +249,7 @@ module Gitlab end def can_create_project_in_namespace?(cmd) - return false unless PUSH_COMMANDS.include?(cmd) && target_namespace + return false unless push?(cmd) && target_namespace user.can?(:create_projects, target_namespace) end @@ -265,6 +266,10 @@ module Gitlab command == 'git-receive-pack' end + def push?(cmd) + PUSH_COMMANDS.include?(cmd) + end + def upload_pack_disabled_over_http? !Gitlab.config.gitlab_shell.upload_pack end diff --git a/lib/gitlab/git_access_wiki.rb b/lib/gitlab/git_access_wiki.rb index f679b5e8ed6..1c9477e84b2 100644 --- a/lib/gitlab/git_access_wiki.rb +++ b/lib/gitlab/git_access_wiki.rb @@ -25,10 +25,6 @@ module Gitlab true end - def check_repository_creation!(cmd) - # Method not used in wiki - end - def push_to_read_only_message ERROR_MESSAGES[:read_only] end diff --git a/lib/gitlab/path_regex.rb b/lib/gitlab/path_regex.rb index 7e5dfd33502..4a2db11a978 100644 --- a/lib/gitlab/path_regex.rb +++ b/lib/gitlab/path_regex.rb @@ -187,6 +187,10 @@ module Gitlab @full_project_path_regex ||= %r{\A#{full_namespace_route_regex}/#{project_route_regex}/\z} end + def full_project_git_path_regex + @full_project_git_path_regex ||= /\A(\/|)(?<namespace_path>#{full_namespace_route_regex})\/(?<project_path>#{project_git_route_regex})\z/.freeze + end + def full_namespace_format_regex @namespace_format_regex ||= /A#{FULL_NAMESPACE_FORMAT_REGEX}\z/.freeze end |