merge_requests_controller.rb 9.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
  skip_before_action :merge_request, only: [:index, :bulk_update]
10
  before_action :authorize_update_issuable!, only: [:close, :edit, :update, :remove_wip, :sort]
11
  before_action :set_issuables_index, only: [:index]
12 13
  before_action :authenticate_user!, only: [:assign_related_issues]

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14
  def index
15
    @merge_requests = @issuables
16

17 18 19 20
    respond_to do |format|
      format.html
      format.json do
        render json: {
21
          html: view_to_html_string("projects/merge_requests/_merge_requests"),
22
          labels: @labels.as_json(methods: :text_color)
23 24 25
        }
      end
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
26 27 28
  end

  def show
29 30 31 32
    validates_merge_request
    close_merge_request_without_source_project
    check_if_can_be_merged

33 34 35
    # Return if the response has already been rendered
    return if response_body

36
    respond_to do |format|
Fatih Acet's avatar
Fatih Acet committed
37
      format.html do
38 39 40 41 42 43
        # Build a note object for comment form
        @note = @project.notes.new(noteable: @merge_request)

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

44 45 46
        @discussions = @merge_request.discussions
        @notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes), @noteable)

47 48 49
        labels

        set_pipeline_variables
50 51 52 53 54

        # 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
55
      end
56

Douwe Maan's avatar
Douwe Maan committed
57
      format.json do
58 59
        Gitlab::PollingInterval.set_header(response, interval: 10_000)

Eric Eastwood's avatar
Eric Eastwood committed
60
        render json: serializer.represent(@merge_request, serializer: params[:serializer])
Douwe Maan's avatar
Douwe Maan committed
61 62
      end

63
      format.patch  do
Douwe Maan's avatar
Douwe Maan committed
64 65 66
        return render_404 unless @merge_request.diff_refs

        send_git_patch @project.repository, @merge_request.diff_refs
67
      end
Douwe Maan's avatar
Douwe Maan committed
68

69
      format.diff do
Douwe Maan's avatar
Douwe Maan committed
70
        return render_404 unless @merge_request.diff_refs
71

Douwe Maan's avatar
Douwe Maan committed
72
        send_git_diff @project.repository, @merge_request.diff_refs
73
      end
74
    end
randx's avatar
randx committed
75 76
  end

77
  def commits
78 79
    # Get commits from repository
    # or from cache if already merged
80 81
    @commits =
      prepare_commits_for_rendering(@merge_request.commits.with_pipeline_status)
82

83
    render json: { html: view_to_html_string('projects/merge_requests/_commits') }
84 85
  end

86
  def pipelines
87
    @pipelines = @merge_request.all_pipelines
88

89
    Gitlab::PollingInterval.set_header(response, interval: 10_000)
90

91 92 93 94 95 96 97 98
    render json: {
      pipelines: PipelineSerializer
        .new(project: @project, current_user: @current_user)
        .represent(@pipelines),
      count: {
        all: @pipelines.count
      }
    }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
99 100
  end

101
  def edit
102
    define_edit_vars
103 104
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
105
  def update
106
    @merge_request = ::MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
107

108 109 110 111 112
    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
113 114
          define_edit_vars

115
          render :edit
116
        end
117
      end
118 119

      format.json do
120
        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])
121
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
122
    end
123
  rescue ActiveRecord::StaleObjectError
124 125
    define_edit_vars if request.format.html?

126
    render_conflict_response
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
127 128
  end

129
  def remove_wip
130
    @merge_request = ::MergeRequests::UpdateService
Fatih Acet's avatar
Fatih Acet committed
131 132
      .new(project, current_user, wip_event: 'unwip')
      .execute(@merge_request)
133

134
    render json: serialize_widget(@merge_request)
135 136
  end

Fatih Acet's avatar
Fatih Acet committed
137 138
  def commit_change_content
    render partial: 'projects/merge_requests/widget/commit_change_content', layout: false
Valery Sizov's avatar
Valery Sizov committed
139 140
  end

141 142
  def cancel_merge_when_pipeline_succeeds
    unless @merge_request.can_cancel_merge_when_pipeline_succeeds?(current_user)
143 144
      return access_denied!
    end
145

146
    ::MergeRequests::MergeWhenPipelineSucceedsService
147 148
      .new(@project, current_user)
      .cancel(@merge_request)
Fatih Acet's avatar
Fatih Acet committed
149

150
    render json: serialize_widget(@merge_request)
151 152
  end

153
  def merge
154
    return access_denied! unless @merge_request.can_be_merged_by?(current_user)
155

Fatih Acet's avatar
Fatih Acet committed
156
    status = merge!
157

Fatih Acet's avatar
Fatih Acet committed
158 159
    if @merge_request.merge_error
      render json: { status: status, merge_error: @merge_request.merge_error }
160
    else
Fatih Acet's avatar
Fatih Acet committed
161
      render json: { status: status }
162
    end
randx's avatar
randx committed
163 164
  end

165
  def assign_related_issues
166
    result = ::MergeRequests::AssignIssuesService.new(project, current_user, merge_request: @merge_request).execute
167

168 169 170 171 172 173 174
    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"
175
    end
176 177

    redirect_to(merge_request_path(@merge_request))
178 179
  end

180
  def pipeline_status
181
    render json: PipelineSerializer
Fatih Acet's avatar
Fatih Acet committed
182
      .new(project: @project, current_user: @current_user)
Shinya Maeda's avatar
Shinya Maeda committed
183
      .represent_status(@merge_request.head_pipeline)
184 185
  end

186
  def ci_environments_status
Z.J. van de Weg's avatar
Z.J. van de Weg committed
187
    environments =
Z.J. van de Weg's avatar
Z.J. van de Weg committed
188
      begin
Douwe Maan's avatar
Douwe Maan committed
189
        @merge_request.environments_for(current_user).map do |environment|
190
          project = environment.project
Z.J. van de Weg's avatar
Z.J. van de Weg committed
191 192
          deployment = environment.first_deployment_for(@merge_request.diff_head_commit)

193
          stop_url =
Kamil Trzcinski's avatar
Kamil Trzcinski committed
194
            if environment.stop_action? && can?(current_user, :create_deployment, environment)
195
              stop_project_environment_path(project, environment)
196 197
            end

Fatih Acet's avatar
Fatih Acet committed
198 199
          metrics_url =
            if can?(current_user, :read_environment, environment) && environment.has_metrics?
200
              metrics_project_environment_deployment_path(environment.project, environment, deployment)
Fatih Acet's avatar
Fatih Acet committed
201
            end
202 203

          metrics_monitoring_url =
204 205 206
            if can?(current_user, :read_environment, environment)
              environment_metrics_path(environment)
            end
Fatih Acet's avatar
Fatih Acet committed
207

Z.J. van de Weg's avatar
Z.J. van de Weg committed
208
          {
Z.J. van de Weg's avatar
Z.J. van de Weg committed
209
            id: environment.id,
Z.J. van de Weg's avatar
Z.J. van de Weg committed
210
            name: environment.name,
211
            url: project_environment_path(project, environment),
Fatih Acet's avatar
Fatih Acet committed
212
            metrics_url: metrics_url,
213
            metrics_monitoring_url: metrics_monitoring_url,
214
            stop_url: stop_url,
Z.J. van de Weg's avatar
Z.J. van de Weg committed
215
            external_url: environment.external_url,
Z.J. van de Weg's avatar
Z.J. van de Weg committed
216 217 218
            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
219 220
          }
        end.compact
221
      end
222

Z.J. van de Weg's avatar
Z.J. van de Weg committed
223
    render json: environments
224 225
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
226 227
  protected

228
  alias_method :subscribable_resource, :merge_request
229
  alias_method :issuable, :merge_request
230
  alias_method :awardable, :merge_request
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
231

232
  def validates_merge_request
233 234
    # Show git not found page
    # if there is no saved commits between source & target branch
235
    if @merge_request.has_no_commits?
236 237
      # and if target branch doesn't exist
      return invalid_mr unless @merge_request.target_branch_exists?
238
    end
239 240
  end

241
  def invalid_mr
242
    # Render special view for MR with removed target branch
243 244 245
    render 'invalid'
  end

246
  def merge_params
247 248 249 250 251
    params.permit(merge_params_attributes)
  end

  def merge_params_attributes
    [:should_remove_source_branch, :commit_message]
252 253
  end

254 255
  def merge_when_pipeline_succeeds_active?
    params[:merge_when_pipeline_succeeds].present? &&
256
      @merge_request.head_pipeline && @merge_request.head_pipeline.active?
257
  end
258

259 260 261 262 263
  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
264 265 266

  private

267 268 269 270
  def check_if_can_be_merged
    @merge_request.check_if_can_be_merged
  end

Fatih Acet's avatar
Fatih Acet committed
271 272 273 274 275 276 277 278 279 280 281 282
  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?
283
      return :failed unless @merge_request.actual_head_pipeline
Fatih Acet's avatar
Fatih Acet committed
284

285
      if @merge_request.actual_head_pipeline.active?
286
        ::MergeRequests::MergeWhenPipelineSucceedsService
Fatih Acet's avatar
Fatih Acet committed
287 288 289 290
          .new(@project, current_user, merge_params)
          .execute(@merge_request)

        :merge_when_pipeline_succeeds
291
      elsif @merge_request.actual_head_pipeline.success?
Fatih Acet's avatar
Fatih Acet committed
292 293
        # This can be triggered when a user clicks the auto merge button while
        # the tests finish at about the same time
294
        @merge_request.merge_async(current_user.id, params)
Fatih Acet's avatar
Fatih Acet committed
295 296 297 298 299 300

        :success
      else
        :failed
      end
    else
301
      @merge_request.merge_async(current_user.id, params)
Fatih Acet's avatar
Fatih Acet committed
302 303 304 305 306

      :success
    end
  end

307 308 309 310
  def serialize_widget(merge_request)
    serializer.represent(merge_request, serializer: 'widget')
  end

Fatih Acet's avatar
Fatih Acet committed
311 312 313
  def serializer
    MergeRequestSerializer.new(current_user: current_user, project: merge_request.project)
  end
314 315 316 317 318 319

  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
320 321 322 323 324

  def set_issuables_index
    @finder_type = MergeRequestsFinder
    super
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
325
end