2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
require 'addressable/uri'
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
class Projects::CompareController < Projects::ApplicationController
|
2016-08-24 12:49:21 +05:30
|
|
|
include DiffForPath
|
2016-06-02 11:05:42 +05:30
|
|
|
include DiffHelper
|
2018-03-17 18:26:18 +05:30
|
|
|
include RendersCommits
|
2021-04-17 20:07:23 +05:30
|
|
|
include CompareHelper
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
# Authorize
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :require_non_empty_project
|
2023-03-04 22:38:38 +05:30
|
|
|
before_action :authorize_read_code!
|
2018-10-15 14:42:47 +05:30
|
|
|
# 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-04-02 18:10:28 +05:30
|
|
|
before_action :merge_request, only: [:index, :show]
|
2018-12-13 13:39:08 +05:30
|
|
|
# Validation
|
|
|
|
before_action :validate_refs!
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
feature_category :source_code_management
|
2021-12-11 22:18:48 +05:30
|
|
|
urgency :low, [:show, :create, :signatures]
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
# 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
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def index
|
2022-04-04 11:22:00 +05:30
|
|
|
compare_params
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2016-09-13 17:45:13 +05:30
|
|
|
apply_diff_view_cookie!
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
render locals: { pagination_params: params.permit(:page) }
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def diff_for_path
|
2018-10-15 14:42:47 +05:30
|
|
|
return render_404 unless compare
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
render_diff_for_path(compare.diffs(diff_options))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2021-04-17 20:07:23 +05:30
|
|
|
from_to_vars = {
|
2022-04-04 11:22:00 +05:30
|
|
|
from: compare_params[:from].presence,
|
|
|
|
to: compare_params[:to].presence,
|
2022-11-25 23:54:43 +05:30
|
|
|
from_project_id: compare_params[:from_project_id].presence,
|
|
|
|
straight: compare_params[:straight].presence
|
2021-04-17 20:07:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if from_to_vars[:from].blank? || from_to_vars[:to].blank?
|
2018-03-17 18:26:18 +05:30
|
|
|
flash[:alert] = "You must select a Source and a Target revision"
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
redirect_to project_compare_index_path(source_project, from_to_vars)
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
2021-04-17 20:07:23 +05:30
|
|
|
redirect_to project_compare_path(source_project, from_to_vars)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
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-04-02 18:10:28 +05:30
|
|
|
private
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def validate_refs!
|
2021-09-04 01:27:46 +05:30
|
|
|
invalid = [head_ref, start_ref].filter { |ref| !valid_ref?(ref) }
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
return if invalid.empty?
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
flash[:alert] = "Invalid branch name(s): #{invalid.join(', ')}"
|
2021-04-17 20:07:23 +05:30
|
|
|
redirect_to project_compare_index_path(source_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
# target == start_ref == from
|
|
|
|
def target_project
|
|
|
|
strong_memoize(:target_project) do
|
2023-07-07 10:43:13 +05:30
|
|
|
target_project =
|
|
|
|
if !compare_params.key?(:from_project_id)
|
|
|
|
source_project.default_merge_request_target
|
|
|
|
elsif compare_params[:from_project_id].to_i == source_project.id
|
|
|
|
source_project
|
|
|
|
else
|
|
|
|
target_projects(source_project).find_by_id(compare_params[:from_project_id])
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
# Just ignore the field if it points at a non-existent or hidden project
|
2023-03-04 22:38:38 +05:30
|
|
|
next source_project unless target_project && can?(current_user, :read_code, target_project)
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
target_project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# source == head_ref == to
|
|
|
|
def source_project
|
2022-07-23 23:45:48 +05:30
|
|
|
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-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def compare
|
|
|
|
return @compare if defined?(@compare)
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
@compare = CompareService.new(source_project, head_ref).execute(target_project, start_ref, straight: straight)
|
|
|
|
end
|
|
|
|
|
|
|
|
def straight
|
|
|
|
compare_params[:straight] == "true"
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def start_ref
|
2022-07-23 23:45:48 +05:30
|
|
|
@start_ref ||= Addressable::URI.unescape(compare_params[:from]).presence
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def head_ref
|
|
|
|
return @ref if defined?(@ref)
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
@ref = @head_ref = Addressable::URI.unescape(compare_params[:to]).presence
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def define_commits
|
2022-07-23 23:45:48 +05:30
|
|
|
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-10-15 14:42:47 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def define_diffs
|
|
|
|
@diffs = compare.present? ? compare.diffs(diff_options) : []
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def define_environment
|
|
|
|
if compare
|
2021-04-17 20:07:23 +05:30
|
|
|
environment_params = source_project.repository.branch_exists?(head_ref) ? { ref: head_ref } : { commit: compare.commit }
|
2020-03-13 15:44:24 +05:30
|
|
|
environment_params[:find_latest] = true
|
2021-06-08 01:23:25 +05:30
|
|
|
@environment = ::Environments::EnvironmentsByDeploymentsFinder.new(source_project, current_user, environment_params).execute.last
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def define_diff_notes_disabled
|
|
|
|
@diff_notes_disabled = compare.present?
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-04-02 18:10:28 +05:30
|
|
|
def merge_request
|
2021-04-17 20:07:23 +05:30
|
|
|
@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-04-02 18:10:28 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
def compare_params
|
2022-11-25 23:54:43 +05:30
|
|
|
@compare_params ||= params.permit(:from, :to, :from_project_id, :straight)
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|