BigW Consortium Gitlab

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

Andrew8xx8 committed
30
  validates :description, length: { within: 0..255 }
31 32
  validates :path,
    length: { within: 1..255 },
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

43
  scope :root, -> { where('type IS NULL') }
44

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

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

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

      path
    end
85 86
  end

87
  def to_param
88
    path
89
  end
90 91 92 93

  def human_name
    owner_name
  end
94 95

  def ensure_dir_exist
96
    gitlab_shell.add_namespace(path)
Dmitriy Zaporozhets committed
97 98
  end

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

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

  def move_dir
115 116 117
    # Ensure old directory exists before moving it
    gitlab_shell.add_namespace(path_was)

118
    if gitlab_shell.mv_namespace(path_was, path)
119 120
      Gitlab::UploadsTransfer.new.rename_namespace(path_was, path)

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

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

  def kind
    type == 'Group' ? 'group' : 'user'
  end
148 149

  def find_fork_of(project)
150
    projects.joins(:forked_project_link).find_by('forked_project_links.forked_from_project_id = ?', project.id)
151
  end
152
end