def asana_comment_from_github_review(review: Review) -> str: """ Extracts the GitHub author and comments from a GitHub Review, and transforms them into a suitable html comment string for Asana. This will involve looking up the GitHub author in DynamoDb to determine the Asana domain user id of the review author and any @mentioned GitHub users. """ user_display_name = _asana_display_name_for_github_user(review.author()) if review.is_just_comments(): # When a user replies to an inline comment, # or writes inline comments without a review, # github still creates a Review object, # even though nothing in github looks like a review # If that's the case, there is no meaningful review state ("commented" isn't helpful) # and the link to it will either not point anywhere, or be less useful than the individual links on each comment. review_action = _wrap_in_tag("strong")("left inline comments:\n") else: review_action = _wrap_in_tag("A", attrs={"href": review.url()})( _review_action_to_text_map.get(review.state(), "commented")) review_body = _format_github_text_for_asana(review.body()) if review_body: header = (_wrap_in_tag("strong") (f"{user_display_name} {review_action} :\n") + review_body) else: header = _wrap_in_tag("strong")(f"{user_display_name} {review_action}") # For each comment, prefix its text with a bracketed number that is a link to the Github comment. inline_comments = [ _wrap_in_tag("li") (_wrap_in_tag("A", attrs={"href": comment.url()})(f"[{i}] ") + _format_github_text_for_asana(comment.body())) for i, comment in enumerate(review.comments(), start=1) ] if inline_comments: comments_html = _wrap_in_tag("ul")("".join(inline_comments)) if not review.is_just_comments(): # If this was an inline reply, we already added "and left inline comments" above. comments_html = ( _wrap_in_tag("strong")("\n\nand left inline comments:\n") + comments_html) else: comments_html = "" return _wrap_in_tag("body")(header + comments_html)
def test_with_status_of_approved__is_just_comments_is_false(self): raw_review = {"state": "APPROVED", "body": ""} review = Review(raw_review) self.assertEqual(review.is_just_comments(), False)
def test_with_status_of_commented_and_populated_body__is_just_comments_is_false( self, ): raw_review = {"state": "COMMENTED", "body": "Here's a body!"} review = Review(raw_review) self.assertEqual(review.is_just_comments(), False)
def test_with_status_of_commented_and_empty_body__is_just_comments_is_true( self): raw_review = {"state": "COMMENTED", "body": ""} review = Review(raw_review) self.assertEqual(review.is_just_comments(), True)
def test_with_status_of_changes_requested__is_just_comments_is_false(self): raw_review = {"state": "CHANGES_REQUESTED", "body": ""} review = Review(raw_review) self.assertEqual(review.is_just_comments(), False)