BigW Consortium Gitlab

namespace.rb 4.53 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
#  public      :boolean          default(FALSE)
Dmitriy Zaporozhets committed
15 16
#

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

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

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

Andrew8xx8 committed
31
  validates :description, length: { within: 0..255 }
32 33 34 35 36
  validates :path,
    uniqueness: { case_sensitive: false },
    presence: true,
    length: { within: 1..255 },
    exclusion: { in: Gitlab::Blacklist.path },
37 38
    format: { with: Gitlab::Regex.namespace_regex,
              message: Gitlab::Regex.namespace_regex_message }
39 40 41

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

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

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

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

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

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

      path
    end
88 89
  end

90
  def to_param
91
    path
92
  end
93 94 95 96

  def human_name
    owner_name
  end
97 98

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

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

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

  def move_dir
118 119 120
    # Ensure old directory exists before moving it
    gitlab_shell.add_namespace(path_was)

121
    if gitlab_shell.mv_namespace(path_was, path)
122 123
      Gitlab::UploadsTransfer.new.rename_namespace(path_was, path)

124 125
      # If repositories moved successfully we need to
      # send update instructions to users.
126 127
      # However we cannot allow rollback since we moved namespace dir
      # So we basically we mute exceptions in next actions
128
      begin
129
        send_update_instructions
130
      rescue
Johannes Schleifenbaum committed
131
        # Returning false does not rollback after_* transaction but gives
132 133
        # us information about failing some of tasks
        false
134
      end
135 136 137 138
    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')
139
    end
140
  end
141

142
  def send_update_instructions
143 144 145
    projects.each do |project|
      project.send_move_instructions("#{path_was}/#{project.path}")
    end
146
  end
147 148 149 150

  def kind
    type == 'Group' ? 'group' : 'user'
  end
151 152 153 154

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