BigW Consortium Gitlab

namespace.rb 4.33 KB
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: namespaces
#
5 6 7
#  id          :integer          not null, primary key
#  name        :string(255)      not null
#  path        :string(255)      not null
Dmitriy Zaporozhets committed
8
#  owner_id    :integer
Dmitriy Zaporozhets committed
9 10
#  created_at  :datetime
#  updated_at  :datetime
11
#  type        :string(255)
Dmitriy Zaporozhets committed
12
#  description :string(255)      default(""), not null
Steven Thonus committed
13
#  avatar      :string(255)
Dmitriy Zaporozhets committed
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 },
27 28
    format: { with: Gitlab::Regex.namespace_name_regex,
              message: Gitlab::Regex.namespace_name_regex_message }
29

Andrew8xx8 committed
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 },
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

45
  scope :root, -> { where('type IS NULL') }
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  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)
62
      path = path.dup
63
      # Get the email username by removing everything after an `@` sign.
64
      path.gsub!(/@.*\z/,             "")
65
      # Usernames can't end in .git, so remove it.
66
      path.gsub!(/\.git\z/,           "")
67
      # Remove dashes at the start of the username.
68
      path.gsub!(/\A-+/,              "")
69
      # Remove periods at the end of the username.
70
      path.gsub!(/\.+\z/,             "")
71
      # Remove everything that's not in the list of allowed characters.
72 73
      path.gsub!(/[^a-zA-Z0-9_\-\.]/, "")

74
      # Users with the great usernames of "." or ".." would end up with a blank username.
75
      # Work around that by setting their username to "blank", followed by a counter.
76 77
      path = "blank" if path.blank?

78 79
      counter = 0
      base = path
80
      while Namespace.find_by_path_or_name(path)
81 82 83 84 85 86
        counter += 1
        path = "#{base}#{counter}"
      end

      path
    end
87 88
  end

89
  def to_param
90
    path
91
  end
92 93 94 95

  def human_name
    owner_name
  end
96 97

  def ensure_dir_exist
98
    gitlab_shell.add_namespace(path)
Dmitriy Zaporozhets committed
99 100
  end

101
  def rm_dir
102 103 104 105
    # Move namespace directory into trash.
    # We will remove it later async
    new_path = "#{path}+#{id}+deleted"

106 107 108 109 110 111 112 113
    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
114
  end
115 116

  def move_dir
117 118 119 120 121
    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
122
      begin
123
        gitlab_shell.rm_satellites(path_was)
124
        send_update_instructions
125
      rescue
Johannes Schleifenbaum committed
126
        # Returning false does not rollback after_* transaction but gives
127 128
        # us information about failing some of tasks
        false
129
      end
130 131 132 133
    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')
134
    end
135
  end
136

137 138 139
  def send_update_instructions
    projects.each(&:send_move_instructions)
  end
140 141 142 143

  def kind
    type == 'Group' ? 'group' : 'user'
  end
144 145 146 147

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