GitLab steht wegen Wartungsarbeiten am Montag, den 10. Mai, zwischen 17:00 und 19:00 Uhr nicht zur Verfügung.

merge_requests_controller.rb 9.68 KB
Newer Older
1
class Projects::MergeRequestsController < Projects::ApplicationController
2
  before_action :module_enabled
3
  before_action :merge_request, only: [
Douwe Maan's avatar
Douwe Maan committed
4
    :edit, :update, :show, :diffs, :commits, :builds, :merge, :merge_check,
5
    :ci_status, :toggle_subscription, :cancel_merge_when_build_succeeds
6
  ]
Douwe Maan's avatar
Douwe Maan committed
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, :merge_check]
Douwe Maan's avatar
Douwe Maan committed
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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

Douwe Maan's avatar
Douwe Maan committed
83 84 85 86 87 88 89
  def builds
    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
90
  def new
91 92 93 94 95 96 97 98 99 100 101
    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
Douwe Maan's avatar
Douwe Maan committed
102
    @commits = @merge_request.compare_commits.reverse
103 104
    @commit = @merge_request.last_commit
    @first_commit = @merge_request.first_commit
105
    @diffs = @merge_request.compare_diffs
Douwe Maan's avatar
Douwe Maan committed
106 107 108 109

    @ci_commit = @merge_request.ci_commit
    @statuses = @ci_commit.statuses if @ci_commit

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

  def create
115
    @target_branches ||= []
116
    @merge_request = MergeRequests::CreateService.new(project, current_user, merge_request_params).execute
117 118

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

Douwe Maan's avatar
Douwe Maan committed
127 128 129 130 131 132
  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
133
  def update
134
    @merge_request = MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
135

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

155
  def merge_check
156
    @merge_request.check_if_can_be_merged
157

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

161
  def cancel_merge_when_build_succeeds
162
    return access_denied! unless @merge_request.can_cancel_merge_when_build_succeeds?(current_user)
163

164
    MergeRequests::MergeWhenBuildSucceedsService.new(@project, current_user).cancel(@merge_request)
165 166
  end

167
  def merge
168
    return access_denied! unless @merge_request.can_be_merged_by?(current_user)
169

170 171 172 173 174 175 176
    unless @merge_request.mergeable?
      @status = :failed
      return
    end

    @merge_request.update(merge_error: nil)

Douwe Maan's avatar
Douwe Maan committed
177
    if params[:merge_when_build_succeeds].present? && @merge_request.ci_commit && @merge_request.ci_commit.active?
178
      MergeRequests::MergeWhenBuildSucceedsService.new(@project, current_user, merge_params)
179 180
                                                      .execute(@merge_request)
      @status = :merge_when_build_succeeds
181
    else
182 183
      MergeWorker.perform_async(@merge_request.id, current_user.id, params)
      @status = :success
184
    end
randx's avatar
randx committed
185 186
  end

187
  def branch_from
188
    #This is always source
Izaak Alpert's avatar
Izaak Alpert committed
189
    @source_project = @merge_request.nil? ? @project : @merge_request.source_project
190
    @commit = @repository.commit(params[:ref]) if params[:ref].present?
191 192 193
  end

  def branch_to
194
    @target_project = selected_target_project
195
    @commit = @target_project.commit(params[:ref]) if params[:ref].present?
196 197
  end

198 199
  def update_branches
    @target_project = selected_target_project
Izaak Alpert's avatar
Izaak Alpert committed
200
    @target_branches = @target_project.repository.branch_names
201 202 203 204

    respond_to do |format|
      format.js
    end
205 206
  end

207
  def ci_status
208
    ci_service = @merge_request.source_project.ci_service
Valery Sizov's avatar
Valery Sizov committed
209
    status = ci_service.commit_status(merge_request.last_commit.sha, merge_request.source_branch)
210 211

    if ci_service.respond_to?(:commit_coverage)
Valery Sizov's avatar
Valery Sizov committed
212
      coverage = ci_service.commit_coverage(merge_request.last_commit.sha, merge_request.source_branch)
213 214 215 216 217 218
    end

    response = {
      status: status,
      coverage: coverage
    }
219 220 221 222

    render json: response
  end

Valery Sizov's avatar
tests  
Valery Sizov committed
223 224
  def toggle_subscription
    @merge_request.toggle_subscription(current_user)
225

Valery Sizov's avatar
Valery Sizov committed
226 227 228
    render nothing: true
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
229 230
  protected

231
  def selected_target_project
232 233 234 235 236
    if @project.id.to_s == params[:target_project_id] || @project.forked_project_link.nil?
      @project
    else
      @project.forked_project_link.forked_from_project
    end
237 238
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
239
  def merge_request
skv's avatar
skv committed
240
    @merge_request ||= @project.merge_requests.find_by!(iid: params[:id])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
241
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
242

243 244 245 246
  def closes_issues
    @closes_issues ||= @merge_request.closes_issues
  end

247
  def authorize_update_merge_request!
248
    return render_404 unless can?(current_user, :update_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
249 250 251
  end

  def authorize_admin_merge_request!
252
    return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
253
  end
254 255 256 257

  def module_enabled
    return render_404 unless @project.merge_requests_enabled
  end
258 259

  def validates_merge_request
260 261 262
    # If source project was removed (Ex. mr from fork to origin)
    return invalid_mr unless @merge_request.source_project

263 264 265
    # Show git not found page
    # if there is no saved commits between source & target branch
    if @merge_request.commits.blank?
266 267
      # and if target branch doesn't exist
      return invalid_mr unless @merge_request.target_branch_exists?
268

269 270
      # or if source branch doesn't exist
      return invalid_mr unless @merge_request.source_branch_exists?
271
    end
272 273 274 275
  end

  def define_show_vars
    # Build a note object for comment form
276
    @note = @project.notes.new(noteable: @merge_request)
277
    @notes = @merge_request.mr_and_commit_notes.nonawards.inc_author.fresh
278
    @discussions = Note.discussions_from_notes(@notes)
279
    @noteable = @merge_request
280

281
    # Get commits from repository
282 283
    # or from cache if already merged
    @commits = @merge_request.commits
284

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
285
    @merge_request_diff = @merge_request.merge_request_diff
286

Douwe Maan's avatar
Douwe Maan committed
287
    @ci_commit = @merge_request.ci_commit
Douwe Maan's avatar
Douwe Maan committed
288
    @statuses = @ci_commit.statuses if @ci_commit
Douwe Maan's avatar
Douwe Maan committed
289

290 291 292 293
    if @merge_request.locked_long_ago?
      @merge_request.unlock_mr
      @merge_request.close
    end
294 295
  end

296 297
  def define_widget_vars
    @ci_commit = @merge_request.ci_commit
298
    closes_issues
299 300
  end

301 302 303 304 305
  def invalid_mr
    # Render special view for MR with removed source or target branch
    render 'invalid'
  end

306
  def merge_request_params
307
    params.require(:merge_request).permit(
308 309
      :title, :assignee_id, :source_project_id, :source_branch,
      :target_project_id, :target_branch, :milestone_id,
310
      :state_event, :description, :task_num, label_ids: []
311 312
    )
  end
313

314 315 316 317
  def merge_params
    params.permit(:should_remove_source_branch, :commit_message)
  end

318 319 320 321 322
  # 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
323
end