From 8470674c62046c1844cd3573ce4a07394ce8c877 Mon Sep 17 00:00:00 2001 From: Pavel Chipiga Date: Mon, 14 May 2012 22:08:31 +0300 Subject: [PATCH] Apply basic build list packages store functionality. Display build list package content. Write specs. Refactor. Refs #444 --- .../projects/build_lists_controller.rb | 2 + app/models/build_list.rb | 51 ++++++++++++------- app/models/build_list/package.rb | 12 +++++ app/models/platform.rb | 2 + app/models/project.rb | 3 +- app/views/projects/build_lists/show.html.haml | 19 +++++++ config/locales/models/build_list.en.yml | 7 +++ config/locales/models/build_list.ru.yml | 7 +++ ...120512102707_create_build_list_packages.rb | 19 +++++++ db/schema.rb | 19 ++++++- .../projects/build_lists_controller_spec.rb | 23 ++++++++- 11 files changed, 141 insertions(+), 23 deletions(-) create mode 100644 app/models/build_list/package.rb create mode 100644 db/migrate/20120512102707_create_build_list_packages.rb diff --git a/app/controllers/projects/build_lists_controller.rb b/app/controllers/projects/build_lists_controller.rb index 76098dc45..330f396b1 100644 --- a/app/controllers/projects/build_lists_controller.rb +++ b/app/controllers/projects/build_lists_controller.rb @@ -112,6 +112,8 @@ class Projects::BuildListsController < Projects::BaseController @build_list.container_path = params[:container_path] @build_list.save + @build_list.set_packages(ActiveSupport::JSON.decode(params[:pkg_info])) if params[:status].to_i == BuildServer::SUCCESS + render :nothing => true, :status => 200 end diff --git a/app/models/build_list.rb b/app/models/build_list.rb index 4d6cefaf2..1ff328c70 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -5,16 +5,15 @@ class BuildList < ActiveRecord::Base belongs_to :save_to_platform, :class_name => 'Platform' belongs_to :build_for_platform, :class_name => 'Platform' belongs_to :user - has_many :items, :class_name => "BuildList::Item", :dependent => :destroy - belongs_to :advisory + has_many :items, :class_name => "BuildList::Item", :dependent => :destroy + has_many :packages, :class_name => "BuildList::Package", :dependent => :destroy - validates :project_id, :project_version, :arch, :include_repos, :presence => true - validates_numericality_of :priority, :greater_than_or_equal_to => 0 - UPDATE_TYPES = %w[security bugfix enhancement recommended newpackage] RELEASE_UPDATE_TYPES = %w[security bugfix] + validates :project_id, :project_version, :arch, :include_repos, :presence => true + validates_numericality_of :priority, :greater_than_or_equal_to => 0 validates :update_type, :inclusion => UPDATE_TYPES, :unless => Proc.new { |b| b.save_to_platform.released } validates :update_type, :inclusion => RELEASE_UPDATE_TYPES, @@ -71,7 +70,6 @@ class BuildList < ActiveRecord::Base } scope :recent, order("#{table_name}.updated_at DESC") - scope :for_status, lambda {|status| where(:status => status) } scope :for_user, lambda { |user| where(:user_id => user.id) } scope :scoped_to_arch, lambda {|arch| where(:arch_id => arch) } @@ -90,7 +88,6 @@ class BuildList < ActiveRecord::Base s } scope :scoped_to_project_name, lambda {|project_name| joins(:project).where('projects.name LIKE ?', "%#{project_name}%")} - scope :outdated, where('updated_at < ? AND status <> ?', Time.now - LIVE_TIME, BUILD_PUBLISHED) serialize :additional_repos @@ -117,6 +114,13 @@ class BuildList < ActiveRecord::Base end end + def set_packages(pkg_hash) + build_package(pkg_hash['srpm'], 'source') {|p| p.save!} + pkg_hash['rpm'].each do |rpm_hash| + build_package(rpm_hash, 'binary') {|p| p.save!} + end + end + def publish return false unless can_publish? has_published = BuildServer.publish_container bs_id @@ -170,18 +174,27 @@ class BuildList < ActiveRecord::Base #[WAITING_FOR_RESPONSE, BuildServer::BUILD_PENDING, BuildServer::BUILD_STARTED].include?(status) end - private - def set_default_status - self.status = WAITING_FOR_RESPONSE unless self.status.present? - return true - end + protected - def place_build - #XML-RPC params: project_name, project_version, plname, arch, bplname, update_type, build_requires, id_web, include_repos, priority - self.status = BuildServer.add_build_list project.name, project_version, save_to_platform.name, arch.name, (save_to_platform_id == build_for_platform_id ? '' : build_for_platform.name), update_type, build_requires, id, include_repos, priority - self.status = BUILD_PENDING if self.status == 0 - save - end - #handle_asynchronously :place_build + def set_default_status + self.status = WAITING_FOR_RESPONSE unless self.status.present? + return true + end + def place_build + #XML-RPC params: project_name, project_version, plname, arch, bplname, update_type, build_requires, id_web, include_repos, priority + self.status = BuildServer.add_build_list project.name, project_version, save_to_platform.name, arch.name, (save_to_platform_id == build_for_platform_id ? '' : build_for_platform.name), update_type, build_requires, id, include_repos, priority + self.status = BUILD_PENDING if self.status == 0 + save + end + #handle_asynchronously :place_build + + def build_package(pkg_hash, package_type) + packages.create(pkg_hash) do |p| + p.project = Project.joins(:repositories => :platform).where('platforms.id = ?', save_to_platform.id).find_by_name!(pkg_hash['name']) + p.platform = save_to_platform + p.package_type = package_type + yield p + end + end end diff --git a/app/models/build_list/package.rb b/app/models/build_list/package.rb new file mode 100644 index 000000000..b491c0d13 --- /dev/null +++ b/app/models/build_list/package.rb @@ -0,0 +1,12 @@ +class BuildList::Package < ActiveRecord::Base + PACKAGE_TYPES = %w(source binary) + + belongs_to :build_list + belongs_to :project + belongs_to :platform + + attr_accessible :fullname, :name, :release, :version + + validates :build_list_id, :project_id, :platform_id, :fullname, :package_type, :name, :release, :version, :presence => true + validates :package_type, :inclusion => PACKAGE_TYPES +end diff --git a/app/models/platform.rb b/app/models/platform.rb index 4cf0de66a..dccf01e92 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -15,6 +15,8 @@ class Platform < ActiveRecord::Base has_and_belongs_to_many :advisories + has_many :packages, :class_name => "BuildList::Package", :dependent => :destroy + validates :description, :presence => true validates :visibility, :presence => true, :inclusion => {:in => VISIBILITIES} validates :name, :uniqueness => {:case_sensitive => false}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-]+$/ } diff --git a/app/models/project.rb b/app/models/project.rb index 1fd0deb1b..c29a0b405 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -18,7 +18,8 @@ class Project < ActiveRecord::Base has_many :collaborators, :through => :relations, :source => :actor, :source_type => 'User' has_many :groups, :through => :relations, :source => :actor, :source_type => 'Group' - has_many :advisories + has_many :advisories, :dependent => :destroy + has_many :packages, :class_name => "BuildList::Package", :dependent => :destroy validates :name, :uniqueness => {:scope => [:owner_id, :owner_type], :case_sensitive => false}, :presence => true, :format => {:with => /^#{NAME_REGEXP}$/} validates :owner, :presence => true diff --git a/app/views/projects/build_lists/show.html.haml b/app/views/projects/build_lists/show.html.haml index 90e9756d5..cd3fb1c85 100644 --- a/app/views/projects/build_lists/show.html.haml +++ b/app/views/projects/build_lists/show.html.haml @@ -102,6 +102,25 @@ %td= item.human_status .both +- if @build_list.packages.present? + .hr + %h3= t("layout.build_lists.packages_header") + %table.tablesorter.width565{:cellpadding => "0", :cellspacing => "0"} + %thead + %tr + %th= t("activerecord.attributes.build_list/package.fullname") + %th= t("activerecord.attributes.build_list/package.name") + %th= t("activerecord.attributes.build_list/package.version") + %th= t("activerecord.attributes.build_list/package.release") + %tbody + - @build_list.packages.each do |package| + %tr + %td= package.fullname + %td= package.name + %td= package.version + %td= package.release + .both + :javascript $('article .all').addClass('bigpadding'); diff --git a/config/locales/models/build_list.en.yml b/config/locales/models/build_list.en.yml index 10c97a048..9688c14c0 100644 --- a/config/locales/models/build_list.en.yml +++ b/config/locales/models/build_list.en.yml @@ -36,6 +36,12 @@ en: version: Version build_list: Build list + build_list/package: + name: Name + fullname: Fullname + release: Release + version: Version + layout: build_lists: filter_header: Filter @@ -48,6 +54,7 @@ en: project_name_search: Search by project name bs_id_not_set: Id has not been configured yet items_header: Build items + packages_header: Container data no_items_data: No data show: Show cancel_success: 'Build canceled' diff --git a/config/locales/models/build_list.ru.yml b/config/locales/models/build_list.ru.yml index 999d44f5d..faebb4372 100644 --- a/config/locales/models/build_list.ru.yml +++ b/config/locales/models/build_list.ru.yml @@ -35,6 +35,12 @@ ru: version: Версия build_list: Сборочный лист + build_list/package: + name: Название + fullname: Полное имя + release: Релиз + version: Версия + layout: build_lists: filter_header: Фильтр @@ -47,6 +53,7 @@ ru: project_name_search: Поиск по названию проекта bs_id_not_set: Id еще не присвоен items_header: Элементы сборки + packages_header: Данные о контейнере no_items_data: Данных нет show: Просмотр cancel_success: 'Сборка отменена.' diff --git a/db/migrate/20120512102707_create_build_list_packages.rb b/db/migrate/20120512102707_create_build_list_packages.rb new file mode 100644 index 000000000..1e6b1acd9 --- /dev/null +++ b/db/migrate/20120512102707_create_build_list_packages.rb @@ -0,0 +1,19 @@ +class CreateBuildListPackages < ActiveRecord::Migration + def change + create_table :build_list_packages do |t| + t.references :build_list + t.references :project + t.references :platform + t.string :fullname + t.string :name + t.string :version + t.string :release + t.string :package_type + + t.timestamps + end + add_index :build_list_packages, :build_list_id + add_index :build_list_packages, :project_id + add_index :build_list_packages, :platform_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 5e397546f..51043e17a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120505101650) do +ActiveRecord::Schema.define(:version => 20120512102707) do create_table "activity_feeds", :force => true do |t| t.integer "user_id", :null => false @@ -75,6 +75,23 @@ ActiveRecord::Schema.define(:version => 20120505101650) do add_index "build_list_items", ["build_list_id"], :name => "index_build_list_items_on_build_list_id" + create_table "build_list_packages", :force => true do |t| + t.integer "build_list_id" + t.integer "project_id" + t.integer "platform_id" + t.string "fullname" + t.string "name" + t.string "version" + t.string "release" + t.string "package_type" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "build_list_packages", ["build_list_id"], :name => "index_build_list_packages_on_build_list_id" + add_index "build_list_packages", ["platform_id"], :name => "index_build_list_packages_on_platform_id" + add_index "build_list_packages", ["project_id"], :name => "index_build_list_packages_on_project_id" + create_table "build_lists", :force => true do |t| t.integer "bs_id" t.string "container_path" diff --git a/spec/controllers/projects/build_lists_controller_spec.rb b/spec/controllers/projects/build_lists_controller_spec.rb index bdaf51cc0..546b93c99 100644 --- a/spec/controllers/projects/build_lists_controller_spec.rb +++ b/spec/controllers/projects/build_lists_controller_spec.rb @@ -344,10 +344,28 @@ describe Projects::BuildListsController do end describe 'status_build' do - before { @item = build_list.items.create(:name => build_list.project.name, :version => build_list.project_version, :level => 0) } + before do + @item = build_list.items.create(:name => build_list.project.name, :version => build_list.project_version, :level => 0) + repo = build_list.save_to_platform.repositories.first + repo.projects << build_list.project + @project2 = FactoryGirl.create(:project) + repo.projects << @project2 + end def do_get - get :status_build, :id => build_list.bs_id, :package_name => build_list.project.name, :status => BuildServer::SUCCESS, :container_path => '/path/to' + get :status_build, :id => build_list.bs_id, :package_name => build_list.project.name, :status => BuildServer::SUCCESS, :container_path => '/path/to', + :pkg_info => ActiveSupport::JSON.encode({'srpm' => {'fullname' => 'srpm_filename.srpm', + 'name' => build_list.project.name, + 'version' => 'version1', + 'release' => 'release1'}, + 'rpm' => [{'fullname' => 'filename1.rpm', + 'name' => build_list.project.name, + 'version' => 'version2', + 'release' => 'release2'}, + {'fullname' => 'filename2.rpm', + 'name' => @project2.name, + 'version' => 'version2', + 'release' => 'release2'}]}) build_list.reload @item.reload end @@ -356,6 +374,7 @@ describe Projects::BuildListsController do it { lambda{ do_get }.should change(@item, :status) } it { lambda{ do_get }.should change(build_list, :container_path) } it { lambda{ do_get }.should change(build_list, :updated_at) } + it('should create packages for build list') { lambda{ do_get }.should change(build_list.packages, :count).to(3) } end describe 'pre_build' do