Read about our upcoming Code of Conduct on this issue

project.rb 8.2 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
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

gitlabhq's avatar
gitlabhq committed
9
10
  belongs_to :owner, :class_name => "User"

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
11
  has_many :merge_requests, :dependent => :destroy
VSizov's avatar
VSizov committed
12
  has_many :issues, :dependent => :destroy, :order => "position"
gitlabhq's avatar
gitlabhq committed
13
14
15
  has_many :users_projects, :dependent => :destroy
  has_many :users, :through => :users_projects
  has_many :notes, :dependent => :destroy
gitlabhq's avatar
gitlabhq committed
16
  has_many :snippets, :dependent => :destroy
17
  has_many :web_hooks, :dependent => :destroy
gitlabhq's avatar
gitlabhq committed
18

Aleksei Kvitinskii's avatar
Aleksei Kvitinskii committed
19
20
  acts_as_taggable

gitlabhq's avatar
gitlabhq committed
21
22
23
24
25
26
27
28
  validates :name,
            :uniqueness => true,
            :presence => true,
            :length   => { :within => 0..255 }

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

gitlabhq's avatar
gitlabhq committed
33
34
35
36
37
38
  validates :description,
            :length   => { :within => 0..2000 }

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

gitlabhq's avatar
gitlabhq committed
43
44
45
  validates :owner,
            :presence => true

Valera Sizov's avatar
Valera Sizov committed
46
  validate :check_limit
gitlabhq's avatar
gitlabhq committed
47
48
  validate :repo_name

49
50
  after_destroy :destroy_repository
  after_save :update_repository
gitlabhq's avatar
gitlabhq committed
51

52
  attr_protected :private_flag, :owner_id
gitlabhq's avatar
gitlabhq committed
53
54
55

  scope :public_only, where(:private_flag => false)

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

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

69
70
71
72
73
  def repository
    @repository ||= Repository.new(self)
  end

  delegate :repo,
74
75
    :url_to_repo,
    :path_to_repo,
76
77
    :update_repository,
    :destroy_repository,
78
79
80
81
82
83
84
85
    :tags,
    :repo_exists?,
    :commit,
    :commits,
    :tree,
    :heads,
    :commits_since,
    :fresh_commits,
86
    :commits_between,
87
88
    :to => :repository, :prefix => nil

gitlabhq's avatar
gitlabhq committed
89
90
91
92
  def to_param
    code
  end

93
94
95
96
97
  def web_url
    [GIT_HOST['host'], code].join("/")
  end

  def execute_web_hooks(oldrev, newrev, ref)
98
99
100
101
102
    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"

103
104
105
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
    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

138
139
140
141
142
  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

143
144
145
146
  def team_member_by_id(user_id)
    users_projects.find_by_user_id(user_id)
  end

gitlabhq's avatar
gitlabhq committed
147
148
149
150
151
152
153
154
  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

gitlabhq's avatar
gitlabhq committed
155
  def common_notes
gitlabhq's avatar
gitlabhq committed
156
    notes.where(:noteable_type => ["", nil]).inc_author_project
gitlabhq's avatar
gitlabhq committed
157
158
  end

159
160
  def build_commit_note(commit)
    notes.new(:noteable_id => commit.id, :noteable_type => "Commit")
gitlabhq's avatar
gitlabhq committed
161
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
162

163
164
  def commit_notes(commit)
    notes.where(:noteable_id => commit.id, :noteable_type => "Commit")
gitlabhq's avatar
gitlabhq committed
165
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
166

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
167
168
169
170
  def has_commits?
    !!commit
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
171
172
  # Compatible with all access rights
  # Should be rewrited for new access rights
gitlabhq's avatar
gitlabhq committed
173
  def add_access(user, *access)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
174
175
176
177
178
179
180
    access = if access.include?(:admin) 
               { :project_access => PROJECT_RWA } 
             elsif access.include?(:write)
               { :project_access => PROJECT_RW } 
             else
               { :project_access => PROJECT_R } 
             end
gitlabhq's avatar
gitlabhq committed
181
    opts = { :user => user }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
182
    opts.merge!(access)
gitlabhq's avatar
gitlabhq committed
183
184
185
186
187
188
189
    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

190
191
192
193
  def repository_readers
    keys = Key.joins({:user => :users_projects}).
      where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_R)
    keys.map(&:identifier)
gitlabhq's avatar
gitlabhq committed
194
195
  end

196
  def repository_writers
197
198
    keys = Key.joins({:user => :users_projects}).
      where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_RW)
gitlabhq's avatar
gitlabhq committed
199
200
201
202
    keys.map(&:identifier)
  end

  def readers
203
204
205
206
207
    @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)
gitlabhq's avatar
gitlabhq committed
208
209
210
  end

  def admins
211
    @admins ||= users_projects.includes(:user).where(:project_access => PROJECT_RWA).map(&:user)
gitlabhq's avatar
gitlabhq committed
212
213
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
214
215
216
217
218
219
220
221
222
223
224
225
  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

gitlabhq's avatar
gitlabhq committed
226
  def root_ref 
227
    default_branch || "master"
gitlabhq's avatar
gitlabhq committed
228
229
  end

gitlabhq's avatar
gitlabhq committed
230
231
232
233
234
235
236
237
  def public?
    !private_flag
  end

  def private?
    private_flag
  end

238
  def last_activity
gitlabhq's avatar
gitlabhq committed
239
    updates(1).first
240
  rescue
gitlabhq's avatar
gitlabhq committed
241
242
243
244
245
246
247
    nil
  end

  def last_activity_date
    last_activity.try(:created_at)
  end

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
266
267
268
269
270
271
272
273
274
  # 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)
275
      Rails.cache.write(activities_key, activities, :expires_in => expire)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
276
277
278
279
280
281
282
    end

    activities
  end

  # Get 20 events for project like
  # commits, issues or notes
gitlabhq's avatar
gitlabhq committed
283
  def updates(n = 3)
284
    [
gitlabhq's avatar
gitlabhq committed
285
      fresh_commits(n),
gitlabhq's avatar
gitlabhq committed
286
287
      fresh_issues(n),
      fresh_notes(n)
gitlabhq's avatar
gitlabhq committed
288
289
    ].compact.flatten.sort do |x, y|
      y.created_at <=> x.created_at
gitlabhq's avatar
gitlabhq committed
290
    end[0...n]
gitlabhq's avatar
gitlabhq committed
291
292
  end

Valera Sizov's avatar
Valera Sizov committed
293
294
  def check_limit
    unless owner.can_create_project?
gitlabhq's avatar
gitlabhq committed
295
      errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
Valera Sizov's avatar
Valera Sizov committed
296
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
297
  rescue
gitlabhq's avatar
gitlabhq committed
298
    errors[:base] << ("Cant check your ability to create project")
Valera Sizov's avatar
Valera Sizov committed
299
300
  end

gitlabhq's avatar
gitlabhq committed
301
  def repo_name
302
303
    if path == "gitolite-admin"
      errors.add(:path, " like 'gitolite-admin' is not allowed")
gitlabhq's avatar
gitlabhq committed
304
305
306
    end
  end

gitlabhq's avatar
gitlabhq committed
307
308
309
310
311
312
313
314
315
316
317
  def valid_repo?
    repo
  rescue
    errors.add(:path, "Invalid repository path")
    false
  end
end
# == Schema Information
#
# Table name: projects
#
Valery Sizov's avatar
Valery Sizov committed
318
319
320
321
322
323
324
325
326
327
#  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
gitlabhq's avatar
gitlabhq committed
328
329
#