2018-12-04 17:17:10 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DiffFileBaseEntity < Grape::Entity
|
|
|
|
include RequestAwareEntity
|
|
|
|
include BlobHelper
|
|
|
|
include DiffHelper
|
|
|
|
include TreeHelper
|
|
|
|
include ChecksCollaboration
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
expose :content_sha
|
|
|
|
expose :submodule?, as: :submodule
|
|
|
|
|
2019-07-17 09:06:19 -04:00
|
|
|
expose :submodule_link do |diff_file, options|
|
2020-09-07 08:08:27 -04:00
|
|
|
memoized_submodule_links(diff_file, options)&.web
|
2018-12-04 17:17:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expose :submodule_tree_url do |diff_file|
|
2020-09-07 08:08:27 -04:00
|
|
|
memoized_submodule_links(diff_file, options)&.tree
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :submodule_compare do |diff_file|
|
|
|
|
url = memoized_submodule_links(diff_file, options)&.compare
|
|
|
|
|
|
|
|
next unless url
|
|
|
|
|
|
|
|
{
|
|
|
|
url: url,
|
|
|
|
old_sha: diff_file.old_blob&.id,
|
|
|
|
new_sha: diff_file.blob&.id
|
|
|
|
}
|
2018-12-04 17:17:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expose :edit_path, if: -> (_, options) { options[:merge_request] } do |diff_file|
|
|
|
|
merge_request = options[:merge_request]
|
|
|
|
|
2020-09-18 17:10:11 -04:00
|
|
|
next unless has_edit_path?(merge_request)
|
2018-12-04 17:17:10 -05:00
|
|
|
|
2020-05-12 08:09:47 -04:00
|
|
|
target_project, target_branch = edit_project_branch_options(merge_request)
|
2018-12-04 17:17:10 -05:00
|
|
|
|
2020-07-11 02:09:15 -04:00
|
|
|
options = merge_request.persisted? && merge_request.source_branch_exists? && !merge_request.merged? ? { from_merge_request_iid: merge_request.iid } : {}
|
2020-05-12 08:09:47 -04:00
|
|
|
|
2020-07-11 02:09:15 -04:00
|
|
|
project_edit_blob_path(target_project, tree_join(target_branch, diff_file.new_path), options)
|
2018-12-04 17:17:10 -05:00
|
|
|
end
|
|
|
|
|
2020-09-18 17:10:11 -04:00
|
|
|
expose :ide_edit_path, if: -> (_, options) { options[:merge_request] } do |diff_file|
|
|
|
|
merge_request = options[:merge_request]
|
|
|
|
|
|
|
|
next unless has_edit_path?(merge_request)
|
|
|
|
|
2020-10-23 17:08:31 -04:00
|
|
|
ide_merge_request_path(merge_request, diff_file.new_path)
|
2020-09-18 17:10:11 -04:00
|
|
|
end
|
|
|
|
|
2018-12-04 17:17:10 -05:00
|
|
|
expose :old_path_html do |diff_file|
|
|
|
|
old_path, _ = mark_inline_diffs(diff_file.old_path, diff_file.new_path)
|
|
|
|
old_path
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :new_path_html do |diff_file|
|
|
|
|
_, new_path = mark_inline_diffs(diff_file.old_path, diff_file.new_path)
|
|
|
|
new_path
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :formatted_external_url, if: -> (_, options) { options[:environment] } do |diff_file|
|
|
|
|
options[:environment].formatted_external_url
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :external_url, if: -> (_, options) { options[:environment] } do |diff_file|
|
|
|
|
options[:environment].external_url_for(diff_file.new_path, diff_file.content_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :blob, using: BlobEntity
|
|
|
|
|
|
|
|
expose :can_modify_blob do |diff_file|
|
|
|
|
merge_request = options[:merge_request]
|
|
|
|
|
|
|
|
next unless diff_file.blob
|
|
|
|
|
|
|
|
if merge_request&.source_project && current_user
|
2020-05-12 08:09:47 -04:00
|
|
|
can_modify_blob?(diff_file.blob, merge_request.source_project, merge_request.source_branch_exists? ? merge_request.source_branch : merge_request.target_branch)
|
2018-12-04 17:17:10 -05:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-22 08:08:15 -04:00
|
|
|
expose :file_identifier_hash
|
|
|
|
expose :file_hash
|
2018-12-04 17:17:10 -05:00
|
|
|
expose :file_path
|
|
|
|
expose :old_path
|
|
|
|
expose :new_path
|
|
|
|
expose :new_file?, as: :new_file
|
2019-02-15 12:56:50 -05:00
|
|
|
expose :renamed_file?, as: :renamed_file
|
|
|
|
expose :deleted_file?, as: :deleted_file
|
|
|
|
|
2018-12-04 17:17:10 -05:00
|
|
|
expose :diff_refs
|
2019-02-15 12:56:50 -05:00
|
|
|
|
2018-12-04 17:17:10 -05:00
|
|
|
expose :stored_externally?, as: :stored_externally
|
|
|
|
expose :external_storage
|
2019-02-15 12:56:50 -05:00
|
|
|
|
2018-12-04 17:17:10 -05:00
|
|
|
expose :mode_changed?, as: :mode_changed
|
|
|
|
expose :a_mode
|
|
|
|
expose :b_mode
|
|
|
|
|
2019-02-15 12:56:50 -05:00
|
|
|
expose :viewer, using: DiffViewerEntity
|
2020-04-28 08:09:44 -04:00
|
|
|
expose :alternate_viewer, using: DiffViewerEntity
|
2019-02-15 12:56:50 -05:00
|
|
|
|
2019-10-29 08:06:40 -04:00
|
|
|
expose :old_size do |diff_file|
|
|
|
|
diff_file.old_blob&.raw_size
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :new_size do |diff_file|
|
|
|
|
diff_file.new_blob&.raw_size
|
|
|
|
end
|
|
|
|
|
2018-12-04 17:17:10 -05:00
|
|
|
private
|
|
|
|
|
2019-07-17 09:06:19 -04:00
|
|
|
def memoized_submodule_links(diff_file, options)
|
2018-12-04 17:17:10 -05:00
|
|
|
strong_memoize(:submodule_links) do
|
2020-09-07 08:08:27 -04:00
|
|
|
next unless diff_file.submodule?
|
|
|
|
|
|
|
|
options[:submodule_links].for(diff_file.blob, diff_file.content_sha, diff_file)
|
2018-12-04 17:17:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
request.current_user
|
|
|
|
end
|
2020-05-12 08:09:47 -04:00
|
|
|
|
|
|
|
def edit_project_branch_options(merge_request)
|
|
|
|
if merge_request.source_branch_exists? && !merge_request.merged?
|
|
|
|
[merge_request.source_project, merge_request.source_branch]
|
|
|
|
else
|
|
|
|
[merge_request.target_project, merge_request.target_branch]
|
|
|
|
end
|
|
|
|
end
|
2020-09-18 17:10:11 -04:00
|
|
|
|
|
|
|
def has_edit_path?(merge_request)
|
|
|
|
merge_request.merged? || merge_request.source_branch_exists?
|
|
|
|
end
|
2018-12-04 17:17:10 -05:00
|
|
|
end
|