def get_last_comment(self, comments, phab, raw_response): """ Helper function to get the last comment from a list of comments Args: comments (lst(str)): An list of comments associated with a revision phab (object): This is the Phabricator object to make API calls (>=0.7.0) raw_response (lst(dict)): The raw response received from individual user.query calls. Keep updating and passing this list as we call user.query Returns: LastComment (object): Returns the LastComment object that can be used in ReviewRot """ if len(comments) > 0: # Get the username for the last comment author, raw_response = self.author_data(author_phid=comments[0]['authorPHID'], phab=phab, raw_response=raw_response) # Convert the timestamp to datetime createdAt = self.time_from_epoch(comments[0]['dateCreated']) # Return the last comment return LastComment(author=author['userName'], body=comments[0]['content'], created_at=createdAt)
def test_get_last_comment(self): pagure = PagureService() res = { "comments": [ { "comment": "first comment", "date_created": "1539776992", "user1": {"name": "username"}, }, { "comment": "last comment", "date_created": "1539777081", "user": {"name": "user2"}, }, ] } last_comment = pagure.get_last_comment(res) self.assertEqual( last_comment, LastComment( author="user2", body="last comment", created_at=datetime.datetime.utcfromtimestamp(1539777081), ), )
def test_get_last_comment_containing_both_types_of_comments(self, mock_pr): github = GithubService() now = datetime.datetime.now() review_comment = test_mock.FakeGithubComment( author="user", body="a review comment", created_at=now - datetime.timedelta(minutes=1), ) issue_comment = test_mock.FakeGithubComment( author="user2", body="last issue comment", created_at=now ) mock_pr.get_comments.return_value = test_mock.FakeGithubPaginatedList( comments=[review_comment] ) mock_pr.get_issue_comments.return_value = test_mock.FakeGithubPaginatedList( comments=[issue_comment] ) last_comment = github.get_last_comment(mock_pr) self.assertEqual( last_comment, LastComment( author="user2", body="last issue comment", created_at=now ), )
def test_get_last_comment(self, mock_mr): gitlab = GitlabService() now = datetime.datetime.now() mock_mr.notes.list.return_value = [ test_mock.FakeProjectMergeRequestNote( system=True, author="Gitlab", created_at=now.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), body="System comment", ), test_mock.FakeProjectMergeRequestNote( system=False, author="user", created_at=now.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), body="last comment by user", ), test_mock.FakeProjectMergeRequestNote( system=False, author="user2", created_at=(now - datetime.timedelta(minutes=1)).strftime( "%Y-%m-%dT%H:%M:%S.%fZ" ), body="user comment", ), ] last_comment = gitlab.get_last_comment(mock_mr) self.assertEqual( last_comment, LastComment( author="user", body="last comment by user", created_at=now ), )
def test_get_last_comment(self, mock_phabricator_author_data): phab = Phabricator(host='dummy.com', token='dummy.token') comments = test_mock.mock_phabricator_get_comments(1201, phab) response = PhabricatorService().get_last_comment(comments, phab, raw_response=[]) createdAt = datetime.datetime.fromtimestamp(float(1551763640)) res = LastComment(author='dummy_user', body='This is some content', created_at=createdAt) self.assertEqual(response, res)
def test_get_last_comment(self): gerrit = GerritService() now = datetime.datetime.now() comments_response = { u"file1.py": [ { u"author": { u"username": u"user1", u"email": u"*****@*****.**", }, u"updated": now.strftime("%Y-%m-%d %H:%M:%S.%f000"), u"message": u"last comment in file1.py", } ], u"file2.py": [ { u"author": { u"username": u"user2", u"email": u"*****@*****.**", }, u"updated": (now - datetime.timedelta(days=1)).strftime( "%Y-%m-%d %H:%M:%S.%f000" ), u"message": u"#1 comment", }, { u"author": { u"username": u"user3", u"email": u"*****@*****.**", }, u"updated": (now - datetime.timedelta(minutes=1)).strftime( "%Y-%m-%d %H:%M:%S.%f000" ), u"message": u"last comment in file2.py", }, ], } last_comment = gerrit.get_last_comment(comments_response) self.assertEqual( last_comment, LastComment( author="user1", body="last comment in file1.py", created_at=now ), )
def test_get_last_comment_containing_issue_comment(self, mock_pr): github = GithubService() now = datetime.datetime.now() issue_comment = test_mock.FakeGithubComment(author="user", body="a issue comment", created_at=now) mock_pr.get_comments.return_value = test_mock.FakeGithubPaginatedList( comments=[]) mock_pr.get_issue_comments.return_value = test_mock.FakeGithubPaginatedList( comments=[issue_comment]) last_comment = github.get_last_comment(mock_pr) self.assertEqual( last_comment, LastComment(author="user", body="a issue comment", created_at=now), )
def get_last_comment(self, mr): """ Returns information about last comment of given merge request Args: mr (gitlab.v4.objects.ProjectMergeRequest): Gitlab merge request Returns: last comment (LastComment): Returns namedtuple LastComment with data related to last comment """ for note in mr.notes.list(): if not note.system: return LastComment( author=note.author['username'], body=note.body, created_at=datetime.datetime.strptime( note.created_at, '%Y-%m-%dT%H:%M:%S.%fZ'))
def get_last_comment(self, pr): """ Returns information about last comment of given pull request Args: pr (github.PullRequest.PullRequest): Github pull request Returns: last comment (LastComment): Returns namedtuple LastComment with data related to last comment """ review_comments = pr.get_comments() issue_comments = pr.get_issue_comments() last_review_comment = None last_issue_comment = None last_comment = None if review_comments.totalCount > 0: last_review_comment = review_comments.reversed[0] if issue_comments.totalCount > 0: last_issue_comment = issue_comments.reversed[0] # check which is newer if pr has both types of comments if last_issue_comment and last_review_comment: if last_issue_comment.created_at > last_review_comment.created_at: last_comment = last_issue_comment else: last_comment = last_review_comment # if pr has only one type of comment else: last_comment = last_issue_comment or last_review_comment if last_comment: return LastComment(author=last_comment.user.login, body=last_comment.body, created_at=last_comment.created_at)
def get_last_comment(self, res): """ Returns information about last comment of given pull request Args: res (dict): dictionary containing comments data of pull request Returns: last comment (LastComment): Returns namedtuple LastComment with data related to last comment """ comments = res['comments'] if comments: last_comment_date = datetime.datetime.utcfromtimestamp( int(comments[-1]['date_created'])) return LastComment(author=str(comments[-1]['user']['name']), body=str(comments[-1]['comment']), created_at=last_comment_date)
def get_last_comment(self, comments_response): """ Returns information about last comment of given pull request Args: comments_response (dict): dictionary containing comments data of gerrit review Returns: last comment (LastComment): Returns namedtuple LastComment with data related to last comment """ comments = [] for response in comments_response: if response != '/COMMIT_MSG': messages = comments_response.get(response) last_comment = messages[-1] # gerrit returns date in format # YYYY-MM-DD HH:mm:ss.000000000 # datetime.strptime is able to handle miliseconds # only up to 6 digits last_comment_date = datetime.strptime( last_comment['updated'][:-3], "%Y-%m-%d %H:%M:%S.%f") author = (last_comment['author'].get('username', None) or last_comment['author']['email']) # add last comment from every commented file to list comments.append( LastComment(author=author, body=last_comment['message'], created_at=last_comment_date)) if comments: # find last comment in list of comments return max(comments, key=lambda c: c.created_at)