webhooks/gitlab: Support topic parameter to specify a topic.

Fixes #7743.
This commit is contained in:
Xavier Cooney
2018-01-09 11:06:52 +00:00
committed by showell
parent ec1297c1e8
commit e417a99830
3 changed files with 15 additions and 2 deletions

View File

@@ -8,6 +8,11 @@ if you are using this default.
{!git-webhook-url-with-branches.md!}
By default, this integration will create many topics, however by providing
a `topic` parameter, the integration will only post to one topic. You may specify a topic in the webhook URL, like so:
`{{ api_url }}{{ integration_url }}?api_key=abcdefgh&stream={{ recommended_stream_name }}&topic=specific%20topic`
Next, go to your repository page and click the gear icon. From there,
select **Webhooks**:

View File

@@ -11,6 +11,12 @@ class GitlabHookTests(WebhookTestCase):
URL_TEMPLATE = "/api/v1/external/gitlab?&api_key={api_key}&stream={stream}"
FIXTURE_DIR_NAME = 'gitlab'
def test_push_event_specified_topic(self) -> None:
self.url = self.build_webhook_url("topic=Specific%20topic")
expected_topic = u"Specific topic"
expected_message = u"Tomasz Kolek [pushed](https://gitlab.com/tomaszkolek0/my-awesome-project/compare/5fcdd5551fc3085df79bece2c32b1400802ac407...eb6ae1e591e0819dc5bf187c6bfe18ec065a80e9) 2 commits to branch tomek.\n\n* b ([66abd2d](https://gitlab.com/tomaszkolek0/my-awesome-project/commit/66abd2da28809ffa128ed0447965cf11d7f863a7))\n* c ([eb6ae1e](https://gitlab.com/tomaszkolek0/my-awesome-project/commit/eb6ae1e591e0819dc5bf187c6bfe18ec065a80e9))"
self.send_and_test_stream_message('push', expected_topic, expected_message, HTTP_X_GITLAB_EVENT="Push Hook")
def test_push_event_message(self) -> None:
expected_subject = u"my-awesome-project / tomek"
expected_message = u"Tomasz Kolek [pushed](https://gitlab.com/tomaszkolek0/my-awesome-project/compare/5fcdd5551fc3085df79bece2c32b1400802ac407...eb6ae1e591e0819dc5bf187c6bfe18ec065a80e9) 2 commits to branch tomek.\n\n* b ([66abd2d](https://gitlab.com/tomaszkolek0/my-awesome-project/commit/66abd2da28809ffa128ed0447965cf11d7f863a7))\n* c ([eb6ae1e](https://gitlab.com/tomaszkolek0/my-awesome-project/commit/eb6ae1e591e0819dc5bf187c6bfe18ec065a80e9))"

View File

@@ -264,13 +264,15 @@ EVENT_FUNCTION_MAPPER = {
@has_request_variables
def api_gitlab_webhook(request: HttpRequest, user_profile: UserProfile,
stream: Text=REQ(default='gitlab'),
topic: Text=REQ(default=None),
payload: Dict[str, Any]=REQ(argument_type='body'),
branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
event = get_event(request, payload, branches)
if event is not None:
body = get_body_based_on_event(event)(payload)
subject = get_subject_based_on_event(event, payload)
check_send_stream_message(user_profile, request.client, stream, subject, body)
if topic is None:
topic = get_subject_based_on_event(event, payload)
check_send_stream_message(user_profile, request.client, stream, topic, body)
return json_success()
def get_body_based_on_event(event: str) -> Any: