rosa-build/app/models/advisory.rb

76 lines
2.6 KiB
Ruby
Raw Normal View History

2012-10-18 15:29:31 +01:00
class Advisory < ActiveRecord::Base
self.include_root_in_json = false
2012-10-18 15:29:31 +01:00
has_and_belongs_to_many :platforms
has_and_belongs_to_many :projects
has_many :build_lists
2012-05-04 18:12:51 +01:00
2014-01-21 04:51:49 +00:00
validates :description, :update_type, presence: true
validates :update_type, inclusion: BuildList::RELEASE_UPDATE_TYPES
2012-10-18 15:29:31 +01:00
after_create :generate_advisory_id
2014-01-21 04:51:49 +00:00
before_save :normalize_references, if: :references_changed?
2012-05-04 18:12:51 +01:00
2014-06-06 13:53:06 +01:00
attr_accessible :description, :references
2014-03-19 14:31:06 +00:00
2012-10-18 15:29:31 +01:00
ID_TEMPLATE = 'ROSA-%<type>s-%<year>d:%<id>04d'
ID_STRING_TEMPLATE = 'ROSA-%<type>s-%<year>04s:%<id>04s'
TYPES = {'security' => 'SA', 'bugfix' => 'A'}
2012-05-04 18:12:51 +01:00
scope :search, ->(q) {
q = q.to_s.strip
2014-02-12 19:53:03 +00:00
where("#{table_name}.advisory_id ILIKE :q OR #{table_name}.description ILIKE :q OR build_list_packages.fullname ILIKE :q", q: "%#{q}%").
joins(build_lists: :packages) if q.present?
}
scope :search_by_id, ->(aid) { where("#{table_name}.advisory_id ILIKE ?", "%#{aid.to_s.strip}%") }
scope :by_update_type, ->(ut) { where(update_type: ut) }
default_scope { order(created_at: :desc) }
2012-10-18 15:29:31 +01:00
def to_param
advisory_id
end
2012-10-18 15:29:31 +01:00
def attach_build_list(build_list)
return false if update_type != build_list.update_type
self.platforms << build_list.save_to_platform unless platforms.include? build_list.save_to_platform
self.projects << build_list.project unless projects.include? build_list.project
build_list.advisory = self
2013-06-14 11:48:26 +01:00
save && build_list.save
end
2012-10-18 15:29:31 +01:00
# this method fetches and structurize packages attached to current advisory.
def fetch_packages_info
packages_info = Hash.new { |h, k| h[k] = {} } # maaagic, it's maaagic ;)
build_lists.includes(:save_to_platform, :packages, :project).find_in_batches do |batch|
2012-10-18 15:29:31 +01:00
batch.each do |build_list|
2014-01-21 04:51:49 +00:00
tmp = build_list.packages.inject({srpm: nil, rpm: []}) do |h, p|
2012-10-18 15:29:31 +01:00
p.package_type == 'binary' ? h[:rpm] << p.fullname : h[:srpm] = p.fullname
h
end
2012-10-18 15:29:31 +01:00
h = { build_list.project => tmp }
packages_info[build_list.save_to_platform].merge!(h) do |pr, old, new|
2014-01-21 04:51:49 +00:00
{srpm: new[:srpm], rpm: old[:rpm].concat(new[:rpm]).uniq}
end
end
end
2012-10-18 15:29:31 +01:00
packages_info
end
2012-10-18 15:29:31 +01:00
protected
2012-10-18 15:29:31 +01:00
def generate_advisory_id
2014-01-21 04:51:49 +00:00
self.advisory_id = sprintf(ID_TEMPLATE, type: TYPES[self.update_type], year: Time.now.utc.year, id: self.id)
2012-10-18 15:29:31 +01:00
self.save
end
2012-10-18 15:29:31 +01:00
def normalize_references
self.references.gsub!(/\r| /, '')
self.references = self.references.split('\n').map do |ref|
ref = CGI::escapeHTML(ref)
ref = "http://#{ref}" unless ref =~ %r[^http(s?)://*]
ref
end.join("\n")
end
2012-10-18 15:29:31 +01:00
2012-05-04 18:12:51 +01:00
end