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

gitlabhq's avatar
gitlabhq committed
43 44 45
  after_destroy :destroy_gitosis_project
  after_save :update_gitosis_project

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 57 58
    :url_to_repo,
    :path_to_repo,
    :update_gitosis_project,
    :destroy_gitosis_project,
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

gitlabhq's avatar
gitlabhq committed
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

gitlabhq's avatar
gitlabhq committed
125 126 127 128
  def root_ref 
    "master"
  end

gitlabhq's avatar
gitlabhq committed
129 130 131 132 133 134 135 136
  def public?
    !private_flag
  end

  def private?
    private_flag
  end

137
  def last_activity
gitlabhq's avatar
gitlabhq committed
138
    updates(1).first
139
  rescue
gitlabhq's avatar
gitlabhq committed
140 141 142 143 144 145 146
    nil
  end

  def last_activity_date
    last_activity.try(:created_at)
  end

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
gitlabhq's avatar
gitlabhq committed
164
  def updates(n = 3)
165
    [
gitlabhq's avatar
gitlabhq committed
166
      fresh_commits(n),
gitlabhq's avatar
gitlabhq committed
167 168
      fresh_issues(n),
      fresh_notes(n)
gitlabhq's avatar
gitlabhq committed
169 170
    ].compact.flatten.sort do |x, y|
      y.created_at <=> x.created_at
gitlabhq's avatar
gitlabhq committed
171
    end[0...n]
gitlabhq's avatar
gitlabhq committed
172 173
  end

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

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

gitlabhq's avatar
gitlabhq committed
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)
207
#  owner_id     :integer
gitlabhq's avatar
gitlabhq committed
208 209
#