Queue webhook flow triggers from Project#execute_hooks
What does this MR do and why?
This is MR 4 of 5 for the issue below. When a project fires merge_request_hooks, issue_hooks, note_hooks or wiki_page_hooks, EE::Project#execute_hooks now queues Ai::FlowTriggers::WebhookEventWorker from the previous MR. The worker runs the matching flow triggers.
The call in execute_hooks is a bridge. When the Events Platform publishes an event at execute_hooks, we move to that event and remove this call. So the call site is one line, and all the queue logic lives in WebhookEventWorker.queue_for. A trigger problem must never stop webhooks, so errors from the queue call are tracked with Gitlab::ErrorTracking and do not raise. The call runs after commit, like the webhooks, so the job sees committed data.
queue_for does the cheapest checks first:
- It returns for scopes that are not in
Ai::FlowTrigger::WEBHOOK_EVENT_TYPES.push_hooksandpipeline_hooksqueue nothing. - It returns when the
ai_flow_trigger_webhook_eventsflag is off for the project. - It returns when the project has no active trigger for the scope (
Ai::FlowTrigger.registered_for?). The job carries the full payload, so this keeps Redis free of jobs for projects without triggers. It also means a lazy payload is only built when a trigger exists. - It returns when the payload has no user.
The triggers run as the user in the payload (user.id). This is the same user that cloud events carry as gitlab_user_id, so both trigger paths authorize the same person. For merge requests, issues and wiki pages, it is the user who made the change. For notes, Gitlab::DataBuilder::Note puts the note author there.
When a flow caused the event, the job gets the composite actor and the workflow ID, in the same way as cloud events. To share this logic, Gitlab::EventStore::CloudEvent.composite_identity_attributes is now a public class method, and build_cloud_event uses it too. Its behavior does not change.
The flag gates the whole path for each project: queueing in execute_hooks, the check in the worker, and saving triggers with these event types.
This MR builds on these merged MRs:
Next MR: MR 5 adds the new event types to GraphQL.
References
Screenshots or screen recordings
Not needed. There is no UI change.
How to set up and validate locally
GraphQL support comes in MR 5, so create the trigger in the Rails console.
-
Pick a project where you are a Developer and Duo Agent Platform flows can run. Enable the flag for it:
project = Project.find_by_full_path('gitlab-org/gitlab-test') Feature.enable(:ai_flow_trigger_webhook_events, project) -
Create a trigger for
merge_request_hooks. Use a service account that can run flows in the project, and a flow config that exists in the repository:Ai::FlowTrigger.create!( project: project, user: User.find_by_username('<service_account>'), description: 'Webhook trigger test', config_path: '.gitlab/duo/flows/<flow>.yml', event_types: [Ai::FlowTrigger::EVENT_TYPES[:merge_request_hooks]] ) -
Update a merge request in that project, for example change its title.
-
Check that
log/sidekiq.logshows a job forAi::FlowTriggers::WebhookEventWorker. -
Check that a flow session started for the trigger's service account in the project.
-
Disable the flag, update the merge request again, and check that no new job is queued.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.