git webhooks: Handle assignment events better.

With the introduction of `assignee_updated` parameter in the library,
- Github, Gitea, Gogs can display the assignee in assignment events.
- Github can display the user unassigned in unassignment events.

Fixes https://chat.zulip.org/#narrow/channel/127-integrations/near/1965136
This commit is contained in:
Niloth P
2024-10-24 07:26:41 +05:30
committed by Tim Abbott
parent 8e85972a03
commit bd83dbfb42
7 changed files with 41 additions and 40 deletions

View File

@@ -386,14 +386,15 @@ class GitHubWebhookTest(WebhookTestCase):
self.check_webhook("pull_request__assigned", expected_topic_name, expected_message)
def test_pull_request_unassigned_msg(self) -> None:
expected_message = (
"eeshangarg unassigned [PR #1](https://github.com/zulip-test-org/helloworld/pull/1)."
)
self.check_webhook(
"pull_request__unassigned",
"helloworld / PR #1 Mention that Zulip rocks!",
expected_message,
)
expected_message = "eeshangarg unassigned eeshangarg from [PR #1](https://github.com/zulip-test-org/helloworld/pull/1)."
expected_topic_name = "helloworld / PR #1 Mention that Zulip rocks!"
self.check_webhook("pull_request__unassigned", expected_topic_name, expected_message)
def test_pull_request_unassigned_msg_with_custom_topic_in_url(self) -> None:
self.url = self.build_webhook_url(topic="notifications")
expected_topic_name = "notifications"
expected_message = "eeshangarg unassigned eeshangarg from [PR #1 Mention that Zulip rocks!](https://github.com/zulip-test-org/helloworld/pull/1)"
self.check_webhook("pull_request__unassigned", expected_topic_name, expected_message)
def test_pull_request_ready_for_review_msg(self) -> None:
expected_message = "**Hypro999** has marked [PR #2](https://github.com/Hypro999/temp-test-github-webhook/pull/2) as ready for review."

View File

@@ -92,20 +92,16 @@ def get_assigned_or_unassigned_pull_request_body(helper: Helper) -> str:
payload = helper.payload
include_title = helper.include_title
pull_request = payload["pull_request"]
assignee = pull_request.get("assignee")
if assignee:
stringified_assignee = assignee["login"].tame(check_string)
assignee = payload["assignee"]["login"].tame(check_string)
base_message = get_pull_request_event_message(
return get_pull_request_event_message(
user_name=get_sender_name(payload),
action=payload["action"].tame(check_string),
url=pull_request["html_url"].tame(check_string),
number=pull_request["number"].tame(check_int),
title=pull_request["title"].tame(check_string) if include_title else None,
assignee_updated=assignee,
)
if assignee:
return base_message.replace("assigned", f"assigned {stringified_assignee} to", 1)
return base_message
def get_closed_pull_request_body(helper: Helper) -> str:
@@ -155,8 +151,7 @@ def get_issue_body(helper: Helper) -> str:
include_title = helper.include_title
action = payload["action"].tame(check_string)
issue = payload["issue"]
has_assignee = "assignee" in payload
base_message = get_issue_event_message(
return get_issue_event_message(
user_name=get_sender_name(payload),
action=action,
url=issue["html_url"].tame(check_string),
@@ -167,17 +162,11 @@ def get_issue_body(helper: Helper) -> str:
else issue["body"].tame(check_none_or(check_string))
),
title=issue["title"].tame(check_string) if include_title else None,
assignee_updated=payload["assignee"]["login"].tame(check_string)
if "assignee" in payload
else None,
)
if has_assignee:
stringified_assignee = payload["assignee"]["login"].tame(check_string)
if action == "assigned":
return base_message.replace("assigned", f"assigned {stringified_assignee} to", 1)
elif action == "unassigned":
return base_message.replace("unassigned", f"unassigned {stringified_assignee} from", 1)
return base_message
def get_issue_comment_body(helper: Helper) -> str:
payload = helper.payload