namespace.rb 3.4 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4
# == Schema Information
#
# Table name: namespaces
#
A
Andrew8xx8 已提交
5 6 7
#  id          :integer          not null, primary key
#  name        :string(255)      not null
#  path        :string(255)      not null
D
Dmitriy Zaporozhets 已提交
8
#  owner_id    :integer
D
Dmitriy Zaporozhets 已提交
9 10
#  created_at  :datetime
#  updated_at  :datetime
A
Andrew8xx8 已提交
11
#  type        :string(255)
D
Dmitriy Zaporozhets 已提交
12
#  description :string(255)      default(""), not null
S
Steven Thonus 已提交
13
#  avatar      :string(255)
D
Dmitriy Zaporozhets 已提交
14 15
#

16
class Namespace < ActiveRecord::Base
17
  include Sortable
18 19
  include Gitlab::ShellAdapter

20
  has_many :projects, dependent: :destroy
21 22
  belongs_to :owner, class_name: "User"

23
  validates :owner, presence: true, unless: ->(n) { n.type == "Group" }
24 25 26
  validates :name,
    presence: true, uniqueness: true,
    length: { within: 0..255 },
D
Douwe Maan 已提交
27 28
    format: { with: Gitlab::Regex.namespace_name_regex,
              message: Gitlab::Regex.namespace_name_regex_message }
29

A
Andrew8xx8 已提交
30
  validates :description, length: { within: 0..255 }
31 32 33 34 35
  validates :path,
    uniqueness: { case_sensitive: false },
    presence: true,
    length: { within: 1..255 },
    exclusion: { in: Gitlab::Blacklist.path },
D
Douwe Maan 已提交
36 37
    format: { with: Gitlab::Regex.namespace_regex,
              message: Gitlab::Regex.namespace_regex_message }
38 39 40

  delegate :name, to: :owner, allow_nil: true, prefix: true

41
  after_create :ensure_dir_exist
42
  after_update :move_dir, if: :path_changed?
43
  after_destroy :rm_dir
44

A
Andrew8xx8 已提交
45
  scope :root, -> { where('type IS NULL') }
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  class << self
    def by_path(path)
      where('lower(path) = :value', value: path.downcase).first
    end

    # Case insensetive search for namespace by path or name
    def find_by_path_or_name(path)
      find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase)
    end

    def search(query)
      where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
    end

    def clean_path(path)
      path.gsub!(/@.*\z/,             "")
      path.gsub!(/\.git\z/,           "")
      path.gsub!(/\A-/,               "")
65
      path.gsub!(/\.+\z/,             "")
66 67 68 69 70 71 72 73 74 75 76
      path.gsub!(/[^a-zA-Z0-9_\-\.]/, "")

      counter = 0
      base = path
      while Namespace.by_path(path).present?
        counter += 1
        path = "#{base}#{counter}"
      end

      path
    end
77 78
  end

79
  def to_param
80
    path
81
  end
82 83 84 85

  def human_name
    owner_name
  end
86 87

  def ensure_dir_exist
88
    gitlab_shell.add_namespace(path)
D
Dmitriy Zaporozhets 已提交
89 90
  end

91 92
  def rm_dir
    gitlab_shell.rm_namespace(path)
93
  end
94 95

  def move_dir
96 97 98 99 100
    if gitlab_shell.mv_namespace(path_was, path)
      # If repositories moved successfully we need to remove old satellites
      # and send update instructions to users.
      # However we cannot allow rollback since we moved namespace dir
      # So we basically we mute exceptions in next actions
G
GitLab 已提交
101
      begin
102
        gitlab_shell.rm_satellites(path_was)
103
        send_update_instructions
104
      rescue
J
Johannes Schleifenbaum 已提交
105
        # Returning false does not rollback after_* transaction but gives
106 107
        # us information about failing some of tasks
        false
108
      end
109 110 111 112
    else
      # if we cannot move namespace directory we should rollback
      # db changes in order to prevent out of sync between db and fs
      raise Exception.new('namespace directory cannot be moved')
113
    end
114
  end
115

116 117 118
  def send_update_instructions
    projects.each(&:send_move_instructions)
  end
119 120 121 122

  def kind
    type == 'Group' ? 'group' : 'user'
  end
123 124 125 126

  def find_fork_of(project)
    projects.joins(:forked_project_link).where('forked_project_links.forked_from_project_id = ?', project.id).first
  end
127
end