Persist Merge Request diff. Auto merge request close on push

This commit is contained in:
Dmitriy Zaporozhets 2012-03-15 00:57:43 +02:00
parent 27e3699814
commit 5a3f23f395
10 changed files with 121 additions and 28 deletions

View file

@ -1,9 +1,14 @@
require File.join(Rails.root, "app/models/commit")
class MergeRequest < ActiveRecord::Base
belongs_to :project
belongs_to :author, :class_name => "User"
belongs_to :assignee, :class_name => "User"
has_many :notes, :as => :noteable, :dependent => :destroy
serialize :st_commits
serialize :st_diffs
attr_protected :author, :author_id, :project, :project_id
attr_accessor :author_id_of_changes
@ -32,7 +37,6 @@ class MergeRequest < ActiveRecord::Base
scope :closed, where(:closed => true)
scope :assigned, lambda { |u| where(:assignee_id => u.id)}
def validate_branches
if target_branch == source_branch
errors.add :base, "You can not use same branch for source and target branches"
@ -44,18 +48,65 @@ class MergeRequest < ActiveRecord::Base
end
def diffs
st_diffs || []
end
def reloaded_diffs
if open? && unmerged_diffs.any?
self.st_diffs = unmerged_diffs
save
end
diffs
end
def unmerged_diffs
commits = project.repo.commits_between(target_branch, source_branch).map {|c| Commit.new(c)}
diffs = project.repo.diff(commits.first.prev_commit.id, commits.last.id) rescue []
end
def last_commit
project.commit(source_branch)
commits.first
end
# Return the number of +1 comments (upvotes)
def upvotes
notes.select(&:upvote?).size
end
def commits
st_commits || []
end
def probably_merged?
unmerged_commits.empty? &&
commits.any? && open?
end
def open?
!closed
end
def mark_as_merged!
self.merged = true
self.closed = true
save
end
def reloaded_commits
if open? && unmerged_commits.any?
self.st_commits = unmerged_commits
save
end
commits
end
def unmerged_commits
self.project.repo.
commits_between(self.target_branch, self.source_branch).
map {|c| Commit.new(c)}.
sort_by(&:created_at).
reverse
end
end
# == Schema Information
#