rosa-build/app/models/project.rb

175 lines
5.5 KiB
Ruby
Raw Normal View History

2011-03-09 17:38:21 +00:00
class Project < ActiveRecord::Base
2011-10-23 22:39:44 +01:00
VISIBILITIES = ['open', 'hidden']
relationable :as => :target
belongs_to :category, :counter_cache => true
2011-10-17 15:21:29 +01:00
belongs_to :owner, :polymorphic => true
2011-10-18 16:00:06 +01:00
2011-04-07 14:20:21 +01:00
has_many :build_lists, :dependent => :destroy
has_many :auto_build_lists, :dependent => :destroy
2011-03-10 12:35:23 +00:00
has_many :project_to_repositories, :dependent => :destroy
has_many :repositories, :through => :project_to_repositories
has_many :relations, :as => :target, :dependent => :destroy
2011-10-18 16:00:06 +01:00
has_many :collaborators, :through => :relations, :source => :object, :source_type => 'User'
has_many :groups, :through => :relations, :source => :object, :source_type => 'Group'
has_many :auto_build_lists, :dependent => :destroy
2011-10-17 15:21:29 +01:00
validates :name, :uniqueness => {:scope => [:owner_id, :owner_type]}, :presence => true, :allow_nil => false, :allow_blank => false
validates :unixname, :uniqueness => {:scope => [:owner_id, :owner_type]}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-\+\.]+$/ }, :allow_nil => false, :allow_blank => false
validates :owner, :presence => true
validate {errors.add(:base, I18n.t('flash.project.save_warning_ssh_key')) if owner.ssh_key.blank?}
2011-03-09 17:38:21 +00:00
2011-10-28 13:25:14 +01:00
#attr_accessible :category_id, :name, :unixname, :description, :visibility
attr_readonly :unixname
2011-03-31 02:15:17 +01:00
scope :recent, order("name ASC")
# scope :by_name, lambda { |name| {:conditions => ['name like ?', '%' + name + '%']} }
scope :by_name, lambda { |name| where('name like ?', '%' + name + '%') }
2011-10-23 22:39:44 +01:00
scope :by_visibilities, lambda {|v| {:conditions => ['visibility in (?)', v.join(',')]}}
scope :addable_to_repository, lambda { |repository_id| where("projects.id NOT IN (SELECT project_to_repositories.project_id FROM project_to_repositories WHERE (project_to_repositories.repository_id = #{ repository_id }))") }
scope :automateable, where("projects.id NOT IN (SELECT auto_build_lists.project_id FROM auto_build_lists)")
2011-10-23 22:39:44 +01:00
2011-10-31 01:02:12 +00:00
# before_save :add_owner_rel
after_create :make_owner_rel
before_save :check_owner_rel
after_create :attach_to_personal_repository
after_create :create_git_repo
before_update :update_git_repo
after_destroy :destroy_git_repo
after_rollback lambda { destroy_git_repo rescue true if new_record? }
2011-10-28 00:18:04 +01:00
def project_versions
2011-11-01 15:46:05 +00:00
res = tags.select { |tag| tag.name =~ /^v\./ }
return res if res and res.size > 0
tags
end
def collected_project_versions
project_versions.collect { |tag| new_tag = tag.name.gsub(/^\w+\./, ""); [new_tag, new_tag] }
end
def tags
self.git_repository.tags
end
2011-10-23 22:39:44 +01:00
2011-10-18 16:00:06 +01:00
def members
collaborators + groups
end
# include Project::HasRepository
2011-03-10 13:38:50 +00:00
# Redefining a method from Project::HasRepository module to reflect current situation
def git_repo_path
2011-10-28 00:18:04 +01:00
@git_repo_path ||= path
end
def git_repo_name
[owner.uname, unixname].join('/')
end
def git_repo_name_was
[owner.uname, unixname_was].join('/')
end
def git_repo_name_changed?; git_repo_name != git_repo_name_was; end
2011-03-10 13:38:50 +00:00
def public?
visibility == 'open'
2011-03-11 09:39:34 +00:00
end
2011-03-11 17:38:28 +00:00
def clone
p = Project.new
p.name = name
p.unixname = unixname
return p
end
def add_to_repository(platf, repo)
result = BuildServer.add_to_repo(repository.name, platf.name)
if result == BuildServer::SUCCESS
return true
else
2011-10-28 01:26:53 +01:00
raise "Failed to add project #{name} to repo #{repo.name} of platform #{platf.name} with code #{result}."
end
end
2011-10-28 00:18:04 +01:00
def path
build_path(git_repo_name)
end
2011-10-28 14:53:46 +01:00
def xml_rpc_create(repository)
result = BuildServer.create_project unixname, repository.platform.unixname, repository.unixname, path
2011-10-28 14:31:26 +01:00
if result == BuildServer::SUCCESS
return true
else
2011-10-28 18:51:02 +01:00
raise "Failed to create project #{unixname} (repo #{repository.unixname}) inside platform #{repository.platform.unixname} in path #{path} with code #{result}."
2011-10-28 14:31:26 +01:00
end
end
2011-10-28 14:53:46 +01:00
def xml_rpc_destroy(repository)
result = BuildServer.delete_project unixname, repository.platform.unixname
2011-10-28 14:31:26 +01:00
if result == BuildServer::SUCCESS
return true
else
raise "Failed to delete repository #{name} (repo main) inside platform #{owner.uname}_personal with code #{result}."
end
end
2011-03-09 17:38:21 +00:00
protected
2011-10-28 00:18:04 +01:00
def build_path(dir)
File.join(APP_CONFIG['root_path'], 'git_projects', "#{dir}.git")
end
def attach_to_personal_repository
repositories << self.owner.personal_repository if !repositories.exists?(:id => self.owner.personal_repository)
end
def create_git_repo
with_ga do |ga|
repo = ga.add_repo git_repo_name
repo.add_key owner.ssh_key, 'RW'
repo.has_anonymous_access!('R') if public?
ga.save_and_release
end
end
def update_git_repo
with_ga do |ga|
if repo = ga.find_repo(git_repo_name_was)
repo.rename(git_repo_name) if git_repo_name_changed?
public? ? repo.has_anonymous_access!('R') : repo.has_not_anonymous_access!
ga.save_and_release
end
end if git_repo_name_changed? or visibility_changed?
2011-03-09 17:38:21 +00:00
end
def destroy_git_repo
with_ga do |ga|
ga.rm_repo git_repo_name
ga.save_and_release
end
2011-10-19 14:14:53 +01:00
end
def add_owner_rel
if new_record? and owner
add_owner owner
elsif owner_id_changed?
remove_owner owner_type_was.classify.find(owner_id_was)
add_owner owner
end
end
2011-10-31 01:02:12 +00:00
def make_owner_rel
add_owner owner
end
def check_owner_rel
if !new_record? and owner_id_changed?
remove_owner owner_type_was.classify.find(owner_id_was) if owner_type_was
add_owner owner if owner
end
end
2011-03-09 17:38:21 +00:00
end