Commit aacb4caf authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor code for merge request version compare feature

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 45a984b8
...@@ -90,18 +90,21 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -90,18 +90,21 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@merge_request.merge_request_diff @merge_request.merge_request_diff
end end
@merge_request_diffs = @merge_request.merge_request_diffs.select_without_diff
@comparable_diffs = @merge_request_diffs.select { |diff| diff.id < @merge_request_diff.id }
if params[:start_sha].present?
@start_sha = params[:start_sha]
validate_start_sha
end
respond_to do |format| respond_to do |format|
format.html { define_discussion_vars } format.html { define_discussion_vars }
format.json do format.json do
unless @merge_request_diff.latest? if @start_sha
# Disable comments if browsing older version of the diff compared_diff_version
@diff_notes_disabled = true
end
if params[:start_sha].present?
compare_diff_version
else else
@diffs = @merge_request_diff.diffs(diff_options) original_diff_version
end end
render json: { html: view_to_html_string("projects/merge_requests/show/_diffs") } render json: { html: view_to_html_string("projects/merge_requests/show/_diffs") }
...@@ -534,14 +537,27 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -534,14 +537,27 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@merge_request = MergeRequests::BuildService.new(project, current_user, merge_request_params).execute @merge_request = MergeRequests::BuildService.new(project, current_user, merge_request_params).execute
end end
def compare_diff_version def compared_diff_version
@compare = CompareService.new.execute(@project, @merge_request_diff.head_commit_sha, @project, params[:start_sha]) compare = CompareService.new.execute(@project, @merge_request_diff.head_commit_sha, @project, @start_sha)
if @compare if compare
@commits = @compare.commits @diffs = compare.diffs(diff_options)
@commit = @compare.commit
@diffs = @compare.diffs(diff_options)
@diff_notes_disabled = true @diff_notes_disabled = true
end end
end end
def original_diff_version
unless @merge_request_diff.latest?
# Disable comments if browsing older version of the diff
@diff_notes_disabled = true
end
@diffs = @merge_request_diff.diffs(diff_options)
end
def validate_start_sha
unless @comparable_diffs.map(&:head_commit_sha).include?(@start_sha)
render_404
end
end
end end
- merge_request_diffs = @merge_request.merge_request_diffs.select_without_diff - if @merge_request_diffs.size > 1
- compareable_diffs = merge_request_diffs.select { |diff| diff.id < @merge_request_diff.id }
- if merge_request_diffs.size > 1
.mr-version-controls .mr-version-controls
Version Version
%span.dropdown.inline.mr-version-dropdown %span.dropdown.inline.mr-version-dropdown
%a.btn-link.dropdown-toggle{ data: {toggle: :dropdown} } %a.btn-link.dropdown-toggle{ data: {toggle: :dropdown} }
%strong.monospace< %strong.monospace<
- if @merge_request_diff.latest? - if @merge_request_diff.latest?
Latest: #{short_sha(@merge_request_diff.head_commit_sha)} Latest:&nbsp;
- else
#{short_sha(@merge_request_diff.head_commit_sha)} #{short_sha(@merge_request_diff.head_commit_sha)}
%span.caret %span.caret
%ul.dropdown-menu.dropdown-menu-selectable %ul.dropdown-menu.dropdown-menu-selectable
- merge_request_diffs.each_with_index do |merge_request_diff, i| - @merge_request_diffs.each_with_index do |merge_request_diff, i|
%li %li
= link_to diffs_namespace_project_merge_request_path(@project.namespace, @project, @merge_request, diff_id: merge_request_diff.id), class: ('is-active' if merge_request_diff == @merge_request_diff) do = link_to mr_version_path(@project, @merge_request, merge_request_diff), class: ('is-active' if merge_request_diff == @merge_request_diff) do
%strong.monospace %strong.monospace
- if i.zero? - if i.zero?
Latest: Latest:
- else - else
#{merge_request_diffs.size - i}. #{@merge_request_diffs.size - i}.
#{merge_request_diff.head_commit.short_id} #{short_sha(merge_request_diff.head_commit_sha)}
%br %br
%small %small
#{number_with_delimiter(merge_request_diff.commits.count)} #{'commit'.pluralize(merge_request_diff.commits.count)}, #{number_with_delimiter(merge_request_diff.commits.count)} #{'commit'.pluralize(merge_request_diff.commits.count)},
= time_ago_with_tooltip(merge_request_diff.created_at) = time_ago_with_tooltip(merge_request_diff.created_at)
- if @merge_request_diff.base_commit_sha
%span.compare-dots ... %span.compare-dots ...
Compared with Compared with
%span.dropdown.inline.mr-version-compare-dropdown %span.dropdown.inline.mr-version-compare-dropdown
%a.btn-link.dropdown-toggle{ data: {toggle: :dropdown} } %a.btn-link.dropdown-toggle{ data: {toggle: :dropdown} }
%strong.monospace< %strong.monospace<
- if params[:start_sha].present? - if @start_sha
#{short_sha(params[:start_sha])} #{short_sha(@start_sha)}
- else - else
Base: #{short_sha(@merge_request_diff.base_commit_sha)} Base: #{short_sha(@merge_request_diff.base_commit_sha)}
%span.caret %span.caret
%ul.dropdown-menu.dropdown-menu-selectable %ul.dropdown-menu.dropdown-menu-selectable
- compareable_diffs.each_with_index do |merge_request_diff, i| - @comparable_diffs.each_with_index do |merge_request_diff, i|
%li %li
= link_to mr_version_path(@project, @merge_request, @merge_request_diff, merge_request_diff.head_commit_sha), class: ('is-active' if merge_request_diff.head_commit_sha == params[:start_sha]) do = link_to mr_version_path(@project, @merge_request, @merge_request_diff, merge_request_diff.head_commit_sha), class: ('is-active' if merge_request_diff.head_commit_sha == @start_sha) do
%strong.monospace %strong.monospace
#{compareable_diffs.size - i}. #{short_sha(merge_request_diff.head_commit_sha)} #{@comparable_diffs.size - i}. #{short_sha(merge_request_diff.head_commit_sha)}
%br %br
%small %small
= time_ago_with_tooltip(merge_request_diff.created_at) = time_ago_with_tooltip(merge_request_diff.created_at)
%li %li
= link_to mr_version_path(@project, @merge_request, @merge_request_diff), class: ('is-active' unless params[:start_sha].present?) do = link_to mr_version_path(@project, @merge_request, @merge_request_diff), class: ('is-active' unless @start_sha) do
%strong.monospace %strong.monospace
Base: #{short_sha(@merge_request_diff.base_commit_sha)} Base: #{short_sha(@merge_request_diff.base_commit_sha)}
- unless @merge_request_diff.latest? && params[:start_sha].blank? - unless @merge_request_diff.latest? && !@start_sha
.prepend-top-10 .prepend-top-10
= icon('info-circle') = icon('info-circle')
Comments are disabled while viewing outdated merge versions or comparing to versions other than base. Comments are disabled while viewing outdated merge versions or comparing to versions other than base.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment