integrations: Improve GitHub force push notifications.

Previously, we didn't explicitly display something special for force-pushes.

Fixes #21969.
This commit is contained in:
sbansal1999 2023-05-04 20:42:59 +05:30 committed by Tim Abbott
parent dba6f84b97
commit 2a3d4041e0
3 changed files with 12 additions and 6 deletions

View File

@ -13,8 +13,8 @@ COMMIT_ROW_TEMPLATE = "* {commit_msg} ([{commit_short_sha}]({commit_url}))\n"
COMMITS_MORE_THAN_LIMIT_TEMPLATE = "[and {commits_number} more commit(s)]"
COMMIT_OR_COMMITS = "commit{}"
PUSH_PUSHED_TEXT_WITH_URL = "[pushed]({compare_url}) {number_of_commits} {commit_or_commits}"
PUSH_PUSHED_TEXT_WITHOUT_URL = "pushed {number_of_commits} {commit_or_commits}"
PUSH_PUSHED_TEXT_WITH_URL = "[{push_type}]({compare_url}) {number_of_commits} {commit_or_commits}"
PUSH_PUSHED_TEXT_WITHOUT_URL = "{push_type} {number_of_commits} {commit_or_commits}"
PUSH_COMMITS_BASE = "{user_name} {pushed_text} to branch {branch_name}."
PUSH_COMMITS_MESSAGE_TEMPLATE_WITH_COMMITTERS = (
@ -35,10 +35,10 @@ PUSH_DELETE_BRANCH_MESSAGE_TEMPLATE = (
"{user_name} [deleted]({compare_url}) the branch {branch_name}."
)
PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_TEMPLATE = (
"{user_name} [pushed]({compare_url}) the branch {branch_name}."
"{user_name} [{push_type}]({compare_url}) the branch {branch_name}."
)
PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_WITHOUT_URL_TEMPLATE = (
"{user_name} pushed the branch {branch_name}."
"{user_name} {push_type} the branch {branch_name}."
)
PUSH_COMMITS_MESSAGE_EXTENSION = "Commits by {}"
PUSH_COMMITTERS_LIMIT_INFO = 3
@ -89,6 +89,7 @@ def get_push_commits_event_message(
commits_data: List[Dict[str, Any]],
is_truncated: bool = False,
deleted: bool = False,
force_push: Optional[bool] = False,
) -> str:
if not commits_data and deleted:
return PUSH_DELETE_BRANCH_MESSAGE_TEMPLATE.format(
@ -97,14 +98,17 @@ def get_push_commits_event_message(
branch_name=branch_name,
)
push_type = "force pushed" if force_push else "pushed"
if not commits_data and not deleted:
if compare_url:
return PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_TEMPLATE.format(
push_type=push_type,
user_name=user_name,
compare_url=compare_url,
branch_name=branch_name,
)
return PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_WITHOUT_URL_TEMPLATE.format(
push_type=push_type,
user_name=user_name,
branch_name=branch_name,
)
@ -114,6 +118,7 @@ def get_push_commits_event_message(
)
pushed_text_message = pushed_message_template.format(
push_type=push_type,
compare_url=compare_url,
number_of_commits=len(commits_data),
commit_or_commits=COMMIT_OR_COMMITS.format("s" if len(commits_data) > 1 else ""),

View File

@ -38,11 +38,11 @@ class GitHubWebhookTest(WebhookTestCase):
self.check_webhook("push__delete_branch", "public-repo / feature", expected_message)
def test_push_force_1_commit(self) -> None:
expected_message = "sbansal1999 [pushed](https://github.com/sbansal1999/zulip/compare/b6de8891fc10...971d76ca3094) 1 commit to branch temp.\n\n* log: Add important.txt which is useful for logging errors. ([971d76ca309](https://github.com/sbansal1999/zulip/commit/971d76ca309446a9c20381f6271cea8a59b4e40a))"
expected_message = "sbansal1999 [force pushed](https://github.com/sbansal1999/zulip/compare/b6de8891fc10...971d76ca3094) 1 commit to branch temp.\n\n* log: Add important.txt which is useful for logging errors. ([971d76ca309](https://github.com/sbansal1999/zulip/commit/971d76ca309446a9c20381f6271cea8a59b4e40a))"
self.check_webhook("push__force_1_commit", "zulip / temp", expected_message)
def test_push__force_remove_commits(self) -> None:
expected_message = "sbansal1999 [pushed](https://github.com/sbansal1999/zulip/compare/2084a91af9ca...9a8749ea8fe7) the branch temp."
expected_message = "sbansal1999 [force pushed](https://github.com/sbansal1999/zulip/compare/2084a91af9ca...9a8749ea8fe7) the branch temp."
self.check_webhook("push__force_remove_commits", "zulip / temp", expected_message)
def test_push_local_branch_without_commits(self) -> None:

View File

@ -283,6 +283,7 @@ def get_push_commits_body(helper: Helper) -> str:
get_branch_name_from_ref(payload["ref"].tame(check_string)),
commits_data,
deleted=payload["deleted"].tame(check_bool),
force_push=payload["forced"].tame(check_bool),
)