webhooks: Fix handling of GitLab Job Hook events.

This fixes an exception for these events.

Fixture from https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#job-events.
This commit is contained in:
Tim Abbott
2020-05-11 14:36:05 -07:00
parent 6baf95d88a
commit 11027161c3
2 changed files with 15 additions and 1 deletions

View File

@@ -547,6 +547,14 @@ class GitlabHookTests(WebhookTestCase):
self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result)
def test_job_hook_event(self) -> None:
expected_topic = "gitlab_test / gitlab-script-trigger"
expected_message = "Build test from test stage was created."
self.send_and_test_stream_message(
'job_hook__build_created',
expected_topic,
expected_message)
def test_system_push_event_message(self) -> None:
expected_topic = "gitlab / master"
expected_message = "John Smith [pushed](http://test.example.com/gitlab/gitlab/compare/95790bf891e76fee5e1747ab589903a6a1f80f22...da1560886d4f094c3e6c9ef40349f7d38b5d27d7) 1 commit to branch master. Commits by Test User (1).\n\n* Add simple search to projects in public area ([c5feabd](https://test.example.com/gitlab/gitlab/-/commit/c5feabde2d8cd023215af4d2ceeb7a64839fc428))"

View File

@@ -273,7 +273,13 @@ def get_pipeline_event_body(payload: Dict[str, Any]) -> str:
return "[Pipeline]({}) {} with build(s):\n{}.".format(pipeline_url, action, builds_status[:-1])
def get_repo_name(payload: Dict[str, Any]) -> str:
return payload['project']['name']
if 'project' in payload:
return payload['project']['name']
# Apparently, Job Hook payloads don't have a `project` section,
# but the repository name is accessible from the `repository`
# section.
return payload['repository']['name']
def get_user_name(payload: Dict[str, Any]) -> str:
return payload['user_name']