Class: Projects::MergeRequestsController

Inherits:
Projects::MergeRequests::ApplicationController
  • Object
show all
Includes:
DiffHelper, Gitlab::Cache::Helpers, IssuableActions, IssuableCollections, RecordUserLastActivity, RendersAssignees, RendersCommits, RendersNotes, SourcegraphDecorator, ToggleAwardEmoji, ToggleSubscriptionAction
Defined in:
app/controllers/projects/merge_requests_controller.rb

Constant Summary

Constants included from Gitlab::Cache::Helpers

Gitlab::Cache::Helpers::DEFAULT_EXPIRY

Constants included from CookiesHelper

CookiesHelper::COOKIE_TYPE_ENCRYPTED, CookiesHelper::COOKIE_TYPE_PERMANENT

Instance Method Summary collapse

Methods included from Gitlab::Cache::Helpers

#cache, #render_cached

Methods included from DiffHelper

#apply_diff_view_cookie!, #collapsed_diff_url, #conflicts, #diff_file_blob_raw_path, #diff_file_blob_raw_url, #diff_file_html_data, #diff_file_old_blob_raw_path, #diff_file_old_blob_raw_url, #diff_file_stats_data, #diff_line_content, #diff_link_number, #diff_match_line, #diff_nomappinginraw_line, #diff_options, #diff_view, #diffs_expanded?, #editable_diff?, #inline_diff_btn, #mark_inline_diffs, #parallel_diff_btn, #parallel_diff_discussions, #render_fork_suggestion, #render_overflow_warning?, #show_only_context_commits?, #submodule_diff_compare_link, #submodule_link

Methods included from RecordUserLastActivity

#set_user_last_activity

Methods included from CookiesHelper

#set_secure_cookie

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from SortingPreference

#default_sort_order, #legacy_sort_cookie_name, #set_sort_order

Methods included from SortingHelper

#admin_groups_sort_options_hash, #admin_users_sort_options, #audit_logs_sort_order_hash, #branches_sort_options_hash, #can_sort_by_issue_weight?, #due_date_option, #forks_reverse_sort_options_hash, #forks_sort_direction_button, #forks_sort_options_hash, #groups_sort_options_hash, #issuable_reverse_sort_order_hash, #issuable_sort_direction_button, #issuable_sort_option_overrides, #issuable_sort_option_title, #issuable_sort_options, #label_priority_option, #label_sort_options_hash, #merged_option, #milestones_sort_options_hash, #packages_reverse_sort_order_hash, #packages_sort_direction_button, #packages_sort_option_title, #packages_sort_options_hash, #popularity_option, #project_sort_direction_button, #projects_reverse_sort_options_hash, #projects_sort_option_titles, #projects_sort_options_hash, #relative_position_option, #runners_sort_options_hash, #sort_direction_button, #sort_direction_icon, #sort_options_hash, #sortable_item, #starrers_sort_options_hash, #subgroups_sort_options_hash, #tags_sort_options_hash, #title_option, #users_sort_options_hash

Methods included from SortingTitlesValuesHelper

#sort_title_closed_date, #sort_title_closed_earlier, #sort_title_closed_recently, #sort_title_contacted_date, #sort_title_created_date, #sort_title_downvotes, #sort_title_due_date, #sort_title_due_date_later, #sort_title_due_date_soon, #sort_title_expire_date, #sort_title_label_priority, #sort_title_largest_group, #sort_title_largest_repo, #sort_title_latest_activity, #sort_title_merged_date, #sort_title_merged_earlier, #sort_title_merged_recently, #sort_title_milestone, #sort_title_milestone_later, #sort_title_milestone_soon, #sort_title_most_stars, #sort_title_name, #sort_title_name_asc, #sort_title_name_desc, #sort_title_oldest_activity, #sort_title_oldest_created, #sort_title_oldest_last_activity, #sort_title_oldest_signin, #sort_title_oldest_starred, #sort_title_oldest_updated, #sort_title_popularity, #sort_title_priority, #sort_title_recently_created, #sort_title_recently_last_activity, #sort_title_recently_signin, #sort_title_recently_starred, #sort_title_recently_updated, #sort_title_relative_position, #sort_title_size, #sort_title_stars, #sort_title_start_date_later, #sort_title_start_date_soon, #sort_title_title, #sort_title_upvotes, #sort_value_closed_date, #sort_value_closed_earlier, #sort_value_closed_recently, #sort_value_contacted_date, #sort_value_created_date, #sort_value_downvotes, #sort_value_due_date, #sort_value_due_date_later, #sort_value_due_date_soon, #sort_value_expire_date, #sort_value_label_priority, #sort_value_largest_group, #sort_value_largest_repo, #sort_value_latest_activity, #sort_value_least_popular, #sort_value_merged_date, #sort_value_merged_earlier, #sort_value_merged_recently, #sort_value_milestone, #sort_value_milestone_later, #sort_value_milestone_soon, #sort_value_most_popular, #sort_value_name, #sort_value_name_desc, #sort_value_oldest_activity, #sort_value_oldest_created, #sort_value_oldest_last_activity, #sort_value_oldest_signin, #sort_value_oldest_updated, #sort_value_popularity, #sort_value_priority, #sort_value_recently_created, #sort_value_recently_last_activity, #sort_value_recently_signin, #sort_value_recently_updated, #sort_value_relative_position, #sort_value_size, #sort_value_stars_asc, #sort_value_stars_desc, #sort_value_start_date_later, #sort_value_start_date_soon, #sort_value_title, #sort_value_title_desc, #sort_value_upvotes

Methods included from ToggleAwardEmoji

#toggle_award_emoji

Methods included from RendersAssignees

#preload_assignees_for_render

Methods included from RendersCommits

#limited_commits, #prepare_commits_for_rendering, #set_commits_for_rendering, #valid_ref?

Methods included from RendersNotes

#prepare_notes_for_rendering

Methods included from IssuableActions

#bulk_update, #check_destroy_confirmation!, #destroy, #realtime_changes

Methods included from Spam::Concerns::HasSpamActionResponseFields

#spam_action_response_fields

Methods included from SpammableActions::AkismetMarkAsSpamAction

#mark_as_spam

Methods included from ToggleSubscriptionAction

#toggle_subscription

Instance Method Details

#accessibility_reportsObject


234
235
236
237
238
239
240
# File 'app/controllers/projects/merge_requests_controller.rb', line 234

def accessibility_reports
  if @merge_request.has_accessibility_reports?
    reports_response(@merge_request.compare_accessibility_reports)
  else
    head :no_content
  end
end

338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'app/controllers/projects/merge_requests_controller.rb', line 338

def assign_related_issues
  result = ::MergeRequests::AssignIssuesService.new(project: project, current_user: current_user, params: { merge_request: @merge_request }).execute

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

  redirect_to(merge_request_path(@merge_request))
end

#cancel_auto_mergeObject


314
315
316
317
318
319
320
321
322
# File 'app/controllers/projects/merge_requests_controller.rb', line 314

def cancel_auto_merge
  unless @merge_request.can_cancel_auto_merge?(current_user)
    return access_denied!
  end

  AutoMergeService.new(project, current_user).cancel(@merge_request)

  render json: serialize_widget(@merge_request)
end

#ci_environments_statusObject


359
360
361
362
363
364
365
366
367
368
# File 'app/controllers/projects/merge_requests_controller.rb', line 359

def ci_environments_status
  environments =
    if ci_environments_status_on_merge_result?
      EnvironmentStatus.for_deployed_merge_request(@merge_request, current_user)
    else
      EnvironmentStatus.for_merge_request(@merge_request, current_user)
    end

  render json: EnvironmentStatusSerializer.new(current_user: current_user).represent(environments)
end

#codequality_mr_diff_reportsObject


250
251
252
# File 'app/controllers/projects/merge_requests_controller.rb', line 250

def codequality_mr_diff_reports
  reports_response(@merge_request.find_codequality_mr_diff_reports, head_pipeline)
end

#codequality_reportsObject


254
255
256
# File 'app/controllers/projects/merge_requests_controller.rb', line 254

def codequality_reports
  reports_response(@merge_request.compare_codequality_reports)
end

#commit_change_contentObject


310
311
312
# File 'app/controllers/projects/merge_requests_controller.rb', line 310

def commit_change_content
  render partial: 'projects/merge_requests/widget/commit_change_content', layout: false
end

#commitsObject

rubocop:enable Metrics/AbcSize


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/controllers/projects/merge_requests_controller.rb', line 180

def commits
  # Get context commits from repository
  @context_commits =
    set_commits_for_rendering(
      @merge_request.recent_context_commits
    )

  # Get commits from repository
  # or from cache if already merged
  @commits =
    set_commits_for_rendering(
      @merge_request.recent_commits(load_from_gitaly: true).with_latest_pipeline(@merge_request.source_branch).with_markdown_cache,
      commits_count: @merge_request.commits_count
    )

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

#context_commitsObject


223
224
225
226
227
228
# File 'app/controllers/projects/merge_requests_controller.rb', line 223

def context_commits
  # Get commits from repository
  # or from cache if already merged
  commits = ContextCommitsFinder.new(project, @merge_request, { search: params[:search], limit: params[:limit], offset: params[:offset] }).execute
  render json: CommitEntity.represent(commits, { type: :full, request: merge_request })
end

#coverage_reportsObject


242
243
244
245
246
247
248
# File 'app/controllers/projects/merge_requests_controller.rb', line 242

def coverage_reports
  if @merge_request.has_coverage_reports?
    reports_response(@merge_request.find_coverage_reports)
  else
    head :no_content
  end
end

#discussionsObject


378
379
380
381
382
# File 'app/controllers/projects/merge_requests_controller.rb', line 378

def discussions
  merge_request.discussions_diffs.load_highlight

  super
end

#editObject


270
271
272
# File 'app/controllers/projects/merge_requests_controller.rb', line 270

def edit
  define_edit_vars
end

#export_csvObject


384
385
386
387
388
389
390
# File 'app/controllers/projects/merge_requests_controller.rb', line 384

def export_csv
  IssuableExportCsvWorker.perform_async(:merge_request, current_user.id, project.id, finder_options.to_h) # rubocop:disable CodeReuse/Worker

  index_path = project_merge_requests_path(project)
  message = _('Your CSV export has started. It will be emailed to %{email} when complete.') % { email: current_user.notification_email_or_default }
  redirect_to(index_path, notice: message)
end

#exposed_artifactsObject


262
263
264
265
266
267
268
# File 'app/controllers/projects/merge_requests_controller.rb', line 262

def exposed_artifacts
  if @merge_request.has_exposed_artifacts?
    reports_response(@merge_request.find_exposed_artifacts)
  else
    head :no_content
  end
end

#indexObject


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/projects/merge_requests_controller.rb', line 98

def index
  @merge_requests = @issuables

  respond_to do |format|
    format.html
    format.atom { render layout: 'xml' }
    format.json do
      render json: {
        html: view_to_html_string("projects/merge_requests/_merge_requests")
      }
    end
  end
end

#mergeObject


324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'app/controllers/projects/merge_requests_controller.rb', line 324

def merge
  access_check_result = merge_access_check

  return access_check_result if access_check_result

  status = merge!

  if @merge_request.merge_error
    render json: { status: status, merge_error: @merge_request.merge_error }
  else
    render json: { status: status }
  end
end

#pipeline_statusObject


353
354
355
356
357
# File 'app/controllers/projects/merge_requests_controller.rb', line 353

def pipeline_status
  render json: PipelineSerializer
    .new(project: @project, current_user: @current_user)
    .represent_status(head_pipeline)
end

#pipelinesObject


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/controllers/projects/merge_requests_controller.rb', line 198

def pipelines
  set_pipeline_variables
  @pipelines = @pipelines.page(params[:page])

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

  render json: {
    pipelines: PipelineSerializer
      .new(project: @project, current_user: @current_user)
      .with_pagination(request, response)
      .represent(@pipelines),
    count: {
      all: @pipelines.count
    }
  }
end

#rebaseObject


370
371
372
373
374
375
376
# File 'app/controllers/projects/merge_requests_controller.rb', line 370

def rebase
  @merge_request.rebase_async(current_user.id)

  head :ok
rescue MergeRequest::RebaseLockTimeout => e
  render json: { merge_error: e.message }, status: :conflict
end

#remove_wipObject


302
303
304
305
306
307
308
# File 'app/controllers/projects/merge_requests_controller.rb', line 302

def remove_wip
  @merge_request = ::MergeRequests::UpdateService
    .new(project: project, current_user: current_user, params: { wip_event: 'ready' })
    .execute(@merge_request)

  render json: serialize_widget(@merge_request)
end

#sast_reportsObject


215
216
217
# File 'app/controllers/projects/merge_requests_controller.rb', line 215

def sast_reports
  reports_response(merge_request.compare_sast_reports(current_user), head_pipeline)
end

#secret_detection_reportsObject


219
220
221
# File 'app/controllers/projects/merge_requests_controller.rb', line 219

def secret_detection_reports
  reports_response(merge_request.compare_secret_detection_reports(current_user), head_pipeline)
end

#showObject

rubocop:disable Metrics/AbcSize


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/controllers/projects/merge_requests_controller.rb', line 113

def show
  close_merge_request_if_no_source_project
  @merge_request.check_mergeability(async: true)

  respond_to do |format|
    format.html do
      # use next to appease Rubocop
      next render('invalid') if target_branch_missing?

      preload_assignees_for_render(@merge_request)

      # Build a note object for comment form
      @note = @project.notes.new(noteable: @merge_request)

      @noteable = @merge_request
      @commits_count = @merge_request.commits_count + @merge_request.context_commits_count
      @diffs_count = get_diffs_count
      @issuable_sidebar = serializer.represent(@merge_request, serializer: 'sidebar')
      @current_user_data = UserSerializer.new(project: @project).represent(current_user, {}, MergeRequestCurrentUserEntity).to_json
      @show_whitespace_default = current_user.nil? || current_user.show_whitespace_in_diffs
      @file_by_file_default = current_user&.view_diffs_file_by_file
      @coverage_path = coverage_reports_project_merge_request_path(@project, @merge_request, format: :json) if @merge_request.has_coverage_reports?
      @update_current_user_path = expose_path(api_v4_user_preferences_path)
      @endpoint_metadata_url = (@project, @merge_request)

      set_pipeline_variables

      @number_of_pipelines = @pipelines.size

      render
    end

    format.json do
      Gitlab::PollingInterval.set_header(response, interval: 10_000)

      if params[:serializer] == 'sidebar_extras'
        cache_context = [
          params[:serializer],
          current_user&.cache_key,
          @merge_request.merge_request_assignees.map(&:cache_key),
          @merge_request.merge_request_reviewers.map(&:cache_key)
        ]

        render_cached(@merge_request,
                      with: serializer,
                      cache_context: -> (_) { [Digest::SHA256.hexdigest(cache_context.to_s)] },
                      serializer: params[:serializer])
      else
        render json: serializer.represent(@merge_request, serializer: params[:serializer])
      end
    end

    format.patch do
      break render_404 unless @merge_request.diff_refs

      send_git_patch @project.repository, @merge_request.diff_refs
    end

    format.diff do
      break render_404 unless @merge_request.diff_refs

      send_git_diff @project.repository, @merge_request.diff_refs
    end
  end
end

#terraform_reportsObject


258
259
260
# File 'app/controllers/projects/merge_requests_controller.rb', line 258

def terraform_reports
  reports_response(@merge_request.find_terraform_reports)
end

#test_reportsObject


230
231
232
# File 'app/controllers/projects/merge_requests_controller.rb', line 230

def test_reports
  reports_response(@merge_request.compare_test_reports)
end

#updateObject


274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'app/controllers/projects/merge_requests_controller.rb', line 274

def update
  @merge_request = ::MergeRequests::UpdateService.new(project: project, current_user: current_user, params: merge_request_update_params).execute(@merge_request)

  respond_to do |format|
    format.html do
      if @merge_request.errors.present?
        define_edit_vars

        render :edit
      else
        redirect_to project_merge_request_path(@merge_request.target_project, @merge_request)
      end
    end

    format.json do
      if merge_request.errors.present?
        render json: @merge_request.errors, status: :bad_request
      else
        render json: serializer.represent(@merge_request, serializer: params[:serializer] || 'basic')
      end
    end
  end
rescue ActiveRecord::StaleObjectError
  define_edit_vars if request.format.html?

  render_conflict_response
end