2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Discussions
|
|
|
|
class CaptureDiffNotePositionService
|
|
|
|
def initialize(merge_request, paths)
|
|
|
|
@project = merge_request.project
|
|
|
|
@tracer = build_tracer(merge_request, paths)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(discussion)
|
|
|
|
# The service has been implemented for text only
|
2021-01-29 00:20:46 +05:30
|
|
|
# We don't need to capture positions for images
|
2020-04-22 19:07:51 +05:30
|
|
|
return unless discussion.on_text?
|
|
|
|
|
|
|
|
result = tracer&.trace(discussion.position)
|
|
|
|
return unless result
|
|
|
|
|
|
|
|
position = result[:position]
|
2020-05-05 14:28:15 +05:30
|
|
|
return unless position
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
line_code = position.line_code(project.repository)
|
|
|
|
return unless line_code
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
# Currently position data is copied across all notes of a discussion
|
|
|
|
# It makes sense to store a position only for the first note instead
|
|
|
|
# Within the newly introduced table we can start doing just that
|
|
|
|
DiffNotePosition.create_or_update_for(discussion.notes.first,
|
|
|
|
diff_type: :head,
|
|
|
|
position: position,
|
2020-10-24 23:57:45 +05:30
|
|
|
line_code: line_code)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :tracer, :project
|
|
|
|
|
|
|
|
def build_tracer(merge_request, paths)
|
|
|
|
return if paths.blank?
|
|
|
|
|
|
|
|
old_diff_refs, new_diff_refs = build_diff_refs(merge_request)
|
|
|
|
|
|
|
|
return unless old_diff_refs && new_diff_refs
|
|
|
|
|
|
|
|
Gitlab::Diff::PositionTracer.new(
|
|
|
|
project: project,
|
|
|
|
old_diff_refs: old_diff_refs,
|
|
|
|
new_diff_refs: new_diff_refs,
|
|
|
|
paths: paths.uniq)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_diff_refs(merge_request)
|
|
|
|
merge_ref_head = merge_request.merge_ref_head
|
|
|
|
return unless merge_ref_head
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
start_sha, _ = merge_ref_head.parent_ids
|
2020-04-22 19:07:51 +05:30
|
|
|
new_diff_refs = Gitlab::Diff::DiffRefs.new(
|
2020-10-24 23:57:45 +05:30
|
|
|
base_sha: start_sha,
|
2020-04-22 19:07:51 +05:30
|
|
|
start_sha: start_sha,
|
|
|
|
head_sha: merge_ref_head.id)
|
|
|
|
|
|
|
|
old_diff_refs = merge_request.diff_refs
|
|
|
|
|
|
|
|
return if new_diff_refs == old_diff_refs
|
|
|
|
|
|
|
|
[old_diff_refs, new_diff_refs]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|