rosa-build/app/models/pull_request.rb

96 lines
2.2 KiB
Ruby
Raw Normal View History

2012-04-16 19:40:50 +01:00
class PullRequest < Issue
2012-04-18 18:08:53 +01:00
extend StateMachine::MacroMethods # no method state_machine WTF?!
2012-04-16 19:40:50 +01:00
serialize :data
2012-04-18 18:08:53 +01:00
#TODO add validates to serialized data
scope :needed_checking, where(:state => ['open', 'blocked', 'ready'])
2012-04-16 19:40:50 +01:00
state_machine :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 [:open, :blocked] => :ready
2012-04-16 19:40:50 +01:00
end
event :block do
2012-04-18 18:08:53 +01:00
transition [:open, :ready] => :block
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
2012-04-18 18:08:53 +01:00
transition [:open, :ready, :blocked] => :closed
end
event :reopen do
transition :closed => :open
2012-04-16 19:40:50 +01:00
end
end
def can_merge?
2012-04-18 18:08:53 +01:00
state == 'ready'
end
def check
if ret = merge
system("cd #{path} && git reset --hard HEAD^") # remove merge commit
ready
else
system("cd #{path} && git reset --hard HEAD")
block
end
end
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}\""
if merge
merging
system("git push origin HEAD")
end
end
2012-04-16 19:40:50 +01:00
end
protected
def path
2012-04-18 18:08:53 +01:00
filename = [id, project.owner.uname, project.name].join('-')
2012-04-16 19:40:50 +01:00
if Rails.env == "production"
2012-04-18 18:08:53 +01:00
File.join('/srv/rosa_build/shared/tmp', "pull_requests", filename)
2012-04-16 19:40:50 +01:00
else
2012-04-18 18:08:53 +01:00
File.join(Rails.root, "tmp", Rails.env, "pull_requests", filename)
2012-04-16 19:40:50 +01:00
end
end
def merge
clone
2012-04-18 18:08:53 +01:00
system("cd #{path} && git checkout #{data[:base_branch]} && git merge --no-ff #{data[:head_branch]}")
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)
Dir.chdir(path) do
system("git clone --local --no-hardlinks #{project.path} #{path}")
end
else
Dir.chdir(path) do
2012-04-18 18:08:53 +01:00
[data[:base_branch], data[:head_branch]].each do |branch|
2012-04-16 19:40:50 +01:00
system "git checkout #{branch} && git pull origin #{branch}"
end
end
end
# TODO catch errors
end
2012-04-18 18:08:53 +01:00
def set_serial_id
self.update_attribute :serial_id, self.project.pull_requests.count
end
2012-04-16 19:40:50 +01:00
end