mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 04:52:12 +00:00
integrations: Improve the branch filtering in Git-related integrations.
- Made the branch-filtering checks uniform across all the integrations, by adding a helper function to git.py, and re-using it. - Instead of checking if the name of the branch that generated the event is a substring of the "branches" parameter, we now check if there's an exact match. For example, if there are two branches named "main" and "release/v1.0-main", and the user wants to track pushes to only the "release/v1.0-main" branch, they wouldn't have been able to previously, it will always track pushes to both branches. There was no way to filter out the smaller named branch when there were overlaps.
This commit is contained in:
@@ -432,3 +432,14 @@ def get_all_committers(commits_data: list[dict[str, Any]]) -> list[tuple[str, in
|
||||
committers_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(",")
|
||||
}
|
||||
|
@@ -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 None
|
||||
if event_name == "git.pullrequest.merged":
|
||||
status = payload["resource"]["status"].tame(check_string)
|
||||
|
@@ -9,7 +9,11 @@ from zerver.lib.response import json_success
|
||||
from zerver.lib.typed_endpoint import typed_endpoint
|
||||
from zerver.lib.validator import WildValue, check_int, check_string
|
||||
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.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 repos
|
||||
git_repo = "uri" in payload
|
||||
if 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(
|
||||
|
@@ -5,7 +5,11 @@ from zerver.lib.response import json_success
|
||||
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
||||
from zerver.lib.validator import WildValue, check_string
|
||||
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.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)
|
||||
|
@@ -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)
|
||||
|
@@ -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 UserProfile
|
||||
from 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):
|
||||
continue
|
||||
data.append(repo_push_branch_data(payload, change))
|
||||
elif event_target_type == "TAG":
|
||||
|
@@ -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 None
|
||||
return "push_commits"
|
||||
else:
|
||||
|
@@ -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 None
|
||||
|
||||
if event in EVENT_FUNCTION_MAPPER:
|
||||
|
@@ -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(
|
||||
|
@@ -9,7 +9,11 @@ from zerver.lib.response import json_success
|
||||
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
||||
from zerver.lib.validator import WildValue, check_string
|
||||
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.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 None
|
||||
if event_name in EVENT_FUNCTION_MAPPER:
|
||||
return event_name
|
||||
|
Reference in New Issue
Block a user