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
|
|
|
# An inline comment with the following structure that identifies
|
|
|
|
# the part of the diff:
|
|
|
|
#
|
|
|
|
# "commentAnchor": {
|
|
|
|
# "diffType": "EFFECTIVE",
|
|
|
|
# "fileType": "TO",
|
|
|
|
# "fromHash": "c5f4288162e2e6218180779c7f6ac1735bb56eab",
|
|
|
|
# "line": 1,
|
|
|
|
# "lineType": "ADDED",
|
|
|
|
# "orphaned": false,
|
|
|
|
# "path": "CHANGELOG.md",
|
|
|
|
# "toHash": "a4c2164330f2549f67c13f36a93884cf66e976be"
|
|
|
|
# }
|
2018-07-26 00:39:52 -04:00
|
|
|
#
|
|
|
|
# More details in https://docs.atlassian.com/bitbucket-server/rest/5.12.0/bitbucket-rest.html.
|
2018-06-25 16:06:10 -04:00
|
|
|
class PullRequestComment < Comment
|
2018-06-28 03:27:04 -04:00
|
|
|
def from_sha
|
|
|
|
comment_anchor['fromHash']
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
2018-06-28 03:27:04 -04:00
|
|
|
def to_sha
|
|
|
|
comment_anchor['toHash']
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
2018-06-28 03:27:04 -04:00
|
|
|
def to?
|
|
|
|
file_type == 'TO'
|
|
|
|
end
|
|
|
|
|
|
|
|
def from?
|
|
|
|
file_type == 'FROM'
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
2018-06-28 16:48:25 -04:00
|
|
|
def added?
|
2018-06-29 01:26:33 -04:00
|
|
|
line_type == 'ADDED'
|
2018-06-28 16:48:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def removed?
|
|
|
|
line_type == 'REMOVED'
|
|
|
|
end
|
|
|
|
|
2018-07-26 00:39:52 -04:00
|
|
|
# There are three line comment types: added, removed, or context.
|
|
|
|
#
|
|
|
|
# 1. An added type means a new line was inserted, so there is no old position.
|
|
|
|
# 2. A removed type means a line was removed, so there is no new position.
|
|
|
|
# 3. A context type means the line was unmodified, so there is both a
|
|
|
|
# old and new position.
|
2018-06-28 03:27:04 -04:00
|
|
|
def new_pos
|
2018-06-28 16:48:25 -04:00
|
|
|
return if removed?
|
|
|
|
return unless line_position
|
2018-06-28 03:27:04 -04:00
|
|
|
|
2018-06-28 16:48:25 -04:00
|
|
|
line_position[1]
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
2018-06-28 03:27:04 -04:00
|
|
|
def old_pos
|
2018-06-28 16:48:25 -04:00
|
|
|
return if added?
|
|
|
|
return unless line_position
|
2018-06-28 03:27:04 -04:00
|
|
|
|
2018-06-28 16:48:25 -04:00
|
|
|
line_position[0]
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
2018-06-28 03:27:04 -04:00
|
|
|
def file_path
|
|
|
|
comment_anchor.fetch('path')
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-07-26 00:39:52 -04:00
|
|
|
def file_type
|
|
|
|
comment_anchor['fileType']
|
|
|
|
end
|
|
|
|
|
2018-06-28 16:48:25 -04:00
|
|
|
def line_type
|
|
|
|
comment_anchor['lineType']
|
|
|
|
end
|
|
|
|
|
2018-07-26 00:39:52 -04:00
|
|
|
# Each comment contains the following information about the diff:
|
|
|
|
#
|
|
|
|
# hunks: [
|
|
|
|
# {
|
|
|
|
# segments: [
|
|
|
|
# {
|
|
|
|
# "lines": [
|
|
|
|
# {
|
|
|
|
# "commentIds": [ N ],
|
|
|
|
# "source": X,
|
|
|
|
# "destination": Y
|
|
|
|
# }, ...
|
|
|
|
# ] ....
|
|
|
|
#
|
|
|
|
# To determine the line position of a comment, we search all the lines
|
|
|
|
# entries until we find this comment ID.
|
2018-06-28 16:48:25 -04:00
|
|
|
def line_position
|
2018-06-29 01:26:33 -04:00
|
|
|
@line_position ||= diff_hunks.each do |hunk|
|
|
|
|
segments = hunk.fetch('segments', [])
|
|
|
|
segments.each do |segment|
|
|
|
|
lines = segment.fetch('lines', [])
|
|
|
|
lines.each do |line|
|
|
|
|
if line['commentIds']&.include?(id)
|
|
|
|
return [line['source'], line['destination']]
|
2018-06-28 16:48:25 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-29 01:26:33 -04:00
|
|
|
end
|
2018-06-28 16:48:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-28 03:27:04 -04:00
|
|
|
def comment_anchor
|
|
|
|
raw.fetch('commentAnchor', {})
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
2018-06-28 16:48:25 -04:00
|
|
|
|
|
|
|
def diff
|
|
|
|
raw.fetch('diff', {})
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff_hunks
|
|
|
|
diff.fetch('hunks', [])
|
|
|
|
end
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|