merge_requests_controller.rb 9.88 KB
Newer Older
1
class Projects::MergeRequestsController < Projects::ApplicationController
2
  before_action :module_enabled
3
  before_action :merge_request, only: [
4
    :edit, :update, :show, :diffs, :commits, :builds, :merge, :merge_check,
5
    :ci_status, :toggle_subscription, :cancel_merge_when_build_succeeds
6
  ]
7 8 9
  before_action :closes_issues, only: [:edit, :update, :show, :diffs, :commits, :builds]
  before_action :validates_merge_request, only: [:show, :diffs, :commits, :builds]
  before_action :define_show_vars, only: [:show, :diffs, :commits, :builds]
10
  before_action :define_widget_vars, only: [:merge, :cancel_merge_when_build_succeeds]
11
  before_action :ensure_ref_fetched, only: [:show, :diffs, :commits, :builds]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12 13

  # Allow read any merge_request
14
  before_action :authorize_read_merge_request!
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15 16

  # Allow write(create) merge_request
17
  before_action :authorize_create_merge_request!, only: [:new, :create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
18 19

  # Allow modify merge_request
20
  before_action :authorize_update_merge_request!, only: [:close, :edit, :update, :sort]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
21

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
22
  def index
23
    terms = params['issue_search']
24
    @merge_requests = get_merge_requests_collection
25 26 27 28 29 30 31 32

    if terms.present?
      if terms =~ /\A[#!](\d+)\z/
        @merge_requests = @merge_requests.where(iid: $1)
      else
        @merge_requests = @merge_requests.full_search(terms)
      end
    end
33

34
    @merge_requests = @merge_requests.page(params[:page]).per(PER_PAGE)
35
    @merge_requests = @merge_requests.preload(:target_project)
36 37 38 39 40 41 42 43 44

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

  def show
48
    @note_counts = Note.where(commit_id: @merge_request.commits.map(&:id)).
49
      group(:commit_id).count
50

51 52
    respond_to do |format|
      format.html
53
      format.json { render json: @merge_request }
54 55
      format.diff { render text: @merge_request.to_diff(current_user) }
      format.patch { render text: @merge_request.to_patch(current_user) }
56
    end
randx's avatar
randx committed
57 58
  end

59
  def diffs
60
    @commit = @merge_request.last_commit
61 62
    @first_commit = @merge_request.first_commit

63
    @comments_allowed = @reply_allowed = true
64 65 66 67
    @comments_target = {
      noteable_type: 'MergeRequest',
      noteable_id: @merge_request.id
    }
68
    @line_notes = @merge_request.notes.where("line_code is not null")
69

70 71 72 73
    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
74 75
  end

76
  def commits
77 78 79 80
    respond_to do |format|
      format.html { render 'show' }
      format.json { render json: { html: view_to_html_string('projects/merge_requests/show/_commits') } }
    end
81 82
  end

83 84 85 86 87 88 89 90 91
  def builds
    @ci_project = @merge_request.source_project.gitlab_ci_project

    respond_to do |format|
      format.html { render 'show' }
      format.json { render json: { html: view_to_html_string('projects/merge_requests/show/_builds') } }
    end
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
92
  def new
93 94 95 96 97 98 99 100 101 102 103
    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
104
    @commits = @merge_request.compare_commits.reverse
105 106
    @commit = @merge_request.last_commit
    @first_commit = @merge_request.first_commit
107
    @diffs = @merge_request.compare_diffs
108 109 110 111 112

    @ci_project = @source_project.gitlab_ci_project
    @ci_commit = @merge_request.ci_commit
    @statuses = @ci_commit.statuses if @ci_commit

113 114
    @note_counts = Note.where(commit_id: @commits.map(&:id)).
      group(:commit_id).count
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
115 116 117
  end

  def create
118
    @target_branches ||= []
119
    @merge_request = MergeRequests::CreateService.new(project, current_user, merge_request_params).execute
120 121

    if @merge_request.valid?
122
      redirect_to(merge_request_path(@merge_request))
123
    else
Izaak Alpert's avatar
Izaak Alpert committed
124 125 126
      @source_project = @merge_request.source_project
      @target_project = @merge_request.target_project
      render action: "new"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
127 128 129
    end
  end

130 131 132 133 134 135
  def edit
    @source_project = @merge_request.source_project
    @target_project = @merge_request.target_project
    @target_branches = @merge_request.target_project.repository.branch_names
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
136
  def update
137
    @merge_request = MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
138

139
    if @merge_request.valid?
140 141 142
      respond_to do |format|
        format.js
        format.html do
Vinnie Okada's avatar
Vinnie Okada committed
143
          redirect_to([@merge_request.target_project.namespace.becomes(Namespace),
144
                       @merge_request.target_project, @merge_request])
145
        end
146 147 148 149 150 151
        format.json do
          render json: {
            saved: @merge_request.valid?,
            assignee_avatar_url: @merge_request.assignee.try(:avatar_url)
          }
        end
152
      end
153
    else
154
      render "edit"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
155 156 157
    end
  end

158
  def merge_check
159
    if @merge_request.unchecked?
Valery Sizov's avatar
Valery Sizov committed
160 161
      @merge_request.check_if_can_be_merged
    end
162

163 164
    closes_issues

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
165
    render partial: "projects/merge_requests/widget/show.html.haml", layout: false
Valery Sizov's avatar
Valery Sizov committed
166 167
  end

168
  def cancel_merge_when_build_succeeds
169
    return access_denied! unless @merge_request.can_cancel_merge_when_build_succeeds?(current_user)
170

171
    MergeRequests::MergeWhenBuildSucceedsService.new(@project, current_user).cancel(@merge_request)
172 173
  end

174
  def merge
175
    return access_denied! unless @merge_request.can_be_merged_by?(current_user)
176

177 178 179 180 181 182 183
    unless @merge_request.mergeable?
      @status = :failed
      return
    end

    @merge_request.update(merge_error: nil)

184
    if params[:merge_when_build_succeeds] && @merge_request.ci_commit && @merge_request.ci_commit.active?
185
      MergeRequests::MergeWhenBuildSucceedsService.new(@project, current_user, merge_params)
186 187
                                                      .execute(@merge_request)
      @status = :merge_when_build_succeeds
188
    else
189 190
      MergeWorker.perform_async(@merge_request.id, current_user.id, params)
      @status = :success
191
    end
randx's avatar
randx committed
192 193
  end

194
  def branch_from
195
    #This is always source
Izaak Alpert's avatar
Izaak Alpert committed
196
    @source_project = @merge_request.nil? ? @project : @merge_request.source_project
197
    @commit = @repository.commit(params[:ref]) if params[:ref].present?
198 199 200
  end

  def branch_to
201
    @target_project = selected_target_project
202
    @commit = @target_project.commit(params[:ref]) if params[:ref].present?
203 204
  end

205 206
  def update_branches
    @target_project = selected_target_project
Izaak Alpert's avatar
Izaak Alpert committed
207
    @target_branches = @target_project.repository.branch_names
208 209 210 211

    respond_to do |format|
      format.js
    end
212 213
  end

214
  def ci_status
215
    ci_service = @merge_request.source_project.ci_service
Valery Sizov's avatar
Valery Sizov committed
216
    status = ci_service.commit_status(merge_request.last_commit.sha, merge_request.source_branch)
217 218

    if ci_service.respond_to?(:commit_coverage)
Valery Sizov's avatar
Valery Sizov committed
219
      coverage = ci_service.commit_coverage(merge_request.last_commit.sha, merge_request.source_branch)
220 221 222 223 224 225
    end

    response = {
      status: status,
      coverage: coverage
    }
226 227 228 229

    render json: response
  end

Valery Sizov's avatar
tests  
Valery Sizov committed
230 231
  def toggle_subscription
    @merge_request.toggle_subscription(current_user)
232

Valery Sizov's avatar
Valery Sizov committed
233 234 235
    render nothing: true
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
236 237
  protected

238
  def selected_target_project
239 240 241 242 243
    if @project.id.to_s == params[:target_project_id] || @project.forked_project_link.nil?
      @project
    else
      @project.forked_project_link.forked_from_project
    end
244 245
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
246
  def merge_request
skv's avatar
skv committed
247
    @merge_request ||= @project.merge_requests.find_by!(iid: params[:id])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
248
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
249

250 251 252 253
  def closes_issues
    @closes_issues ||= @merge_request.closes_issues
  end

254
  def authorize_update_merge_request!
255
    return render_404 unless can?(current_user, :update_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
256 257 258
  end

  def authorize_admin_merge_request!
259
    return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
260
  end
261 262 263 264

  def module_enabled
    return render_404 unless @project.merge_requests_enabled
  end
265 266

  def validates_merge_request
267 268 269
    # If source project was removed (Ex. mr from fork to origin)
    return invalid_mr unless @merge_request.source_project

270 271 272
    # Show git not found page
    # if there is no saved commits between source & target branch
    if @merge_request.commits.blank?
273 274
      # and if target branch doesn't exist
      return invalid_mr unless @merge_request.target_branch_exists?
275

276 277
      # or if source branch doesn't exist
      return invalid_mr unless @merge_request.source_branch_exists?
278
    end
279 280 281
  end

  def define_show_vars
282
    @participants = @merge_request.participants(current_user)
283

284
    # Build a note object for comment form
285
    @note = @project.notes.new(noteable: @merge_request)
286
    @notes = @merge_request.mr_and_commit_notes.nonawards.inc_author.fresh
287
    @discussions = Note.discussions_from_notes(@notes)
288
    @noteable = @merge_request
289

290
    # Get commits from repository
291 292
    # or from cache if already merged
    @commits = @merge_request.commits
293

294
    @merge_request_diff = @merge_request.merge_request_diff
295

296
    @ci_commit = @merge_request.ci_commit
297
    @statuses = @ci_commit.statuses if @ci_commit
298

299 300 301 302
    if @merge_request.locked_long_ago?
      @merge_request.unlock_mr
      @merge_request.close
    end
303 304
  end

305 306 307 308
  def define_widget_vars
    @ci_commit = @merge_request.ci_commit
  end

309 310 311 312 313
  def invalid_mr
    # Render special view for MR with removed source or target branch
    render 'invalid'
  end

314
  def merge_request_params
315
    params.require(:merge_request).permit(
316 317
      :title, :assignee_id, :source_project_id, :source_branch,
      :target_project_id, :target_branch, :milestone_id,
318
      :state_event, :description, :task_num, label_ids: []
319 320
    )
  end
321

322 323 324 325
  def merge_params
    params.permit(:should_remove_source_branch, :commit_message)
  end

326 327 328 329 330
  # Make sure merge requests created before 8.0
  # have head file in refs/merge-requests/
  def ensure_ref_fetched
    @merge_request.ensure_ref_fetched
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
331
end