Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

gitlab: Fix crash on group-level webhooks with custom topics.

The Dance44dcc267f12e
diff --git a/zerver/webhooks/gitlab/tests.py b/zerver/webhooks/gitlab/tests.py
index 501d55e0d17..41a732e05b3 100644
--- a/zerver/webhooks/gitlab/tests.py
+++ b/zerver/webhooks/gitlab/tests.py
@@ -721,6 +721,15 @@ A trivial change that should probably be ignored.
"resource_access_token_hook__group_expiry", expected_topic_name, expected_message
)
+ def test_resource_access_token_group_expiry_with_custom_topic(self) -> None:
+ self.url = self.build_webhook_url(topic="notifications")
+ expected_topic_name = "notifications"
+ expected_message = "[[Twitter](https://gitlab.com/groups/twitter)] The access token [acd](https://gitlab.com/groups/twitter/-/settings/access_tokens) will expire on Jan 26, 2024."
+
+ self.check_webhook(
+ "resource_access_token_hook__group_expiry", expected_topic_name, expected_message
+ )
+
def test_deployment_started_event_message(self) -> None:
expected_topic_name = "test / production"
expected_message = "[Vedant Joshi](https://gitlab.com/theofficialvedantjoshi) started a new [deployment](https://gitlab.com/vedant8600317/test/-/jobs/9905389091):\n> [5879366](https://gitlab.com/vedant8600317/test/-/commit/58793660b22d6ceacbdc23b28a0562bca339702c) Update .gitlab-ci.yml file"
diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.py
index c363dae79e6..a58ef077c2c 100644
--- a/zerver/webhooks/gitlab/view.py
+++ b/zerver/webhooks/gitlab/view.py
@@ -580,10 +580,15 @@ def get_repo_name(payload: WildValue) -> str:
if "project" in payload:
return payload["project"]["name"].tame(check_string)
- # Apparently, Job Hook payloads don't have a `project` section,
- # but the repository name is accessible from the `repository`
- # section.
- return payload["repository"]["name"].tame(check_string)
+ if "repository" in payload:
+ # Job Hook payloads don't have a `project` section,
+ # but the repository name is accessible from the `repository`
+ # section.
+ return payload["repository"]["name"].tame(check_string)
+
+ # Group-level events (e.g. group access token expiry) have neither
+ # `project` nor `repository`, but have a `group` section.
+ return payload["group"]["group_name"].tame(check_string)
def get_user_name(payload: WildValue) -> str:
@@ -597,7 +602,11 @@ def get_issue_user_name(payload: WildValue) -> str:
def get_project_homepage(payload: WildValue) -> str:
if "project" in payload:
return payload["project"]["web_url"].tame(check_string)
- return payload["repository"]["homepage"].tame(check_string)
+ if "repository" in payload:
+ return payload["repository"]["homepage"].tame(check_string)
+ # Group-level events have a group path instead of a project URL.
+ group_path = payload["group"]["full_path"].tame(check_string)
+ return f"https://gitlab.com/groups/{group_path}"
def get_branch_name(payload: WildValue) -> str: