project.rb 4.93 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
2
3
require "grit"

class Project < ActiveRecord::Base
gitlabhq's avatar
gitlabhq committed
4
5
  belongs_to :owner, :class_name => "User"

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6
  has_many :merge_requests, :dependent => :destroy
VSizov's avatar
VSizov committed
7
  has_many :issues, :dependent => :destroy, :order => "position"
gitlabhq's avatar
gitlabhq committed
8
9
10
  has_many :users_projects, :dependent => :destroy
  has_many :users, :through => :users_projects
  has_many :notes, :dependent => :destroy
gitlabhq's avatar
gitlabhq committed
11
  has_many :snippets, :dependent => :destroy
gitlabhq's avatar
gitlabhq committed
12

Aleksei Kvitinskii's avatar
Aleksei Kvitinskii committed
13
14
  acts_as_taggable

gitlabhq's avatar
gitlabhq committed
15
16
17
18
19
20
21
22
  validates :name,
            :uniqueness => true,
            :presence => true,
            :length   => { :within => 0..255 }

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

gitlabhq's avatar
gitlabhq committed
27
28
29
30
31
32
  validates :description,
            :length   => { :within => 0..2000 }

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

gitlabhq's avatar
gitlabhq committed
37
38
39
  validates :owner,
            :presence => true

Valera Sizov's avatar
Valera Sizov committed
40
  validate :check_limit
gitlabhq's avatar
gitlabhq committed
41
42
  validate :repo_name

43
44
  after_destroy :destroy_repository
  after_save :update_repository
gitlabhq's avatar
gitlabhq committed
45

46
  attr_protected :private_flag, :owner_id
gitlabhq's avatar
gitlabhq committed
47
48
49

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

50
51
52
53
54
  def repository
    @repository ||= Repository.new(self)
  end

  delegate :repo,
55
56
    :url_to_repo,
    :path_to_repo,
57
58
    :update_repository,
    :destroy_repository,
59
60
61
62
63
64
65
66
67
68
    :tags,
    :repo_exists?,
    :commit,
    :commits,
    :tree,
    :heads,
    :commits_since,
    :fresh_commits,
    :to => :repository, :prefix => nil

gitlabhq's avatar
gitlabhq committed
69
70
71
72
  def to_param
    code
  end

73
74
75
76
77
  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

gitlabhq's avatar
gitlabhq committed
78
79
80
81
82
83
84
85
  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
86
  def common_notes
gitlabhq's avatar
gitlabhq committed
87
    notes.where(:noteable_type => ["", nil]).inc_author_project
gitlabhq's avatar
gitlabhq committed
88
89
  end

90
91
  def build_commit_note(commit)
    notes.new(:noteable_id => commit.id, :noteable_type => "Commit")
gitlabhq's avatar
gitlabhq committed
92
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
93

94
95
  def commit_notes(commit)
    notes.where(:noteable_id => commit.id, :noteable_type => "Commit")
gitlabhq's avatar
gitlabhq committed
96
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
97

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
98
99
100
101
  def has_commits?
    !!commit
  end

gitlabhq's avatar
gitlabhq committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  def add_access(user, *access)
    opts = { :user => user }
    access.each { |name| opts.merge!(name => true) }
    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

  def writers
    @writers ||= users_projects.includes(:user).where(:write => true).map(&:user)
  end

116
  def repository_writers
gitlabhq's avatar
gitlabhq committed
117
118
119
120
121
122
123
124
125
126
127
128
    keys = Key.joins({:user => :users_projects}).where("users_projects.project_id = ? AND users_projects.write = ?", id, true)
    keys.map(&:identifier)
  end

  def readers
    @readers ||= users_projects.includes(:user).where(:read => true).map(&:user)
  end

  def admins
    @admins ||=users_projects.includes(:user).where(:admin => true).map(&:user)
  end

gitlabhq's avatar
gitlabhq committed
129
130
131
132
  def root_ref 
    "master"
  end

gitlabhq's avatar
gitlabhq committed
133
134
135
136
137
138
139
140
  def public?
    !private_flag
  end

  def private?
    private_flag
  end

141
  def last_activity
gitlabhq's avatar
gitlabhq committed
142
    updates(1).first
143
  rescue
gitlabhq's avatar
gitlabhq committed
144
145
146
147
148
149
150
    nil
  end

  def last_activity_date
    last_activity.try(:created_at)
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  # 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)
      Rails.cache.write(activities_key, activities, :expires_in => 60.seconds)
    end

    activities
  end

  # Get 20 events for project like
  # commits, issues or notes
gitlabhq's avatar
gitlabhq committed
168
  def updates(n = 3)
169
    [
gitlabhq's avatar
gitlabhq committed
170
      fresh_commits(n),
gitlabhq's avatar
gitlabhq committed
171
172
      fresh_issues(n),
      fresh_notes(n)
gitlabhq's avatar
gitlabhq committed
173
174
    ].compact.flatten.sort do |x, y|
      y.created_at <=> x.created_at
gitlabhq's avatar
gitlabhq committed
175
    end[0...n]
gitlabhq's avatar
gitlabhq committed
176
177
  end

Valera Sizov's avatar
Valera Sizov committed
178
179
  def check_limit
    unless owner.can_create_project?
gitlabhq's avatar
gitlabhq committed
180
      errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
Valera Sizov's avatar
Valera Sizov committed
181
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
182
  rescue
gitlabhq's avatar
gitlabhq committed
183
    errors[:base] << ("Cant check your ability to create project")
Valera Sizov's avatar
Valera Sizov committed
184
185
  end

gitlabhq's avatar
gitlabhq committed
186
  def repo_name
187
188
    if path == "gitolite-admin"
      errors.add(:path, " like 'gitolite-admin' is not allowed")
gitlabhq's avatar
gitlabhq committed
189
190
191
    end
  end

gitlabhq's avatar
gitlabhq committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  def valid_repo?
    repo
  rescue
    errors.add(:path, "Invalid repository path")
    false
  end
end
# == Schema Information
#
# Table name: projects
#
#  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)
211
#  owner_id     :integer
gitlabhq's avatar
gitlabhq committed
212
213
#