[refs #90] initial

This commit is contained in:
Alexander Machehin 2012-04-17 00:40:50 +06:00
parent 02095e62c1
commit d48a3413cc
4 changed files with 76 additions and 2 deletions

View File

@ -6,6 +6,7 @@ class Project < ActiveRecord::Base
belongs_to :owner, :polymorphic => true, :counter_cache => :own_projects_count belongs_to :owner, :polymorphic => true, :counter_cache => :own_projects_count
has_many :issues, :dependent => :destroy has_many :issues, :dependent => :destroy
has_many :pull_requests, :dependent => :destroy
has_many :build_lists, :dependent => :destroy has_many :build_lists, :dependent => :destroy
has_many :project_imports, :dependent => :destroy has_many :project_imports, :dependent => :destroy

View File

@ -0,0 +1,62 @@
class PullRequest < Issue
serialize :data
state_machine :initial => :open do
event :open do
transition :closed => :open
transition :blocked => :open, :if => lambda {|pull| pull.can_merge?}
end
event :block do
transition :open => :blocked
end
event :merge do
transition :open => :merged, :if => lambda {|pull| pull.can_merge?}
transition :open => :blocked, :if => lambda {|pull| !pull.can_merge?}
end
event :close do
transition [:open, :blocked] => :closed
end
end
def can_merge?
!merge
end
protected
def path
if Rails.env == "production"
File.join('/srv/rosa_build/shared/tmp', "pull_requests", [id, project.owner.uname, project.name].join('-'))
else
File.join(Rails.root, "tmp", Rails.env, "pull_requests", [id, project.owner.uname, project.name].join('-'))
end
end
def merge
clone
system "cd #{path} && git checkout #{data.base_branch} && git merge #{data.head_branch} && git reset --hard #{data.head_branch}"
end
def clone
git = Git.new(path)
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
[data.base_branch, data.head_branch].each do |branch|
system "git checkout #{branch} && git pull origin #{branch}"
end
end
end
# TODO catch errors
end
end

View File

@ -0,0 +1,6 @@
class CreatePullRequests < ActiveRecord::Migration
def change
add_column :issues, :type, :string
add_column :issues, :data, :text, :null => false, :default => 0
end
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe PullRequest do
pending "add some examples to (or delete) #{__FILE__}"
end