Skip to content

Commit

Permalink
webhooks/gci: Improve message formatting.
Browse files Browse the repository at this point in the history
* Use Student Name instead of Task Name in subject.
* Use Task Instance URL instead of Task Definition URL (and workaround
  for a bug in the API).
  • Loading branch information
sampritipanda authored and timabbott committed Nov 28, 2017
1 parent 2d7536b commit bea653f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
24 changes: 12 additions & 12 deletions zerver/webhooks/gci/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@ class GoogleCodeInTests(WebhookTestCase):
FIXTURE_DIR_NAME = 'gci'

def test_abandon_event_message(self) -> None:
expected_subject = u'Task: Sails unspread it stopped at kearney'
expected_message = u'**student-yqqtag** abandoned the task [Sails unspread it stopped at kearney](https://0.0.0.0:8000/dashboard/tasks/6694926301528064/).'
expected_subject = u'student-yqqtag'
expected_message = u'**student-yqqtag** abandoned the task [Sails unspread it stopped at kearney](https://codein.withgoogle.com/dashboard/task-instances/6296903092273152/).'
self.send_and_test_stream_message('task_abandoned_by_student',
expected_subject, expected_message)

def test_comment_event_message(self) -> None:
expected_subject = u'Task: Sails unspread it stopped at kearney'
expected_message = u'**student-yqqtag** commented on the task [Sails unspread it stopped at kearney](https://0.0.0.0:8000/dashboard/tasks/6694926301528064/).'
expected_subject = u'student-yqqtag'
expected_message = u'**student-yqqtag** commented on the task [Sails unspread it stopped at kearney](https://codein.withgoogle.com/dashboard/task-instances/6296903092273152/).'
self.send_and_test_stream_message('student_commented_on_task',
expected_subject, expected_message)

def test_submit_event_message(self) -> None:
expected_subject = u'Task: Sails unspread it stopped at kearney'
expected_message = u'**student-yqqtag** submitted the task [Sails unspread it stopped at kearney](https://0.0.0.0:8000/dashboard/tasks/6694926301528064/).'
expected_subject = u'student-yqqtag'
expected_message = u'**student-yqqtag** submitted the task [Sails unspread it stopped at kearney](https://codein.withgoogle.com/dashboard/task-instances/6296903092273152/).'
self.send_and_test_stream_message('task_submitted_by_student',
expected_subject, expected_message)

def test_claim_event_message(self) -> None:
expected_subject = u'Task: Sails unspread it stopped at kearney'
expected_message = u'**student-yqqtag** claimed the task [Sails unspread it stopped at kearney](https://0.0.0.0:8000/dashboard/tasks/6694926301528064/).'
expected_subject = u'student-yqqtag'
expected_message = u'**student-yqqtag** claimed the task [Sails unspread it stopped at kearney](https://codein.withgoogle.com/dashboard/task-instances/6296903092273152/).'
self.send_and_test_stream_message('task_claimed_by_student',
expected_subject, expected_message)

def test_approve_event_message(self) -> None:
expected_subject = u'Task: Sails unspread it stopped at kearney'
expected_message = u'**eeshangarg** approved the task [Sails unspread it stopped at kearney](https://0.0.0.0:8000/dashboard/tasks/6694926301528064/).'
expected_subject = u'student-yqqtag'
expected_message = u'**eeshangarg** approved the task [Sails unspread it stopped at kearney](https://codein.withgoogle.com/dashboard/task-instances/6296903092273152/).'
self.send_and_test_stream_message('task_approved_by_mentor',
expected_subject, expected_message)

def test_needswork_event_message(self) -> None:
expected_subject = u'Task: Sails unspread it stopped at kearney'
expected_message = u'**eeshangarg** submitted the task [Sails unspread it stopped at kearney](http://localhost:8080/dashboard/tasks/6051711999279104/) for more work.'
expected_subject = u'student-yqqtag'
expected_message = u'**eeshangarg** submitted the task [Sails unspread it stopped at kearney](https://codein.withgoogle.com/dashboard/task-instances/5136918324969472/) for more work.'
self.send_and_test_stream_message('task_submitted_by_mentor_for_more_work',
expected_subject, expected_message)
19 changes: 11 additions & 8 deletions zerver/webhooks/gci/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
from zerver.models import UserProfile

GCI_MESSAGE_TEMPLATE = u'**{actor}** {action} the task [{task_name}]({task_url}).'
GCI_SUBJECT_TEMPLATE = u'Task: {task_name}'
GCI_SUBJECT_TEMPLATE = u'{student_name}'


def build_instance_url(instance_id):
return "https://codein.withgoogle.com/dashboard/task-instances/{}/".format(instance_id)

class UnknownEventType(Exception):
pass

Expand All @@ -20,39 +23,39 @@ def get_abandon_event_body(payload: Dict[Text, Any]) -> Text:
actor=payload['task_claimed_by'],
action='{}ed'.format(payload['event_type']),
task_name=payload['task_definition_name'],
task_url=payload['task_definition_url'],
task_url=build_instance_url(payload['task_instance']),
)

def get_submit_event_body(payload: Dict[Text, Any]) -> Text:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload['task_claimed_by'],
action='{}ted'.format(payload['event_type']),
task_name=payload['task_definition_name'],
task_url=payload['task_definition_url'],
task_url=build_instance_url(payload['task_instance']),
)

def get_comment_event_body(payload: Dict[Text, Any]) -> Text:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload['author'],
action='{}ed on'.format(payload['event_type']),
task_name=payload['task_definition_name'],
task_url=payload['task_definition_url'],
task_url=build_instance_url(payload['task_instance']),
)

def get_claim_event_body(payload: Dict[Text, Any]) -> Text:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload['task_claimed_by'],
action='{}ed'.format(payload['event_type']),
task_name=payload['task_definition_name'],
task_url=payload['task_definition_url'],
task_url=build_instance_url(payload['task_instance']),
)

def get_approve_event_body(payload: Dict[Text, Any]) -> Text:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload['author'],
action='{}d'.format(payload['event_type']),
task_name=payload['task_definition_name'],
task_url=payload['task_definition_url'],
task_url=build_instance_url(payload['task_instance']),
)

def get_needswork_event_body(payload: Dict[Text, Any]) -> Text:
Expand All @@ -61,7 +64,7 @@ def get_needswork_event_body(payload: Dict[Text, Any]) -> Text:
actor=payload['author'],
action='submitted',
task_name=payload['task_definition_name'],
task_url=payload['task_definition_url'],
task_url=build_instance_url(payload['task_instance']),
)

@api_key_only_webhook_view("Google-Code-In")
Expand All @@ -73,7 +76,7 @@ def api_gci_webhook(request, user_profile, stream=REQ(default='gci'),
if event is not None:
body = get_body_based_on_event(event)(payload)
subject = GCI_SUBJECT_TEMPLATE.format(
task_name=payload['task_definition_name']
student_name=payload['task_claimed_by']
)
check_send_stream_message(user_profile, request.client,
stream, subject, body)
Expand Down

0 comments on commit bea653f

Please sign in to comment.