Draft: PoC: Trigger AI flows on project webhook events [skip ci]
What does this MR do and why?
This is a proof of concept. It is not for merge. We share it so people can read the code and see the shape of the architecture.
The product idea has two parts:
- Any event that fires a webhook can trigger an AI flow.
- Any field in the webhook payload can decide if the trigger fires.
Today we build each trigger type by hand. This PoC adds one generic path. A single dispatch point in Project#execute_hooks serves several webhook scopes. To add a scope, you add it to one list and to EVENT_TYPES.
Shape
service (MR approved, issue created, note added, wiki page saved)
-> Project#execute_hooks(data, scope) the existing webhook funnel
-> webhooks, system hooks unchanged
-> EE::Project#execute_hooks, after commit
-> Ai::FlowTriggers::WebhookDispatchService in the request
1. scope in WEBHOOK_EVENT_TYPES? no query
2. Ai::FlowTrigger.registered_for? one query
3. actor from payload user.id
4. caused by a flow? (composite identity) skip, or find the parent workflow
5. enqueue the worker with the built payload
-> Ai::FlowTriggers::WebhookEventWorker in Sidekiq
-> Ai::FlowTriggers::EventTriggerService
-> FilterEvaluator on the payload any field, any operator
-> resource from the payload per scope
-> Ai::FlowTriggers::RunServiceGeneric parts: the funnel, the dispatch checks, the worker, the filter, and the run.
Parts that are still per scope:
- The resource lookup in
EventTriggerService(MR, issue, the noteable of a note, none for a wiki page). - The actor field. All four scopes here use the top-level
user. Other scopes do not.
Scopes in this PoC
merge_request_hooksissue_hooksnote_hookswiki_page_hooks, added in the last commit to show the cost of one more scope
Their callers always call execute_hooks, even when the project has no webhook. Scopes like push, pipeline, job, emoji and milestone only call it when a webhook exists, so they are not in this PoC.
pipeline_hooks keeps its own path in EE::Ci::Pipelines::HookService. It is not in the list, so its triggers do not run twice.
Example filter
This trigger fires when alice approves an MR, and not when bob does:
{
"merge_request_hooks": {
"match": "all",
"rules": [
{ "field": "object_attributes.action", "operator": "in", "value": ["approval", "approved"] },
{ "field": "user.username", "operator": "eq", "value": "alice" }
]
}
}What is not in this PoC
- No UI. The new event types are GraphQL experiments.
- No group-level hooks, and no confidential scopes.
- No feature flag. A real version would ship behind a
wipflag on the project. - No change to the
pipeline_hookspath or to the CloudEvent triggers.
Known limits
Gitlab::FilterEvaluatorcannot look inside arrays. Rules likelabels.titleorreviewers.statenever match. A label rule must usecontainson the wholelabelsvalue.RunServicerecords the run under the webhook event type, for examplemerge_request_hooks. Features keyed onmerge_requestorwork_item, such as the fix loop and on-finish settings, do not apply to these runs.- Each
execute_hookscall for the four scopes adds oneregistered_for?query, also in projects without triggers. - The trigger form drops event types it does not know. Editing one of these triggers in the UI removes its event type.
References
Screenshots or screen recordings
Not applicable. This MR has no UI changes.
How to set up and validate locally
You need a license with Duo Agent Platform, and a user who can trigger AI flows in the project.
-
In a Rails console, create a trigger on a project. Use a
config_pathflow or a custom catalog flow. The Developer flow has no goal template for these events, so it skips them.project = Project.find_by_full_path('group/project') service_account = User.service_account.first Ai::FlowTrigger.create!( project: project, user: service_account, config_path: '.gitlab/duo/flow.yml', description: 'Run when alice approves', event_types: [Ai::FlowTrigger::EVENT_TYPES[:merge_request_hooks]], filter: { 'merge_request_hooks' => { 'match' => 'all', 'rules' => [ { 'field' => 'object_attributes.action', 'operator' => 'in', 'value' => %w[approval approved] }, { 'field' => 'user.username', 'operator' => 'eq', 'value' => 'alice' } ] } } ) -
Approve an MR in that project as
alice. A flow session starts. -
Approve another MR as
bob. No session starts.
Manual check in GDK
I tested the PoC by hand in GDK with real API requests. The triggers worked as expected.
Setup in gitlab-duo/test:
- Trigger 1 on
merge_request_hooksfires when the action isapprovalorapprovedand the user isalice. - Trigger 2 on
issue_hooksfires when the action isopenandlabelscontainsbackend. - Both triggers use a small
config_pathflow. It only prints the event name and the payload.
Results:
| Action | Expected | Result |
|---|---|---|
alice approves an MR |
run | A session started, the job printed event=merge_request_hooks, and the progress note was posted on the MR |
bob approves an MR |
no run | The worker ran, and the filter rejected the event |
alice opens an issue with the backend label |
run | A session started, the job printed event=issue_hooks, and the progress note was posted on the issue |
alice opens an issue without a label |
no run | The worker ran, and the filter rejected the event |
root opens two MRs |
no run | The worker ran for each event, and the filter rejected them |
Each approval sent two webhook events, so it started two worker jobs. See the query cost in "Known limits".
The sessions end as "dropped" because the test flow does not start the Duo agent.
To run Duo Agent Platform flows in GDK, I needed two changes that the setup guide does not mention:
CLOUD_CONNECTOR_SELF_SIGN_TOKENS: '1'underenvingdk.yml. Without it, the AI feature catalog is empty on self-managed with an online license, and every flow fails with403 forbidden to access duo workflow.trusted_forwarded_hosts = ["gdk.test:3000"]ingitlab/workhorse/config.toml. Without it, Workhorse rejects the WebSocket of the flow job, because the HTTP router changes theHostheader to the NGINX port.gdk reconfigureremoves this line.
MR acceptance checklist
This is a PoC and is not for merge. The checklist does not apply.