webhooks/jira: Improve punctuation and message formatting.

This commit is contained in:
Eeshan Garg
2019-05-08 23:10:38 -02:30
committed by Tim Abbott
parent ef070ef019
commit 1948924cdb
2 changed files with 84 additions and 40 deletions

View File

@@ -18,15 +18,22 @@ class JiraHookTests(WebhookTestCase):
stream_name="jira_custom",
content_type="application/json")
self.assertEqual(msg.topic_name(), "BUG-15: New bug with hook")
self.assertEqual(msg.content, """Leo Franchi **created** [BUG-15](http://lfranchi.com:8080/browse/BUG-15) priority Major, assigned to **no one**:
expected_message = """
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
> New bug with hook""")
* **Priority**: Major
* **Assignee**: no one
""".strip()
self.assertEqual(msg.content, expected_message)
def test_created(self) -> None:
expected_topic = "BUG-15: New bug with hook"
expected_message = """Leo Franchi **created** [BUG-15](http://lfranchi.com:8080/browse/BUG-15) priority Major, assigned to **no one**:
expected_message = """
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
> New bug with hook"""
* **Priority**: Major
* **Assignee**: no one
""".strip()
self.send_and_test_stream_message('created_v1', expected_topic, expected_message)
self.send_and_test_stream_message('created_v2', expected_topic, expected_message)
@@ -41,9 +48,12 @@ class JiraHookTests(WebhookTestCase):
self.assert_json_success(result)
expected_topic = "BUG-15: New bug with hook"
expected_message = """Leo Franchi **created** [BUG-15](http://lfranchi.com:8080/browse/BUG-15) priority Major, assigned to **no one**:
expected_message = """
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
> New bug with hook"""
* **Priority**: Major
* **Assignee**: no one
""".strip()
msg = self.get_last_message()
self.assertEqual(msg.content, expected_message)
self.assertEqual(msg.topic_name(), expected_topic)
@@ -59,9 +69,12 @@ class JiraHookTests(WebhookTestCase):
self.assert_json_success(result)
expected_topic = "BUG-15: New bug with hook"
expected_message = """Leo Franchi **created** [BUG-15](http://lfranchi.com:8080/browse/BUG-15) priority Major, assigned to **no one**:
expected_message = """
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
> New bug with hook"""
* **Priority**: Major
* **Assignee**: no one
""".strip()
msg = self.get_last_message()
self.assertEqual(msg.content, expected_message)
self.assertEqual(msg.topic_name(), expected_topic)
@@ -69,64 +82,80 @@ class JiraHookTests(WebhookTestCase):
def test_created_with_topic_with_spaces_double_escaped(self) -> None:
self.url = self.build_webhook_url(topic=quote(quote('alerts test')))
expected_topic = "alerts test"
expected_message = """Leo Franchi **created** [BUG-15](http://lfranchi.com:8080/browse/BUG-15) priority Major, assigned to **no one**:
expected_message = """
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
> New bug with hook"""
* **Priority**: Major
* **Assignee**: no one
""".strip()
self.send_and_test_stream_message('created_v1', expected_topic, expected_message)
def test_created_with_unicode(self) -> None:
expected_topic = u"BUG-15: New bug with à hook"
expected_message = u"""Leo Franchià **created** [BUG-15](http://lfranchi.com:8080/browse/BUG-15) priority Major, assigned to **no one**:
expected_message = """
Leo Franchià created [BUG-15: New bug with à hook](http://lfranchi.com:8080/browse/BUG-15):
> New bug with à hook"""
* **Priority**: Major
* **Assignee**: no one
""".strip()
self.send_and_test_stream_message('created_with_unicode_v1', expected_topic, expected_message)
self.send_and_test_stream_message('created_with_unicode_v2', expected_topic, expected_message)
def test_created_assignee(self) -> None:
expected_topic = "TEST-4: Test Created Assignee"
expected_message = """Leonardo Franchi [Administrator] **created** [TEST-4](https://zulipp.atlassian.net/browse/TEST-4) priority Major, assigned to **Leonardo Franchi [Administrator]**:
expected_message = """
Leonardo Franchi [Administrator] created [TEST-4: Test Created Assignee](https://zulipp.atlassian.net/browse/TEST-4):
> Test Created Assignee"""
* **Priority**: Major
* **Assignee**: Leonardo Franchi [Administrator]
""".strip()
self.send_and_test_stream_message('created_assignee_v1', expected_topic, expected_message)
self.send_and_test_stream_message('created_assignee_v2', expected_topic, expected_message)
def test_commented(self) -> None:
expected_topic = "BUG-15: New bug with hook"
expected_message = """Leo Franchi **added comment to** [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
expected_message = """
Leo Franchi commented on [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
Adding a comment. Oh, what a comment it is!"""
``` quote
Adding a comment. Oh, what a comment it is!
```
""".strip()
self.send_and_test_stream_message('commented_v1', expected_topic, expected_message)
self.send_and_test_stream_message('commented_v2', expected_topic, expected_message)
def test_comment_edited(self) -> None:
expected_topic = "BUG-15: New bug with hook"
expected_message = """Leo Franchi **edited comment on** [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
expected_message = """
Leo Franchi edited a comment on [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
Adding a comment. Oh, what a comment it is!"""
``` quote
Adding a comment. Oh, what a comment it is!
```
""".strip()
self.send_and_test_stream_message('comment_edited_v2', expected_topic, expected_message)
def test_comment_deleted(self) -> None:
expected_topic = "TOM-1: New Issue"
expected_message = "Tomasz Kolek **deleted comment from** [TOM-1: New Issue](https://zuliptomek.atlassian.net/browse/TOM-1) (assigned to **kolaszek@go2.pl**)"
expected_message = "Tomasz Kolek deleted a comment from [TOM-1: New Issue](https://zuliptomek.atlassian.net/browse/TOM-1) (assigned to **kolaszek@go2.pl**)."
self.send_and_test_stream_message('comment_deleted_v2', expected_topic, expected_message)
def test_commented_markup(self) -> None:
expected_topic = "TEST-7: Testing of rich text"
expected_message = """Leonardo Franchi [Administrator] **added comment to** [TEST-7: Testing of rich text](https://zulipp.atlassian.net/browse/TEST-7):\n\n\nThis is a comment that likes to **exercise** a lot of _different_ `conventions` that `jira uses`.\r\n\r\n~~~\n\r\nthis code is not highlighted, but monospaced\r\n\n~~~\r\n\r\n~~~\n\r\ndef python():\r\n print "likes to be formatted"\r\n\n~~~\r\n\r\n[http://www.google.com](http://www.google.com) is a bare link, and [Google](http://www.google.com) is given a title.\r\n\r\nThanks!\r\n\r\n~~~ quote\n\r\nSomeone said somewhere\r\n\n~~~"""
expected_message = """Leonardo Franchi [Administrator] commented on [TEST-7: Testing of rich text](https://zulipp.atlassian.net/browse/TEST-7):\n\n``` quote\nThis is a comment that likes to **exercise** a lot of _different_ `conventions` that `jira uses`.\r\n\r\n~~~\n\r\nthis code is not highlighted, but monospaced\r\n\n~~~\r\n\r\n~~~\n\r\ndef python():\r\n print "likes to be formatted"\r\n\n~~~\r\n\r\n[http://www.google.com](http://www.google.com) is a bare link, and [Google](http://www.google.com) is given a title.\r\n\r\nThanks!\r\n\r\n~~~ quote\n\r\nSomeone said somewhere\r\n\n~~~\n```"""
self.send_and_test_stream_message('commented_markup_v1', expected_topic, expected_message)
self.send_and_test_stream_message('commented_markup_v2', expected_topic, expected_message)
def test_deleted(self) -> None:
expected_topic = "BUG-15: New bug with hook"
expected_message = "Leo Franchi **deleted** [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15)!"
expected_message = "Leo Franchi deleted [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15)."
self.send_and_test_stream_message('deleted_v1', expected_topic, expected_message)
self.send_and_test_stream_message('deleted_v2', expected_topic, expected_message)
def test_reassigned(self) -> None:
expected_topic = "BUG-15: New bug with hook"
expected_message = """Leo Franchi **updated** [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
expected_message = """Leo Franchi updated [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
* Changed assignee to **Othello, the Moor of Venice**"""
self.send_and_test_stream_message('reassigned_v1', expected_topic, expected_message)
@@ -134,7 +163,7 @@ Adding a comment. Oh, what a comment it is!"""
def test_priority_updated(self) -> None:
expected_topic = "TEST-1: Fix That"
expected_message = """Leonardo Franchi [Administrator] **updated** [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1) (assigned to **leo@zulip.com**):
expected_message = """Leonardo Franchi [Administrator] updated [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1) (assigned to **leo@zulip.com**):
* Changed priority from **Critical** to **Major**"""
self.send_and_test_stream_message('updated_priority_v1', expected_topic, expected_message)
@@ -142,7 +171,7 @@ Adding a comment. Oh, what a comment it is!"""
def test_status_changed(self) -> None:
expected_topic = "TEST-1: Fix That"
expected_message = """Leonardo Franchi [Administrator] **updated** [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1):
expected_message = """Leonardo Franchi [Administrator] updated [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1):
* Changed status from **To Do** to **In Progress**"""
self.send_and_test_stream_message('change_status_v1', expected_topic, expected_message)

View File

@@ -1,5 +1,6 @@
# Webhooks for external integrations.
import re
import string
from typing import Any, Dict, List, Optional
from django.db.models import Q
@@ -181,24 +182,26 @@ def handle_updated_issue_event(payload: Dict[str, Any], user_profile: UserProfil
sub_event = get_sub_event_for_update_issue(payload)
if 'comment' in sub_event:
if sub_event == 'issue_commented':
verb = 'added comment to'
verb = 'commented on'
elif sub_event == 'issue_comment_edited':
verb = 'edited comment on'
verb = 'edited a comment on'
else:
verb = 'deleted comment from'
verb = 'deleted a comment from'
if payload.get('webhookEvent') == 'comment_created':
author = payload['comment']['author']['displayName']
else:
author = get_issue_author(payload)
content = u"{} **{}** {}{}".format(author, verb, issue, assignee_blurb)
content = u"{} {} {}{}".format(author, verb, issue, assignee_blurb)
comment = get_in(payload, ['comment', 'body'])
if comment:
comment = convert_jira_markup(comment, user_profile.realm)
content = u"{}:\n\n\n{}\n".format(content, comment)
content = u"{}:\n\n``` quote\n{}\n```".format(content, comment)
else:
content = "{}.".format(content)
else:
content = u"{} **updated** {}{}:\n\n".format(get_issue_author(payload), issue, assignee_blurb)
content = u"{} updated {}{}:\n\n".format(get_issue_author(payload), issue, assignee_blurb)
changelog = get_in(payload, ['changelog'])
if changelog != '':
@@ -226,17 +229,29 @@ def handle_updated_issue_event(payload: Dict[str, Any], user_profile: UserProfil
return content
def handle_created_issue_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
return u"{} **created** {} priority {}, assigned to **{}**:\n\n> {}".format(
get_issue_author(payload),
get_issue_string(payload),
get_in(payload, ['issue', 'fields', 'priority', 'name']),
get_in(payload, ['issue', 'fields', 'assignee', 'displayName'], 'no one'),
get_issue_title(payload)
template = """
{author} created {issue_string}:
* **Priority**: {priority}
* **Assignee**: {assignee}
""".strip()
return template.format(
author=get_issue_author(payload),
issue_string=get_issue_string(payload, with_title=True),
priority=get_in(payload, ['issue', 'fields', 'priority', 'name']),
assignee=get_in(payload, ['issue', 'fields', 'assignee', 'displayName'], 'no one')
)
def handle_deleted_issue_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
return u"{} **deleted** {}!".format(
get_issue_author(payload), get_issue_string(payload, with_title=True))
template = "{author} deleted {issue_string}{punctuation}"
title = get_issue_title(payload)
punctuation = '.' if title[-1] not in string.punctuation else ''
return template.format(
author=get_issue_author(payload),
issue_string=get_issue_string(payload, with_title=True),
punctuation=punctuation
)
JIRA_CONTENT_FUNCTION_MAPPER = {
"jira:issue_created": handle_created_issue_event,