-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Alerting v2] Create workflow extension service #267924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
x-pack/platform/plugins/shared/alerting_v2/common/workflows_extensions/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Alerting V2 workflows_extensions integration | ||
|
|
||
| This document explains how to register workflow triggers in `alerting_v2` using the new wrapper services. | ||
|
|
||
| ## Architecture | ||
|
|
||
| - `server/lib/services/workflow_extensions_service/workflow_extensions_service.ts` | ||
| - Wraps `WorkflowsExtensionsServerPluginSetup` and `WorkflowsExtensionsServerPluginStart`. | ||
| - Use `registerTriggerDefinitions(...)` and `registerStepDefinitions(...)` during setup. | ||
| - Use `emitEvent(...)` at runtime when you need to emit a trigger event. | ||
| - `public/services/workflow_extensions_service.ts` | ||
| - Wraps `WorkflowsExtensionsPublicPluginSetup`. | ||
| - Use `registerPublicTriggerDefinitions(...)` during public setup to register trigger UI metadata. | ||
| - `public/lib/workflow_extensions/register_trigger_definitions.ts` | ||
| - Exports `registerTriggerDefinitions(service)`; calls `registerPublicTriggerDefinitions([...])`. | ||
|
|
||
| ## Typical trigger setup flow | ||
|
|
||
| 1. Define a shared trigger definition in `common` (`id` + `eventSchema`) with Zod. | ||
| 2. Register it on the server in `server/lib/workflow_extensions/register_trigger_definitions.ts` via `registerTriggerDefinitions(service)`, which calls `WorkflowExtensionsService.registerTriggerDefinitions(...)`. | ||
| 3. Define a public trigger definition (`PublicTriggerDefinition`) with UI metadata (title, description, icon, docs). | ||
| 4. Register it on the public side in `public/lib/workflow_extensions/register_trigger_definitions.ts` via `registerTriggerDefinitions(service)`. | ||
| 5. Add/update the trigger schema hash in: | ||
| - `src/platform/plugins/shared/workflows_extensions/test/scout/api/fixtures/approved_trigger_definitions.ts` | ||
|
|
||
| ## Server usage | ||
|
|
||
| `bind_on_setup.ts` calls `registerTriggerDefinitions(container.get(WorkflowExtensionsService))` from `server/lib/workflow_extensions/register_trigger_definitions.ts`. | ||
|
|
||
| Add your `ServerTriggerDefinition` entries to the array inside that function. | ||
|
|
||
| ## Public usage | ||
|
|
||
| `public/index.ts` calls `registerTriggerDefinitions(container.get(WorkflowExtensionsService))` from `public/lib/workflow_extensions/register_trigger_definitions.ts`. | ||
|
|
||
| Add your `PublicTriggerDefinition` entries to the array inside that function. | ||
|
|
||
| ## Notes | ||
|
|
||
| - Registration should happen in setup, not start. | ||
| - Keep server and public trigger IDs aligned. | ||
| - Public registration controls Workflows UI discoverability; server registration controls runtime validation/execution. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...plugins/shared/alerting_v2/public/lib/workflow_extensions/register_trigger_definitions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { WorkflowExtensionsPublicServiceContract } from '../../services/workflow_extensions_service'; | ||
|
|
||
| /** | ||
| * Registers all alerting-v2 public workflow trigger definitions (UI metadata). | ||
| * Call once during plugin setup with the resolved {@link WorkflowExtensionsService}. | ||
| */ | ||
| export function registerTriggerDefinitions( | ||
| workflowExtensionsService: WorkflowExtensionsPublicServiceContract | ||
| ): void { | ||
| workflowExtensionsService.registerPublicTriggerDefinitions([ | ||
| // Add PublicTriggerDefinition entries here (spread common id + eventSchema + title, icon, docs). | ||
| ]); | ||
| } |
23 changes: 23 additions & 0 deletions
23
x-pack/platform/plugins/shared/alerting_v2/public/services/workflow_extensions_service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { WorkflowsExtensionsPublicPluginSetup } from '@kbn/workflows-extensions/public'; | ||
| import type { PublicTriggerDefinition } from '@kbn/workflows-extensions/public'; | ||
|
|
||
| export interface WorkflowExtensionsPublicServiceContract { | ||
| registerPublicTriggerDefinitions(triggerDefinitions: PublicTriggerDefinition[]): void; | ||
| } | ||
|
|
||
| export class WorkflowExtensionsService implements WorkflowExtensionsPublicServiceContract { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand we have two |
||
| constructor(private readonly workflowsExtensions: WorkflowsExtensionsPublicPluginSetup) {} | ||
|
|
||
| public registerPublicTriggerDefinitions(triggerDefinitions: PublicTriggerDefinition[]): void { | ||
| triggerDefinitions.forEach((triggerDefinition) => | ||
| this.workflowsExtensions.registerTriggerDefinition(triggerDefinition) | ||
| ); | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
...form/plugins/shared/alerting_v2/server/lib/services/workflow_extensions_service/tokens.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { ServiceIdentifier } from 'inversify'; | ||
| import type { WorkflowExtensionsServiceContract } from './workflow_extensions_service'; | ||
|
|
||
| export const WorkflowExtensionsServiceToken = Symbol.for( | ||
| 'alerting_v2.WorkflowExtensionsService' | ||
| ) as ServiceIdentifier<WorkflowExtensionsServiceContract>; |
58 changes: 58 additions & 0 deletions
58
...ng_v2/server/lib/services/workflow_extensions_service/workflow_extensions_service.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { z } from '@kbn/zod/v4'; | ||
| import { httpServerMock } from '@kbn/core-http-server-mocks'; | ||
| import { workflowsExtensionsMock } from '@kbn/workflows-extensions/server/mocks'; | ||
| import { WorkflowExtensionsService } from './workflow_extensions_service'; | ||
|
|
||
| function createService() { | ||
| const setup = workflowsExtensionsMock.createSetup(); | ||
| const start = workflowsExtensionsMock.createStart(); | ||
| const service = new WorkflowExtensionsService(setup, () => start); | ||
| return { service, setup, start }; | ||
| } | ||
|
|
||
| describe('WorkflowExtensionsService', () => { | ||
| it('registerTriggerDefinitions delegates to workflows extensions setup', () => { | ||
| const { service, setup } = createService(); | ||
| const triggerDefinition = { | ||
| id: 'alerting-v2-unit-test.trigger' as const, | ||
| eventSchema: z.object({ key: z.string() }), | ||
| }; | ||
|
|
||
| service.registerTriggerDefinitions([triggerDefinition]); | ||
|
|
||
| expect(setup.registerTriggerDefinition).toHaveBeenCalledTimes(1); | ||
| expect(setup.registerTriggerDefinition).toHaveBeenCalledWith(triggerDefinition); | ||
| }); | ||
|
|
||
| it('registerStepDefinitions delegates to workflows extensions setup', () => { | ||
| const { service, setup } = createService(); | ||
| const stepLoader = () => Promise.resolve(undefined); | ||
|
|
||
| service.registerStepDefinitions([stepLoader]); | ||
|
|
||
| expect(setup.registerStepDefinition).toHaveBeenCalledTimes(1); | ||
| expect(setup.registerStepDefinition).toHaveBeenCalledWith(stepLoader); | ||
| }); | ||
|
|
||
| it('emitEvent resolves the workflows client and delegates emitEvent', async () => { | ||
| const { service, start } = createService(); | ||
| const emitEvent = jest.fn().mockResolvedValue(undefined); | ||
| start.getClient.mockResolvedValue({ | ||
| isWorkflowsAvailable: true, | ||
| emitEvent, | ||
| }); | ||
| const request = httpServerMock.createKibanaRequest(); | ||
|
|
||
| await service.emitEvent(request, 'some.trigger', { a: 1 }); | ||
|
|
||
| expect(start.getClient).toHaveBeenCalledWith(request); | ||
| expect(emitEvent).toHaveBeenCalledWith('some.trigger', { a: 1 }); | ||
| }); | ||
| }); |
59 changes: 59 additions & 0 deletions
59
...lerting_v2/server/lib/services/workflow_extensions_service/workflow_extensions_service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,59 @@ | ||||||
| /* | ||||||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||||||
| * or more contributor license agreements. Licensed under the Elastic License | ||||||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||||||
| * 2.0. | ||||||
| */ | ||||||
|
|
||||||
| import type { KibanaRequest } from '@kbn/core/server'; | ||||||
| import type { | ||||||
| WorkflowsExtensionsServerPluginSetup, | ||||||
| WorkflowsExtensionsServerPluginStart, | ||||||
| ServerTriggerDefinition, | ||||||
| } from '@kbn/workflows-extensions/server'; | ||||||
|
|
||||||
| type ServerStepDefinitionOrLoader = Parameters< | ||||||
| WorkflowsExtensionsServerPluginSetup['registerStepDefinition'] | ||||||
| >[0]; | ||||||
|
|
||||||
| export interface WorkflowExtensionsServiceContract { | ||||||
| /** | ||||||
| * Registers all alerting-v2-owned workflow triggers and steps. Call once during | ||||||
| * plugin setup (see bind_on_setup). | ||||||
| */ | ||||||
| registerTriggerDefinitions(triggerDefinitions: ServerTriggerDefinition[]): void; | ||||||
| registerStepDefinitions(stepDefintions: ServerStepDefinitionOrLoader[]): void; | ||||||
| emitEvent( | ||||||
| request: KibanaRequest, | ||||||
| triggerId: string, | ||||||
| payload: Record<string, unknown> | ||||||
| ): Promise<void>; | ||||||
| } | ||||||
|
|
||||||
| export class WorkflowExtensionsService implements WorkflowExtensionsServiceContract { | ||||||
| constructor( | ||||||
| private readonly workflowsExtensionsSetup: WorkflowsExtensionsServerPluginSetup, | ||||||
| private readonly getWorkflowsExtensionsStart: () => WorkflowsExtensionsServerPluginStart | ||||||
| ) {} | ||||||
|
|
||||||
| public registerStepDefinitions(stepDefintions: ServerStepDefinitionOrLoader[]): void { | ||||||
| stepDefintions.forEach((stepDefinition) => | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| this.workflowsExtensionsSetup.registerStepDefinition(stepDefinition) | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| public registerTriggerDefinitions(triggerDefinitions: ServerTriggerDefinition[]): void { | ||||||
| triggerDefinitions.forEach((triggerDefinition) => | ||||||
| this.workflowsExtensionsSetup.registerTriggerDefinition(triggerDefinition) | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| public async emitEvent( | ||||||
| request: KibanaRequest, | ||||||
| triggerId: string, | ||||||
| payload: Record<string, unknown> | ||||||
| ): Promise<void> { | ||||||
| const client = await this.getWorkflowsExtensionsStart().getClient(request); | ||||||
| await client.emitEvent(triggerId, payload); | ||||||
| } | ||||||
| } | ||||||
20 changes: 20 additions & 0 deletions
20
...plugins/shared/alerting_v2/server/lib/workflow_extensions/register_trigger_definitions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { WorkflowExtensionsServiceContract } from '../services/workflow_extensions_service/workflow_extensions_service'; | ||
|
|
||
| /** | ||
| * Registers all alerting-v2 server-side workflow trigger definitions. | ||
| * Call once during plugin setup with the resolved {@link WorkflowExtensionsService}. | ||
| */ | ||
| export function registerTriggerDefinitions( | ||
| workflowExtensionsService: WorkflowExtensionsServiceContract | ||
| ): void { | ||
| workflowExtensionsService.registerTriggerDefinitions([ | ||
| // Add CommonTriggerDefinition-backed entries here (import from common when added). | ||
| ]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add an entry for defining a step definition?