Class: Projects::MergeRequestsController

Inherits:
ApplicationController show all
Includes:
DiffHelper, IssuableActions, ToggleSubscriptionAction
Defined in:
app/controllers/projects/merge_requests_controller.rb

Instance Method Summary collapse

Methods included from IssuableActions

#destroy

Methods included from DiffHelper

#commit_for_diff, #diff_file_html_data, #diff_hard_limit_enabled?, #diff_line_content, #diff_options, #diff_view, #editable_diff?, #generate_line_code, #inline_diff_btn, #line_comments, #mark_inline_diffs, #organize_comments, #parallel_diff_btn, #safe_diff_files, #submodule_link, #unfold_bottom_class, #unfold_class

Methods included from ToggleSubscriptionAction

#toggle_subscription

Methods inherited from ApplicationController

#redirect_back_or_default

Methods included from PageLayoutHelper

#blank_container, #container_class, #fluid_layout, #header_title, #nav, #page_card_attributes, #page_card_meta_tags, #page_description, #page_image, #page_title, #sidebar

Methods included from GitlabRoutingHelper

#activity_project_path, #edit_project_path, #edit_project_url, #issue_path, #issue_url, #merge_request_path, #merge_request_url, #milestone_path, #project_builds_path, #project_commits_path, #project_files_path, #project_path, #project_pipelines_path, #project_snippet_url, #project_url, #runner_path, #runners_path, #toggle_subscription_path

Methods included from Gitlab::GonHelper

#add_gon_variables

Methods included from Gitlab::CurrentSettings

#current_application_settings, #fake_application_settings

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Projects::ApplicationController

Instance Method Details

#branch_fromObject


206
207
208
209
210
211
# File 'app/controllers/projects/merge_requests_controller.rb', line 206

def branch_from
  #This is always source
  @source_project = @merge_request.nil? ? @project : @merge_request.source_project
  @commit = @repository.commit(params[:ref]) if params[:ref].present?
  render layout: false
end

#branch_toObject


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

def branch_to
  @target_project = selected_target_project
  @commit = @target_project.commit(params[:ref]) if params[:ref].present?
  render layout: false
end

#buildsObject


96
97
98
99
100
101
# File 'app/controllers/projects/merge_requests_controller.rb', line 96

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

#cancel_merge_when_build_succeedsObject


178
179
180
181
182
# File 'app/controllers/projects/merge_requests_controller.rb', line 178

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

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

#ci_statusObject


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'app/controllers/projects/merge_requests_controller.rb', line 226

def ci_status
  ci_commit = @merge_request.ci_commit
  if ci_commit
    status = ci_commit.status
    coverage = ci_commit.try(:coverage)
  else
    ci_service = @merge_request.source_project.ci_service
    status = ci_service.commit_status(merge_request.last_commit.sha, merge_request.source_branch) if ci_service

    if ci_service.respond_to?(:commit_coverage)
      coverage = ci_service.commit_coverage(merge_request.last_commit.sha, merge_request.source_branch)
    end
  end

  status = "preparing" if status.nil?

  response = {
    title: merge_request.title,
    sha: merge_request.last_commit_short_sha,
    status: status,
    coverage: coverage
  }

  render json: response
end

#commitsObject


89
90
91
92
93
94
# File 'app/controllers/projects/merge_requests_controller.rb', line 89

def commits
  respond_to do |format|
    format.html { render 'show' }
    format.json { render json: { html: view_to_html_string('projects/merge_requests/show/_commits') } }
  end
end

#createObject


128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/projects/merge_requests_controller.rb', line 128

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

  if @merge_request.valid?
    redirect_to(merge_request_path(@merge_request))
  else
    @source_project = @merge_request.source_project
    @target_project = @merge_request.target_project
    render action: "new"
  end
end

#diffsObject


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/projects/merge_requests_controller.rb', line 66

def diffs
  apply_diff_view_cookie!

  @commit = @merge_request.last_commit
  @base_commit = @merge_request.diff_base_commit

  # MRs created before 8.4 don't have a diff_base_commit,
  # but we need it for the "View file @ ..." link by deleted files
  @base_commit ||= @merge_request.first_commit.parent || @merge_request.first_commit

  @comments_allowed = @reply_allowed = true
  @comments_target = {
    noteable_type: 'MergeRequest',
    noteable_id: @merge_request.id
  }
  @line_notes = @merge_request.notes.where("line_code is not null")

  respond_to do |format|
    format.html
    format.json { render json: { html: view_to_html_string("projects/merge_requests/show/_diffs") } }
  end
end

#editObject


141
142
143
144
145
# File 'app/controllers/projects/merge_requests_controller.rb', line 141

def edit
  @source_project = @merge_request.source_project
  @target_project = @merge_request.target_project
  @target_branches = @merge_request.target_project.repository.branch_names
end

#indexObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/projects/merge_requests_controller.rb', line 26

def index
  terms = params['issue_search']
  @merge_requests = get_merge_requests_collection

  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

  @merge_requests = @merge_requests.page(params[:page])
  @merge_requests = @merge_requests.preload(:target_project)

  @labels = @project.labels.where(title: params[:label_name])

  respond_to do |format|
    format.html
    format.json do
      render json: {
        html: view_to_html_string("projects/merge_requests/_merge_requests"),
        labels: @labels.as_json(methods: :text_color)
      }
    end
  end
end

#mergeObject


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/controllers/projects/merge_requests_controller.rb', line 184

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

  unless @merge_request.mergeable?
    @status = :failed
    return
  end

  TodoService.new.merge_merge_request(merge_request, current_user)

  @merge_request.update(merge_error: nil)

  if params[:merge_when_build_succeeds].present? && @merge_request.ci_commit && @merge_request.ci_commit.active?
    MergeRequests::MergeWhenBuildSucceedsService.new(@project, current_user, merge_params)
                                                    .execute(@merge_request)
    @status = :merge_when_build_succeeds
  else
    MergeWorker.perform_async(@merge_request.id, current_user.id, params)
    @status = :success
  end
end

#merge_checkObject


172
173
174
175
176
# File 'app/controllers/projects/merge_requests_controller.rb', line 172

def merge_check
  @merge_request.check_if_can_be_merged

  render partial: "projects/merge_requests/widget/show.html.haml", layout: false
end

#newObject


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/projects/merge_requests_controller.rb', line 103

def new
  params[:merge_request] ||= ActionController::Parameters.new(source_project: @project)
  @merge_request = MergeRequests::BuildService.new(project, current_user, merge_request_params).execute
  @noteable = @merge_request

  @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
  @commits = @merge_request.compare_commits.reverse
  @commit = @merge_request.last_commit
  @base_commit = @merge_request.diff_base_commit
  @diffs = @merge_request.compare.diffs(diff_options) if @merge_request.compare

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

  @note_counts = Note.where(commit_id: @commits.map(&:id)).
    group(:commit_id).count
end

#remove_wipObject


165
166
167
168
169
170
# File 'app/controllers/projects/merge_requests_controller.rb', line 165

def remove_wip
  MergeRequests::UpdateService.new(project, current_user, title: @merge_request.wipless_title).execute(@merge_request)

  redirect_to namespace_project_merge_request_path(@project.namespace, @project, @merge_request),
    notice: "The merge request can now be merged."
end

#showObject


54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/projects/merge_requests_controller.rb', line 54

def show
  @note_counts = Note.where(commit_id: @merge_request.commits.map(&:id)).
    group(:commit_id).count

  respond_to do |format|
    format.html
    format.json { render json: @merge_request }
    format.diff { render text: @merge_request.to_diff }
    format.patch { render text: @merge_request.to_patch }
  end
end

#updateObject


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/projects/merge_requests_controller.rb', line 147

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

  if @merge_request.valid?
    respond_to do |format|
      format.html do
        redirect_to([@merge_request.target_project.namespace.becomes(Namespace),
                     @merge_request.target_project, @merge_request])
      end
      format.json do
        render json: @merge_request.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } })
      end
    end
  else
    render "edit"
  end
end

#update_branchesObject


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

def update_branches
  @target_project = selected_target_project
  @target_branches = @target_project.repository.branch_names

  render layout: false
end