webhooks/gogs: Include title in message body if not in topic.

This is a follow-up in response to Tim's comments on #9951.

In instances where all messages from a Gogs integration are
grouped under one user specified topic (specified in the URL), we
should include the title of the PR in the message body, since
the availability of a user-specified topic precludes us from
including it in the topic itself (which was the default behaviour).
This commit is contained in:
Eeshan Garg 2018-07-23 15:44:24 -02:30 committed by Tim Abbott
parent e1df70f61f
commit 85b555b1a8
2 changed files with 16 additions and 3 deletions

View File

@ -69,6 +69,13 @@ class GogsHookTests(WebhookTestCase):
from `feature` to `master`"""
self.send_and_test_stream_message('pull_request_opened', expected_subject, expected_message, HTTP_X_GOGS_EVENT='pull_request')
def test_pull_request_opened_with_custom_topic_in_url(self) -> None:
self.url = self.build_webhook_url(topic='notifications')
expected_subject = u"notifications"
expected_message = u"""john opened [PR #1 Title Text for Pull Request](http://localhost:3000/john/try-git/pulls/1)
from `feature` to `master`"""
self.send_and_test_stream_message('pull_request_opened', expected_subject, expected_message, HTTP_X_GOGS_EVENT='pull_request')
def test_pull_request_closed(self) -> None:
expected_subject = u"try-git / PR #1 Title Text for Pull Request"
expected_message = u"""john closed [PR #1](http://localhost:3000/john/try-git/pulls/1)

View File

@ -43,7 +43,8 @@ def format_new_branch_event(payload: Dict[str, Any]) -> str:
}
return get_create_branch_event_message(**data)
def format_pull_request_event(payload: Dict[str, Any]) -> str:
def format_pull_request_event(payload: Dict[str, Any],
include_title: Optional[bool]=False) -> str:
data = {
'user_name': payload['pull_request']['user']['username'],
@ -52,6 +53,7 @@ def format_pull_request_event(payload: Dict[str, Any]) -> str:
'number': payload['pull_request']['number'],
'target_branch': payload['pull_request']['head_branch'],
'base_branch': payload['pull_request']['base_branch'],
'title': payload['pull_request']['title'] if include_title else None
}
if payload['pull_request']['merged']:
@ -64,7 +66,8 @@ def format_pull_request_event(payload: Dict[str, Any]) -> str:
@has_request_variables
def api_gogs_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'),
branches: Optional[str]=REQ(default=None)) -> HttpResponse:
branches: Optional[str]=REQ(default=None),
user_specified_topic: Optional[str]=REQ("topic", default=None)) -> HttpResponse:
repo = payload['repository']['name']
event = validate_extract_webhook_http_header(request, 'X_GOGS_EVENT', 'Gogs')
@ -84,7 +87,10 @@ def api_gogs_webhook(request: HttpRequest, user_profile: UserProfile,
branch=payload['ref']
)
elif event == 'pull_request':
body = format_pull_request_event(payload)
body = format_pull_request_event(
payload,
include_title=user_specified_topic is not None
)
topic = SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=repo,
type='PR',