1 # == Schema Information
5 # id :integer not null, primary key
9 # created_at :datetime not null
10 # updated_at :datetime not null
12 # default_branch :string(255)
13 # issues_enabled :boolean default(TRUE), not null
14 # wall_enabled :boolean default(TRUE), not null
15 # merge_requests_enabled :boolean default(TRUE), not null
16 # wiki_enabled :boolean default(TRUE), not null
17 # namespace_id :integer
18 # public :boolean default(FALSE), not null
19 # issues_tracker :string(255) default("gitlab"), not null
20 # issues_tracker_id :string(255)
21 # snippets_enabled :boolean default(TRUE), not null
22 # last_activity_at :datetime
27 class Project < ActiveRecord::Base
28 include Gitlab::ShellAdapter
31 attr_accessible :name, :path, :description, :default_branch, :issues_tracker,
32 :issues_enabled, :wall_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id,
33 :wiki_enabled, :public, :import_url, :last_activity_at, as: [:default, :admin]
35 attr_accessible :namespace_id, :creator_id, as: :admin
37 attr_accessor :import_url
40 belongs_to :creator, foreign_key: "creator_id", class_name: "User"
41 belongs_to :group, foreign_key: "namespace_id", conditions: "type = 'Group'"
44 has_one :last_event, class_name: 'Event', order: 'events.created_at DESC', foreign_key: 'project_id'
45 has_one :gitlab_ci_service, dependent: :destroy
47 has_many :events, dependent: :destroy
48 has_many :merge_requests, dependent: :destroy
49 has_many :issues, dependent: :destroy, order: "state DESC, created_at DESC"
50 has_many :milestones, dependent: :destroy
51 has_many :users_projects, dependent: :destroy
52 has_many :notes, dependent: :destroy
53 has_many :snippets, dependent: :destroy
54 has_many :deploy_keys, dependent: :destroy, class_name: "Key", foreign_key: "project_id"
55 has_many :hooks, dependent: :destroy, class_name: "ProjectHook"
56 has_many :wikis, dependent: :destroy
57 has_many :protected_branches, dependent: :destroy
58 has_many :user_team_project_relationships, dependent: :destroy
60 has_many :users, through: :users_projects
61 has_many :user_teams, through: :user_team_project_relationships
62 has_many :user_team_user_relationships, through: :user_teams
63 has_many :user_teams_members, through: :user_team_user_relationships
65 delegate :name, to: :owner, allow_nil: true, prefix: true
68 validates :creator, presence: true
69 validates :description, length: { within: 0..2000 }
70 validates :name, presence: true, length: { within: 0..255 },
71 format: { with: Gitlab::Regex.project_name_regex,
72 message: "only letters, digits, spaces & '_' '-' '.' allowed. Letter should be first" }
73 validates :path, presence: true, length: { within: 0..255 },
74 format: { with: Gitlab::Regex.path_regex,
75 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
76 validates :issues_enabled, :wall_enabled, :merge_requests_enabled,
77 :wiki_enabled, inclusion: { in: [true, false] }
78 validates :issues_tracker_id, length: { within: 0..255 }
80 validates_uniqueness_of :name, scope: :namespace_id
81 validates_uniqueness_of :path, scope: :namespace_id
83 validates :import_url,
84 format: { with: URI::regexp(%w(http https)), message: "should be a valid url" },
87 validate :check_limit, :repo_name
90 scope :without_user, ->(user) { where("projects.id NOT IN (:ids)", ids: user.authorized_projects.map(&:id) ) }
91 scope :without_team, ->(team) { team.projects.present? ? where("projects.id NOT IN (:ids)", ids: team.projects.map(&:id)) : scoped }
92 scope :not_in_group, ->(group) { where("projects.id NOT IN (:ids)", ids: group.project_ids ) }
93 scope :in_team, ->(team) { where("projects.id IN (:ids)", ids: team.projects.map(&:id)) }
94 scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) }
95 scope :in_group_namespace, -> { joins(:group) }
96 scope :sorted_by_activity, -> { order("projects.last_activity_at DESC") }
97 scope :personal, ->(user) { where(namespace_id: user.namespace_id) }
98 scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) }
99 scope :public_only, -> { where(public: true) }
101 enumerize :issues_tracker, in: (Gitlab.config.issues_tracker.keys).append(:gitlab), default: :gitlab
105 project_ids = Event.select('max(created_at) as latest_date, project_id').
107 having('latest_date < ?', 6.months.ago).map(&:project_id)
109 where(id: project_ids)
113 includes(:events).where('events.action = ?', Event::PUSHED)
117 joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC")
121 where("projects.name LIKE :query OR projects.path LIKE :query", query: "%#{query}%")
124 def find_with_namespace(id)
127 namespace = Namespace.find_by_path(id.first)
128 return nil unless namespace
130 where(namespace_id: namespace.id).find_by_path(id.second)
132 where(path: id, namespace_id: nil).last
137 UsersProject.access_roles
142 @team ||= ProjectTeam.new(self)
146 @repository ||= Repository.new(path_with_namespace, default_branch)
158 unless creator.can_create_project?
159 errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it")
162 errors[:base] << ("Can't check your ability to create project")
166 denied_paths = %w(admin dashboard groups help profile projects search)
168 if denied_paths.include?(path)
169 errors.add(:path, "like #{path} is not allowed")
175 namespace.path + "/" + path
182 [Gitlab.config.gitlab.url, path_with_namespace].join("/")
185 def build_commit_note(commit)
186 notes.new(commit_id: commit.id, noteable_type: "Commit")
193 def last_activity_date
194 last_activity_at || updated_at
202 issues.tag_counts_on(:labels)
205 def issue_exists?(issue_id)
206 if used_default_issues_tracker?
207 self.issues.where(id: issue_id).first.present?
213 def used_default_issues_tracker?
214 self.issues_tracker == Project.issues_tracker.default_value
217 def can_have_issues_tracker_id?
218 self.issues_enabled && !self.used_default_issues_tracker?
222 [gitlab_ci_service].compact
226 gitlab_ci_service && gitlab_ci_service.active
229 # For compatibility with old code
238 when 'merge_request' then
243 def send_move_instructions
244 self.users_projects.each do |member|
245 Notify.delay.project_was_moved_email(member.id)
257 def team_member_by_name_or_email(name = nil, email = nil)
258 user = users.where("name like ? or email like ?", name, email).first
259 users_projects.where(user: user) if user
262 # Get Team Member record by user id
263 def team_member_by_id(user_id)
264 users_projects.find_by_user_id(user_id)
267 def name_with_namespace
268 @name_with_namespace ||= begin
270 namespace.human_name + " / " + name
278 namespace.try(:owner)
281 def path_with_namespace
283 namespace.path + '/' + path
289 def transfer(new_namespace)
290 ProjectTransferService.new.transfer(self, new_namespace)
293 def execute_hooks(data)
294 hooks.each { |hook| hook.async_execute(data) }
297 def execute_services(data)
298 services.each do |service|
300 # Call service hook only if it is active
301 service.execute(data) if service.active
305 def discover_default_branch
306 # Discover the default branch, but only if it hasn't already been set to
308 if repository && default_branch.nil?
309 update_attributes(default_branch: self.repository.discover_default_branch)
313 def update_merge_requests(oldrev, newrev, ref, user)
314 return true unless ref =~ /heads/
315 branch_name = ref.gsub("refs/heads/", "")
316 c_ids = self.repository.commits_between(oldrev, newrev).map(&:id)
318 # Update code for merge requests
319 mrs = self.merge_requests.opened.by_branch(branch_name).all
320 mrs.each { |merge_request| merge_request.reload_code; merge_request.mark_as_unchecked }
322 # Close merge requests
323 mrs = self.merge_requests.opened.where(target_branch: branch_name).all
324 mrs = mrs.select(&:last_commit).select { |mr| c_ids.include?(mr.last_commit.id) }
325 mrs.each { |merge_request| merge_request.merge!(user.id) }
333 errors.add(:path, "Invalid repository path")
338 !repository.exists? || repository.empty?
341 def ensure_satellite_exists
342 self.satellite.create unless self.satellite.exists?
346 @satellite ||= Gitlab::Satellite::Satellite.new(self)
354 gitlab_shell.url_to_repo(path_with_namespace)
358 namespace.try(:path) || ''
362 @repo_exists ||= repository.exists?
368 all_branches = repository.branches
370 if protected_branches.present?
371 all_branches.reject! do |branch|
372 protected_branches_names.include?(branch.name)
379 def protected_branches_names
380 @protected_branches_names ||= protected_branches.map(&:name)
383 def root_ref?(branch)
384 repository.root_ref == branch
392 http_url = [Gitlab.config.gitlab.url, "/", path_with_namespace, ".git"].join('')
395 def project_access_human(member)
396 project_user_relation = self.users_projects.find_by_user_id(member.id)
397 self.class.access_options.key(project_user_relation.project_access)
400 # Check if current branch name is marked as protected in the system
401 def protected_branch? branch_name
402 protected_branches_names.include?(branch_name)