Commit 710d8210 authored by Stan Hu's avatar Stan Hu

Track parental relationships in comments

parent 89d164fb
......@@ -23,6 +23,16 @@ module BitbucketServer
# }
# }
class Comment < Representation::Base
attr_reader :parent_comment
CommentNode = Struct.new(:raw_comments, :parent)
def initialize(raw, parent_comment: nil)
super(raw)
@parent_comment = parent_comment
end
def id
raw_comment['id']
end
......@@ -61,24 +71,45 @@ module BitbucketServer
# Since GitLab only supports a single thread, we flatten all these
# comments into a single discussion.
def comments
workset = [raw_comment['comments']].compact
@comments ||= flatten_comments
end
private
# In order to provide context for each reply, we need to track
# the parent of each comment. This method works as follows:
#
# 1. Insert the root comment into the workset. The root element is the current note.
# 2. For each node in the workset:
# a. Examine if it has replies to that comment. If it does,
# insert that node into the workset.
# b. Parse that note into a Comment structure and add it to a flat list.
def flatten_comments
comments = raw_comment['comments']
workset =
if comments
[CommentNode.new(comments, self)]
else
[]
end
all_comments = []
until workset.empty?
comments = workset.pop
node = workset.pop
parent = node.parent
comments.each do |comment|
node.raw_comments.each do |comment|
new_comments = comment.delete('comments')
workset << new_comments if new_comments
all_comments << Comment.new({ 'comment' => comment })
current_comment = Comment.new({ 'comment' => comment }, parent_comment: parent)
all_comments << current_comment
workset << CommentNode.new(new_comments, current_comment) if new_comments
end
end
all_comments
end
private
def raw_comment
raw.fetch('comment', {})
end
......
......@@ -34,5 +34,22 @@ describe BitbucketServer::Representation::Comment do
it { expect(subject.comments.count).to eq(4) }
it { expect(subject.comments).to all( be_a(described_class) ) }
it { expect(subject.comments.map(&:note)).to match_array(["Hello world", "Ok", "hello", "hi"]) }
# The thread should look like:
#
# is this a new line? (subject)
# -> Hello world (first)
# -> Ok (third)
# -> Hi (fourth)
# -> hello (second)
it 'comments have the right parent' do
first, second, third, fourth = subject.comments[0..4]
expect(subject.parent_comment).to be_nil
expect(first.parent_comment).to eq(subject)
expect(second.parent_comment).to eq(subject)
expect(third.parent_comment).to eq(first)
expect(fourth.parent_comment).to eq(first)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment