Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Improve the branch filtering in Git-related integrations.
Niloth Pc32e6f49405e
diff --git a/zerver/lib/webhooks/git.py b/zerver/lib/webhooks/git.pyindex 8a15e24b0c9..f7056f92a96 100644--- a/zerver/lib/webhooks/git.py+++ b/zerver/lib/webhooks/git.py@@ -432,3 +432,14 @@ def get_all_committers(commits_data: list[dict[str, Any]]) -> list[tuple[str, incommitters_items.append(("others", others_number_of_commits))return committers_items+++def is_branch_name_notifiable(branch: str, branches: str | None) -> bool:+ """+ Check if the branch name is in the list of branches to notify.+ This helper function is used in all Git-related integrations that+ support branch filtering.+ """+ return branches is None or branch in {+ branch_name.strip() for branch_name in branches.split(",")+ }diff --git a/zerver/webhooks/azuredevops/view.py b/zerver/webhooks/azuredevops/view.pyindex 228da64e138..6da692f2d6c 100644--- a/zerver/webhooks/azuredevops/view.py+++ b/zerver/webhooks/azuredevops/view.py@@ -13,6 +13,7 @@ from zerver.lib.webhooks.git import (TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE,get_pull_request_event_message,get_push_commits_event_message,+ is_branch_name_notifiable,)from zerver.models import UserProfile@@ -143,7 +144,7 @@ def get_event_name(payload: WildValue, branches: str | None) -> str | None:event_name = payload["eventType"].tame(check_string)if event_name == "git.push" and branches is not None:branch = get_code_push_branch_name(payload)- if branches.find(branch) == -1:+ if not is_branch_name_notifiable(branch, branches):return Noneif event_name == "git.pullrequest.merged":status = payload["resource"]["status"].tame(check_string)diff --git a/zerver/webhooks/beanstalk/view.py b/zerver/webhooks/beanstalk/view.pyindex ac416dffbfe..1730b44fea9 100644--- a/zerver/webhooks/beanstalk/view.py+++ b/zerver/webhooks/beanstalk/view.py@@ -9,7 +9,11 @@ from zerver.lib.response import json_successfrom zerver.lib.typed_endpoint import typed_endpointfrom zerver.lib.validator import WildValue, check_int, check_stringfrom 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.lib.webhooks.git import (+ TOPIC_WITH_BRANCH_TEMPLATE,+ get_push_commits_event_message,+ is_branch_name_notifiable,+)from zerver.models import UserProfile@@ -66,7 +70,8 @@ def api_beanstalk_webhook(# 'uri' key that is only present for Git reposgit_repo = "uri" in payloadif git_repo:- if branches is not None and branches.find(payload["branch"].tame(check_string)) == -1:+ branch = payload["branch"].tame(check_string)+ if not is_branch_name_notifiable(branch, branches):return json_success(request)topic_name, content = build_message_from_gitlog(diff --git a/zerver/webhooks/bitbucket/view.py b/zerver/webhooks/bitbucket/view.pyindex 96e3f53a2f6..8495caadcb0 100644--- a/zerver/webhooks/bitbucket/view.py+++ b/zerver/webhooks/bitbucket/view.py@@ -5,7 +5,11 @@ from zerver.lib.response import json_successfrom zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpointfrom zerver.lib.validator import WildValue, check_stringfrom 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.lib.webhooks.git import (+ TOPIC_WITH_BRANCH_TEMPLATE,+ get_push_commits_event_message,+ is_branch_name_notifiable,+)from zerver.models import UserProfile@@ -48,7 +52,7 @@ def api_bitbucket_webhook()else:branch = payload["commits"][-1]["branch"].tame(check_string)- if branches is not None and branches.find(branch) == -1:+ if not is_branch_name_notifiable(branch, branches):return json_success(request)committer = payload.get("user", "Someone").tame(check_string)diff --git a/zerver/webhooks/bitbucket2/view.py b/zerver/webhooks/bitbucket2/view.pyindex c2632f974d2..becf7574f92 100644--- a/zerver/webhooks/bitbucket2/view.py+++ b/zerver/webhooks/bitbucket2/view.py@@ -27,6 +27,7 @@ from zerver.lib.webhooks.git import (get_push_tag_event_message,get_remove_branch_event_message,get_short_sha,+ is_branch_name_notifiable,)from zerver.models import UserProfile@@ -92,7 +93,7 @@ def api_bitbucket2_webhook(if not payload["push"]["changes"]:return json_success(request)branch = get_branch_name_for_push_event(payload)- if branch and branches and branches.find(branch) == -1:+ if branch and not is_branch_name_notifiable(branch, branches):return json_success(request)topic_names = get_push_topics(payload)diff --git a/zerver/webhooks/bitbucket3/view.py b/zerver/webhooks/bitbucket3/view.pyindex b3a4c284e3d..0d1ff39f6c3 100644--- a/zerver/webhooks/bitbucket3/view.py+++ b/zerver/webhooks/bitbucket3/view.py@@ -22,6 +22,7 @@ from zerver.lib.webhooks.git import (get_pull_request_event_message,get_push_tag_event_message,get_remove_branch_event_message,+ is_branch_name_notifiable,)from zerver.models import UserProfilefrom zerver.webhooks.bitbucket2.view import BITBUCKET_REPO_UPDATED_CHANGED, BITBUCKET_TOPIC_TEMPLATE@@ -203,7 +204,7 @@ def repo_push_handler(event_target_type = change["ref"]["type"].tame(check_string)if event_target_type == "BRANCH":branch = change["ref"]["displayId"].tame(check_string)- if branches and branch not in branches:+ if not is_branch_name_notifiable(branch, branches):continuedata.append(repo_push_branch_data(payload, change))elif event_target_type == "TAG":diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.pyindex c97885ffac9..ed1970bb5c3 100644--- a/zerver/webhooks/github/view.py+++ b/zerver/webhooks/github/view.py@@ -31,6 +31,7 @@ from zerver.lib.webhooks.git import (get_push_tag_event_message,get_release_event_message,get_short_sha,+ is_branch_name_notifiable,)from zerver.models import UserProfile@@ -1030,7 +1031,7 @@ def get_zulip_event_name(if is_commit_push_event(payload):if branches is not None:branch = get_branch_name_from_ref(payload["ref"].tame(check_string))- if branches.find(branch) == -1:+ if not is_branch_name_notifiable(branch, branches):return Nonereturn "push_commits"else:diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.pyindex 3d1cab6c9c0..6ec02bd3ff6 100644--- a/zerver/webhooks/gitlab/view.py+++ b/zerver/webhooks/gitlab/view.py@@ -27,6 +27,7 @@ from zerver.lib.webhooks.git import (get_push_commits_event_message,get_push_tag_event_message,get_remove_branch_event_message,+ is_branch_name_notifiable,)from zerver.models import UserProfile@@ -553,7 +554,7 @@ def get_event(request: HttpRequest, payload: WildValue, branches: str | None) ->event = f"{event} {action}"elif event == "Push Hook" and branches is not None:branch = get_branch_name(payload)- if branches.find(branch) == -1:+ if not is_branch_name_notifiable(branch, branches):return Noneif event in EVENT_FUNCTION_MAPPER:diff --git a/zerver/webhooks/gogs/view.py b/zerver/webhooks/gogs/view.pyindex 7fe905a06f1..6cd9282f3fa 100644--- a/zerver/webhooks/gogs/view.py+++ b/zerver/webhooks/gogs/view.py@@ -23,6 +23,7 @@ from zerver.lib.webhooks.git import (get_pull_request_event_message,get_push_commits_event_message,get_release_event_message,+ is_branch_name_notifiable,)from zerver.models import UserProfile@@ -199,7 +200,7 @@ def gogs_webhook_main(event = validate_extract_webhook_http_header(request, http_header_name, integration_name)if event == "push":branch = payload["ref"].tame(check_string).replace("refs/heads/", "")- if branches is not None and branch not in branches.split(","):+ if not is_branch_name_notifiable(branch, branches):return json_success(request)body = format_push_event(payload)topic_name = TOPIC_WITH_BRANCH_TEMPLATE.format(diff --git a/zerver/webhooks/rhodecode/view.py b/zerver/webhooks/rhodecode/view.pyindex 2795fc13368..2545d4efa63 100644--- a/zerver/webhooks/rhodecode/view.py+++ b/zerver/webhooks/rhodecode/view.py@@ -9,7 +9,11 @@ from zerver.lib.response import json_successfrom zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpointfrom zerver.lib.validator import WildValue, check_stringfrom 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.lib.webhooks.git import (+ TOPIC_WITH_BRANCH_TEMPLATE,+ get_push_commits_event_message,+ is_branch_name_notifiable,+)from zerver.models import UserProfile@@ -49,7 +53,7 @@ def get_event_name(payload: WildValue, branches: str | None) -> str | None:event_name = payload["event"]["name"].tame(check_string)if event_name == "repo-push" and branches is not None:branch = get_push_branch_name(payload)- if branches.find(branch) == -1:+ if not is_branch_name_notifiable(branch, branches):return Noneif event_name in EVENT_FUNCTION_MAPPER:return event_name