# Class MergeRequest < ActiveRecord::Base

- app/models/merge_request.rb

Methods

A
  - automerge!
B
  - broken_diffs?
C
  - can_be_merged?,
  - check_if_can_be_merged,
  - closed_event,
  - commits
D
  - diffs
F
  - find_all_by_branch,
  - find_all_by_milestone
H
  - human_state
L
  - last_commit,
  - last_commit_short_sha
M
  - mark_as_merged!,
  - mark_as_unchecked,
  - mark_as_unmergable,
  - merge!,
  - merge_event,
  - merged?,
  - mr_and_commit_notes
O
  - open?
P
  - probably_merged?
R
  - reload_code,
  - reloaded_commits,
  - reloaded_diffs
T
  - to_diff,
  - to_patch
U
  - unchecked?,
  - unmerged_commits,
  - unmerged_diffs
V
  - valid_diffs?,
  - validate_branches

Included Modules

- IssueCommonality
- Votes

Constants

BROKEN_DIFF | = | "--broken-diff"
UNCHECKED | = | 1
CAN_BE_MERGED | = | 2
CANNOT_BE_MERGED | = | 3

Attributes

[RW] | should_remove_source_branch |

Class Public methods
find_all_by_branch(branch_name) Link

Source: show

    # File app/models/merge_request.rb, line 47
    def self.find_all_by_branch(branch_name)
      where("source_branch LIKE :branch OR target_branch LIKE :branch", branch: branch_name)
    end

find_all_by_milestone(milestone) Link

Source: show

    # File app/models/merge_request.rb, line 51
    def self.find_all_by_milestone(milestone)
      where("milestone_id = :milestone_id", milestone_id: milestone)
    end

Instance Public methods
automerge!(current_user) Link

Source: show

    # File app/models/merge_request.rb, line 195
    def automerge!(current_user)
      if Gitlab::Satellite::MergeAction.new(current_user, self).merge! && self.unmerged_commits.empty?
        self.merge!(current_user.id)
        true
      end
    rescue
      self.mark_as_unmergable
      false
    end

broken_diffs?() Link

Source: show

    # File app/models/merge_request.rb, line 112
    def broken_diffs?
      diffs == [BROKEN_DIFF]
    end

can_be_merged?() Link

Source: show

    # File app/models/merge_request.rb, line 84
    def can_be_merged?
      state == CAN_BE_MERGED
    end

check_if_can_be_merged() Link

Source: show

    # File app/models/merge_request.rb, line 88
    def check_if_can_be_merged
      self.state = if Gitlab::Satellite::MergeAction.new(self.author, self).can_be_merged?
                     CAN_BE_MERGED
                   else
                     CANNOT_BE_MERGED
                   end
      self.save
    end

closed_event() Link

Source: show

    # File app/models/merge_request.rb, line 140
    def closed_event
      self.project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::Closed).last
    end

commits() Link

Source: show

    # File app/models/merge_request.rb, line 144
    def commits
      st_commits || []
    end

diffs() Link

Source: show

    # File app/models/merge_request.rb, line 97
    def diffs
      st_diffs || []
    end

human_state() Link

Source: show

    # File app/models/merge_request.rb, line 55
    def human_state
      states = {
        CAN_BE_MERGED =>  "can_be_merged",
        CANNOT_BE_MERGED => "cannot_be_merged",
        UNCHECKED => "unchecked"
      }
      states[self.state]
    end

last_commit() Link

Source: show

    # File app/models/merge_request.rb, line 128
    def last_commit
      commits.first
    end

last_commit_short_sha() Link

Source: show

    # File app/models/merge_request.rb, line 224
    def last_commit_short_sha
      @last_commit_short_sha ||= last_commit.sha[0..10]
    end

mark_as_merged!() Link

Source: show

    # File app/models/merge_request.rb, line 157
    def mark_as_merged!
      self.merged = true
      self.closed = true
      save
    end

mark_as_unchecked() Link

Source: show

    # File app/models/merge_request.rb, line 79
    def mark_as_unchecked
      self.state = UNCHECKED
      self.save
    end

mark_as_unmergable() Link

Source: show

    # File app/models/merge_request.rb, line 163
    def mark_as_unmergable
      self.state = CANNOT_BE_MERGED
      self.save
    end

merge!(user_id) Link

Source: show

    # File app/models/merge_request.rb, line 184
    def merge!(user_id)
      self.mark_as_merged!
      Event.create(
        project: self.project,
        action: Event::Merged,
        target_id: self.id,
        target_type: "MergeRequest",
        author_id: user_id
      )
    end

merge_event() Link

Source: show

    # File app/models/merge_request.rb, line 136
    def merge_event
      self.project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::Merged).last
    end

merged?() Link

Source: show

    # File app/models/merge_request.rb, line 132
    def merged?
      merged && merge_event
    end

mr_and_commit_notes() Link

Source: show

    # File app/models/merge_request.rb, line 205
    def mr_and_commit_notes
      commit_ids = commits.map(&:id)
      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)
    end

open?() Link

Source: show

    # File app/models/merge_request.rb, line 153
    def open?
      !closed
    end

probably_merged?() Link

Source: show

    # File app/models/merge_request.rb, line 148
    def probably_merged?
      unmerged_commits.empty? &&
        commits.any? && open?
    end

reload_code() Link

Source: show

    # File app/models/merge_request.rb, line 70
    def reload_code
      self.reloaded_commits
      self.reloaded_diffs
    end

reloaded_commits() Link

Source: show

    # File app/models/merge_request.rb, line 168
    def reloaded_commits
      if open? && unmerged_commits.any?
        self.st_commits = unmerged_commits
        save
      end
      commits
    end

reloaded_diffs() Link

Source: show

    # File app/models/merge_request.rb, line 101
    def reloaded_diffs
      if open? && unmerged_diffs.any?
        self.st_diffs = unmerged_diffs
        self.save
      end

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

to_diff() Link

Returns the raw diff for this merge request

see “git diff”

Source: show

    # File app/models/merge_request.rb, line 213
    def to_diff
      project.repo.git.native(:diff, {timeout: 30, raise: true}, "#{target_branch}...#{source_branch}")
    end

to_patch() Link

Returns the commit as a series of email patches.

see “git format-patch”

Source: show

    # File app/models/merge_request.rb, line 220
    def to_patch
      project.repo.git.format_patch({timeout: 30, raise: true, stdout: true}, "#{target_branch}..#{source_branch}")
    end

unchecked?() Link

Source: show

    # File app/models/merge_request.rb, line 75
    def unchecked?
      state == UNCHECKED
    end

unmerged_commits() Link

Source: show

    # File app/models/merge_request.rb, line 176
    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

unmerged_diffs() Link

Source: show

    # File app/models/merge_request.rb, line 120
    def unmerged_diffs
      # 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)
    end

valid_diffs?() Link

Source: show

    # File app/models/merge_request.rb, line 116
    def valid_diffs?
      !broken_diffs?
    end

validate_branches() Link

Source: show

    # File app/models/merge_request.rb, line 64
    def validate_branches
      if target_branch == source_branch
        errors.add :base, "You can not use same branch for source and target branches"
      end
    end