2011-03-09 17:38:21 +00:00
|
|
|
class Project < ActiveRecord::Base
|
2011-10-19 21:58:31 +01:00
|
|
|
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
|
2011-03-10 12:35:23 +00:00
|
|
|
|
2011-10-13 23:35:16 +01:00
|
|
|
has_many :project_to_repositories
|
|
|
|
has_many :repositories, :through => :project_to_repositories
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2011-10-18 16:00:06 +01:00
|
|
|
has_many :relations, :as => :target
|
|
|
|
has_many :collaborators, :through => :relations, :source => :object, :source_type => 'User'
|
|
|
|
has_many :groups, :through => :relations, :source => :object, :source_type => 'Group'
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2011-10-17 15:21:29 +01:00
|
|
|
validates :name, :uniqueness => {:scope => [:owner_id, :owner_type]}, :presence => true, :allow_nil => false, :allow_blank => false
|
2011-10-18 16:00:06 +01:00
|
|
|
validates :unixname, :uniqueness => {:scope => [:owner_id, :owner_type]}, :presence => true, :format => { :with => /^[a-zA-Z0-9_]+$/ }, :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
|
2011-03-09 17:38:21 +00:00
|
|
|
|
2011-03-10 12:38:42 +00:00
|
|
|
include Project::HasRepository
|
|
|
|
|
2011-03-31 02:15:17 +01:00
|
|
|
scope :recent, order("name ASC")
|
2011-04-28 12:01:58 +01:00
|
|
|
scope :by_name, lambda { |name| {:conditions => ['name like ?', '%' + name + '%']} }
|
2011-03-31 02:15:17 +01:00
|
|
|
|
2011-10-19 14:14:53 +01:00
|
|
|
before_save :create_directory#, :create_git_repo
|
|
|
|
before_save :make_owner_rel
|
|
|
|
after_destroy :remove_directory
|
2011-10-16 21:50:56 +01:00
|
|
|
# before_create :xml_rpc_create
|
|
|
|
# before_destroy :xml_rpc_destroy
|
2011-03-11 10:25:50 +00:00
|
|
|
|
2011-10-18 16:00:06 +01:00
|
|
|
def members
|
|
|
|
collaborators + groups
|
|
|
|
end
|
|
|
|
|
2011-03-10 13:38:50 +00:00
|
|
|
# Redefining a method from Project::HasRepository module to reflect current situation
|
|
|
|
def git_repo_path
|
2011-04-15 14:55:49 +01:00
|
|
|
@git_repo_path ||= File.join(repository.platform.path, "projects", unixname + ".git")
|
2011-03-10 13:38:50 +00:00
|
|
|
end
|
|
|
|
|
2011-03-11 09:39:34 +00:00
|
|
|
def path
|
2011-03-11 10:25:50 +00:00
|
|
|
build_path(unixname)
|
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
|
|
|
|
|
2011-04-07 10:32:14 +01:00
|
|
|
def add_to_repository(platf, repo)
|
|
|
|
result = BuildServer.add_to_repo(repository.name, platf.name)
|
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
2011-04-07 15:37:28 +01:00
|
|
|
raise "Failed to add project #{name} to repo #{repo.name} of platform #{platf.name}."
|
2011-04-07 10:32:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-09 17:38:21 +00:00
|
|
|
protected
|
|
|
|
|
2011-10-17 15:21:29 +01:00
|
|
|
def make_owner_rel
|
2011-10-19 14:14:53 +01:00
|
|
|
unless groups.include? owner or collaborators.include? owner
|
|
|
|
collaborators << owner if owner.instance_of? User
|
|
|
|
groups << owner if owner.instance_of? Group
|
|
|
|
end
|
2011-10-17 15:21:29 +01:00
|
|
|
end
|
|
|
|
|
2011-03-11 10:25:50 +00:00
|
|
|
def build_path(dir)
|
2011-10-19 14:14:53 +01:00
|
|
|
File.join(APP_CONFIG['root_path'], 'projects', dir)
|
2011-03-11 10:25:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_directory
|
|
|
|
exists = File.exists?(path) && File.directory?(path)
|
|
|
|
raise "Directory #{path} already exists" if exists
|
|
|
|
if new_record?
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
elsif unixname_changed?
|
|
|
|
FileUtils.mv(build_path(unixname_was), buildpath(unixname))
|
|
|
|
end
|
2011-03-09 17:38:21 +00:00
|
|
|
end
|
2011-03-15 11:24:45 +00:00
|
|
|
|
2011-10-19 14:14:53 +01:00
|
|
|
def remove_directory
|
|
|
|
exists = File.exists?(path) && File.directory?(path)
|
|
|
|
raise "Directory #{path} didn't exists" unless exists
|
|
|
|
FileUtils.rm_rf(path)
|
|
|
|
end
|
|
|
|
|
2011-04-07 10:32:14 +01:00
|
|
|
def xml_rpc_create
|
2011-04-14 12:50:18 +01:00
|
|
|
result = BuildServer.create_project unixname, repository.platform.unixname, repository.unixname
|
2011-04-07 10:32:14 +01:00
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
2011-04-14 12:51:35 +01:00
|
|
|
raise "Failed to create project #{name} (repo #{repository.name}) inside platform #{repository.platform.name}."
|
2011-04-07 10:32:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def xml_rpc_destroy
|
2011-04-20 15:26:29 +01:00
|
|
|
result = BuildServer.delete_project unixname, repository.platform.unixname
|
2011-04-07 10:32:14 +01:00
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
2011-04-28 08:32:27 +01:00
|
|
|
raise "Failed to delete repository #{name} (repo #{repository.name}) inside platform #{repository.platform.name}."
|
2011-04-07 10:32:14 +01:00
|
|
|
end
|
|
|
|
end
|
2011-03-09 17:38:21 +00:00
|
|
|
end
|