2018-07-25 08:22:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-28 03:27:04 -04:00
|
|
|
module BitbucketServer
|
2018-06-25 16:06:10 -04:00
|
|
|
module Representation
|
2018-06-28 03:27:04 -04:00
|
|
|
# A general comment with the structure:
|
|
|
|
# "comment": {
|
|
|
|
# "author": {
|
|
|
|
# "active": true,
|
|
|
|
# "displayName": "root",
|
|
|
|
# "emailAddress": "stanhu+bitbucket@gitlab.com",
|
|
|
|
# "id": 1,
|
|
|
|
# "links": {
|
|
|
|
# "self": [
|
|
|
|
# {
|
|
|
|
# "href": "http://localhost:7990/users/root"
|
|
|
|
# }
|
|
|
|
# ]
|
|
|
|
# },
|
|
|
|
# "name": "root",
|
|
|
|
# "slug": "root",
|
|
|
|
# "type": "NORMAL"
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
2018-06-25 16:06:10 -04:00
|
|
|
class Comment < Representation::Base
|
2018-07-31 14:12:10 -04:00
|
|
|
attr_reader :parent_comment
|
|
|
|
|
|
|
|
CommentNode = Struct.new(:raw_comments, :parent)
|
|
|
|
|
|
|
|
def initialize(raw, parent_comment: nil)
|
|
|
|
super(raw)
|
|
|
|
|
|
|
|
@parent_comment = parent_comment
|
|
|
|
end
|
|
|
|
|
2018-06-28 03:27:04 -04:00
|
|
|
def id
|
2018-06-28 03:50:10 -04:00
|
|
|
raw_comment['id']
|
2018-06-28 03:27:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def author_username
|
2020-09-14 14:09:48 -04:00
|
|
|
author['username'] ||
|
|
|
|
author['slug'] ||
|
|
|
|
author['displayName']
|
2018-06-28 03:27:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def author_email
|
2018-07-25 23:53:25 -04:00
|
|
|
author['emailAddress']
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def note
|
2018-06-28 03:50:10 -04:00
|
|
|
raw_comment['text']
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def created_at
|
2018-07-29 00:48:37 -04:00
|
|
|
self.class.convert_timestamp(created_date)
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def updated_at
|
2018-07-29 00:48:37 -04:00
|
|
|
self.class.convert_timestamp(created_date)
|
2018-06-28 03:27:04 -04:00
|
|
|
end
|
|
|
|
|
2018-07-25 23:53:25 -04:00
|
|
|
# Bitbucket Server supports the ability to reply to any comment
|
|
|
|
# and created multiple threads. It represents these as a linked list
|
|
|
|
# of comments within comments. For example:
|
|
|
|
#
|
|
|
|
# "comments": [
|
|
|
|
# {
|
|
|
|
# "author" : ...
|
|
|
|
# "comments": [
|
|
|
|
# {
|
|
|
|
# "author": ...
|
|
|
|
#
|
|
|
|
# Since GitLab only supports a single thread, we flatten all these
|
|
|
|
# comments into a single discussion.
|
2018-06-28 03:27:04 -04:00
|
|
|
def comments
|
2018-07-31 14:12:10 -04:00
|
|
|
@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
|
|
|
|
|
2018-06-28 03:27:04 -04:00
|
|
|
all_comments = []
|
|
|
|
|
|
|
|
until workset.empty?
|
2018-07-31 14:12:10 -04:00
|
|
|
node = workset.pop
|
|
|
|
parent = node.parent
|
2018-06-28 03:27:04 -04:00
|
|
|
|
2018-07-31 14:12:10 -04:00
|
|
|
node.raw_comments.each do |comment|
|
2018-06-28 03:27:04 -04:00
|
|
|
new_comments = comment.delete('comments')
|
2018-07-31 14:12:10 -04:00
|
|
|
current_comment = Comment.new({ 'comment' => comment }, parent_comment: parent)
|
|
|
|
all_comments << current_comment
|
|
|
|
workset << CommentNode.new(new_comments, current_comment) if new_comments
|
2018-06-28 03:27:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
all_comments
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
2018-06-28 03:50:10 -04:00
|
|
|
def raw_comment
|
|
|
|
raw.fetch('comment', {})
|
|
|
|
end
|
|
|
|
|
2018-06-28 03:27:04 -04:00
|
|
|
def author
|
2018-07-29 00:50:48 -04:00
|
|
|
raw_comment['author']
|
2018-06-28 03:27:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def created_date
|
2018-07-29 00:50:48 -04:00
|
|
|
raw_comment['createdDate']
|
2018-06-28 03:27:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def updated_date
|
2018-07-29 00:50:48 -04:00
|
|
|
raw_comment['updatedDate']
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|