Skip to content

Commit 3117de4

Browse files
authored
Merge pull request #17 from teams-notifier/feat-filter-participants
feat: add filter on participant
2 parents a0bd102 + 0c14490 commit 3117de4

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ On your GitLab repository, follow these steps to add a new webhook:
3838
4. Fill out the form with the following details:
3939

4040
* **URL**: https://hostname-of-this-api.example.org/api/v1/gitlab-webhook
41+
* Since v0.12.0, you can specify a filter to publish notifications only if certain users are involved as the opener, reviewers, or assignees. \
42+
Use query parameters `filter_on_participant_ids` with a comma-separated list of user IDs (integers).
4143
* **Add custom header**:
4244
* name: `X-Conversation-Token`
4345
* value: comma separated list of conversation tokens you want the MR messages sent to

app.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ async def handle_webhook(
9292
payload: MergeRequestPayload | PipelinePayload | EmojiPayload,
9393
x_conversation_token: Annotated[str, Header()],
9494
x_gitlab_token: Annotated[str, Header()],
95+
filter_on_participant_ids: str | None = None,
9596
):
9697
validate_gitlab_token(x_gitlab_token)
9798
conversation_tokens = list(
@@ -100,9 +101,20 @@ async def handle_webhook(
100101
[validate_uuid(ct.strip()) for ct in x_conversation_token.split(",")],
101102
)
102103
)
104+
103105
try:
104106
if isinstance(payload, MergeRequestPayload):
105-
await webhook.merge_request(payload, conversation_tokens)
107+
participant_ids_filter: list[int] = []
108+
if filter_on_participant_ids:
109+
try:
110+
participant_ids_filter = [int(entry) for entry in filter_on_participant_ids.split(",")]
111+
except ValueError:
112+
raise HTTPException(
113+
status_code=400,
114+
detail="filter_on_participant_ids must be a list of comma separated integers",
115+
)
116+
117+
await webhook.merge_request(payload, conversation_tokens, participant_ids_filter)
106118
if isinstance(payload, PipelinePayload):
107119
await webhook.pipeline(payload, conversation_tokens)
108120
if isinstance(payload, EmojiPayload):

webhook/merge_request.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,26 @@ async def create_or_update_message(
134134
async def merge_request(
135135
mr: MergeRequestPayload,
136136
conversation_tokens: list[str],
137+
participant_ids_filter: list[int],
137138
):
138139
mri = await dbh.get_merge_request_ref_infos(mr)
139140
convtoken_to_msgrefs = await get_or_create_message_refs(
140141
mri.merge_request_ref_id,
141142
conversation_tokens,
142143
)
143144

145+
participant_found = True
146+
if participant_ids_filter:
147+
participant_found = False
148+
participant_found |= mri.merge_request_extra_state.opener.id in participant_ids_filter
149+
participant_found |= any(
150+
[
151+
user.id in participant_ids_filter
152+
for userlist in (mri.merge_request_payload.assignees, mri.merge_request_payload.reviewers)
153+
for user in userlist
154+
]
155+
)
156+
144157
connection: asyncpg.Connection
145158

146159
if mr.object_attributes.action in ("update"):
@@ -196,7 +209,8 @@ async def merge_request(
196209
"merged",
197210
)
198211
or mr.object_attributes.draft
199-
or mr.object_attributes.work_in_progress,
212+
or mr.object_attributes.work_in_progress
213+
or not participant_found,
200214
)
201215

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

0 commit comments

Comments
 (0)