Commit 439ee8ec authored by Naveen Kumar Sangi's avatar Naveen Kumar Sangi 👽

Comment: Implement JiraComment

parent 44c88d77
"""
Represents the comment resource from JIRA.
"""
from datetime import datetime
from typing import Union
from IGitt.Interfaces import delete
from IGitt.Interfaces import put
from IGitt.Interfaces import BasicAuthorizationToken
from IGitt.Interfaces.Comment import Comment
from IGitt.Interfaces.Comment import CommentType
from IGitt.Interfaces.User import User
from IGitt.Jira import JiraMixin
from IGitt.Jira import JiraOAuth1Token
class JiraComment(JiraMixin, Comment):
"""
Represents a comment on JIRA, mainly with a body and author - oh and it's
deletable!
"""
def __init__(self,
token: Union[BasicAuthorizationToken, JiraOAuth1Token],
issue_id: Union[int, str],
comment_id: int):
"""
Instantiates a new JiraComment instance from the given data.
:param token:
The token to be used for authentication.
:param issue_id:
The unique identifier or key for the issue on JIRA.
:param comment_id:
The unique identifier of the comment on JIRA.
"""
self._token = token
self._id = comment_id
self._issue = issue_id
self._url = '/issue/{}/comment/{}'.format(self._issue, self._id)
@property
def number(self) -> int:
"""
Retrieves the id of the comment.
"""
return self._id
@property
def type(self) -> CommentType:
"""
Retrieves the type of the comment it links to.
"""
return CommentType.ISSUE
@property
def body(self):
"""
Retrieves the content of the comment:
:return: A string containing the body.
"""
return self.data['body']
@body.setter
def body(self, new_body: str):
"""
Modifies the comment body to ``new_body``.
:param new_body: A string containing comment body.
"""
payload = {'body': new_body}
self.data = put(self._token, self.url, payload)
@property
def author(self) -> User:
"""
Retrieves the author of the comment.
:return: A User object.
"""
raise NotImplementedError
@property
def created(self) -> datetime:
"""
Retrieves a timestamp on when the comment was created.
:return: A datetime.datetime object representing the time of
creation of the comment.
"""
return datetime.strptime(self.data['created'],
'%Y-%m-%dT%H:%M:%S.%f%z')
@property
def updated(self) -> datetime:
"""
Retrieves a timestamp on when the comment was updated the last time.
:return: A datetime.datetime object representing the time of the
last update of the comment.
"""
return datetime.strptime(self.data['updated'],
'%Y-%m-%dT%H:%M:%S.%f%z')
def delete(self):
"""
Deletes the comment.
"""
delete(self._token, self.url)
@property
def repository(self):
"""
Returns the JIRA project this comment was posted in, as a
JiraRepository instance.
"""
raise NotImplementedError
interactions:
- request:
body: '{}'
headers:
Accept:
- !!binary |
Ki8q
Accept-Encoding:
- !!binary |
Z3ppcCwgZGVmbGF0ZQ==
Connection:
- !!binary |
a2VlcC1hbGl2ZQ==
Content-Length: ['2']
Content-Type:
- !!binary |
YXBwbGljYXRpb24vanNvbg==
User-Agent:
- !!binary |
SUdpdHQ=
method: GET
uri: https://jira.gitmate.io/rest/api/2/issue/10001/comment/10001?per_page=100
response:
body:
string: !!binary |
H4sIAAAAAAAAAOyUS2vcMBDHv4rQoZc41mMdr9ewJJu0p9JemlxKIGit8VpZyzJ67IOQ714p7osQ
KIU95iI0f41+M2L+6Ak76Ftc48770dWEPCor8o3yWnjIlSEWnCdiVIQT5VwAwiiljDRGaxj8FOEM
KxkZvwIRfGcsrp/+Ax4c2Mu0DELD8hi26kG5h7WxICMxiZHzWt7C8Q0VtFD9SspIdz+Pr/4UTf3t
hBf2zvYu9VhUh6L6q8n9fp9v7JSTx3eSaUtAgmgXbC6Ldl62i9mar5u2ZO0FXYAsgV7KpdYf3DLC
MsyLAy9OCI2wDLPywMoTQiMswzN+mPETQiPsOcNSubEXx6/T3L6ETmgtJPoslBMWrey2E0OaROPV
LmZ4GyDDXmn4boZ045NvyN3tTUKtjUxTvrEQBygRQ1IcHRIbc2/vh2vRbHuzQd6gb9BDkzJaY9FH
2EFvxuRRdIE6E6xDjCOthuDh5XasHkYZkat3t7679WRubSabRpFTVp3T2Tkrb2lV06Lm83xRVmfx
k6S/zffPxOcfAAAA//8DAPbolYeiBQAA
headers:
Cache-Control: ['no-cache, no-store, no-transform']
Content-Encoding: [gzip]
Content-Length: ['423']
Content-Security-Policy: [frame-ancestors 'self']
Content-Type: [application/json;charset=UTF-8]
Date: ['Fri, 23 Mar 2018 13:26:05 GMT']
Set-Cookie: [JSESSIONID=0333AC2A25040CA2E8F8CFAA4C2540FB;path=/;HttpOnly, atlassian.xsrf.token=B15V-IRV7-Q0YS-ZCZZ|0d83180fc0d3740d23d2efa3bc587db1beee81ea|lin;path=/]
Strict-Transport-Security: [max-age=315569260; preload]
Vary: [User-Agent]
X-Arequestid: [806x7133x1]
X-Asen: [SEN-L11373089]
X-Asessionid: [1vajkol]
X-Ausername: [nkprince007]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [DENY, SAMEORIGIN]
X-Seraph-Loginreason: [OK]
X-Xss-Protection: [1; mode=block, 1; mode=block]
status: {code: 200, message: OK}
version: 1
interactions:
- request:
body: '{"body": "test comment body has changed"}'
headers:
Accept:
- !!binary |
Ki8q
Accept-Encoding:
- !!binary |
Z3ppcCwgZGVmbGF0ZQ==
Connection:
- !!binary |
a2VlcC1hbGl2ZQ==
Content-Length: ['41']
Content-Type:
- !!binary |
YXBwbGljYXRpb24vanNvbg==
User-Agent:
- !!binary |
SUdpdHQ=
method: PUT
uri: https://jira.gitmate.io/rest/api/2/issue/10001/comment/10001
response:
body:
string: !!binary |
H4sIAAAAAAAAAKyUS2+cMBRG/wryopsQ/GI8gDRKR1VXUbPpZJNN5MFmcMGAbDMPRfnvtUPaPBRV
M81sEL5cjq/1HfkBWNlWoAC1c4MtIPylDE82ymnuZKJ6aKR1kA8KEqisHSXECCEMy15r2blpBWKg
hGf8WfDR1b0BxcMJ8NFKcxUeHddycRgbda/s/bo3UnhiKHrO+3IjDx9UpeaqXQrh6fb589eXTcN8
W+64uTWtDTOm2T7NXg252+2SjZl6En9OOL1CKSSvcjwXaTVnVU7XZF1WDFczlEvBJLoSC62/2IWH
xYCke5KeEephMcBsj9kZoR4WA0r2lJwR6mGPMRDKDi0/3Ey5/RhrrjUX0TVXlptoaZqadyGJ0qmt
73BmlDFwSsu7vgt/fHclvF19C6h1L0LKzrsSPWsXhVpUcxuVnrN5Sn0chA94+Wn1umYwqislQvMX
8d4WJ+3e1t5J1/GtlF1i/XRnkS9DBM9yWnHBZmTGM8xShtFaVBnOaEXof8l3LPQk+Y6FniTfsdCP
5Lt5iiL6GaL4l3JLqzi87tvG7xgopZE+tHCvEYSzS0QvMVuhrEBpQeZJzrILf92hv+a9biR0hWlB
WIFRgiidGh9/AwAA//8DAHs5VCBsBQAA
headers:
Cache-Control: ['no-cache, no-store, no-transform']
Content-Encoding: [gzip]
Content-Length: ['480']
Content-Security-Policy: [frame-ancestors 'self']
Content-Type: [application/json;charset=UTF-8]
Date: ['Fri, 23 Mar 2018 13:26:10 GMT']
Location: ['https://jira.gitmate.io/rest/api/2/issue/10001/comment/10001']
Set-Cookie: [JSESSIONID=E0D51E209AE17030C16B5F8E359B0AFF;path=/;HttpOnly, atlassian.xsrf.token=B15V-IRV7-Q0YS-ZCZZ|15f593a72d2e331a44767632f0d46f39ef9bebb4|lin;path=/]
Strict-Transport-Security: [max-age=315569260; preload]
Vary: [User-Agent]
X-Arequestid: [806x7134x1]
X-Asen: [SEN-L11373089]
X-Asessionid: [kshq35]
X-Ausername: [nkprince007]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [DENY, SAMEORIGIN]
X-Seraph-Loginreason: [OK]
X-Xss-Protection: [1; mode=block, 1; mode=block]
status: {code: 200, message: OK}
version: 1
interactions:
- request:
body: '{}'
headers:
Accept:
- !!binary |
Ki8q
Accept-Encoding:
- !!binary |
Z3ppcCwgZGVmbGF0ZQ==
Connection:
- !!binary |
a2VlcC1hbGl2ZQ==
Content-Length: ['2']
Content-Type:
- !!binary |
YXBwbGljYXRpb24vanNvbg==
User-Agent:
- !!binary |
SUdpdHQ=
method: GET
uri: https://jira.gitmate.io/rest/api/2/issue/10001/comment/10001?per_page=100
response:
body:
string: !!binary |
H4sIAAAAAAAAAKyUS2+cMBRG/wryopsQ/GI8gDRKR1VXUbPpZJNN5MFmcMGAbDMPRfnvtUPaPBRV
M81sEL5cjq/1HfkBWNlWoAC1c4MtIPylDE82ymnuZKJ6aKR1kA8KEqisHSXECCEMy15r2blpBWKg
hGf8WfDR1b0BxcMJ8NFKcxUeHddycRgbda/s/bo3UnhiKHrO+3IjDx9UpeaqXQrh6fb589eXTcN8
W+64uTWtDTOm2T7NXg252+2SjZl6En9OOL1CKSSvcjwXaTVnVU7XZF1WDFczlEvBJLoSC62/2IWH
xYCke5KeEephMcBsj9kZoR4WA0r2lJwR6mGPMRDKDi0/3Ey5/RhrrjUX0TVXlptoaZqadyGJ0qmt
73BmlDFwSsu7vgt/fHclvF19C6h1L0LKzrsSPWsXhVpUcxuVnrN5Sn0chA94+Wn1umYwqislQvMX
8d4WJ+3e1t5J1/GtlF1i/XRnkS9DBM9yWnHBZmTGM8xShtFaVBnOaEXof8l3LPQk+Y6FniTfsdCP
5Lt5iiL6GaL4l3JLqzi87tvG7xgopZE+tHCvEYSzS0QvMVuhrEBpQeZJzrILf92hv+a9biR0hWlB
WIFRgiidGh9/AwAA//8DAHs5VCBsBQAA
headers:
Cache-Control: ['no-cache, no-store, no-transform']
Content-Encoding: [gzip]
Content-Length: ['480']
Content-Security-Policy: [frame-ancestors 'self']
Content-Type: [application/json;charset=UTF-8]
Date: ['Fri, 23 Mar 2018 13:26:14 GMT']
Set-Cookie: [JSESSIONID=8E3C855DE3EA8977F4329A7B467214ED;path=/;HttpOnly, atlassian.xsrf.token=B15V-IRV7-Q0YS-ZCZZ|81a00b6fa99ef1f98811ad4fcf1764e254702fff|lin;path=/]
Strict-Transport-Security: [max-age=315569260; preload]
Vary: [User-Agent]
X-Arequestid: [806x7135x1]
X-Asen: [SEN-L11373089]
X-Asessionid: [o3vpdg]
X-Ausername: [nkprince007]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [DENY, SAMEORIGIN]
X-Seraph-Loginreason: [OK]
X-Xss-Protection: [1; mode=block, 1; mode=block]
status: {code: 200, message: OK}
version: 1
import os
import datetime
import requests_mock
from IGitt.Jira import JiraOAuth1Token
from IGitt.Jira.JiraComment import JiraComment
from IGitt.Interfaces.Comment import CommentType
from tests import IGittTestCase
class JiraCommentTest(IGittTestCase):
def setUp(self):
self.token = JiraOAuth1Token(os.environ['JIRA_CLIENT_KEY'],
os.environ['JIRA_TEST_TOKEN'],
os.environ['JIRA_TEST_SECRET'])
self.comment = JiraComment(self.token, 10001, 10001)
def test_number(self):
self.assertEqual(self.comment.number, 10001)
def test_type(self):
self.assertEqual(self.comment.type, CommentType.ISSUE)
def test_body(self):
self.assertEqual(self.comment.body,
'Created 1 days ago\r\nBacklog to Selected for '
'Development 5 hours 12 minutes ago')
def test_body_setter(self):
self.comment.body = 'test comment body has changed'
self.assertEqual(self.comment.body, 'test comment body has changed')
def test_author(self):
with self.assertRaises(NotImplementedError):
self.assertEqual(self.comment.author, 'yuki_is_bored')
def test_time(self):
self.assertEqual(self.comment.created,
datetime.datetime(2018, 3, 16, 8, 4, 27, 968000,
tzinfo=datetime.timezone.utc))
self.assertEqual(self.comment.updated,
datetime.datetime(2018, 3, 23, 13, 26, 10, 33000,
tzinfo=datetime.timezone.utc))
def test_delete(self):
with requests_mock.Mocker() as m:
m.delete(requests_mock.ANY, text='{}')
self.comment.delete()
def test_repository(self):
with self.assertRaises(NotImplementedError):
self.assertEqual(self.comment.repository.full_name,
'gitmate-test-user/test')
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