diff options
author | Tiago Botelho <tiagonbotelho@hotmail.com> | 2018-01-26 14:28:08 +0000 |
---|---|---|
committer | Tiago Botelho <tiagonbotelho@hotmail.com> | 2018-02-06 13:35:35 +0000 |
commit | dc229c076cdc0ef6e7f3f74f6e462c22880ff08c (patch) | |
tree | ec2c979bf3dcb0af71661a7903afd02b2e6f6114 /lib | |
parent | e42a548f1dac02577d0c1731fef508dab68c45a5 (diff) | |
download | gitlab-ce-dc229c076cdc0ef6e7f3f74f6e462c22880ff08c.tar.gz |
Abstracts ProjectMoved and ProjectCreated into a BaseProject
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/helpers/internal_helpers.rb | 8 | ||||
-rw-r--r-- | lib/api/internal.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/checks/base_project.rb | 46 | ||||
-rw-r--r-- | lib/gitlab/checks/project_created.rb | 39 | ||||
-rw-r--r-- | lib/gitlab/checks/project_moved.rb | 38 | ||||
-rw-r--r-- | lib/gitlab/git_access.rb | 34 | ||||
-rw-r--r-- | lib/gitlab/path_regex.rb | 2 |
7 files changed, 79 insertions, 92 deletions
diff --git a/lib/api/helpers/internal_helpers.rb b/lib/api/helpers/internal_helpers.rb index 2340e962918..bff245fe9a2 100644 --- a/lib/api/helpers/internal_helpers.rb +++ b/lib/api/helpers/internal_helpers.rb @@ -1,6 +1,8 @@ module API module Helpers module InternalHelpers + include Gitlab::Utils::StrongMemoize + attr_reader :redirected_path def wiki? @@ -65,13 +67,15 @@ module API end def project_namespace - @project_namespace ||= project&.namespace || Namespace.find_by_full_path(project_match[:namespace_path]) + strong_memoize(:project_namespace) do + project&.namespace || Namespace.find_by_full_path(project_match[:namespace_path]) + end end private def project_match - @project_match ||= params[:project].match(Gitlab::PathRegex.full_project_git_path_regex) + @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 841a34eb67f..ed6d022df97 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -215,8 +215,8 @@ module API # A user is not guaranteed to be returned; an orphaned write deploy # key could be used if user - redirect_message = Gitlab::Checks::ProjectMoved.fetch_redirect_message(user.id, project.id) - project_created_message = Gitlab::Checks::ProjectCreated.fetch_project_created_message(user.id, project.id) + redirect_message = Gitlab::Checks::ProjectMoved.fetch_message(user.id, project.id) + project_created_message = Gitlab::Checks::ProjectCreated.fetch_message(user.id, project.id) output[:redirected_message] = redirect_message if redirect_message output[:project_created_message] = project_created_message if project_created_message diff --git a/lib/gitlab/checks/base_project.rb b/lib/gitlab/checks/base_project.rb new file mode 100644 index 00000000000..dd6c007b356 --- /dev/null +++ b/lib/gitlab/checks/base_project.rb @@ -0,0 +1,46 @@ +module Gitlab + module Checks + class BaseProject + def initialize(project, user, protocol) + @project = project + @user = user + @protocol = protocol + end + + def self.fetch_message(user_id, project_id) + key = message_key(user_id, project_id) + + Gitlab::Redis::SharedState.with do |redis| + message = redis.get(key) + redis.del(key) + message + end + end + + def add_message + return unless user.present? && project.present? + + Gitlab::Redis::SharedState.with do |redis| + key = self.class.message_key(user.id, project.id) + redis.setex(key, 5.minutes, message) + end + end + + def message + raise NotImplementedError + end + + protected + + attr_reader :project, :user, :protocol + + def self.message_key(user_id, project_id) + raise NotImplementedError + end + + def url_to_repo + protocol == 'ssh' ? project.ssh_url_to_repo : project.http_url_to_repo + end + end + end +end diff --git a/lib/gitlab/checks/project_created.rb b/lib/gitlab/checks/project_created.rb index f05e8b4a7e8..bd1e204bc81 100644 --- a/lib/gitlab/checks/project_created.rb +++ b/lib/gitlab/checks/project_created.rb @@ -1,40 +1,15 @@ module Gitlab module Checks - class ProjectCreated + class ProjectCreated < BaseProject PROJECT_CREATED = "project_created".freeze - def initialize(user, project, protocol) - @user = user - @project = project - @protocol = protocol - end - - 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(project_created_key) - redis.del(project_created_key) - message - end - end - - def add_project_created_message - return unless user.present? && project.present? - - Gitlab::Redis::SharedState.with do |redis| - key = self.class.project_created_message_key(user.id, project.id) - redis.setex(key, 5.minutes, project_created_message) - end - end - - def project_created_message + def message <<~MESSAGE.strip_heredoc The private project #{project.full_path} was created. To configure the remote, run: - git remote add origin #{git_url} + git remote add origin #{url_to_repo} To view the project, visit: #{project_url} @@ -44,19 +19,13 @@ module Gitlab private - attr_reader :project, :user, :protocol - - def self.project_created_message_key(user_id, project_id) + def self.message_key(user_id, project_id) "#{PROJECT_CREATED}:#{user_id}:#{project_id}" end def project_url Gitlab::Routing.url_helpers.project_url(project) end - - def git_url - protocol == 'ssh' ? project.ssh_url_to_repo : project.http_url_to_repo - end end end end diff --git a/lib/gitlab/checks/project_moved.rb b/lib/gitlab/checks/project_moved.rb index dfb2f4d4054..eca59e88e24 100644 --- a/lib/gitlab/checks/project_moved.rb +++ b/lib/gitlab/checks/project_moved.rb @@ -1,37 +1,15 @@ module Gitlab module Checks - class ProjectMoved + class ProjectMoved < BaseProject REDIRECT_NAMESPACE = "redirect_namespace".freeze - def initialize(project, user, redirected_path, protocol) - @project = project - @user = user + def initialize(project, user, protocol, redirected_path) @redirected_path = redirected_path - @protocol = protocol - end - - def self.fetch_redirect_message(user_id, project_id) - redirect_key = redirect_message_key(user_id, project_id) - Gitlab::Redis::SharedState.with do |redis| - message = redis.get(redirect_key) - redis.del(redirect_key) - message - end - end - - def add_redirect_message - # Don't bother with sending a redirect message for anonymous clones - # because they never see it via the `/internal/post_receive` endpoint - return unless user.present? && project.present? - - Gitlab::Redis::SharedState.with do |redis| - key = self.class.redirect_message_key(user.id, project.id) - redis.setex(key, 5.minutes, redirect_message) - end + super(project, user, protocol) end - def redirect_message(rejected: false) + def message(rejected: false) <<~MESSAGE.strip_heredoc Project '#{redirected_path}' was moved to '#{project.full_path}'. @@ -47,17 +25,17 @@ module Gitlab private - attr_reader :project, :redirected_path, :protocol, :user + attr_reader :redirected_path - def self.redirect_message_key(user_id, project_id) + def self.message_key(user_id, project_id) "#{REDIRECT_NAMESPACE}:#{user_id}:#{project_id}" end def remote_url_message(rejected) if rejected - "git remote set-url origin #{url} and try again." + "git remote set-url origin #{url_to_repo} and try again." else - "git remote set-url origin #{url}" + "git remote set-url origin #{url_to_repo}" end end diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 32a2395a26b..3d07e112e2b 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -19,8 +19,7 @@ module Gitlab 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.", - namespace_not_found: 'The namespace you were looking for could not be found.' + cannot_push_to_read_only: "You can't push code to a read-only GitLab instance." }.freeze DOWNLOAD_COMMANDS = %w{ git-upload-pack git-upload-archive }.freeze @@ -53,7 +52,6 @@ module Gitlab check_download_access! when *PUSH_COMMANDS check_push_access!(cmd, changes) - check_namespace_accessibility! end true @@ -100,7 +98,7 @@ module Gitlab end def check_project_accessibility!(cmd) - if (project.blank? || !can_read_project?) && !can_create_project_in_namespace?(cmd) + unless can_create_project_in_namespace?(cmd) || can_read_project? raise NotFoundError, ERROR_MESSAGES[:project_not_found] end end @@ -108,12 +106,12 @@ module Gitlab def check_project_moved! return if redirected_path.nil? - project_moved = Checks::ProjectMoved.new(project, user, redirected_path, protocol) + project_moved = Checks::ProjectMoved.new(project, user, protocol, redirected_path) if project_moved.permanent_redirect? - project_moved.add_redirect_message + project_moved.add_message else - raise ProjectMovedError, project_moved.redirect_message(rejected: true) + raise ProjectMovedError, project_moved.message(rejected: true) end end @@ -144,19 +142,11 @@ module Gitlab end def check_repository_existence!(cmd) - if (project.blank? || !project.repository.exists?) && !can_create_project_in_namespace?(cmd) + unless can_create_project_in_namespace?(cmd) || project.repository.exists? raise UnauthorizedError, ERROR_MESSAGES[:no_repo] end end - def check_namespace_accessibility! - return unless project.blank? - - unless target_namespace - raise NotFoundError, ERROR_MESSAGES[:namespace_not_found] - end - end - def check_download_access! return if deploy_key? @@ -170,16 +160,16 @@ module Gitlab end def check_push_access!(cmd, changes) - return if project.blank? && can_create_project_in_namespace?(cmd) + if Gitlab::Database.read_only? + raise UnauthorizedError, push_to_read_only_message + end + + return if can_create_project_in_namespace?(cmd) if project.repository_read_only? raise UnauthorizedError, ERROR_MESSAGES[:read_only] end - if Gitlab::Database.read_only? - raise UnauthorizedError, push_to_read_only_message - end - if deploy_key check_deploy_key_push_access! elsif user @@ -249,7 +239,7 @@ module Gitlab end def can_create_project_in_namespace?(cmd) - return false unless push?(cmd) && target_namespace + return false unless push?(cmd) && target_namespace && project.blank? user.can?(:create_projects, target_namespace) end diff --git a/lib/gitlab/path_regex.rb b/lib/gitlab/path_regex.rb index 4a2db11a978..c6a594d38d1 100644 --- a/lib/gitlab/path_regex.rb +++ b/lib/gitlab/path_regex.rb @@ -188,7 +188,7 @@ module Gitlab 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 + @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 |