project.rb 9.1 KB
Newer Older
G
gitlabhq 已提交
1 2 3
require "grit"

class Project < ActiveRecord::Base
4 5 6 7 8
  PROJECT_N = 0
  PROJECT_R = 1
  PROJECT_RW = 2
  PROJECT_RWA = 3

G
gitlabhq 已提交
9 10
  belongs_to :owner, :class_name => "User"

D
Dmitriy Zaporozhets 已提交
11
  has_many :merge_requests, :dependent => :destroy
V
VSizov 已提交
12
  has_many :issues, :dependent => :destroy, :order => "position"
G
gitlabhq 已提交
13 14 15
  has_many :users_projects, :dependent => :destroy
  has_many :users, :through => :users_projects
  has_many :notes, :dependent => :destroy
G
gitlabhq 已提交
16
  has_many :snippets, :dependent => :destroy
17
  has_many :deploy_keys, :dependent => :destroy, :foreign_key => "project_id", :class_name => "Key"
A
Ariejan de Vroom 已提交
18
  has_many :web_hooks, :dependent => :destroy
G
gitlabhq 已提交
19

A
Aleksei Kvitinskii 已提交
20 21
  acts_as_taggable

G
gitlabhq 已提交
22 23 24 25 26 27 28 29
  validates :name,
            :uniqueness => true,
            :presence => true,
            :length   => { :within => 0..255 }

  validates :path,
            :uniqueness => true,
            :presence => true,
30
            :format => { :with => /^[a-zA-Z0-9_\-\.]*$/,
31
                         :message => "only letters, digits & '_' '-' '.' allowed" },
G
gitlabhq 已提交
32
            :length   => { :within => 0..255 }
N
Nihad Abbasov 已提交
33

G
gitlabhq 已提交
34 35 36 37 38 39
  validates :description,
            :length   => { :within => 0..2000 }

  validates :code,
            :presence => true,
            :uniqueness => true,
40
            :format => { :with => /^[a-zA-Z0-9_\-\.]*$/,
41
                         :message => "only letters, digits & '_' '-' '.' allowed"  },
G
gitlabhq 已提交
42
            :length   => { :within => 3..255 }
G
gitlabhq 已提交
43

G
gitlabhq 已提交
44 45 46
  validates :owner,
            :presence => true

V
Valera Sizov 已提交
47
  validate :check_limit
G
gitlabhq 已提交
48 49
  validate :repo_name

50 51
  after_destroy :destroy_repository
  after_save :update_repository
G
gitlabhq 已提交
52

V
Valera Sizov 已提交
53
  attr_protected :private_flag, :owner_id
G
gitlabhq 已提交
54 55

  scope :public_only, where(:private_flag => false)
D
Dmitriy Zaporozhets 已提交
56
  scope :without_user, lambda { |user|  where("id not in (:ids)", :ids => user.projects.map(&:id) ) }
G
gitlabhq 已提交
57

58 59 60
  def self.active
    joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC")
  end
61 62 63 64 65 66 67 68 69 70

  def self.access_options
    {
      "Denied" => PROJECT_N,
      "Read"   => PROJECT_R,
      "Report" => PROJECT_RW,
      "Admin"  => PROJECT_RWA
    }
  end

D
Dmitriy Zaporozhets 已提交
71 72 73 74 75
  def repository
    @repository ||= Repository.new(self)
  end

  delegate :repo,
76 77
    :url_to_repo,
    :path_to_repo,
78 79
    :update_repository,
    :destroy_repository,
D
Dmitriy Zaporozhets 已提交
80 81 82 83
    :tags,
    :repo_exists?,
    :commit,
    :commits,
84
    :commits_with_refs,
D
Dmitriy Zaporozhets 已提交
85 86 87 88
    :tree,
    :heads,
    :commits_since,
    :fresh_commits,
A
Ariejan de Vroom 已提交
89
    :commits_between,
D
Dmitriy Zaporozhets 已提交
90 91
    :to => :repository, :prefix => nil

G
gitlabhq 已提交
92 93 94 95
  def to_param
    code
  end

A
Ariejan de Vroom 已提交
96 97 98 99 100
  def web_url
    [GIT_HOST['host'], code].join("/")
  end

  def execute_web_hooks(oldrev, newrev, ref)
A
Ariejan de Vroom 已提交
101 102 103 104 105
    ref_parts = ref.split('/')

    # Return if this is not a push to a branch (e.g. new commits)
    return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000"

A
Ariejan de Vroom 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    data = web_hook_data(oldrev, newrev, ref)
    web_hooks.each { |web_hook| web_hook.execute(data) }
  end

  def web_hook_data(oldrev, newrev, ref)
    data = {
      before: oldrev,
      after: newrev,
      ref: ref,
      repository: {
        name: name,
        url: web_url,
        description: description,
        homepage: web_url,
        private: private?
      },
      commits: []
    }

    commits_between(oldrev, newrev).each do |commit|
      data[:commits] << {
        id: commit.id,
        message: commit.safe_message,
        timestamp: commit.date.xmlschema,
        url: "http://#{GIT_HOST['host']}/#{code}/commits/#{commit.id}",
        author: {
          name: commit.author_name,
          email: commit.author_email
        }
      }
    end

    data
  end

141 142 143 144 145
  def team_member_by_name_or_email(email = nil, name = nil)
    user = users.where("email like ? or name like ?", email, name).first
    users_projects.find_by_user_id(user.id) if user
  end

146 147 148 149
  def team_member_by_id(user_id)
    users_projects.find_by_user_id(user_id)
  end

150 151 152 153
  def fresh_merge_requests(n)
    merge_requests.includes(:project, :author).order("created_at desc").first(n)
  end

G
gitlabhq 已提交
154 155 156 157 158 159 160 161
  def fresh_issues(n)
    issues.includes(:project, :author).order("created_at desc").first(n)
  end

  def fresh_notes(n)
    notes.inc_author_project.order("created_at desc").first(n)
  end

G
gitlabhq 已提交
162
  def common_notes
G
gitlabhq 已提交
163
    notes.where(:noteable_type => ["", nil]).inc_author_project
G
gitlabhq 已提交
164 165
  end

166 167
  def build_commit_note(commit)
    notes.new(:noteable_id => commit.id, :noteable_type => "Commit")
G
gitlabhq 已提交
168
  end
N
Nihad Abbasov 已提交
169

170
  def commit_notes(commit)
D
Dmitriy Zaporozhets 已提交
171 172 173 174
    notes.where(:noteable_id => commit.id, :noteable_type => "Commit", :line_code => nil)
  end

  def commit_line_notes(commit)
V
Valery Sizov 已提交
175
    notes.where(:noteable_id => commit.id, :noteable_type => "Commit").where("line_code is not null")
G
gitlabhq 已提交
176
  end
N
Nihad Abbasov 已提交
177

D
Dmitriy Zaporozhets 已提交
178 179 180 181
  def has_commits?
    !!commit
  end

D
Dmitriy Zaporozhets 已提交
182 183
  # Compatible with all access rights
  # Should be rewrited for new access rights
G
gitlabhq 已提交
184
  def add_access(user, *access)
D
Dmitriy Zaporozhets 已提交
185 186 187 188 189 190 191
    access = if access.include?(:admin) 
               { :project_access => PROJECT_RWA } 
             elsif access.include?(:write)
               { :project_access => PROJECT_RW } 
             else
               { :project_access => PROJECT_R } 
             end
G
gitlabhq 已提交
192
    opts = { :user => user }
D
Dmitriy Zaporozhets 已提交
193
    opts.merge!(access)
G
gitlabhq 已提交
194 195 196 197 198 199 200
    users_projects.create(opts)
  end

  def reset_access(user)
    users_projects.where(:project_id => self.id, :user_id => user.id).destroy if self.id
  end

201 202 203
  def repository_readers
    keys = Key.joins({:user => :users_projects}).
      where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_R)
204
    keys.map(&:identifier) + deploy_keys.map(&:identifier)
G
gitlabhq 已提交
205 206
  end

207
  def repository_writers
208 209
    keys = Key.joins({:user => :users_projects}).
      where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_RW)
G
gitlabhq 已提交
210 211 212 213
    keys.map(&:identifier)
  end

  def readers
214 215 216 217 218
    @readers ||= users_projects.includes(:user).where(:project_access => [PROJECT_R, PROJECT_RW, PROJECT_RWA]).map(&:user)
  end

  def writers
    @writers ||= users_projects.includes(:user).where(:project_access => [PROJECT_RW, PROJECT_RWA]).map(&:user)
G
gitlabhq 已提交
219 220 221
  end

  def admins
222
    @admins ||= users_projects.includes(:user).where(:project_access => PROJECT_RWA).map(&:user)
G
gitlabhq 已提交
223 224
  end

D
Dmitriy Zaporozhets 已提交
225 226 227 228 229 230 231 232 233 234 235 236
  def allow_read_for?(user)
    !users_projects.where(:user_id => user.id, :project_access => [PROJECT_R, PROJECT_RW, PROJECT_RWA]).empty?
  end

  def allow_write_for?(user)
    !users_projects.where(:user_id => user.id, :project_access => [PROJECT_RW, PROJECT_RWA]).empty?
  end

  def allow_admin_for?(user)
    !users_projects.where(:user_id => user.id, :project_access => [PROJECT_RWA]).empty? || owner_id == user.id
  end

237 238 239 240
  def allow_pull_for?(user)
    !users_projects.where(:user_id => user.id, :repo_access => [Repository::REPO_R, Repository::REPO_RW]).empty?
  end

G
gitlabhq 已提交
241
  def root_ref 
242
    default_branch || "master"
G
gitlabhq 已提交
243 244
  end

G
gitlabhq 已提交
245 246 247 248 249 250 251 252
  def public?
    !private_flag
  end

  def private?
    private_flag
  end

N
Nihad Abbasov 已提交
253
  def last_activity
G
gitlabhq 已提交
254
    updates(1).first
N
Nihad Abbasov 已提交
255
  rescue
G
gitlabhq 已提交
256 257 258 259 260 261 262
    nil
  end

  def last_activity_date
    last_activity.try(:created_at)
  end

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
  def last_activity_date_cached(expire = 1.hour)
    activity_date_key = "project_#{id}_activity_date"

    cached_activities = Rails.cache.read(activity_date_key)
    if cached_activities
      activity_date = if cached_activities == "Never"
                        nil
                      else
                        cached_activities
                      end
    else
      activity_date = last_activity_date
      Rails.cache.write(activity_date_key, activity_date || "Never", :expires_in => expire)
    end

    activity_date
  end

D
Dmitriy Zaporozhets 已提交
281 282 283 284 285 286 287 288 289
  # Get project updates from cache
  # or calculate. 
  def cached_updates(limit, expire = 2.minutes)
    activities_key = "project_#{id}_activities"
    cached_activities = Rails.cache.read(activities_key)
    if cached_activities
      activities = cached_activities
    else
      activities = updates(limit)
290
      Rails.cache.write(activities_key, activities, :expires_in => expire)
D
Dmitriy Zaporozhets 已提交
291 292 293 294 295 296 297
    end

    activities
  end

  # Get 20 events for project like
  # commits, issues or notes
G
gitlabhq 已提交
298
  def updates(n = 3)
N
Nihad Abbasov 已提交
299
    [
G
gitlabhq 已提交
300
      fresh_commits(n),
G
gitlabhq 已提交
301 302
      fresh_issues(n),
      fresh_notes(n)
G
gitlabhq 已提交
303
    ].compact.flatten.sort do |x, y|
304 305 306 307
      y.created_at <=> x.created_at
    end[0...n]
  end

D
Dmitriy Zaporozhets 已提交
308
  def activities(n=3)
309 310 311
    [
      fresh_issues(n),
      fresh_merge_requests(n),
D
Dmitriy Zaporozhets 已提交
312
      notes.inc_author_project.where("noteable_type is not null").order("created_at desc").first(n)
313
    ].compact.flatten.sort do |x, y|
G
gitlabhq 已提交
314
      y.created_at <=> x.created_at
G
gitlabhq 已提交
315
    end[0...n]
G
gitlabhq 已提交
316 317
  end

V
Valera Sizov 已提交
318 319
  def check_limit
    unless owner.can_create_project?
G
gitlabhq 已提交
320
      errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
V
Valera Sizov 已提交
321
    end
N
Nihad Abbasov 已提交
322
  rescue
G
gitlabhq 已提交
323
    errors[:base] << ("Cant check your ability to create project")
V
Valera Sizov 已提交
324 325
  end

G
gitlabhq 已提交
326
  def repo_name
327 328
    if path == "gitolite-admin"
      errors.add(:path, " like 'gitolite-admin' is not allowed")
G
gitlabhq 已提交
329 330 331
    end
  end

G
gitlabhq 已提交
332 333 334 335 336 337 338 339 340 341 342
  def valid_repo?
    repo
  rescue
    errors.add(:path, "Invalid repository path")
    false
  end
end
# == Schema Information
#
# Table name: projects
#
V
Valery Sizov 已提交
343 344 345 346 347 348 349 350 351 352
#  id             :integer         not null, primary key
#  name           :string(255)
#  path           :string(255)
#  description    :text
#  created_at     :datetime
#  updated_at     :datetime
#  private_flag   :boolean         default(TRUE), not null
#  code           :string(255)
#  owner_id       :integer
#  default_branch :string(255)     default("master"), not null
G
gitlabhq 已提交
353 354
#