merge_requests_controller.rb 10.3 KB
Newer Older
1
class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationController
2
  include ToggleSubscriptionAction
3
  include IssuableActions
Douwe Maan's avatar
Douwe Maan committed
4
  include RendersNotes
5
  include RendersCommits
6
  include ToggleAwardEmoji
7
  include IssuableCollections
8

9 10 11
  skip_before_action :merge_request, only: [:index, :bulk_update]
  skip_before_action :ensure_ref_fetched, only: [:index, :bulk_update]

12
  before_action :authorize_update_merge_request!, only: [:close, :edit, :update, :remove_wip, :sort]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13

14 15
  before_action :authenticate_user!, only: [:assign_related_issues]

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
16
  def index
17 18 19
    @collection_type    = "MergeRequest"
    @merge_requests     = merge_requests_collection
    @merge_requests     = @merge_requests.page(params[:page])
20
    @merge_requests     = @merge_requests.preload(merge_request_diff: :merge_request)
21
    @issuable_meta_data = issuable_meta_data(@merge_requests, @collection_type)
22
    @total_pages        = merge_requests_page_count(@merge_requests)
23

24
    return if redirect_out_of_range(@merge_requests, @total_pages)
25

26 27 28 29
    if params[:label_name].present?
      labels_params = { project_id: @project.id, title: params[:label_name] }
      @labels = LabelsFinder.new(current_user, labels_params).execute
    end
Tap's avatar
Tap committed
30

31 32 33 34 35 36 37 38 39 40 41
    @users = []
    if params[:assignee_id].present?
      assignee = User.find_by_id(params[:assignee_id])
      @users.push(assignee) if assignee
    end

    if params[:author_id].present?
      author = User.find_by_id(params[:author_id])
      @users.push(author) if author
    end

42 43 44 45
    respond_to do |format|
      format.html
      format.json do
        render json: {
46
          html: view_to_html_string("projects/merge_requests/_merge_requests"),
47
          labels: @labels.as_json(methods: :text_color)
48 49 50
        }
      end
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
51 52 53
  end

  def show
54 55 56 57 58
    validates_merge_request
    ensure_ref_fetched
    close_merge_request_without_source_project
    check_if_can_be_merged

59 60 61
    # Return if the response has already been rendered
    return if response_body

62
    respond_to do |format|
Fatih Acet's avatar
Fatih Acet committed
63
      format.html do
64 65 66 67 68 69
        # Build a note object for comment form
        @note = @project.notes.new(noteable: @merge_request)

        @noteable = @merge_request
        @commits_count = @merge_request.commits_count

70 71 72
        @discussions = @merge_request.discussions
        @notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes), @noteable)

73 74 75
        labels

        set_pipeline_variables
76 77 78 79 80

        # n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37432
        Gitlab::GitalyClient.allow_n_plus_1_calls do
          render
        end
Fatih Acet's avatar
Fatih Acet committed
81
      end
82

Douwe Maan's avatar
Douwe Maan committed
83
      format.json do
84 85
        Gitlab::PollingInterval.set_header(response, interval: 10_000)

Fatih Acet's avatar
Fatih Acet committed
86
        render json: serializer.represent(@merge_request, basic: params[:basic])
Douwe Maan's avatar
Douwe Maan committed
87 88
      end

89
      format.patch  do
Douwe Maan's avatar
Douwe Maan committed
90 91 92
        return render_404 unless @merge_request.diff_refs

        send_git_patch @project.repository, @merge_request.diff_refs
93
      end
Douwe Maan's avatar
Douwe Maan committed
94

95
      format.diff do
Douwe Maan's avatar
Douwe Maan committed
96
        return render_404 unless @merge_request.diff_refs
97

Douwe Maan's avatar
Douwe Maan committed
98
        send_git_diff @project.repository, @merge_request.diff_refs
99
      end
100
    end
randx's avatar
randx committed
101 102
  end

103
  def commits
104 105
    # Get commits from repository
    # or from cache if already merged
106
    @commits = prepare_commits_for_rendering(@merge_request.commits)
107 108
    @note_counts = Note.where(commit_id: @commits.map(&:id))
      .group(:commit_id).count
109

110
    render json: { html: view_to_html_string('projects/merge_requests/_commits') }
111 112
  end

113
  def pipelines
114
    @pipelines = @merge_request.all_pipelines
115

116
    Gitlab::PollingInterval.set_header(response, interval: 10_000)
117

118 119 120 121 122 123 124 125
    render json: {
      pipelines: PipelineSerializer
        .new(project: @project, current_user: @current_user)
        .represent(@pipelines),
      count: {
        all: @pipelines.count
      }
    }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
126 127
  end

128
  def edit
129
    define_edit_vars
130 131
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
132
  def update
133
    @merge_request = ::MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
134

135 136 137 138 139
    respond_to do |format|
      format.html do
        if @merge_request.valid?
          redirect_to([@merge_request.target_project.namespace.becomes(Namespace), @merge_request.target_project, @merge_request])
        else
140 141
          define_edit_vars

142
          render :edit
143
        end
144
      end
145 146

      format.json do
147
        render json: @merge_request.to_json(include: { milestone: {}, assignee: { only: [:name, :username], methods: [:avatar_url] }, labels: { methods: :text_color } }, methods: [:task_status, :task_status_short])
148
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
149
    end
150
  rescue ActiveRecord::StaleObjectError
151 152
    define_edit_vars if request.format.html?

153
    render_conflict_response
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
154 155
  end

156
  def remove_wip
157
    @merge_request = ::MergeRequests::UpdateService
Fatih Acet's avatar
Fatih Acet committed
158 159
      .new(project, current_user, wip_event: 'unwip')
      .execute(@merge_request)
160

Fatih Acet's avatar
Fatih Acet committed
161
    render json: serializer.represent(@merge_request)
162 163
  end

Fatih Acet's avatar
Fatih Acet committed
164 165
  def commit_change_content
    render partial: 'projects/merge_requests/widget/commit_change_content', layout: false
Valery Sizov's avatar
Valery Sizov committed
166 167
  end

168 169
  def cancel_merge_when_pipeline_succeeds
    unless @merge_request.can_cancel_merge_when_pipeline_succeeds?(current_user)
170 171
      return access_denied!
    end
172

173
    ::MergeRequests::MergeWhenPipelineSucceedsService
174 175
      .new(@project, current_user)
      .cancel(@merge_request)
Fatih Acet's avatar
Fatih Acet committed
176 177

    render json: serializer.represent(@merge_request)
178 179
  end

180
  def merge
181
    return access_denied! unless @merge_request.can_be_merged_by?(current_user)
182

Fatih Acet's avatar
Fatih Acet committed
183
    status = merge!
184

Fatih Acet's avatar
Fatih Acet committed
185 186
    if @merge_request.merge_error
      render json: { status: status, merge_error: @merge_request.merge_error }
187
    else
Fatih Acet's avatar
Fatih Acet committed
188
      render json: { status: status }
189
    end
randx's avatar
randx committed
190 191
  end

192
  def assign_related_issues
193
    result = ::MergeRequests::AssignIssuesService.new(project, current_user, merge_request: @merge_request).execute
194

195 196 197 198 199 200 201
    case result[:count]
    when 0
      flash[:error] = "Failed to assign you issues related to the merge request"
    when 1
      flash[:notice] = "1 issue has been assigned to you"
    else
      flash[:notice] = "#{result[:count]} issues have been assigned to you"
202
    end
203 204

    redirect_to(merge_request_path(@merge_request))
205 206
  end

207
  def pipeline_status
208
    render json: PipelineSerializer
Fatih Acet's avatar
Fatih Acet committed
209
      .new(project: @project, current_user: @current_user)
Shinya Maeda's avatar
Shinya Maeda committed
210
      .represent_status(@merge_request.head_pipeline)
211 212
  end

213
  def ci_environments_status
Z.J. van de Weg's avatar
Z.J. van de Weg committed
214
    environments =
Z.J. van de Weg's avatar
Z.J. van de Weg committed
215
      begin
Douwe Maan's avatar
Douwe Maan committed
216
        @merge_request.environments_for(current_user).map do |environment|
217
          project = environment.project
Z.J. van de Weg's avatar
Z.J. van de Weg committed
218 219
          deployment = environment.first_deployment_for(@merge_request.diff_head_commit)

220
          stop_url =
Kamil Trzcinski's avatar
Kamil Trzcinski committed
221
            if environment.stop_action? && can?(current_user, :create_deployment, environment)
222
              stop_project_environment_path(project, environment)
223 224
            end

Fatih Acet's avatar
Fatih Acet committed
225 226
          metrics_url =
            if can?(current_user, :read_environment, environment) && environment.has_metrics?
227
              metrics_project_environment_deployment_path(environment.project, environment, deployment)
Fatih Acet's avatar
Fatih Acet committed
228
            end
229 230

          metrics_monitoring_url =
231 232 233
            if can?(current_user, :read_environment, environment)
              environment_metrics_path(environment)
            end
Fatih Acet's avatar
Fatih Acet committed
234

Z.J. van de Weg's avatar
Z.J. van de Weg committed
235
          {
Z.J. van de Weg's avatar
Z.J. van de Weg committed
236
            id: environment.id,
Z.J. van de Weg's avatar
Z.J. van de Weg committed
237
            name: environment.name,
238
            url: project_environment_path(project, environment),
Fatih Acet's avatar
Fatih Acet committed
239
            metrics_url: metrics_url,
240
            metrics_monitoring_url: metrics_monitoring_url,
241
            stop_url: stop_url,
Z.J. van de Weg's avatar
Z.J. van de Weg committed
242
            external_url: environment.external_url,
Z.J. van de Weg's avatar
Z.J. van de Weg committed
243 244 245
            external_url_formatted: environment.formatted_external_url,
            deployed_at: deployment.try(:created_at),
            deployed_at_formatted: deployment.try(:formatted_deployment_time)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
246 247
          }
        end.compact
248
      end
249

Z.J. van de Weg's avatar
Z.J. van de Weg committed
250
    render json: environments
251 252
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
253 254
  protected

255
  alias_method :subscribable_resource, :merge_request
256
  alias_method :issuable, :merge_request
257
  alias_method :awardable, :merge_request
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
258

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

  def authorize_admin_merge_request!
264
    return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
265
  end
266

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

276
  def invalid_mr
277
    # Render special view for MR with removed target branch
278 279 280
    render 'invalid'
  end

281
  def merge_params
282 283 284 285 286
    params.permit(merge_params_attributes)
  end

  def merge_params_attributes
    [:should_remove_source_branch, :commit_message]
287 288
  end

289 290
  def merge_when_pipeline_succeeds_active?
    params[:merge_when_pipeline_succeeds].present? &&
291
      @merge_request.head_pipeline && @merge_request.head_pipeline.active?
292
  end
293

294 295 296 297 298
  def close_merge_request_without_source_project
    if !@merge_request.source_project && @merge_request.open?
      @merge_request.close
    end
  end
Fatih Acet's avatar
Fatih Acet committed
299 300 301

  private

302 303 304 305
  def check_if_can_be_merged
    @merge_request.check_if_can_be_merged
  end

Fatih Acet's avatar
Fatih Acet committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  def merge!
    # Disable the CI check if merge_when_pipeline_succeeds is enabled since we have
    # to wait until CI completes to know
    unless @merge_request.mergeable?(skip_ci_check: merge_when_pipeline_succeeds_active?)
      return :failed
    end

    return :sha_mismatch if params[:sha] != @merge_request.diff_head_sha

    @merge_request.update(merge_error: nil)

    if params[:merge_when_pipeline_succeeds].present?
      return :failed unless @merge_request.head_pipeline

      if @merge_request.head_pipeline.active?
321
        ::MergeRequests::MergeWhenPipelineSucceedsService
Fatih Acet's avatar
Fatih Acet committed
322 323 324 325 326 327 328
          .new(@project, current_user, merge_params)
          .execute(@merge_request)

        :merge_when_pipeline_succeeds
      elsif @merge_request.head_pipeline.success?
        # This can be triggered when a user clicks the auto merge button while
        # the tests finish at about the same time
329
        @merge_request.merge_async(current_user.id, params)
Fatih Acet's avatar
Fatih Acet committed
330 331 332 333 334 335

        :success
      else
        :failed
      end
    else
336
      @merge_request.merge_async(current_user.id, params)
Fatih Acet's avatar
Fatih Acet committed
337 338 339 340 341 342 343 344

      :success
    end
  end

  def serializer
    MergeRequestSerializer.new(current_user: current_user, project: merge_request.project)
  end
345 346 347 348 349 350

  def define_edit_vars
    @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
351
end