2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-27 02:42:29 -04:00
|
|
|
require 'addressable/uri'
|
|
|
|
|
2013-06-23 12:47:22 -04:00
|
|
|
class Projects::CompareController < Projects::ApplicationController
|
2016-07-06 13:15:27 -04:00
|
|
|
include DiffForPath
|
2016-03-03 12:38:44 -05:00
|
|
|
include DiffHelper
|
2017-08-23 12:53:29 -04:00
|
|
|
include RendersCommits
|
2021-02-22 10:10:48 -05:00
|
|
|
include CompareHelper
|
2016-03-03 12:38:44 -05:00
|
|
|
|
2012-09-20 15:20:48 -04:00
|
|
|
# Authorize
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :require_non_empty_project
|
|
|
|
before_action :authorize_download_code!
|
2018-05-07 04:41:36 -04:00
|
|
|
# Defining ivars
|
|
|
|
before_action :define_diffs, only: [:show, :diff_for_path]
|
|
|
|
before_action :define_environment, only: [:show]
|
|
|
|
before_action :define_diff_notes_disabled, only: [:show, :diff_for_path]
|
|
|
|
before_action :define_commits, only: [:show, :diff_for_path, :signatures]
|
2016-02-01 18:43:51 -05:00
|
|
|
before_action :merge_request, only: [:index, :show]
|
2018-10-08 02:16:45 -04:00
|
|
|
# Validation
|
|
|
|
before_action :validate_refs!
|
2012-09-20 15:20:48 -04:00
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :source_code_management
|
2021-11-12 13:12:20 -05:00
|
|
|
urgency :low, [:show, :create, :signatures]
|
2020-10-08 14:08:32 -04:00
|
|
|
|
2021-04-20 17:09:07 -04:00
|
|
|
# Diffs may be pretty chunky, the less is better in this endpoint.
|
|
|
|
# Pagination design guides: https://design.gitlab.com/components/pagination/#behavior
|
|
|
|
COMMIT_DIFFS_PER_PAGE = 20
|
|
|
|
|
2012-09-25 23:10:50 -04:00
|
|
|
def index
|
2022-02-17 10:18:08 -05:00
|
|
|
compare_params
|
2012-09-25 23:10:50 -04:00
|
|
|
end
|
|
|
|
|
2012-09-20 15:20:48 -04:00
|
|
|
def show
|
2016-07-07 07:36:33 -04:00
|
|
|
apply_diff_view_cookie!
|
2018-02-28 17:22:44 -05:00
|
|
|
|
2022-04-11 11:09:47 -04:00
|
|
|
render locals: { pagination_params: params.permit(:page) }
|
2012-09-20 15:20:48 -04:00
|
|
|
end
|
2012-09-25 23:10:50 -04:00
|
|
|
|
2016-06-28 12:25:32 -04:00
|
|
|
def diff_for_path
|
2018-05-07 04:41:36 -04:00
|
|
|
return render_404 unless compare
|
2016-06-28 12:25:32 -04:00
|
|
|
|
2018-05-07 04:41:36 -04:00
|
|
|
render_diff_for_path(compare.diffs(diff_options))
|
2016-06-28 12:25:32 -04:00
|
|
|
end
|
|
|
|
|
2012-09-25 23:10:50 -04:00
|
|
|
def create
|
2021-02-22 10:10:48 -05:00
|
|
|
from_to_vars = {
|
2022-02-17 10:18:08 -05:00
|
|
|
from: compare_params[:from].presence,
|
|
|
|
to: compare_params[:to].presence,
|
|
|
|
from_project_id: compare_params[:from_project_id].presence
|
2021-02-22 10:10:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if from_to_vars[:from].blank? || from_to_vars[:to].blank?
|
2017-09-12 10:56:22 -04:00
|
|
|
flash[:alert] = "You must select a Source and a Target revision"
|
2021-02-22 10:10:48 -05:00
|
|
|
|
|
|
|
redirect_to project_compare_index_path(source_project, from_to_vars)
|
2017-01-10 17:11:36 -05:00
|
|
|
else
|
2021-02-22 10:10:48 -05:00
|
|
|
redirect_to project_compare_path(source_project, from_to_vars)
|
2017-01-10 17:11:36 -05:00
|
|
|
end
|
2012-09-25 23:10:50 -04:00
|
|
|
end
|
2016-02-01 18:43:51 -05:00
|
|
|
|
2018-05-07 04:41:36 -04:00
|
|
|
def signatures
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
signatures: @commits.select(&:has_signature?).map do |commit|
|
|
|
|
{
|
|
|
|
commit_sha: commit.sha,
|
|
|
|
html: view_to_html_string('projects/commit/_signature', signature: commit.signature)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-01 18:43:51 -05:00
|
|
|
private
|
|
|
|
|
2018-10-08 02:16:45 -04:00
|
|
|
def validate_refs!
|
2021-06-02 05:09:46 -04:00
|
|
|
invalid = [head_ref, start_ref].filter { |ref| !valid_ref?(ref) }
|
2018-10-08 02:16:45 -04:00
|
|
|
|
2021-06-02 05:09:46 -04:00
|
|
|
return if invalid.empty?
|
2018-10-08 02:16:45 -04:00
|
|
|
|
2021-06-02 05:09:46 -04:00
|
|
|
flash[:alert] = "Invalid branch name(s): #{invalid.join(', ')}"
|
2021-02-22 10:10:48 -05:00
|
|
|
redirect_to project_compare_index_path(source_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
# target == start_ref == from
|
|
|
|
def target_project
|
|
|
|
strong_memoize(:target_project) do
|
2022-02-17 10:18:08 -05:00
|
|
|
next source_project unless compare_params.key?(:from_project_id)
|
|
|
|
next source_project if compare_params[:from_project_id].to_i == source_project.id
|
2021-02-22 10:10:48 -05:00
|
|
|
|
2022-02-17 10:18:08 -05:00
|
|
|
target_project = target_projects(source_project).find_by_id(compare_params[:from_project_id])
|
2021-02-22 10:10:48 -05:00
|
|
|
|
|
|
|
# Just ignore the field if it points at a non-existent or hidden project
|
|
|
|
next source_project unless target_project && can?(current_user, :download_code, target_project)
|
|
|
|
|
|
|
|
target_project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# source == head_ref == to
|
|
|
|
def source_project
|
2022-06-06 05:09:28 -04:00
|
|
|
strong_memoize(:source_project) do
|
|
|
|
# Eager load project's avatar url to prevent batch loading
|
|
|
|
# for all forked projects
|
|
|
|
project&.tap(&:avatar_url)
|
|
|
|
end
|
2018-10-08 02:16:45 -04:00
|
|
|
end
|
|
|
|
|
2018-05-07 04:41:36 -04:00
|
|
|
def compare
|
|
|
|
return @compare if defined?(@compare)
|
|
|
|
|
2021-02-22 10:10:48 -05:00
|
|
|
@compare = CompareService.new(source_project, head_ref).execute(target_project, start_ref)
|
2018-05-07 04:41:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_ref
|
2022-06-01 08:09:17 -04:00
|
|
|
@start_ref ||= Addressable::URI.unescape(compare_params[:from]).presence
|
2018-05-07 04:41:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def head_ref
|
|
|
|
return @ref if defined?(@ref)
|
|
|
|
|
2022-06-01 08:09:17 -04:00
|
|
|
@ref = @head_ref = Addressable::URI.unescape(compare_params[:to]).presence
|
2016-02-01 18:43:51 -05:00
|
|
|
end
|
|
|
|
|
2018-05-07 04:41:36 -04:00
|
|
|
def define_commits
|
2022-05-30 20:09:17 -04:00
|
|
|
strong_memoize(:commits) do
|
|
|
|
if compare.present?
|
|
|
|
commits = compare.commits.with_markdown_cache.with_latest_pipeline(head_ref)
|
|
|
|
set_commits_for_rendering(commits)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
2018-05-07 04:41:36 -04:00
|
|
|
end
|
2016-07-06 13:15:27 -04:00
|
|
|
|
2018-05-07 04:41:36 -04:00
|
|
|
def define_diffs
|
|
|
|
@diffs = compare.present? ? compare.diffs(diff_options) : []
|
|
|
|
end
|
2016-07-06 13:15:27 -04:00
|
|
|
|
2018-05-07 04:41:36 -04:00
|
|
|
def define_environment
|
|
|
|
if compare
|
2021-02-22 10:10:48 -05:00
|
|
|
environment_params = source_project.repository.branch_exists?(head_ref) ? { ref: head_ref } : { commit: compare.commit }
|
2019-12-18 13:08:04 -05:00
|
|
|
environment_params[:find_latest] = true
|
2021-04-22 11:09:56 -04:00
|
|
|
@environment = ::Environments::EnvironmentsByDeploymentsFinder.new(source_project, current_user, environment_params).execute.last
|
2016-07-06 13:15:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-07 04:41:36 -04:00
|
|
|
def define_diff_notes_disabled
|
|
|
|
@diff_notes_disabled = compare.present?
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-02-01 18:43:51 -05:00
|
|
|
def merge_request
|
2021-02-22 10:10:48 -05:00
|
|
|
@merge_request ||= MergeRequestsFinder.new(current_user, project_id: target_project.id).execute.opened
|
|
|
|
.find_by(source_project: source_project, source_branch: head_ref, target_branch: start_ref)
|
2016-02-01 18:43:51 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2022-02-17 10:18:08 -05:00
|
|
|
|
|
|
|
def compare_params
|
|
|
|
@compare_params ||= params.permit(:from, :to, :from_project_id)
|
|
|
|
end
|
2012-09-20 15:20:48 -04:00
|
|
|
end
|