rosa-build/app/models/pull_request.rb

219 lines
6.4 KiB
Ruby
Raw Normal View History

2012-04-28 18:28:57 +01:00
class PullRequest < ActiveRecord::Base
2012-06-04 18:00:19 +01:00
STATUSES = %w(ready already blocked merged closed)
2012-04-28 18:28:57 +01:00
belongs_to :issue, :autosave => true, :dependent => :destroy, :touch => true, :validate => true
belongs_to :base_project, :class_name => 'Project', :foreign_key => 'base_project_id'
belongs_to :head_project, :class_name => 'Project', :foreign_key => 'head_project_id'
2012-07-10 17:58:39 +01:00
delegate :user, :user_id, :title, :body, :serial_id, :assignee, :status, :to_param,
:created_at, :updated_at, :comments, :to => :issue, :allow_nil => true
2012-05-31 17:56:27 +01:00
validate :uniq_merge
validates_each :head_ref, :base_ref do |record, attr, value|
project = attr == :head_ref ? record.head_project : record.base_project
2012-08-06 19:16:54 +01:00
if !((project.repo.branches_and_tags).map(&:name).include?(value) || project.repo.commits.map(&:id).include?(value))
record.errors.add attr, I18n.t('projects.pull_requests.wrong_ref')
end
end
2012-05-22 19:23:00 +01:00
before_create :clean_dir
after_destroy :clean_dir
2012-05-31 17:56:27 +01:00
accepts_nested_attributes_for :issue
2012-06-07 18:23:28 +01:00
attr_accessible :issue_attributes, :base_ref, :head_ref
2012-05-31 17:56:27 +01:00
scope :needed_checking, includes(:issue).where(:issues => {:status => ['open', 'blocked', 'ready']})
2012-04-28 18:28:57 +01:00
state_machine :status, :initial => :open do
2012-04-18 18:08:53 +01:00
#after_transition [:ready, :blocked] => [:merged, :closed] do |pull, transition|
# FileUtils.rm_rf(pull.path) # What about diff?
#end
2012-04-16 19:40:50 +01:00
2012-04-18 18:08:53 +01:00
event :ready do
transition [:ready, :open, :blocked] => :ready
2012-04-16 19:40:50 +01:00
end
event :already do
transition [:ready, :open, :blocked] => :already
end
2012-04-16 19:40:50 +01:00
event :block do
transition [:ready, :open, :blocked] => :blocked
2012-04-16 19:40:50 +01:00
end
2012-04-18 18:08:53 +01:00
event :merging do
transition :ready => :merged
2012-04-16 19:40:50 +01:00
end
event :close do
transition [:ready, :open, :blocked] => :closed
2012-04-18 18:08:53 +01:00
end
event :reopen do
transition :closed => :open
2012-04-16 19:40:50 +01:00
end
end
def status=(value)
issue.status = value
2012-05-10 10:42:25 +01:00
end
2012-04-16 19:40:50 +01:00
def can_merge?
status == 'ready'
2012-04-18 18:08:53 +01:00
end
2012-06-28 14:47:29 +01:00
def check(do_transaction = true)
new_status = case merge
when /Already up-to-date/
'already'
when /Merge made by the 'recursive' strategy/
system("cd #{path} && git reset --hard HEAD^") # remove merge commit
'ready'
when /Automatic merge failed/
system("cd #{path} && git reset --hard HEAD") # clean git index
'block'
else
raise ret
end
if do_transaction
if new_status == 'already'
ready; merging
else
send(new_status)
end
2012-05-24 18:10:49 +01:00
else
2012-06-28 14:47:29 +01:00
self.status = new_status == 'block' ? 'blocked' : new_status
2012-05-24 18:10:49 +01:00
end
end
2012-04-18 18:08:53 +01:00
def merge!(who)
return false unless can_merge?
Dir.chdir(path) do
system "git config user.name \"#{who.uname}\" && git config user.email \"#{who.email}\""
2012-04-18 18:08:53 +01:00
if merge
system("git push origin HEAD")
system("git reset --hard HEAD^") # for diff maybe FIXME
2012-06-28 11:44:55 +01:00
set_user_and_time who
merging
2012-04-18 18:08:53 +01:00
end
end
2012-04-16 19:40:50 +01:00
end
2012-04-28 18:28:57 +01:00
def self.default_base_project(project)
project.is_root? ? project : project.root
end
2012-04-16 19:40:50 +01:00
def path
2012-05-17 17:47:36 +01:00
filename = [id, base_ref, head_project.owner.uname, head_project.name, head_ref].compact.join('-')
2012-07-04 11:35:35 +01:00
File.join(APP_CONFIG['root_path'], 'pull_requests', base_project.owner.uname, base_project.name, filename)
2012-04-16 19:40:50 +01:00
end
2012-05-12 17:23:39 +01:00
def head_branch
if base_project != head_project
"head_#{head_ref}"
else
head_ref
end
end
2012-06-07 18:23:28 +01:00
def common_ancestor
return @common_ancestor if @common_ancestor
repo = Grit::Repo.new(path)
base_commit = repo.commits(base_ref).first
head_commit = repo.commits(head_branch).first
@common_ancestor = repo.commit(repo.git.merge_base({}, base_commit, head_commit))
end
def diff_stats(repo, a,b)
2012-05-29 18:09:43 +01:00
stats = []
Dir.chdir(path) do
2012-06-07 20:19:18 +01:00
lines = repo.git.native(:diff, {:numstat => true, :M => true}, "#{a.id}...#{b.id}").split("\n")
2012-05-29 18:09:43 +01:00
while !lines.empty?
files = []
while lines.first =~ /^([-\d]+)\s+([-\d]+)\s+(.+)/
2012-06-07 20:19:18 +01:00
additions, deletions, filename = lines.shift.gsub(' => ', '=>').split
2012-05-29 18:09:43 +01:00
additions, deletions = additions.to_i, deletions.to_i
stat = Grit::DiffStat.new filename, additions, deletions
2012-05-29 18:09:43 +01:00
stats << stat
end
end
stats
end
end
2012-06-07 18:23:28 +01:00
# FIXME копипизд from grit (maybe move to warpc/gri?)
def diff(repo, a, b)
2012-06-07 20:19:18 +01:00
diff = repo.git.native('diff', {:M => true}, "#{a}...#{b}")
2012-06-07 18:23:28 +01:00
if diff =~ /diff --git a/
diff = diff.sub(/.*?(diff --git a)/m, '\1')
else
diff = ''
end
Grit::Diff.list_from_string(repo, diff)
end
2012-07-12 15:15:28 +01:00
def set_user_and_time user
issue.closed_at = Time.now.utc
issue.closer = user
end
2012-05-05 17:57:12 +01:00
protected
2012-04-16 19:40:50 +01:00
def merge
clone
2012-07-27 13:52:31 +01:00
message = "Merge pull request ##{serial_id} from #{head_project.fullname}:#{head_ref}\r\n #{title}"
%x(cd #{path} && git checkout #{base_ref} && git merge --no-ff #{head_branch} -m '#{message}')
2012-04-16 19:40:50 +01:00
end
def clone
2012-04-18 18:08:53 +01:00
git = Grit::Git.new(path)
2012-04-16 19:40:50 +01:00
unless git.exist?
FileUtils.mkdir_p(path)
2012-04-28 18:28:57 +01:00
system("git clone --local --no-hardlinks #{base_project.path} #{path}")
if base_project != head_project
Dir.chdir(path) do
system 'git', 'remote', 'add', 'head', head_project.path
end
end
2012-04-19 20:08:33 +01:00
end
2012-04-28 18:28:57 +01:00
clean
2012-04-19 20:08:33 +01:00
Dir.chdir(path) do
2012-04-28 18:28:57 +01:00
system 'git', 'checkout', base_ref
system 'git', 'pull', 'origin', base_ref
if base_project == head_project
system 'git', 'checkout', head_ref
system 'git', 'pull', 'origin', head_ref
else
2012-05-12 17:23:39 +01:00
system 'git', 'fetch', 'head', "+#{head_ref}:#{head_branch}"
2012-04-16 19:40:50 +01:00
end
end
# TODO catch errors
end
def clean
Dir.chdir(path) do
base_project.repo.branches.each {|branch| system 'git', 'checkout', branch.name}
system 'git', 'checkout', base_ref
base_project.repo.branches.each do |branch|
system 'git', 'branch', '-D', branch.name unless [base_ref, head_branch].include? branch.name
end
base_project.repo.tags.each do |tag|
system 'git', 'tag', '-d', tag.name unless [base_ref, head_branch].include? tag.name
end
end
end
def uniq_merge
2012-05-22 19:23:00 +01:00
if base_project.pull_requests.needed_checking.where(:head_project_id => head_project, :base_ref => base_ref, :head_ref => head_ref).where('pull_requests.id <> :id or :id is null', :id => id).count > 0
errors.add(:base_branch, I18n.t('projects.pull_requests.duplicate', :head_ref => head_ref))
end
end
def clean_dir
FileUtils.rm_rf path
end
2012-04-16 19:40:50 +01:00
end