Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ On your GitLab repository, follow these steps to add a new webhook:
4. Fill out the form with the following details:

* **URL**: https://hostname-of-this-api.example.org/api/v1/gitlab-webhook
* Since v0.12.0, you can specify a filter to publish notifications only if certain users are involved as the opener, reviewers, or assignees. \
Use query parameters `filter_on_participant_ids` with a comma-separated list of user IDs (integers).
* **Add custom header**:
* name: `X-Conversation-Token`
* value: comma separated list of conversation tokens you want the MR messages sent to
Expand Down
14 changes: 13 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ async def handle_webhook(
payload: MergeRequestPayload | PipelinePayload | EmojiPayload,
x_conversation_token: Annotated[str, Header()],
x_gitlab_token: Annotated[str, Header()],
filter_on_participant_ids: str | None = None,
):
validate_gitlab_token(x_gitlab_token)
conversation_tokens = list(
Expand All @@ -100,9 +101,20 @@ async def handle_webhook(
[validate_uuid(ct.strip()) for ct in x_conversation_token.split(",")],
)
)

try:
if isinstance(payload, MergeRequestPayload):
await webhook.merge_request(payload, conversation_tokens)
participant_ids_filter: list[int] = []
if filter_on_participant_ids:
try:
participant_ids_filter = [int(entry) for entry in filter_on_participant_ids.split(",")]
except ValueError:
raise HTTPException(
status_code=400,
detail="filter_on_participant_ids must be a list of comma separated integers",
)

await webhook.merge_request(payload, conversation_tokens, participant_ids_filter)
if isinstance(payload, PipelinePayload):
await webhook.pipeline(payload, conversation_tokens)
if isinstance(payload, EmojiPayload):
Expand Down
16 changes: 15 additions & 1 deletion webhook/merge_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,26 @@ async def create_or_update_message(
async def merge_request(
mr: MergeRequestPayload,
conversation_tokens: list[str],
participant_ids_filter: list[int],
):
mri = await dbh.get_merge_request_ref_infos(mr)
convtoken_to_msgrefs = await get_or_create_message_refs(
mri.merge_request_ref_id,
conversation_tokens,
)

participant_found = True
if participant_ids_filter:
participant_found = False
participant_found |= mri.merge_request_extra_state.opener.id in participant_ids_filter
participant_found |= any(
[
user.id in participant_ids_filter
for userlist in (mri.merge_request_payload.assignees, mri.merge_request_payload.reviewers)
for user in userlist
]
)

connection: asyncpg.Connection

if mr.object_attributes.action in ("update"):
Expand Down Expand Up @@ -196,7 +209,8 @@ async def merge_request(
"merged",
)
or mr.object_attributes.draft
or mr.object_attributes.work_in_progress,
or mr.object_attributes.work_in_progress
or not participant_found,
)

if mr.object_attributes.action in ("merge", "close") or mr.object_attributes.state in (
Expand Down