From bc78ae6985ee37f9ac2ffc2dbf6f445078d16038 Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Mon, 22 Jan 2018 18:10:56 +0000 Subject: Add specs --- lib/api/helpers/internal_helpers.rb | 17 ++++++++++++++--- lib/api/internal.rb | 9 +-------- lib/gitlab/checks/new_project.rb | 2 ++ lib/gitlab/git_access.rb | 11 +++-------- 4 files changed, 20 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/api/helpers/internal_helpers.rb b/lib/api/helpers/internal_helpers.rb index c0fcae43638..fd568c5ef30 100644 --- a/lib/api/helpers/internal_helpers.rb +++ b/lib/api/helpers/internal_helpers.rb @@ -64,6 +64,15 @@ 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 + } + end + private def project_path_regex @@ -71,11 +80,13 @@ module API end def project_match - @match ||= params[:project].match(project_path_regex).captures + @project_match ||= params[:project].match(project_path_regex) end - def namespace - @namespace ||= Namespace.find_by_path_or_name(project_match[:namespace_id]) + def project_namespace + return unless project_match + + @project_namespace ||= Namespace.find_by_path_or_name(project_match[:namespace_id]) end # rubocop:disable Gitlab/ModuleWithInstanceVariables diff --git a/lib/api/internal.rb b/lib/api/internal.rb index f641ef457a3..b7475af2044 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -43,7 +43,7 @@ module API access_checker_klass = wiki? ? Gitlab::GitAccessWiki : Gitlab::GitAccess access_checker = access_checker_klass - .new(actor, project, protocol, authentication_abilities: ssh_authentication_abilities, redirected_path: redirected_path, target_namespace: namespace) + .new(actor, project, protocol, authentication_abilities: ssh_authentication_abilities, redirected_path: redirected_path, target_namespace: project_namespace) begin access_checker.check(params[:action], params[:changes]) @@ -52,13 +52,6 @@ module API end if user && project.blank? && receive_pack? - project_params = { - description: "", - path: Project.parse_project_id(project_match[:project_name]), - namespace_id: namespace&.id, - visibility_level: Gitlab::VisibilityLevel::PRIVATE.to_s - } - @project = ::Projects::CreateService.new(user, project_params).execute if @project.saved? diff --git a/lib/gitlab/checks/new_project.rb b/lib/gitlab/checks/new_project.rb index 40d0acefaba..488c5c03c32 100644 --- a/lib/gitlab/checks/new_project.rb +++ b/lib/gitlab/checks/new_project.rb @@ -20,6 +20,8 @@ module Gitlab end def add_new_project_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) diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 598506aa418..38649a4fdef 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.", - create: "Creating a repository to that namespace is not allowed." + 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,7 @@ module Gitlab check_download_access! when *PUSH_COMMANDS check_push_access!(cmd, changes) - check_repository_creation!(cmd) + check_namespace_accessibility!(cmd) end true @@ -149,16 +148,12 @@ module Gitlab end end - def check_repository_creation!(cmd) + def check_namespace_accessibility!(cmd) return unless project.blank? unless target_namespace raise NotFoundError, ERROR_MESSAGES[:namespace_not_found] end - - unless can_create_project_in_namespace?(cmd) - raise UnauthorizedError, ERROR_MESSAGES[:create] - end end def check_download_access! -- cgit v1.2.1