merge_request.rb 5.4 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4 5 6 7 8 9 10 11
# == Schema Information
#
# Table name: merge_requests
#
#  id            :integer          not null, primary key
#  target_branch :string(255)      not null
#  source_branch :string(255)      not null
#  project_id    :integer          not null
#  author_id     :integer
#  assignee_id   :integer
#  title         :string(255)
A
Andrew8xx8 已提交
12
#  state         :string(255)      not null
D
Dmitriy Zaporozhets 已提交
13 14 15 16
#  created_at    :datetime         not null
#  updated_at    :datetime         not null
#  st_commits    :text(2147483647)
#  st_diffs      :text(2147483647)
A
Andrew8xx8 已提交
17
#  merge_status  :integer          default(1), not null
D
Dmitriy Zaporozhets 已提交
18
#
A
Andrew8xx8 已提交
19
#  milestone_id  :integer
D
Dmitriy Zaporozhets 已提交
20

21
require Rails.root.join("app/models/commit")
22
require Rails.root.join("lib/static_model")
23

D
Dmitriy Zaporozhets 已提交
24
class MergeRequest < ActiveRecord::Base
25
  include Issuable
26

27 28
  BROKEN_DIFF = "--broken-diff"

A
Andrew8xx8 已提交
29
  attr_accessible :title, :assignee_id, :target_branch, :source_branch, :milestone_id,
A
Andrew8xx8 已提交
30
                  :author_id_of_changes, :state_event
31

A
Andrey Kumanyaev 已提交
32 33
  attr_accessor :should_remove_source_branch

A
Andrew8xx8 已提交
34
  state_machine :state, initial: :opened do
A
Andrew8xx8 已提交
35 36 37 38 39 40 41 42 43
    event :close do
      transition [:reopened, :opened] => :closed
    end

    event :merge do
      transition [:reopened, :opened] => :merged
    end

    event :reopen do
A
Andrew8xx8 已提交
44
      transition closed: :reopened
A
Andrew8xx8 已提交
45 46 47 48 49 50 51 52 53 54 55
    end

    state :opened

    state :reopened

    state :closed

    state :merged
  end

56 57 58 59 60 61 62 63 64 65 66 67 68
  state_machine :merge_status, initial: :unchecked do
    event :mark_as_unchecked do
      transition [:can_be_merged, :cannot_be_merged] => :unchecked
    end

    event :mark_as_mergeable do
      transition unchecked: :can_be_merged
    end

    event :mark_as_unmergeable do
      transition unchecked: :cannot_be_merged
    end

69
    state :unchecked
70

71 72 73 74
    state :can_be_merged

    state :cannot_be_merged
  end
75

76 77 78
  serialize :st_commits
  serialize :st_diffs

A
Andrey Kumanyaev 已提交
79 80
  validates :source_branch, presence: true
  validates :target_branch, presence: true
81
  validate  :validate_branches
D
Dmitriy Zaporozhets 已提交
82

A
Andrew8xx8 已提交
83
  scope :merged, -> { with_state(:merged) }
A
Andrew8xx8 已提交
84 85
  scope :by_branch, ->(branch_name) { where("source_branch LIKE :branch OR target_branch LIKE :branch", branch: branch_name) }
  scope :cared, ->(user) { where('assignee_id = :user OR author_id = :user', user: user.id) }
86
  scope :by_milestone, ->(milestone) { where(milestone_id: milestone) }
87

88 89 90 91
  # Closed scope for merge request should return
  # both merged and closed mr's
  scope :closed, -> { with_states(:closed, :merged) }

92
  def validate_branches
93
    if target_branch == source_branch
94
      errors.add :branch_conflict, "You can not use same branch for source and target branches"
95 96 97
    end
  end

98 99 100 101 102
  def reload_code
    self.reloaded_commits
    self.reloaded_diffs
  end

103
  def check_if_can_be_merged
104 105 106 107 108
    if Gitlab::Satellite::MergeAction.new(self.author, self).can_be_merged?
      mark_as_mergeable
    else
      mark_as_unmergeable
    end
109 110
  end

D
Dmitriy Zaporozhets 已提交
111
  def diffs
112 113 114 115
    st_diffs || []
  end

  def reloaded_diffs
A
Andrew8xx8 已提交
116
    if opened? && unmerged_diffs.any?
117
      self.st_diffs = unmerged_diffs
118
      self.save
119
    end
120 121 122 123 124 125 126 127 128 129 130 131

  rescue Grit::Git::GitTimeout
    self.st_diffs = [BROKEN_DIFF]
    self.save
  end

  def broken_diffs?
    diffs == [BROKEN_DIFF]
  end

  def valid_diffs?
    !broken_diffs?
132 133 134
  end

  def unmerged_diffs
135 136 137 138 139
    # Only show what is new in the source branch compared to the target branch, not the other way around.
    # The linex below with merge_base is equivalent to diff with three dots (git diff branch1...branch2)
    # From the git documentation: "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B"
    common_commit = project.repo.git.native(:merge_base, {}, [target_branch, source_branch]).strip
    diffs = project.repo.diff(common_commit, source_branch)
D
Dmitriy Zaporozhets 已提交
140 141 142
  end

  def last_commit
143
    commits.first
D
Dmitriy Zaporozhets 已提交
144
  end
145

D
Dmitriy Zaporozhets 已提交
146
  def merge_event
A
Andrew8xx8 已提交
147
    self.project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::MERGED).last
D
Dmitriy Zaporozhets 已提交
148 149
  end

150
  def closed_event
A
Andrew8xx8 已提交
151
    self.project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::CLOSED).last
152 153
  end

154 155 156 157 158
  def commits
    st_commits || []
  end

  def probably_merged?
159
    unmerged_commits.empty? &&
A
Andrew8xx8 已提交
160
      commits.any? && opened?
161 162
  end

163
  def reloaded_commits
A
Andrew8xx8 已提交
164
    if opened? && unmerged_commits.any?
165 166 167 168 169 170 171 172 173 174 175 176 177
      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
178 179

  def merge!(user_id)
180
    self.author_id_of_changes = user_id
A
Andrew8xx8 已提交
181
    self.merge
182
  end
183

184
  def automerge!(current_user)
185
    if Gitlab::Satellite::MergeAction.new(current_user, self).merge! && self.unmerged_commits.empty?
186 187 188
      self.merge!(current_user.id)
      true
    end
Z
Zevs 已提交
189
  rescue
190
    mark_as_unmergeable
191 192
    false
  end
R
randx 已提交
193

194 195
  def mr_and_commit_notes
    commit_ids = commits.map(&:id)
196
    Note.where("(noteable_type = 'MergeRequest' AND noteable_id = :mr_id) OR (noteable_type = 'Commit' AND commit_id IN (:commit_ids))", mr_id: id, commit_ids: commit_ids)
197
  end
198 199 200 201 202 203 204 205 206 207 208 209 210 211

  # Returns the raw diff for this merge request
  #
  # see "git diff"
  def to_diff
    project.repo.git.native(:diff, {timeout: 30, raise: true}, "#{target_branch}...#{source_branch}")
  end

  # Returns the commit as a series of email patches.
  #
  # see "git format-patch"
  def to_patch
    project.repo.git.format_patch({timeout: 30, raise: true, stdout: true}, "#{target_branch}..#{source_branch}")
  end
212 213 214 215

  def last_commit_short_sha
    @last_commit_short_sha ||= last_commit.sha[0..10]
  end
D
Dmitriy Zaporozhets 已提交
216
end