integrations: Add RhodeCode webhook integration.

Fixes #19627.
This commit is contained in:
Christopher Chong
2022-07-09 10:23:36 +00:00
committed by Tim Abbott
parent f93d3f37a8
commit 5362a6b555
15 changed files with 2050 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

View File

@@ -433,6 +433,7 @@ WEBHOOK_INTEGRATIONS: List[WebhookIntegration] = [
WebhookIntegration("radarr", ["entertainment"], display_name="Radarr"), WebhookIntegration("radarr", ["entertainment"], display_name="Radarr"),
WebhookIntegration("raygun", ["monitoring"], display_name="Raygun"), WebhookIntegration("raygun", ["monitoring"], display_name="Raygun"),
WebhookIntegration("reviewboard", ["version-control"], display_name="Review Board"), WebhookIntegration("reviewboard", ["version-control"], display_name="Review Board"),
WebhookIntegration("rhodecode", ["version-control"], display_name="RhodeCode"),
WebhookIntegration("semaphore", ["continuous-integration", "deployment"]), WebhookIntegration("semaphore", ["continuous-integration", "deployment"]),
WebhookIntegration("sentry", ["monitoring"]), WebhookIntegration("sentry", ["monitoring"]),
WebhookIntegration( WebhookIntegration(
@@ -773,6 +774,7 @@ DOC_SCREENSHOT_CONFIG: Dict[str, List[BaseScreenshotConfig]] = {
"radarr": [ScreenshotConfig("radarr_movie_grabbed.json")], "radarr": [ScreenshotConfig("radarr_movie_grabbed.json")],
"raygun": [ScreenshotConfig("new_error.json")], "raygun": [ScreenshotConfig("new_error.json")],
"reviewboard": [ScreenshotConfig("review_request_published.json")], "reviewboard": [ScreenshotConfig("review_request_published.json")],
"rhodecode": [ScreenshotConfig("push.json")],
"semaphore": [ScreenshotConfig("pull_request.json")], "semaphore": [ScreenshotConfig("pull_request.json")],
"sentry": [ "sentry": [
ScreenshotConfig("event_for_exception_python.json"), ScreenshotConfig("event_for_exception_python.json"),

View File

@@ -37,6 +37,9 @@ PUSH_DELETE_BRANCH_MESSAGE_TEMPLATE = (
PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_TEMPLATE = ( PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_TEMPLATE = (
"{user_name} [pushed]({compare_url}) the branch {branch_name}." "{user_name} [pushed]({compare_url}) the branch {branch_name}."
) )
PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_WITHOUT_URL_TEMPLATE = (
"{user_name} pushed the branch {branch_name}."
)
PUSH_COMMITS_MESSAGE_EXTENSION = "Commits by {}" PUSH_COMMITS_MESSAGE_EXTENSION = "Commits by {}"
PUSH_COMMITTERS_LIMIT_INFO = 3 PUSH_COMMITTERS_LIMIT_INFO = 3
@@ -81,9 +84,14 @@ def get_push_commits_event_message(
) )
if not commits_data and not deleted: if not commits_data and not deleted:
return PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_TEMPLATE.format( if compare_url:
return PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_TEMPLATE.format(
user_name=user_name,
compare_url=compare_url,
branch_name=branch_name,
)
return PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_WITHOUT_URL_TEMPLATE.format(
user_name=user_name, user_name=user_name,
compare_url=compare_url,
branch_name=branch_name, branch_name=branch_name,
) )

View File

View File

@@ -0,0 +1,20 @@
Get RhodeCode notifications in Zulip!
1. {!create-stream.md!}
1. {!create-bot-construct-url.md!}
{!git-webhook-url-with-branches.md!}
1. Go to your repository on RhodeCode and click on the **Repository
Settings** tab. Select **Integrations**. Click on **Create new
integration**. Select **Webhook**.
1. Set **Webhook URL** to the URL constructed above. Select the events
you would like to receive notifications for, and click
**Submit**. This integration supports the following events:
* repository push
{!congrats.md!}
![](/static/images/integrations/rhodecode/001.png)

View File

@@ -0,0 +1,61 @@
{
"event": {
"repo": {
"repo_name": "u/yuroitaki/zulip-testing",
"permalink_url": "https://code.rhodecode.com/_1936",
"repo_type": "git",
"extra_fields": {},
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing",
"repo_id": 1936
},
"actor_ip": "14.192.208.163",
"utc_timestamp": "2022-06-26T10:14:12.449",
"name": "repo-push",
"actor": {
"user_id": 2263,
"username": "yuroitaki"
},
"push": {
"issues": {},
"commits": [
{
"parents": [
{
"raw_id": "3d6c5671a9b74b34d21b7b545f19ae7e9553a75a"
}
],
"message_html_title": "Modify README",
"raw_id": "2b8c0ebf50710bc2e1cdb6a33071dd2435ad667c",
"author": "Yuro Itaki <yuroitaki@email.com>",
"short_id": "2b8c0ebf5071",
"branch": "master",
"revision": 1,
"permalink_url": "https://code.rhodecode.com/1936/changeset/2b8c0ebf50710bc2e1cdb6a33071dd2435ad667c",
"message_html": "Modify README\n",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/2b8c0ebf50710bc2e1cdb6a33071dd2435ad667c",
"message": "Modify README\n",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": [
"master"
]
},
"date": "2022-06-26T10:14:05",
"reviewers": []
}
],
"branches": [
{
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changelog?branch=master",
"name": "master"
}
],
"tags": []
},
"server_url": "https://code.rhodecode.com"
},
"token": ""
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,32 @@
{
"event": {
"repo": {
"repo_name": "u/yuroitaki/zulip-testing",
"permalink_url": "https://code.rhodecode.com/_1936",
"repo_type": "git",
"extra_fields": {},
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing",
"repo_id": 1936
},
"actor_ip": "14.192.212.32",
"utc_timestamp": "2022-07-10T08:07:32.747",
"name": "repo-push",
"actor": {
"user_id": 2263,
"username": "yuroitaki"
},
"push": {
"issues": {},
"commits": [],
"branches": [
{
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changelog?branch=dev",
"name": "dev"
}
],
"tags": []
},
"server_url": "https://code.rhodecode.com"
},
"token": ""
}

View File

@@ -0,0 +1,87 @@
{
"event": {
"repo": {
"repo_name": "u/yuroitaki/zulip-testing",
"permalink_url": "https://code.rhodecode.com/_1936",
"repo_type": "git",
"extra_fields": {},
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing",
"repo_id": 1936
},
"actor_ip": "14.192.212.32",
"utc_timestamp": "2022-07-10T08:22:48.930",
"name": "repo-push",
"actor": {
"user_id": 2263,
"username": "yuroitaki"
},
"push": {
"issues": {},
"commits": [
{
"parents": [
{
"raw_id": "2b8c0ebf50710bc2e1cdb6a33071dd2435ad667c"
}
],
"message_html_title": "Add test.py",
"raw_id": "b0d892e1cdd4236b1f74debca1772ea330ff5acd",
"author": "Yuro Itaki <yuroitaki@email.com>",
"short_id": "b0d892e1cdd4",
"branch": "master",
"revision": 2,
"permalink_url": "https://code.rhodecode.com/1936/changeset/b0d892e1cdd4236b1f74debca1772ea330ff5acd",
"message_html": "Add test.py\n",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/b0d892e1cdd4236b1f74debca1772ea330ff5acd",
"message": "Add test.py\n",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": []
},
"date": "2022-07-10T08:21:51",
"reviewers": []
},
{
"parents": [
{
"raw_id": "b0d892e1cdd4236b1f74debca1772ea330ff5acd"
}
],
"message_html_title": "Modify test.py",
"raw_id": "6dbae5f842f80ccb05508a1de7aace9d0f327473",
"author": "Itachi Sensei <itachisensei@email.com>",
"short_id": "6dbae5f842f8",
"branch": "master",
"revision": 3,
"permalink_url": "https://code.rhodecode.com/1936/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message_html": "Modify test.py\n",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message": "Modify test.py\n",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": [
"master"
]
},
"date": "2022-07-10T08:22:42",
"reviewers": []
}
],
"branches": [
{
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changelog?branch=master",
"name": "master"
}
],
"tags": []
},
"server_url": "https://code.rhodecode.com"
},
"token": ""
}

View File

@@ -0,0 +1,197 @@
{
"event": {
"repo": {
"repo_name": "u/yuroitaki/zulip-testing",
"permalink_url": "https://code.rhodecode.com/_1936",
"repo_type": "git",
"extra_fields": {},
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing",
"repo_id": 1936
},
"actor_ip": "14.192.212.32",
"utc_timestamp": "2022-07-10T08:22:48.930",
"name": "repo-push",
"actor": {
"user_id": 2263,
"username": "yuroitaki"
},
"push": {
"issues": {},
"commits": [
{
"parents": [
{
"raw_id": "2b8c0ebf50710bc2e1cdb6a33071dd2435ad667c"
}
],
"message_html_title": "Add test.py",
"raw_id": "b0d892e1cdd4236b1f74debca1772ea330ff5acd",
"author": "Yuro Itaki <yuroitaki@email.com>",
"short_id": "b0d892e1cdd4",
"branch": "master",
"revision": 2,
"permalink_url": "https://code.rhodecode.com/1936/changeset/b0d892e1cdd4236b1f74debca1772ea330ff5acd",
"message_html": "Add test.py\n",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/b0d892e1cdd4236b1f74debca1772ea330ff5acd",
"message": "Add test.py\n",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": []
},
"date": "2022-07-10T08:21:51",
"reviewers": []
},
{
"parents": [
{
"raw_id": "b0d892e1cdd4236b1f74debca1772ea330ff5acd"
}
],
"message_html_title": "Modify test.py",
"raw_id": "6dbae5f842f80ccb05508a1de7aace9d0f327473",
"author": "Itachi Sensei <itachisensei@email.com>",
"short_id": "6dbae5f842f8",
"branch": "master",
"revision": 3,
"permalink_url": "https://code.rhodecode.com/1936/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message_html": "Modify test.py\n",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message": "Modify test.py\n",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": [
"master"
]
},
"date": "2022-07-10T08:22:42",
"reviewers": []
},
{
"parents": [
{
"raw_id": "b0d892e1cdd4236b1f74debca1772ea330ff5acd"
}
],
"message_html_title": "Modify test.py",
"raw_id": "6dbae5f842f80ccb05508a1de7aace9d0f327473",
"author": "Itachi Sensei <itachisensei@email.com>",
"short_id": "6dbae5f842f8",
"branch": "master",
"revision": 3,
"permalink_url": "https://code.rhodecode.com/1936/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message_html": "Modify test.py\n",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message": "Modify test.py\n",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": [
"master"
]
},
"date": "2022-07-10T08:22:42",
"reviewers": []
},
{
"parents": [
{
"raw_id": "b0d892e1cdd4236b1f74debca1772ea330ff5acd"
}
],
"message_html_title": "Modify test.py",
"raw_id": "6dbae5f842f80ccb05508a1de7aace9d0f327473",
"author": "Jonas Nielsen <jonasnielsen@email.com>",
"short_id": "6dbae5f842f8",
"branch": "master",
"revision": 3,
"permalink_url": "https://code.rhodecode.com/1936/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message_html": "Modify test.py\n",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message": "Modify test.py\n",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": [
"master"
]
},
"date": "2022-07-10T08:22:42",
"reviewers": []
},
{
"parents": [
{
"raw_id": "b0d892e1cdd4236b1f74debca1772ea330ff5acd"
}
],
"message_html_title": "Modify test.py",
"raw_id": "6dbae5f842f80ccb05508a1de7aace9d0f327473",
"author": "Lelouch Strange <lelouchstrange@email.com>",
"short_id": "6dbae5f842f8",
"branch": "master",
"revision": 3,
"permalink_url": "https://code.rhodecode.com/1936/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message_html": "Modify test.py\n",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473",
"message": "Modify test.py\n",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": [
"master"
]
},
"date": "2022-07-10T08:22:42",
"reviewers": []
},
{
"parents": [
{
"raw_id": "2b8c0ebf50710bc2e1cdb6a33071dd2435ad667c"
}
],
"message_html_title": "Add test.py",
"raw_id": "b0d892e1cdd4236b1f74debca1772ea330ff5acd",
"author": "Yuro Itaki <yuroitaki@email.com>",
"short_id": "b0d892e1cdd4",
"branch": "master",
"revision": 2,
"permalink_url": "https://code.rhodecode.com/1936/changeset/b0d892e1cdd4236b1f74debca1772ea330ff5acd",
"message_html": "Add test.py\n",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/b0d892e1cdd4236b1f74debca1772ea330ff5acd",
"message": "Add test.py\n",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": []
},
"date": "2022-07-10T08:21:51",
"reviewers": []
}
],
"branches": [
{
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changelog?branch=master",
"name": "master"
}
],
"tags": []
},
"server_url": "https://code.rhodecode.com"
},
"token": ""
}

View File

@@ -0,0 +1,49 @@
{
"event": {
"repo": {
"repo_name": "u/yuroitaki/zulip-testing",
"permalink_url": "https://code.rhodecode.com/_1936",
"repo_type": "git",
"extra_fields": {},
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing",
"repo_id": 1936
},
"actor_ip": "14.192.212.32",
"utc_timestamp": "2022-07-10T09:01:54.648",
"name": "repo-push",
"actor": {
"user_id": 2263,
"username": "yuroitaki"
},
"push": {
"issues": {},
"commits": [
{
"message_html_title": "Deleted branch dev",
"raw_id": "delete_branch=>dev",
"author": "Yuro Itaki <yuroitaki@email.com>",
"reviewers": [],
"branch": null,
"short_id": "delete_branch=>dev",
"permalink_url": "https://code.rhodecode.com/1936/changeset/delete_branch=%3Edev",
"message_html": "Deleted branch dev",
"mentions": [],
"url": "https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/delete_branch=%3Edev",
"message": "Deleted branch dev",
"issues": [],
"refs": {
"tags": [],
"bookmarks": [],
"branches": []
},
"date": "2022-07-10T09:01:54.670",
"git_ref_change": "branch_delete"
}
],
"branches": [],
"tags": []
},
"server_url": "https://code.rhodecode.com"
},
"token": ""
}

View File

@@ -0,0 +1,60 @@
from unittest.mock import MagicMock, patch
from zerver.lib.test_classes import WebhookTestCase
from zerver.lib.webhooks.git import COMMITS_LIMIT
class RhodecodeHookTests(WebhookTestCase):
STREAM_NAME = "rhodecode"
URL_TEMPLATE = "/api/v1/external/rhodecode?&api_key={api_key}&stream={stream}"
WEBHOOK_DIR_NAME = "rhodecode"
def test_push_event_message(self) -> None:
expected_topic = "u/yuroitaki/zulip-testing / master"
expected_message = "yuroitaki pushed 1 commit to branch master. Commits by Yuro Itaki <yuroitaki@email.com> (1).\n\n* Modify README ([2b8c0eb](https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/2b8c0ebf50710bc2e1cdb6a33071dd2435ad667c))"
self.check_webhook("push", expected_topic, expected_message)
def test_push_event_message_filtered_by_branches(self) -> None:
self.url = self.build_webhook_url(branches="master,dev")
expected_topic = "u/yuroitaki/zulip-testing / master"
expected_message = "yuroitaki pushed 1 commit to branch master. Commits by Yuro Itaki <yuroitaki@email.com> (1).\n\n* Modify README ([2b8c0eb](https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/2b8c0ebf50710bc2e1cdb6a33071dd2435ad667c))"
self.check_webhook("push", expected_topic, expected_message)
@patch("zerver.lib.webhooks.common.check_send_webhook_message")
def test_push_event_message_filtered_by_branches_ignore(
self, check_send_webhook_message_mock: MagicMock
) -> None:
self.url = self.build_webhook_url(branches="development")
payload = self.get_body("push")
result = self.client_post(self.url, payload, content_type="application/json")
self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result)
def test_push_local_branch_without_commits(self) -> None:
expected_topic = "u/yuroitaki/zulip-testing / dev"
expected_message = "yuroitaki pushed the branch dev."
self.check_webhook("push__local_branch_without_commits", expected_topic, expected_message)
def test_push_multiple_committers(self) -> None:
expected_topic = "u/yuroitaki/zulip-testing / master"
expected_message = "yuroitaki pushed 2 commits to branch master. Commits by Itachi Sensei <itachisensei@email.com> (1) and Yuro Itaki <yuroitaki@email.com> (1).\n\n* Add test.py ([b0d892e](https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/b0d892e1cdd4236b1f74debca1772ea330ff5acd))\n* Modify test.py ([6dbae5f](https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473))"
self.check_webhook("push__multiple_committers", expected_topic, expected_message)
def test_push_multiple_committers_with_others(self) -> None:
expected_topic = "u/yuroitaki/zulip-testing / master"
commits_info = "* Modify test.py ([6dbae5f](https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/6dbae5f842f80ccb05508a1de7aace9d0f327473))\n"
expected_message = f"yuroitaki pushed 6 commits to branch master. Commits by Itachi Sensei <itachisensei@email.com> (2), Yuro Itaki <yuroitaki@email.com> (2), Jonas Nielsen <jonasnielsen@email.com> (1) and others (1).\n\n* Add test.py ([b0d892e](https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/b0d892e1cdd4236b1f74debca1772ea330ff5acd))\n{commits_info * 4}* Add test.py ([b0d892e](https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/b0d892e1cdd4236b1f74debca1772ea330ff5acd))"
self.check_webhook(
"push__multiple_committers_with_others", expected_topic, expected_message
)
def test_push_commits_more_than_limit(self) -> None:
expected_topic = "u/yuroitaki/zulip-testing / master"
commits_info = "* Modify README ([2b8c0eb](https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/2b8c0ebf50710bc2e1cdb6a33071dd2435ad667c))\n"
expected_message = f"yuroitaki pushed 50 commits to branch master. Commits by Yuro Itaki <yuroitaki@email.com> (50).\n\n{commits_info * COMMITS_LIMIT}[and {50 - COMMITS_LIMIT} more commit(s)]"
self.check_webhook("push__commits_more_than_limit", expected_topic, expected_message)
def test_push_remove_branch(self) -> None:
expected_topic = "u/yuroitaki/zulip-testing / dev"
expected_message = "yuroitaki pushed 1 commit to branch dev. Commits by Yuro Itaki <yuroitaki@email.com> (1).\n\n* Deleted branch dev ([delete_](https://code.rhodecode.com/u/yuroitaki/zulip-testing/changeset/delete_branch=%3Edev))"
self.check_webhook("push__remove_branch", expected_topic, expected_message)

View File

@@ -0,0 +1,98 @@
from typing import Callable, Dict, Optional
from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse
from zerver.decorator import webhook_view
from zerver.lib.exceptions import UnsupportedWebhookEventType
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
from zerver.lib.validator import WildValue, check_string, to_wild_value
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.lib.webhooks.git import TOPIC_WITH_BRANCH_TEMPLATE, get_push_commits_event_message
from zerver.models import UserProfile
def get_push_commits_body(payload: WildValue) -> str:
commits_data = []
for commit in payload["event"]["push"]["commits"]:
commits_data.append(
{
"name": commit["author"].tame(check_string),
"sha": commit["raw_id"].tame(check_string),
"url": commit["url"].tame(check_string),
"message": commit["message"].tame(check_string),
}
)
return get_push_commits_event_message(
get_user_name(payload),
None,
get_push_branch_name(payload),
commits_data,
)
def get_user_name(payload: WildValue) -> str:
return payload["event"]["actor"]["username"].tame(check_string)
def get_push_branch_name(payload: WildValue) -> str:
branches = payload["event"]["push"]["branches"]
try:
return branches[0]["name"].tame(check_string)
# this error happens when the event is a push to delete remote branch, where
# branches will be an empty list
except ValidationError:
return payload["event"]["push"]["commits"][0]["raw_id"].tame(check_string).split("=>")[1]
def get_event_name(payload: WildValue, branches: Optional[str]) -> Optional[str]:
event_name = payload["event"]["name"].tame(check_string)
if event_name == "repo-push":
if branches is not None:
branch = get_push_branch_name(payload)
if branches.find(branch) == -1:
return None
if event_name in EVENT_FUNCTION_MAPPER:
return event_name
raise UnsupportedWebhookEventType(event_name)
def get_repository_name(payload: WildValue) -> str:
return payload["event"]["repo"]["repo_name"].tame(check_string)
def get_topic_based_on_event(payload: WildValue, event: str) -> str:
if event == "repo-push":
return TOPIC_WITH_BRANCH_TEMPLATE.format(
repo=get_repository_name(payload), branch=get_push_branch_name(payload)
)
return get_repository_name(payload) # nocoverage
EVENT_FUNCTION_MAPPER: Dict[str, Callable[[WildValue], str]] = {
"repo-push": get_push_commits_body,
}
ALL_EVENT_TYPES = list(EVENT_FUNCTION_MAPPER.keys())
@webhook_view("RhodeCode", all_event_types=ALL_EVENT_TYPES)
@has_request_variables
def api_rhodecode_webhook(
request: HttpRequest,
user_profile: UserProfile,
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
branches: Optional[str] = REQ(default=None),
) -> HttpResponse:
event = get_event_name(payload, branches)
if event is None:
return json_success(request)
topic = get_topic_based_on_event(payload, event)
body_function = EVENT_FUNCTION_MAPPER[event]
body = body_function(payload)
check_send_webhook_message(request, user_profile, topic, body)
return json_success(request)