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

class Project < ActiveRecord::Base
G
gitlabhq 已提交
4 5
  belongs_to :owner, :class_name => "User"

D
Dmitriy Zaporozhets 已提交
6
  has_many :merge_requests, :dependent => :destroy
V
VSizov 已提交
7
  has_many :issues, :dependent => :destroy, :order => "position"
G
gitlabhq 已提交
8 9 10
  has_many :users_projects, :dependent => :destroy
  has_many :users, :through => :users_projects
  has_many :notes, :dependent => :destroy
G
gitlabhq 已提交
11
  has_many :snippets, :dependent => :destroy
G
gitlabhq 已提交
12

A
Aleksei Kvitinskii 已提交
13 14
  acts_as_taggable

G
gitlabhq 已提交
15 16 17 18 19 20 21 22
  validates :name,
            :uniqueness => true,
            :presence => true,
            :length   => { :within => 0..255 }

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

G
gitlabhq 已提交
27 28 29 30 31 32
  validates :description,
            :length   => { :within => 0..2000 }

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

G
gitlabhq 已提交
37 38 39
  validates :owner,
            :presence => true

V
Valera Sizov 已提交
40
  validate :check_limit
G
gitlabhq 已提交
41 42
  validate :repo_name

G
gitlabhq 已提交
43 44 45
  after_destroy :destroy_gitosis_project
  after_save :update_gitosis_project

V
Valera Sizov 已提交
46
  attr_protected :private_flag, :owner_id
G
gitlabhq 已提交
47 48 49

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

D
Dmitriy Zaporozhets 已提交
50 51 52 53 54
  def repository
    @repository ||= Repository.new(self)
  end

  delegate :repo,
55 56 57 58
    :url_to_repo,
    :path_to_repo,
    :update_gitosis_project,
    :destroy_gitosis_project,
D
Dmitriy Zaporozhets 已提交
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

G
gitlabhq 已提交
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

G
gitlabhq 已提交
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

G
gitlabhq 已提交
86
  def common_notes
G
gitlabhq 已提交
87
    notes.where(:noteable_type => ["", nil]).inc_author_project
G
gitlabhq 已提交
88 89
  end

90 91
  def build_commit_note(commit)
    notes.new(:noteable_id => commit.id, :noteable_type => "Commit")
G
gitlabhq 已提交
92
  end
N
Nihad Abbasov 已提交
93

94 95
  def commit_notes(commit)
    notes.where(:noteable_id => commit.id, :noteable_type => "Commit")
G
gitlabhq 已提交
96
  end
N
Nihad Abbasov 已提交
97

G
gitlabhq 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  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

  def gitosis_writers
    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

G
gitlabhq 已提交
125 126 127 128
  def root_ref 
    "master"
  end

G
gitlabhq 已提交
129 130 131 132 133 134 135 136
  def public?
    !private_flag
  end

  def private?
    private_flag
  end

N
Nihad Abbasov 已提交
137
  def last_activity
G
gitlabhq 已提交
138
    updates(1).first
N
Nihad Abbasov 已提交
139
  rescue
G
gitlabhq 已提交
140 141 142 143 144 145 146
    nil
  end

  def last_activity_date
    last_activity.try(:created_at)
  end

D
Dmitriy Zaporozhets 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  # 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
G
gitlabhq 已提交
164
  def updates(n = 3)
N
Nihad Abbasov 已提交
165
    [
G
gitlabhq 已提交
166
      fresh_commits(n),
G
gitlabhq 已提交
167 168
      fresh_issues(n),
      fresh_notes(n)
G
gitlabhq 已提交
169 170
    ].compact.flatten.sort do |x, y|
      y.created_at <=> x.created_at
G
gitlabhq 已提交
171
    end[0...n]
G
gitlabhq 已提交
172 173
  end

V
Valera Sizov 已提交
174 175
  def check_limit
    unless owner.can_create_project?
G
gitlabhq 已提交
176
      errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
V
Valera Sizov 已提交
177
    end
N
Nihad Abbasov 已提交
178
  rescue
G
gitlabhq 已提交
179
    errors[:base] << ("Cant check your ability to create project")
V
Valera Sizov 已提交
180 181
  end

G
gitlabhq 已提交
182
  def repo_name
D
Dmitriy Zaporozhets 已提交
183
    if path == "gitosis-admin" && path == "gitolite-admin"
G
gitlabhq 已提交
184
      errors.add(:path, " like 'gitosis-admin' is not allowed")
G
gitlabhq 已提交
185 186 187
    end
  end

G
gitlabhq 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  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)
V
Valera Sizov 已提交
207
#  owner_id     :integer
G
gitlabhq 已提交
208 209
#