BigW Consortium Gitlab

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

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

11
  before_action :authorize_update_merge_request!, only: [:close, :edit, :update, :remove_wip, :sort]
12

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

15
  def index
16 17 18
    @collection_type    = "MergeRequest"
    @merge_requests     = merge_requests_collection
    @merge_requests     = @merge_requests.page(params[:page])
19
    @merge_requests     = @merge_requests.preload(merge_request_diff: :merge_request)
20
    @issuable_meta_data = issuable_meta_data(@merge_requests, @collection_type)
21

22
    if @merge_requests.out_of_range? && @merge_requests.total_pages != 0
23
      return redirect_to url_for(params.merge(page: @merge_requests.total_pages, only_path: true))
24
    end
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 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
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
    respond_to do |format|
60
      format.html do
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        # Build a note object for comment form
        @note = @project.notes.new(noteable: @merge_request)

        @discussions = @merge_request.discussions
        @notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes))

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

        if @merge_request.locked_long_ago?
          @merge_request.unlock_mr
          @merge_request.close
        end

        labels

        set_pipeline_variables
78
      end
79

Douwe Maan committed
80
      format.json do
81 82
        Gitlab::PollingInterval.set_header(response, interval: 10_000)

83
        render json: serializer.represent(@merge_request, basic: params[:basic])
Douwe Maan committed
84 85
      end

86
      format.patch  do
Douwe Maan committed
87 88 89
        return render_404 unless @merge_request.diff_refs

        send_git_patch @project.repository, @merge_request.diff_refs
90
      end
Douwe Maan committed
91

92
      format.diff do
Douwe Maan committed
93
        return render_404 unless @merge_request.diff_refs
94

Douwe Maan committed
95
        send_git_diff @project.repository, @merge_request.diff_refs
96
      end
97
    end
randx committed
98 99
  end

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

107
    render json: { html: view_to_html_string('projects/merge_requests/_commits') }
108 109
  end

110
  def pipelines
111
    @pipelines = @merge_request.all_pipelines
112

113
    Gitlab::PollingInterval.set_header(response, interval: 10_000)
114

115 116 117
    render json: PipelineSerializer
      .new(project: @project, current_user: @current_user)
      .represent(@pipelines)
118 119
  end

120
  def edit
121
    define_edit_vars
122 123
  end

124
  def update
125
    @merge_request = ::MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
126

127 128 129 130 131
    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
132 133
          define_edit_vars

134
          render :edit
135
        end
136
      end
137 138

      format.json do
139
        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])
140
      end
141
    end
142
  rescue ActiveRecord::StaleObjectError
143 144
    define_edit_vars if request.format.html?

145
    render_conflict_response
146 147
  end

148
  def remove_wip
149
    @merge_request = ::MergeRequests::UpdateService
150 151
      .new(project, current_user, wip_event: 'unwip')
      .execute(@merge_request)
152

153
    render json: serializer.represent(@merge_request)
154 155
  end

156 157
  def commit_change_content
    render partial: 'projects/merge_requests/widget/commit_change_content', layout: false
158 159
  end

160 161
  def cancel_merge_when_pipeline_succeeds
    unless @merge_request.can_cancel_merge_when_pipeline_succeeds?(current_user)
162 163
      return access_denied!
    end
164

165
    ::MergeRequests::MergeWhenPipelineSucceedsService
166 167
      .new(@project, current_user)
      .cancel(@merge_request)
168 169

    render json: serializer.represent(@merge_request)
170 171
  end

172
  def merge
173
    return access_denied! unless @merge_request.can_be_merged_by?(current_user)
174

175
    status = merge!
176

177 178
    if @merge_request.merge_error
      render json: { status: status, merge_error: @merge_request.merge_error }
179
    else
180
      render json: { status: status }
181
    end
randx committed
182 183
  end

184
  def assign_related_issues
185
    result = ::MergeRequests::AssignIssuesService.new(project, current_user, merge_request: @merge_request).execute
186

187 188 189 190 191 192 193
    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"
194
    end
195 196

    redirect_to(merge_request_path(@merge_request))
197 198
  end

199
  def pipeline_status
200
    render json: PipelineSerializer
201
      .new(project: @project, current_user: @current_user)
Shinya Maeda committed
202
      .represent_status(@merge_request.head_pipeline)
203 204
  end

205
  def ci_environments_status
206
    environments =
207
      begin
Douwe Maan committed
208
        @merge_request.environments_for(current_user).map do |environment|
209
          project = environment.project
210 211
          deployment = environment.first_deployment_for(@merge_request.diff_head_commit)

212
          stop_url =
Kamil Trzcinski committed
213
            if environment.stop_action? && can?(current_user, :create_deployment, environment)
214
              stop_project_environment_path(project, environment)
215 216
            end

217 218
          metrics_url =
            if can?(current_user, :read_environment, environment) && environment.has_metrics?
219
              metrics_project_environment_deployment_path(environment.project, environment, deployment)
220 221
            end

222
          {
223
            id: environment.id,
224
            name: environment.name,
225
            url: project_environment_path(project, environment),
226
            metrics_url: metrics_url,
227
            stop_url: stop_url,
228
            external_url: environment.external_url,
229 230 231
            external_url_formatted: environment.formatted_external_url,
            deployed_at: deployment.try(:created_at),
            deployed_at_formatted: deployment.try(:formatted_deployment_time)
232 233
          }
        end.compact
234
      end
235

236
    render json: environments
237 238
  end

239 240
  protected

241
  alias_method :subscribable_resource, :merge_request
242
  alias_method :issuable, :merge_request
243
  alias_method :awardable, :merge_request
244

245
  def authorize_update_merge_request!
246
    return render_404 unless can?(current_user, :update_merge_request, @merge_request)
247 248 249
  end

  def authorize_admin_merge_request!
250
    return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
251
  end
252

253
  def validates_merge_request
254 255
    # Show git not found page
    # if there is no saved commits between source & target branch
256
    if @merge_request.has_no_commits?
257 258
      # and if target branch doesn't exist
      return invalid_mr unless @merge_request.target_branch_exists?
259
    end
260 261
  end

262
  def invalid_mr
263
    # Render special view for MR with removed target branch
264 265 266
    render 'invalid'
  end

267
  def merge_params
268 269 270 271 272
    params.permit(merge_params_attributes)
  end

  def merge_params_attributes
    [:should_remove_source_branch, :commit_message]
273 274
  end

275 276
  def merge_when_pipeline_succeeds_active?
    params[:merge_when_pipeline_succeeds].present? &&
277
      @merge_request.head_pipeline && @merge_request.head_pipeline.active?
278
  end
279

280 281 282 283 284
  def close_merge_request_without_source_project
    if !@merge_request.source_project && @merge_request.open?
      @merge_request.close
    end
  end
285 286 287

  private

288 289 290 291
  def check_if_can_be_merged
    @merge_request.check_if_can_be_merged
  end

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  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?
307
        ::MergeRequests::MergeWhenPipelineSucceedsService
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
          .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
        MergeWorker.perform_async(@merge_request.id, current_user.id, params)

        :success
      else
        :failed
      end
    else
      MergeWorker.perform_async(@merge_request.id, current_user.id, params)

      :success
    end
  end

  def serializer
    MergeRequestSerializer.new(current_user: current_user, project: merge_request.project)
  end
331 332 333 334 335 336

  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
337
end