webhook_common: Add a method to build preset WebhookUrlOption.

This adds `WebhookUrlOption.build_preset_config` method which builds
pre-configured WebhookUrlOptions objects. It can be used to abstract
commonly used WebhookUrlOption settings or to construct special
settings that have additional logic and UI in the web-app modal for
generating an incoming webhook URL.

Currently, one such setting is the "branches" url option. This setting
is meant to be used by "versioncontrol" integrations such as GitHub,
Gitea, etc. It adds UI that lets the user to choose which branches of
their repository can trigger notifications. So, we refactor those
integrations to use `build_preset_config` for the "branches" option.

Co-authored-by: Lauryn Menard <lauryn@zulip.com>
This commit is contained in:
PieterCK
2025-04-10 18:39:36 +07:00
committed by Tim Abbott
parent f0a88d51cc
commit 57ff908af9
4 changed files with 79 additions and 12 deletions

View File

@@ -259,6 +259,44 @@ would want to include in a Zulip notification message.
The `config_options` field in the `WebhookIntegration` class is reserved
for this use case.
### WebhookUrlOption presets
The `build_preset_config` method creates `WebhookUrlOption` objects with
pre-configured fields. These preset URL options primarily serve two
purposes:
- To construct common `WebhookUrlOption` objects that are used in various
incoming webhook integrations.
- To construct `WebhookUrlOption` objects with special UI in the web-app
for [generating incoming webhook URLs](/help/generate-integration-url).
Using a preset URL option with the `build_preset_config` method:
```python
# zerver/lib/integrations.py
from zerver.lib.webhooks.common import PresetUrlOption, WebhookUrlOption
# -- snip --
WebhookIntegration(
"github",
# -- snip --
url_options=[
WebhookUrlOption.build_preset_config(PresetUrlOption.BRANCHES),
],
),
```
Currently configured preset URL options:
- **`BRANCHES`**: This preset is intended to be used for [version control
integrations](/integrations/version-control), and adds UI for the user to
configure which branches of a project's repository will trigger Zulip
notification messages. When the user specifies which branches to receive
notifications from, the `branches` parameter will be added to the [generated
integration URL](/help/generate-integration-url). For example, if the user
input `main` and `dev` for the branches of their repository, then
`&branches=main%2Cdev` would be appended to the generated integration URL.
## Step 4: Manually testing the webhook
For either one of the command line tools, first, you'll need to get an