namespace.rb 4.5 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
  validates :name,
    length: { within: 0..255 },
26 27 28
    namespace_name: true,
    presence: true,
    uniqueness: true
29

A
Andrew8xx8 已提交
30
  validates :description, length: { within: 0..255 }
31 32
  validates :path,
    length: { within: 1..255 },
R
Robert Speicher 已提交
33 34 35
    namespace: true,
    presence: true,
    uniqueness: { case_sensitive: false }
36 37 38

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

39
  after_create :ensure_dir_exist
40
  after_update :move_dir, if: :path_changed?
41
  after_destroy :rm_dir
42

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

45 46
  class << self
    def by_path(path)
G
Gabriel Mazetto 已提交
47
      find_by('lower(path) = :value', value: path.downcase)
48 49 50 51 52 53 54
    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

55 56 57 58 59 60 61
    # Searches for namespaces matching the given query.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # query - The search query as a String
    #
    # Returns an ActiveRecord::Relation
62
    def search(query)
63 64 65 66
      t = arel_table
      pattern = "%#{query}%"

      where(t[:name].matches(pattern).or(t[:path].matches(pattern)))
67 68 69
    end

    def clean_path(path)
70
      path = path.dup
D
Douwe Maan 已提交
71
      # Get the email username by removing everything after an `@` sign.
72
      path.gsub!(/@.*\z/,             "")
D
Douwe Maan 已提交
73
      # Usernames can't end in .git, so remove it.
74
      path.gsub!(/\.git\z/,           "")
D
Douwe Maan 已提交
75
      # Remove dashes at the start of the username.
76
      path.gsub!(/\A-+/,              "")
D
Douwe Maan 已提交
77
      # Remove periods at the end of the username.
78
      path.gsub!(/\.+\z/,             "")
D
Douwe Maan 已提交
79
      # Remove everything that's not in the list of allowed characters.
80 81
      path.gsub!(/[^a-zA-Z0-9_\-\.]/, "")

82
      # Users with the great usernames of "." or ".." would end up with a blank username.
83
      # Work around that by setting their username to "blank", followed by a counter.
84 85
      path = "blank" if path.blank?

86 87
      counter = 0
      base = path
88
      while Namespace.find_by_path_or_name(path)
89 90 91 92 93 94
        counter += 1
        path = "#{base}#{counter}"
      end

      path
    end
95 96
  end

97
  def to_param
98
    path
99
  end
100 101 102 103

  def human_name
    owner_name
  end
104 105

  def ensure_dir_exist
106
    gitlab_shell.add_namespace(path)
D
Dmitriy Zaporozhets 已提交
107 108
  end

109
  def rm_dir
110 111 112 113
    # Move namespace directory into trash.
    # We will remove it later async
    new_path = "#{path}+#{id}+deleted"

114 115 116 117 118 119 120 121
    if gitlab_shell.mv_namespace(path, new_path)
      message = "Namespace directory \"#{path}\" moved to \"#{new_path}\""
      Gitlab::AppLogger.info message

      # Remove namespace directroy async with delay so
      # GitLab has time to remove all projects first
      GitlabShellWorker.perform_in(5.minutes, :rm_namespace, new_path)
    end
122
  end
123 124

  def move_dir
125 126 127
    # Ensure old directory exists before moving it
    gitlab_shell.add_namespace(path_was)

128
    if gitlab_shell.mv_namespace(path_was, path)
129 130
      Gitlab::UploadsTransfer.new.rename_namespace(path_was, path)

131 132
      # If repositories moved successfully we need to
      # send update instructions to users.
133 134
      # However we cannot allow rollback since we moved namespace dir
      # So we basically we mute exceptions in next actions
G
GitLab 已提交
135
      begin
136
        send_update_instructions
137
      rescue
J
Johannes Schleifenbaum 已提交
138
        # Returning false does not rollback after_* transaction but gives
139 140
        # us information about failing some of tasks
        false
141
      end
142 143 144 145
    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')
146
    end
147
  end
148

149
  def send_update_instructions
150 151 152
    projects.each do |project|
      project.send_move_instructions("#{path_was}/#{project.path}")
    end
153
  end
154 155 156 157

  def kind
    type == 'Group' ? 'group' : 'user'
  end
158 159

  def find_fork_of(project)
G
Gabriel Mazetto 已提交
160
    projects.joins(:forked_project_link).find_by('forked_project_links.forked_from_project_id = ?', project.id)
161
  end
162
end