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

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

  # Allow read any merge_request
  before_filter :authorize_read_merge_request!

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

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
19
  def index
20
    @merge_requests = get_merge_requests_collection
21
    @merge_requests = @merge_requests.page(params[:page]).per(PER_PAGE)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
22 23 24
  end

  def show
25
    @note_counts = Note.where(commit_id: @merge_request.commits.map(&:id)).
26
      group(:commit_id).count
27

28 29
    respond_to do |format|
      format.html
30
      format.json { render json: @merge_request }
31 32
      format.diff { render text: @merge_request.to_diff(current_user) }
      format.patch { render text: @merge_request.to_patch(current_user) }
33
    end
randx's avatar
randx committed
34 35
  end

36
  def diffs
37
    @commit = @merge_request.last_commit
38
    @comments_allowed = @reply_allowed = true
39 40 41 42
    @comments_target = {
      noteable_type: 'MergeRequest',
      noteable_id: @merge_request.id
    }
43
    @line_notes = @merge_request.notes.where("line_code is not null")
44

45 46 47 48
    respond_to do |format|
      format.html
      format.json { render json: { html: view_to_html_string("projects/merge_requests/show/_diffs") } }
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
49 50 51
  end

  def new
52 53 54 55 56 57 58 59 60 61 62 63
    params[:merge_request] ||= ActionController::Parameters.new(source_project: @project)
    @merge_request = MergeRequests::BuildService.new(project, current_user, merge_request_params).execute

    @target_branches = if @merge_request.target_project
                         @merge_request.target_project.repository.branch_names
                       else
                         []
                       end

    @target_project = merge_request.target_project
    @source_project = merge_request.source_project
    @commits = @merge_request.compare_commits
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
64
    @commit = @merge_request.compare_commits.last
65 66 67
    @diffs = @merge_request.compare_diffs
    @note_counts = Note.where(commit_id: @commits.map(&:id)).
      group(:commit_id).count
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
68 69 70
  end

  def edit
Izaak Alpert's avatar
Izaak Alpert committed
71 72
    @source_project = @merge_request.source_project
    @target_project = @merge_request.target_project
73
    @target_branches = @merge_request.target_project.repository.branch_names
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
74 75 76
  end

  def create
77
    @target_branches ||= []
78
    @merge_request = MergeRequests::CreateService.new(project, current_user, merge_request_params).execute
79 80

    if @merge_request.valid?
81
      redirect_to(merge_request_path(@merge_request))
82
    else
Izaak Alpert's avatar
Izaak Alpert committed
83 84 85
      @source_project = @merge_request.source_project
      @target_project = @merge_request.target_project
      render action: "new"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
86 87 88 89
    end
  end

  def update
90
    @merge_request = MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
91

92
    if @merge_request.valid?
93 94 95
      respond_to do |format|
        format.js
        format.html do
Vinnie Okada's avatar
Vinnie Okada committed
96
          redirect_to([@merge_request.target_project.namespace.becomes(Namespace),
97
                       @merge_request.target_project, @merge_request])
98
        end
99 100 101 102 103 104
        format.json do
          render json: {
            saved: @merge_request.valid?,
            assignee_avatar_url: @merge_request.assignee.try(:avatar_url)
          }
        end
105
      end
106
    else
107
      render "edit"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
108 109 110
    end
  end

Valery Sizov's avatar
Valery Sizov committed
111
  def automerge_check
112
    if @merge_request.unchecked?
Valery Sizov's avatar
Valery Sizov committed
113 114
      @merge_request.check_if_can_be_merged
    end
115 116

    render json: { merge_status: @merge_request.merge_status_name }
Valery Sizov's avatar
Valery Sizov committed
117 118
  end

randx's avatar
randx committed
119
  def automerge
120 121
    return access_denied! unless allowed_to_merge?

122
    if @merge_request.open? && @merge_request.can_be_merged?
123
      AutoMergeWorker.perform_async(@merge_request.id, current_user.id, params)
124 125 126 127
      @status = true
    else
      @status = false
    end
randx's avatar
randx committed
128 129
  end

130
  def branch_from
131
    #This is always source
Izaak Alpert's avatar
Izaak Alpert committed
132
    @source_project = @merge_request.nil? ? @project : @merge_request.source_project
133
    @commit = @repository.commit(params[:ref]) if params[:ref].present?
134 135 136
  end

  def branch_to
137
    @target_project = selected_target_project
138
    @commit = @target_project.repository.commit(params[:ref]) if params[:ref].present?
139 140
  end

141 142
  def update_branches
    @target_project = selected_target_project
Izaak Alpert's avatar
Izaak Alpert committed
143
    @target_branches = @target_project.repository.branch_names
144 145 146 147

    respond_to do |format|
      format.js
    end
148 149
  end

150
  def ci_status
151 152 153 154 155 156 157 158 159 160 161
    ci_service = @merge_request.source_project.ci_service
    status = ci_service.commit_status(merge_request.last_commit.sha)

    if ci_service.respond_to?(:commit_coverage)
      coverage = ci_service.commit_coverage(merge_request.last_commit.sha)
    end

    response = {
      status: status,
      coverage: coverage
    }
162 163 164 165

    render json: response
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
166 167
  protected

168
  def selected_target_project
169 170 171 172 173
    if @project.id.to_s == params[:target_project_id] || @project.forked_project_link.nil?
      @project
    else
      @project.forked_project_link.forked_from_project
    end
174 175
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
176
  def merge_request
skv's avatar
skv committed
177
    @merge_request ||= @project.merge_requests.find_by!(iid: params[:id])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
178
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
179

180 181 182 183
  def closes_issues
    @closes_issues ||= @merge_request.closes_issues
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
184
  def authorize_modify_merge_request!
185
    return render_404 unless can?(current_user, :modify_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
186 187 188
  end

  def authorize_admin_merge_request!
189
    return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
190
  end
191 192 193 194

  def module_enabled
    return render_404 unless @project.merge_requests_enabled
  end
195 196

  def validates_merge_request
197 198 199
    # If source project was removed (Ex. mr from fork to origin)
    return invalid_mr unless @merge_request.source_project

200 201 202
    # Show git not found page
    # if there is no saved commits between source & target branch
    if @merge_request.commits.blank?
203 204
      # and if target branch doesn't exist
      return invalid_mr unless @merge_request.target_branch_exists?
205

206 207
      # or if source branch doesn't exist
      return invalid_mr unless @merge_request.source_branch_exists?
208
    end
209 210 211 212
  end

  def define_show_vars
    # Build a note object for comment form
213
    @note = @project.notes.new(noteable: @merge_request)
214 215
    @notes = @merge_request.mr_and_commit_notes.inc_author.fresh
    @discussions = Note.discussions_from_notes(@notes)
216
    @noteable = @merge_request
217

218
    # Get commits from repository
219 220
    # or from cache if already merged
    @commits = @merge_request.commits
221

222
    @merge_request_diff = @merge_request.merge_request_diff
223
    @allowed_to_merge = allowed_to_merge?
224
    @show_merge_controls = @merge_request.open? && @commits.any? && @allowed_to_merge
225
    @source_branch = @merge_request.source_project.repository.find_branch(@merge_request.source_branch).try(:name)
226 227 228 229 230

    if @merge_request.locked_long_ago?
      @merge_request.unlock_mr
      @merge_request.close
    end
231 232 233
  end

  def allowed_to_merge?
234
    allowed_to_push_code?(project, @merge_request.target_branch)
235 236 237 238 239 240 241
  end

  def invalid_mr
    # Render special view for MR with removed source or target branch
    render 'invalid'
  end

242
  def allowed_to_push_code?(project, branch)
243
    ::Gitlab::GitAccess.can_push_to_branch?(current_user, project, branch)
244
  end
245 246 247 248 249

  def merge_request_params
    params.require(:merge_request).permit(
      :title, :assignee_id, :source_project_id, :source_branch,
      :target_project_id, :target_branch, :milestone_id,
250
      :state_event, :description, :task_num, label_ids: []
251 252
    )
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
253
end