2011-04-07 14:20:21 +01:00
class BuildList < ActiveRecord :: Base
belongs_to :project
belongs_to :arch
2011-10-22 16:28:41 +01:00
belongs_to :pl , :class_name = > 'Platform'
belongs_to :bpl , :class_name = > 'Platform'
2011-04-11 11:47:57 +01:00
has_many :items , :class_name = > " BuildList::Item " , :dependent = > :destroy
2011-04-07 14:20:21 +01:00
validates :project_id , :presence = > true
2011-10-22 16:28:41 +01:00
validates :project_version , :presence = > true
#validates_inclusion_of :update_type, :in => UPDATE_TYPES#, :message => "extension %s is not included in the list"
UPDATE_TYPES = %w[ security bugfix enhancement recommended newpackage ]
validates :update_type , :inclusion = > UPDATE_TYPES
validate lambda {
2011-10-29 13:18:41 +01:00
errors . add ( :bpl , I18n . t ( 'flash.build_list.wrong_platform' ) ) if pl . platform_type == 'main' && pl_id != bpl_id
2011-10-22 16:28:41 +01:00
}
2011-11-11 10:38:17 +00:00
validate lambda {
errors . add ( :bpl , I18n . t ( 'flash.build_list.can_not_published' ) ) if status == BUILD_PUBLISHED && status_was != BuildServer :: SUCCESS
}
2011-10-22 16:28:41 +01:00
BUILD_CANCELED = 5000
2011-04-11 17:37:09 +01:00
WAITING_FOR_RESPONSE = 4000
BUILD_PENDING = 2000
BUILD_STARTED = 3000
2011-11-11 10:38:17 +00:00
BUILD_PUBLISHED = 6000
2011-11-17 22:34:34 +00:00
TEST_FAILD = 21
2011-04-11 17:37:09 +01:00
STATUSES = [ WAITING_FOR_RESPONSE ,
BuildServer :: SUCCESS ,
BUILD_PENDING ,
BUILD_STARTED ,
BuildServer :: BUILD_ERROR ,
BuildServer :: PLATFORM_NOT_FOUND ,
BuildServer :: PLATFORM_PENDING ,
BuildServer :: PROJECT_NOT_FOUND ,
2011-10-29 18:50:47 +01:00
BuildServer :: PROJECT_VERSION_NOT_FOUND ,
2011-10-30 13:44:45 +00:00
BUILD_CANCELED ,
2011-10-29 18:50:47 +01:00
TEST_FAILD ]
2011-04-07 15:56:28 +01:00
2011-04-07 14:20:21 +01:00
HUMAN_STATUSES = { BuildServer :: BUILD_ERROR = > :build_error ,
2011-04-07 15:56:28 +01:00
BUILD_PENDING = > :build_pending ,
BUILD_STARTED = > :build_started ,
2011-04-11 17:37:09 +01:00
BuildServer :: SUCCESS = > :success ,
WAITING_FOR_RESPONSE = > :waiting_for_response ,
BuildServer :: PLATFORM_NOT_FOUND = > :platform_not_found ,
BuildServer :: PLATFORM_PENDING = > :platform_pending ,
BuildServer :: PROJECT_NOT_FOUND = > :project_not_found ,
2011-10-29 18:50:47 +01:00
BuildServer :: PROJECT_VERSION_NOT_FOUND = > :project_version_not_found ,
2011-10-30 13:44:45 +00:00
TEST_FAILD = > :testing_faild ,
BUILD_CANCELED = > :build_canceled
2011-04-07 14:20:21 +01:00
}
2011-11-02 00:21:06 +00:00
scope :recent , order ( " updated_at DESC " )
2011-04-11 17:37:09 +01:00
scope :current , lambda {
2011-10-23 11:34:42 +01:00
outdatable_statuses = [ BuildServer :: SUCCESS , BuildServer :: ERROR , BuildServer :: PLATFORM_NOT_FOUND , BuildServer :: PLATFORM_PENDING , BuildServer :: PROJECT_NOT_FOUND , BuildServer :: PROJECT_VERSION_NOT_FOUND ]
2011-04-11 17:37:09 +01:00
where ( [ " status in (?) OR (status in (?) AND notified_at >= ?) " , [ WAITING_FOR_RESPONSE , BUILD_PENDING , BUILD_STARTED ] , outdatable_statuses , Time . now - 2 . days ] )
}
2011-04-07 14:20:21 +01:00
scope :for_status , lambda { | status | where ( :status = > status ) }
scope :scoped_to_arch , lambda { | arch | where ( :arch_id = > arch ) }
2011-10-23 11:34:42 +01:00
scope :scoped_to_project_version , lambda { | project_version | where ( :project_version = > project_version ) }
2011-04-07 15:56:28 +01:00
scope :scoped_to_is_circle , lambda { | is_circle | where ( :is_circle = > is_circle ) }
2011-04-07 14:20:21 +01:00
scope :for_creation_date_period , lambda { | start_date , end_date |
if start_date && end_date
where ( [ " created_at BETWEEN ? AND ? " , start_date , end_date ] )
elsif start_date && ! end_date
where ( [ " created_at >= ? " , start_date ] )
elsif ! start_date && end_date
where ( [ " created_at <= ? " , end_date ] )
end
}
scope :for_notified_date_period , lambda { | start_date , end_date |
if start_date && end_date
where ( [ " notified_at BETWEEN ? AND ? " , start_date , end_date ] )
elsif start_date && ! end_date
where ( [ " notified_at >= ? " , start_date ] )
elsif ! start_date && end_date
where ( [ " notified_at <= ? " , end_date ] )
end
}
2011-04-11 11:47:57 +01:00
serialize :additional_repos
2011-04-11 17:37:09 +01:00
before_create :set_default_status
after_create :place_build
2011-04-11 11:47:57 +01:00
2011-04-07 14:20:21 +01:00
def self . human_status ( status )
I18n . t ( " layout.build_lists.statuses. #{ HUMAN_STATUSES [ status ] } " )
end
def human_status
self . class . human_status ( status )
end
2011-04-11 11:47:57 +01:00
def set_items ( items_hash )
self . items = [ ]
items_hash . each do | level , items |
items . each do | item |
2011-10-29 15:05:40 +01:00
self . items << self . items . build ( :name = > item [ 'name' ] , :version = > item [ 'version' ] , :level = > level . to_i )
2011-04-11 11:47:57 +01:00
end
end
end
2011-09-15 18:56:20 +01:00
def publish
2011-09-16 15:15:50 +01:00
return false unless can_published?
2011-09-15 18:56:20 +01:00
BuildServer . publish_container bs_id
2011-11-11 10:38:17 +00:00
self . update_attribute ( :status , BUILD_PUBLISHED )
#self.destroy # self.delete
2011-09-15 18:56:20 +01:00
end
2011-09-16 15:15:50 +01:00
def can_published?
self . status == BuildServer :: SUCCESS
end
2011-04-11 11:47:57 +01:00
2011-10-22 16:28:41 +01:00
def delete_build_list
has_canceled = BuildServer . delete_build_list bs_id
2011-10-30 14:06:33 +00:00
update_attribute ( :status , BUILD_CANCELED ) if has_canceled == 0
2011-10-22 16:28:41 +01:00
return has_canceled == 0
end
#TODO: Share this checking on product owner.
def can_canceled?
2011-10-29 20:09:14 +01:00
self . status == BUILD_PENDING && bs_id
2011-10-22 16:28:41 +01:00
end
2011-10-29 15:50:22 +01:00
def event_log_message
2011-10-31 00:11:49 +00:00
{ :project = > project . unixname , :version = > project_version , :arch = > arch . name } . inspect
2011-10-29 15:50:22 +01:00
end
2011-04-11 17:37:09 +01:00
private
def set_default_status
self . status = WAITING_FOR_RESPONSE unless self . status . present?
2011-04-22 16:13:26 +01:00
return true
2011-04-11 17:37:09 +01:00
end
def place_build
2011-10-22 16:28:41 +01:00
#XML-RPC params: project_name, project_version, plname, arch, bplname, update_type, build_requires, id_web
2011-10-25 16:17:06 +01:00
self . status = BuildServer . add_build_list project . name , project_version , pl . unixname , arch . name , ( pl_id == bpl_id ? '' : bpl . unixname ) , update_type , build_requires , id
2011-04-11 17:37:09 +01:00
self . status = BUILD_PENDING if self . status == 0
save
end
2011-04-22 16:21:57 +01:00
#handle_asynchronously :place_build
2011-04-11 17:37:09 +01:00
2011-10-22 16:28:41 +01:00
end