2018-07-19 14:43:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
class DiffsEntity < Grape::Entity
|
|
|
|
include DiffHelper
|
|
|
|
include RequestAwareEntity
|
|
|
|
|
|
|
|
expose :real_size
|
|
|
|
expose :size
|
|
|
|
|
|
|
|
expose :branch_name do |diffs|
|
|
|
|
merge_request&.source_branch
|
|
|
|
end
|
|
|
|
|
2020-05-12 08:09:47 -04:00
|
|
|
expose :source_branch_exists do |diffs|
|
|
|
|
merge_request&.source_branch_exists?
|
|
|
|
end
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
expose :target_branch_name do |diffs|
|
|
|
|
merge_request&.target_branch
|
|
|
|
end
|
|
|
|
|
2018-09-26 04:28:50 -04:00
|
|
|
expose :commit do |diffs, options|
|
2020-04-02 08:08:18 -04:00
|
|
|
CommitEntity.represent(options[:commit], commit_options(options))
|
2018-06-21 08:22:40 -04:00
|
|
|
end
|
|
|
|
|
2020-01-29 10:08:59 -05:00
|
|
|
expose :context_commits, using: API::Entities::Commit, if: -> (diffs, options) { merge_request&.project&.context_commits_enabled? } do |diffs|
|
|
|
|
options[:context_commits]
|
|
|
|
end
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
expose :merge_request_diff, using: MergeRequestDiffEntity do |diffs|
|
|
|
|
options[:merge_request_diff]
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :start_version, using: MergeRequestDiffEntity do |diffs|
|
|
|
|
options[:start_version]
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :latest_diff do |diffs|
|
|
|
|
options[:latest_diff]
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :latest_version_path, if: -> (*) { merge_request } do |diffs|
|
|
|
|
diffs_project_merge_request_path(merge_request&.project, merge_request)
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-06-21 08:22:40 -04:00
|
|
|
expose :added_lines do |diffs|
|
2019-12-16 16:08:00 -05:00
|
|
|
diffs.raw_diff_files.sum(&:added_lines)
|
2018-06-21 08:22:40 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-06-21 08:22:40 -04:00
|
|
|
expose :removed_lines do |diffs|
|
2019-12-16 16:08:00 -05:00
|
|
|
diffs.raw_diff_files.sum(&:removed_lines)
|
2018-06-21 08:22:40 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-06-21 08:22:40 -04:00
|
|
|
|
|
|
|
expose :render_overflow_warning do |diffs|
|
2019-10-10 08:06:19 -04:00
|
|
|
render_overflow_warning?(diffs)
|
2018-06-21 08:22:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expose :email_patch_path, if: -> (*) { merge_request } do |diffs|
|
|
|
|
merge_request_path(merge_request, format: :patch)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :plain_diff_path, if: -> (*) { merge_request } do |diffs|
|
|
|
|
merge_request_path(merge_request, format: :diff)
|
|
|
|
end
|
|
|
|
|
2019-07-17 09:06:19 -04:00
|
|
|
expose :diff_files do |diffs, options|
|
|
|
|
submodule_links = Gitlab::SubmoduleLinks.new(merge_request.project.repository)
|
2020-04-02 05:08:14 -04:00
|
|
|
|
|
|
|
DiffFileEntity.represent(diffs.diff_files,
|
2021-07-21 17:10:10 -04:00
|
|
|
options.merge(
|
|
|
|
submodule_links: submodule_links,
|
|
|
|
code_navigation_path: code_navigation_path(diffs),
|
|
|
|
conflicts: conflicts(allow_tree_conflicts: options[:allow_tree_conflicts])
|
|
|
|
)
|
|
|
|
)
|
2019-07-17 09:06:19 -04:00
|
|
|
end
|
2018-06-21 08:22:40 -04:00
|
|
|
|
|
|
|
expose :merge_request_diffs, using: MergeRequestDiffEntity, if: -> (_, options) { options[:merge_request_diffs]&.any? } do |diffs|
|
|
|
|
options[:merge_request_diffs]
|
|
|
|
end
|
|
|
|
|
2020-09-29 11:10:08 -04:00
|
|
|
expose :definition_path_prefix do |diffs|
|
2021-01-25 16:09:03 -05:00
|
|
|
next unless merge_request.diff_head_sha
|
|
|
|
|
|
|
|
project_blob_path(merge_request.project, merge_request.diff_head_sha)
|
2020-04-02 05:08:14 -04:00
|
|
|
end
|
|
|
|
|
2021-04-29 11:10:07 -04:00
|
|
|
expose :context_commits_diff, if: -> (_) { merge_request&.project&.context_commits_enabled? } do |diffs, options|
|
|
|
|
next unless merge_request.context_commits_diff.commits_count > 0
|
|
|
|
|
|
|
|
ContextCommitsDiffEntity.represent(
|
|
|
|
merge_request.context_commits_diff,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
def merge_request
|
|
|
|
options[:merge_request]
|
|
|
|
end
|
2020-04-02 08:08:18 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def commit_ids
|
|
|
|
@commit_ids ||= merge_request.recent_commits.map(&:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_neighbors(commit_id)
|
|
|
|
index = commit_ids.index(commit_id)
|
|
|
|
|
|
|
|
return [] unless index
|
|
|
|
|
|
|
|
[(index > 0 ? commit_ids[index - 1] : nil), commit_ids[index + 1]]
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_options(options)
|
2020-04-08 14:09:16 -04:00
|
|
|
next_commit_id, prev_commit_id = *commit_neighbors(options[:commit]&.id)
|
2020-04-02 08:08:18 -04:00
|
|
|
|
|
|
|
options.merge(
|
|
|
|
type: :full,
|
|
|
|
commit_url_params: { merge_request_iid: merge_request&.iid },
|
|
|
|
pipeline_ref: merge_request&.source_branch,
|
|
|
|
pipeline_project: merge_request&.source_project,
|
|
|
|
prev_commit_id: prev_commit_id,
|
|
|
|
next_commit_id: next_commit_id
|
|
|
|
)
|
|
|
|
end
|
2018-06-21 08:22:40 -04:00
|
|
|
end
|