merge_requests_controller.rb 4.68 KB
Newer Older
1 2
require 'gitlab/satellite/satellite'

3
class MergeRequestsController < ProjectResourceController
4
  before_filter :module_enabled
5
  before_filter :merge_request, only: [:edit, :update, :show, :commits, :diffs, :automerge, :automerge_check, :ci_status]
6
  before_filter :validates_merge_request, only: [:show, :diffs]
7
  before_filter :define_show_vars, only: [:show, :diffs]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
8 9 10 11 12

  # Allow read any merge_request
  before_filter :authorize_read_merge_request!

  # Allow write(create) merge_request
13
  before_filter :authorize_write_merge_request!, only: [:new, :create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14 15

  # Allow modify merge_request
16
  before_filter :authorize_modify_merge_request!, only: [:close, :edit, :update, :sort]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
18
  def index
19
    @merge_requests = MergeRequestsLoadContext.new(project, current_user, params).execute
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
20 21 22
  end

  def show
Riyad Preukschas's avatar
Riyad Preukschas committed
23 24 25
    @target_type = :merge_request
    @target_id = @merge_request.id

26 27
    respond_to do |format|
      format.html
28
      format.js
29

30 31 32
      format.diff  { render text: @merge_request.to_diff }
      format.patch { render text: @merge_request.to_patch }
    end
randx's avatar
randx committed
33 34
  end

35
  def diffs
36 37
    @diffs = @merge_request.diffs
    @commit = @merge_request.last_commit
38

39 40 41
    @comments_allowed = @reply_allowed = true
    @comments_target  = { noteable_type: 'MergeRequest',
                          noteable_id: @merge_request.id }
42
    @line_notes = @merge_request.notes.where("line_code is not null")
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
43 44 45
  end

  def new
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
46
    @merge_request = @project.merge_requests.new(params[:merge_request])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
47 48 49 50 51 52 53 54 55
  end

  def edit
  end

  def create
    @merge_request = @project.merge_requests.new(params[:merge_request])
    @merge_request.author = current_user

56 57 58 59
    if @merge_request.save
      @merge_request.reload_code
      redirect_to [@project, @merge_request], notice: 'Merge request was successfully created.'
    else
60
      render action: "new"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
61 62 63 64
    end
  end

  def update
65
    if @merge_request.update_attributes(params[:merge_request].merge(author_id_of_changes: current_user.id))
66 67 68 69 70
      @merge_request.reload_code
      @merge_request.mark_as_unchecked
      redirect_to [@project, @merge_request], notice: 'Merge request was successfully updated.'
    else
      render action: "edit"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
71 72 73
    end
  end

Valery Sizov's avatar
Valery Sizov committed
74
  def automerge_check
75
    if @merge_request.unchecked?
Valery Sizov's avatar
Valery Sizov committed
76 77
      @merge_request.check_if_can_be_merged
    end
78
    render json: {merge_status: @merge_request.merge_status_name}
79
  rescue Gitlab::SatelliteNotExistError
Andrew8xx8's avatar
Andrew8xx8 committed
80
    render json: {merge_status: :no_satellite}
Valery Sizov's avatar
Valery Sizov committed
81 82
  end

randx's avatar
randx committed
83
  def automerge
84 85
    return access_denied! unless allowed_to_merge?

86
    if @merge_request.opened? && @merge_request.can_be_merged?
87
      @merge_request.should_remove_source_branch = params[:should_remove_source_branch]
88 89 90 91 92
      @merge_request.automerge!(current_user)
      @status = true
    else
      @status = false
    end
randx's avatar
randx committed
93 94
  end

95
  def branch_from
96
    @commit = @repository.commit(params[:ref])
97
    @commit = CommitDecorator.decorate(@commit)
98 99 100
  end

  def branch_to
101
    @commit = @repository.commit(params[:ref])
102
    @commit = CommitDecorator.decorate(@commit)
103 104
  end

105 106 107 108 109 110 111
  def ci_status
    status = project.gitlab_ci_service.commit_status(merge_request.last_commit.sha)
    response = { status: status }

    render json: response
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
112 113 114 115 116
  protected

  def merge_request
    @merge_request ||= @project.merge_requests.find(params[:id])
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
117 118

  def authorize_modify_merge_request!
119
    return render_404 unless can?(current_user, :modify_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
120 121 122
  end

  def authorize_admin_merge_request!
123
    return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
124
  end
125 126 127 128

  def module_enabled
    return render_404 unless @project.merge_requests_enabled
  end
129 130 131

  def validates_merge_request
    # Show git not found page if target branch doesnt exist
132
    return git_not_found! unless @project.repo.heads.map(&:name).include?(@merge_request.target_branch)
133 134 135 136 137 138 139 140

    # Show git not found page if source branch doesnt exist
    # and there is no saved commits between source & target branch
    return git_not_found! if !@project.repo.heads.map(&:name).include?(@merge_request.source_branch) && @merge_request.commits.blank?
  end

  def define_show_vars
    # Build a note object for comment form
141
    @note = @project.notes.new(noteable: @merge_request)
142

143
    # Get commits from repository
144 145
    # or from cache if already merged
    @commits = @merge_request.commits
146
    @commits = CommitDecorator.decorate_collection(@commits)
147 148 149 150 151 152 153 154 155 156 157 158 159

    @allowed_to_merge = allowed_to_merge?
    @show_merge_controls = @merge_request.opened? && @commits.any? && @allowed_to_merge
  end

  def allowed_to_merge?
    action = if project.protected_branch?(@merge_request.target_branch)
               :push_code_to_protected_branches
             else
               :push_code
             end

    can?(current_user, action, @project)
160
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
161
end