rosa-build/app/models/project.rb

280 lines
9.3 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
2011-03-09 17:38:21 +00:00
class Project < ActiveRecord::Base
2011-10-23 22:39:44 +01:00
VISIBILITIES = ['open', 'hidden']
MAX_OWN_PROJECTS = 32000
2011-10-23 22:39:44 +01:00
belongs_to :category, :counter_cache => true
belongs_to :owner, :polymorphic => true, :counter_cache => :own_projects_count
2011-10-18 16:00:06 +01:00
has_many :issues, :dependent => :destroy
2011-04-07 14:20:21 +01:00
has_many :build_lists, :dependent => :destroy
has_many :auto_build_lists, :dependent => :destroy
2011-03-10 12:35:23 +00:00
has_many :project_imports, :dependent => :destroy
has_many :project_to_repositories, :dependent => :destroy
has_many :repositories, :through => :project_to_repositories
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'
2012-02-23 14:48:31 +00:00
has_many :labels
validates :name, :uniqueness => {:scope => [:owner_id, :owner_type], :case_sensitive => false}, :presence => true, :format => {:with => /^[a-zA-Z0-9_\-\+\.]+$/}
validates :owner, :presence => true
validate { errors.add(:base, :can_have_less_or_equal, :count => MAX_OWN_PROJECTS) if owner.projects.size >= MAX_OWN_PROJECTS }
# validate {errors.add(:base, I18n.t('flash.project.save_warning_ssh_key')) if owner.ssh_key.blank?}
validates_attachment_size :srpm, :less_than => 500.megabytes
validates_attachment_content_type :srpm, :content_type => ['application/octet-stream', "application/x-rpm", "application/x-redhat-package-manager"], :message => I18n.t('layout.invalid_content_type')
2011-03-09 17:38:21 +00:00
#attr_accessible :category_id, :name, :description, :visibility
attr_readonly :name
2011-03-31 02:15:17 +01:00
scope :recent, order("name ASC")
scope :search, lambda {|q| by_name("%#{q}%").open}
scope :by_name, lambda {|name| where('projects.name ILIKE ?', name)}
scope :by_visibilities, lambda {|v| where(:visibility => v)}
scope :open, where(:visibility => 'open')
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 }))") }
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
after_create :attach_to_personal_repository
after_create :create_git_repo
after_save :create_wiki
after_destroy :destroy_git_repo
after_destroy :destroy_wiki
after_save {|p| p.delay.import_attached_srpm if p.srpm?} # should be after create_git_repo
# after_rollback lambda { destroy_git_repo rescue true if new_record? }
has_ancestry
2011-10-28 00:18:04 +01:00
has_attached_file :srpm
include Modules::Models::Owner
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,
:project_version => versions.last,
:build_requires => true,
:update_type => 'bugfix') unless build_lists.for_creation_date_period(Time.current - 15.seconds, Time.current).present?
end
end
2012-02-23 20:20:44 +00:00
def build_for(platform, user)
build_lists.create do |bl|
bl.pl = platform
bl.bpl = platform
2012-02-23 20:20:44 +00:00
bl.update_type = 'newpackage'
2012-01-27 01:00:44 +00:00
bl.arch = Arch.find_by_name('x86_64') # Return i586 after mass rebuild
2012-02-23 20:20:44 +00:00
bl.project_version = "latest_#{platform.name}" # "latest_import_mandriva2011"
bl.build_requires = false # already set as db default
bl.user = user
2012-01-23 16:19:10 +00:00
bl.auto_publish = true # already set as db default
bl.include_repos = [platform.repositories.find_by_name('main').id]
end
end
def tags
self.git_repository.tags #.sort_by{|t| t.name.gsub(/[a-zA-Z.]+/, '').to_i}
end
def branches
self.git_repository.branches
end
def last_active_branch
@last_active_branch ||= branches.inject do |r, c|
r_last = r.commit.committed_date || r.commit.authored_date unless r.nil?
c_last = c.commit.committed_date || c.commit.authored_date
if r.nil? or r_last < c_last
r = c
end
r
end
@last_active_branch
end
def branch(name = nil)
name = default_branch if name.blank?
branches.select{|b| b.name == name}.first
end
def tree_info(tree, treeish = nil, path = nil)
treeish = tree.id unless treeish.present?
# initialize result as hash of <tree_entry> => nil
res = (tree.trees.sort + tree.blobs.sort).inject({}){|h, e| h.merge!({e => nil})}
# fills result vith commits that describes this file
res = res.inject(res) do |h, (entry, commit)|
# only if commit == nil ...
if commit.nil? and entry.respond_to? :name
# ... find last commit corresponds to this file ...
c = git_repository.log(treeish, File.join([path, entry.name].compact), :max_count => 1).first
# ... and add it to result.
h[entry] = c
# find another files, that linked to this commit and set them their commit
c.diffs.map{|diff| diff.b_path.split(File::SEPARATOR, 2).first}.each do |name|
h.each_pair do |k, v|
h[k] = c if k.name == name and v.nil?
end
end
end
h
end
end
def versions
tags.map(&:name) + branches.map{|b| "latest_#{b.name}"}
end
2011-10-23 22:39:44 +01:00
2011-10-18 16:00:06 +01:00
def members
collaborators + groups.map(&:members).flatten
2011-10-18 16:00:06 +01:00
end
def git_repository
@git_repository ||= Git::Repository.new(path)
end
def git_repo_name
File.join owner.uname, name
end
2011-03-10 13:38:50 +00:00
def wiki_repo_name
File.join owner.uname, "#{name}.wiki"
end
def public?
visibility == 'open'
2011-03-11 09:39:34 +00:00
end
def fork(new_owner)
clone.tap do |c|
c.parent_id = id
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
def wiki_path
build_wiki_path(git_repo_name)
end
2011-10-28 14:53:46 +01:00
def xml_rpc_create(repository)
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
raise "Failed to create project #{name} (repo #{repository.name}) inside platform #{repository.platform.name} in path #{path} with code #{result}."
2012-01-11 18:15:35 +00:00
end
2011-10-28 14:31:26 +01:00
end
2011-10-28 14:53:46 +01:00
def xml_rpc_destroy(repository)
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
def platforms
@platforms ||= repositories.map(&:platform).uniq
end
def import_srpm(srpm_path = srpm.path, branch_name = 'import')
system("#{Rails.root.join('bin', 'import_srpm.sh')} #{srpm_path} #{path} #{branch_name} >> /dev/null 2>&1")
end
2012-01-11 18:15:35 +00:00
class << self
def commit_comments(commit, project)
comments = Comment.where(:commentable_id => commit.id.hex, :commentable_type => 'Grit::Commit')
2012-01-20 15:17:05 +00:00
comments.each {|x| x.project = project; x.helper}
2012-01-11 18:15:35 +00:00
end
end
2012-01-25 17:33:26 +00:00
def owner?(user)
2012-01-29 20:18:14 +00:00
owner == user
2012-01-25 17:33:26 +00:00
end
def self.process_hook(owner_uname, repo, newrev, oldrev, ref, newrev_type, oldrev_type)
rec = GitHook.new(owner_uname, repo, newrev, oldrev, ref, newrev_type, oldrev_type)
2012-02-10 17:33:05 +00:00
ActivityFeedObserver.instance.after_create rec
end
2012-02-14 16:46:08 +00:00
def owner_and_admin_ids
recipients = self.relations.by_role('admin').where(:object_type => 'User').map { |rel| rel.read_attribute(:object_id) }
recipients = recipients | [self.owner_id] if self.owner_type == 'User'
recipients
end
2011-03-09 17:38:21 +00:00
protected
def build_path(dir)
File.join(APP_CONFIG['root_path'], 'git_projects', "#{dir}.git")
end
2011-10-28 00:18:04 +01:00
def build_wiki_path(dir)
File.join(APP_CONFIG['root_path'], 'git_projects', "#{dir}.wiki.git")
end
def attach_to_personal_repository
repositories << self.owner.personal_repository if !repositories.exists?(:id => self.owner.personal_repository)
end
2011-10-28 00:18:04 +01:00
def create_git_repo
is_root? ? Grit::Repo.init_bare(path) : parent.git_repository.repo.delay.fork_bare(path)
write_hook.delay
end
def destroy_git_repo
FileUtils.rm_rf path
end
def import_attached_srpm
if srpm?
import_srpm # srpm.path
self.srpm = nil; save # clear srpm
end
end
def create_wiki
if has_wiki && !FileTest.exist?(wiki_path)
Grit::Repo.init_bare(wiki_path)
wiki = Gollum::Wiki.new(wiki_path, {:base_path => Rails.application.routes.url_helpers.project_wiki_index_path(self)})
wiki.write_page('Home', :markdown, I18n.t("wiki.seed.welcome_content"),
{:name => owner.name, :email => owner.email, :message => 'Initial commit'})
end
end
def destroy_wiki
FileUtils.rm_rf wiki_path
end
def write_hook
2012-02-16 15:08:08 +00:00
is_production = ENV['RAILS_ENV'] == 'production'
hook = File.join(::Rails.root.to_s, 'tmp', "post-receive-hook")
FileUtils.cp(File.join(::Rails.root.to_s, 'bin', "post-receive-hook.partial"), hook)
File.open(hook, 'a') do |f|
2012-02-16 16:29:41 +00:00
s = "\n /bin/bash -l -c \"cd #{is_production ? '/srv/rosa_build/current' : Rails.root.to_s} && #{is_production ? 'RAILS_ENV=production' : ''} bundle exec rails runner 'Project.delay.process_hook(\"$owner\", \"$reponame\", \"$newrev\", \"$oldrev\", \"$ref\", \"$newrev_type\", \"$oldrev_type\")'\""
2012-02-16 15:08:08 +00:00
s << " > /dev/null 2>&1" if is_production
s << "\ndone\n"
f.write(s)
end
hook_file = File.join(path, 'hooks', 'post-receive')
2012-02-16 15:08:08 +00:00
FileUtils.cp(hook, hook_file)
FileUtils.rm_rf(hook)
rescue Exception # FIXME
end
2011-03-09 17:38:21 +00:00
end