2011-03-09 17:38:21 +00:00
class Project < ActiveRecord :: Base
2011-10-23 22:39:44 +01:00
VISIBILITIES = [ 'open' , 'hidden' ]
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-11-02 19:18:25 +00:00
has_many :auto_build_lists , :dependent = > :destroy
2011-03-10 12:35:23 +00:00
2011-10-26 21:57:51 +01:00
has_many :project_to_repositories , :dependent = > :destroy
2011-10-13 23:35:16 +01:00
has_many :repositories , :through = > :project_to_repositories
2011-10-13 16:55:03 +01:00
2011-10-26 21:57:51 +01:00
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'
2011-10-13 16:55:03 +01:00
2011-11-28 15:41:42 +00:00
validates :name , :uniqueness = > { :scope = > [ :owner_id , :owner_type ] } , :presence = > true , :format = > { :with = > / ^[a-zA-Z0-9_ \ - \ + \ .]+$ / }
2011-10-26 21:57:51 +01:00
validates :owner , :presence = > true
2011-11-17 21:57:30 +00:00
# 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-11-28 15:41:42 +00:00
#attr_accessible :category_id, :name, :description, :visibility
attr_readonly :name
2011-03-10 12:38:42 +00:00
2011-03-31 02:15:17 +01:00
scope :recent , order ( " name ASC " )
2011-10-28 23:36:02 +01:00
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 ( ',' ) ] } }
2011-11-01 13:22:41 +00:00
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 } )) " ) }
2011-10-29 20:09:14 +01:00
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-27 14:04:03 +01:00
after_create :attach_to_personal_repository
2011-11-01 00:52:24 +00:00
after_create :create_git_repo
2011-10-28 18:15:35 +01:00
after_destroy :destroy_git_repo
2011-11-23 15:52:33 +00:00
# after_rollback lambda { destroy_git_repo rescue true if new_record? }
has_ancestry
2011-10-28 00:18:04 +01:00
2011-11-24 21:46:19 +00:00
include Modules :: Models :: Owner
2011-11-17 21:57:30 +00:00
def auto_build
auto_build_lists . each do | auto_build_list |
build_lists . create (
:pl = > auto_build_list . pl ,
:bpl = > auto_build_list . bpl ,
:arch = > auto_build_list . arch ,
2011-11-17 22:28:02 +00:00
:project_version = > collected_project_versions . last ,
2011-11-17 21:57:30 +00:00
:build_requires = > true ,
2011-11-18 01:28:34 +00:00
:update_type = > 'bugfix' ) unless build_lists . for_creation_date_period ( Time . current - 15 . seconds , Time . current ) . present?
2011-11-17 21:57:30 +00:00
end
end
2011-10-23 11:34:42 +01:00
def project_versions
2011-11-17 22:28:02 +00:00
res = tags . select { | tag | tag . name =~ / ^v \ . / }
2011-11-01 15:46:05 +00:00
return res if res and res . size > 0
tags
2011-10-29 20:09:14 +01:00
end
2011-11-17 22:28:02 +00:00
2011-10-30 21:11:59 +00:00
def collected_project_versions
2011-11-17 22:28:02 +00:00
project_versions . collect { | tag | tag . name . gsub ( / ^ \ w+ \ . / , " " ) }
2011-10-30 21:11:59 +00:00
end
2011-10-29 20:09:14 +01:00
def tags
2011-11-18 01:28:34 +00:00
self . git_repository . tags . sort_by { | t | t . name . gsub ( / [a-zA-Z.]+ / , '' ) . to_i }
2011-10-23 11:34:42 +01:00
end
2011-10-23 22:39:44 +01:00
2011-10-18 16:00:06 +01:00
def members
2011-11-17 21:57:30 +00:00
collaborators + groups . map ( & :members ) . flatten
2011-10-18 16:00:06 +01:00
end
2011-11-10 09:55:50 +00:00
def git_repository
2011-11-23 15:52:33 +00:00
@git_repository || = Git :: Repository . new ( path )
2011-10-26 21:57:51 +01:00
end
2011-11-23 15:52:33 +00:00
2011-10-26 21:57:51 +01:00
def git_repo_name
2011-11-28 15:41:42 +00:00
File . join owner . uname , name
2011-10-26 21:57:51 +01:00
end
2011-03-10 13:38:50 +00:00
2011-10-26 21:57:51 +01:00
def public?
visibility == 'open'
2011-03-11 09:39:34 +00:00
end
2011-11-23 15:52:33 +00:00
def fork ( new_owner )
clone . tap do | c |
2011-11-29 22:23:09 +00:00
c . parent_id = id
2011-11-23 15:52:33 +00:00
c . owner = new_owner
c . updated_at = nil ; c . created_at = nil # :id = nil
c . save
end
2011-03-11 17:38:28 +00:00
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 )
2011-11-28 15:41:42 +00:00
result = BuildServer . create_project name , repository . platform . name , repository . name , path
2011-10-28 14:31:26 +01:00
if result == BuildServer :: SUCCESS
return true
else
2011-11-28 15:41:42 +00:00
raise " Failed to create project #{ name } (repo #{ repository . name } ) inside platform #{ repository . platform . name } 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 )
2011-11-28 15:41:42 +00:00
result = BuildServer . delete_project name , repository . platform . name
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
2011-10-26 21:57:51 +01:00
def create_git_repo
2011-11-23 15:52:33 +00:00
is_root? ? Grit :: Repo . init_bare ( path ) : parent . git_repository . repo . delay . fork_bare ( path )
2011-03-09 17:38:21 +00:00
end
2011-03-15 11:24:45 +00:00
2011-10-26 21:57:51 +01:00
def destroy_git_repo
2011-11-23 15:52:33 +00:00
FileUtils . rm_rf path
2011-10-19 14:14:53 +01:00
end
2011-03-09 17:38:21 +00:00
end