• Markus Koller's avatar
    Generalize repository routing · abe3a8cf
    Markus Koller authored
    Previously we used both a `:namespace_id` and a `:repository_id` in the
    repository routes, which originally made sense for project repositories,
    but was confusing with personal snippets where `:namespace_id` was just
    the `/snippets` prefix and not an actual namespace.
    
    For group wiki repositories we also need to support toplevel groups,
    where we only have a namespace path.
    
    So we combine `:namespace_id` and `:repository_id` into a single
    `:repository_path` route argument, and refactor the related controllers
    and internal APIs accordingly.
    
    The repository path is mainly used in `Gitlab::RepoPath` to determine
    the container for the repository. In the `GitAccess` classes it's only
    used in `GitAccessProject` when auto-creating project repositories,
    where we need to know in which namespace to create the new project.
    abe3a8cf
git_access_project.rb 1.93 KB
# frozen_string_literal: true

module Gitlab
  class GitAccessProject < GitAccess
    extend ::Gitlab::Utils::Override

    CreationError = Class.new(StandardError)

    ERROR_MESSAGES = {
      namespace_not_found: 'The namespace you were looking for could not be found.'
    }.freeze

    override :download_ability
    def download_ability
      :download_code
    end

    override :push_ability
    def push_ability
      :push_code
    end

    private

    override :check_container!
    def check_container!
      check_namespace!
      ensure_project_on_push!

      super
    end

    def check_namespace!
      raise NotFoundError, ERROR_MESSAGES[:namespace_not_found] unless namespace_path.present?
    end

    def namespace
      strong_memoize(:namespace) { Namespace.find_by_full_path(namespace_path) }
    end

    def namespace_path
      strong_memoize(:namespace_path) { repository_path_match[:namespace_path] }
    end

    def project_path
      strong_memoize(:project_path) { repository_path_match[:project_path] }
    end

    def repository_path_match
      strong_memoize(:repository_path_match) { repository_path.match(Gitlab::PathRegex.full_project_git_path_regex) || {} }
    end

    def ensure_project_on_push!
      return if project || deploy_key?
      return unless receive_pack? && changes == ANY && authentication_abilities.include?(:push_code)
      return unless user&.can?(:create_projects, namespace)

      project_params = {
        path: project_path,
        namespace_id: namespace.id,
        visibility_level: Gitlab::VisibilityLevel::PRIVATE
      }

      project = Projects::CreateService.new(user, project_params).execute

      unless project.saved?
        raise CreationError, "Could not create project: #{project.errors.full_messages.join(', ')}"
      end

      self.container = project
      user_access.container = project

      Checks::ProjectCreated.new(repository, user, protocol).add_message
    end
  end
end