-
Notifications
You must be signed in to change notification settings - Fork 12
[APPS][WIP] Prototype direct action execution in local dev server #483
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| /* eslint-disable no-await-in-loop */ | ||
|
|
||
| import { setExecuteActionImplementation } from '@datadog/action-catalog'; | ||
| import type { AuthOptionsWithDefaults, Logger } from '@dd/core/types'; | ||
| import { randomUUID } from 'crypto'; | ||
| import type { IncomingMessage, ServerResponse } from 'http'; | ||
|
|
@@ -124,6 +125,65 @@ async function bundleBackendFunction( | |
| return { func: enrichedFunc, code }; | ||
| } | ||
|
|
||
| function executeAction({ | ||
| func, | ||
| auth, | ||
| fqn, | ||
| input, | ||
| doAuthenticatedRequest, | ||
| }: { | ||
| func: BackendFunction; | ||
| auth: AuthConfig; | ||
| fqn: string; | ||
| inputs: unknown; | ||
| doAuthenticatedRequest: DoAuthenticatedRequest; | ||
| }) { | ||
| const endpoint = `https://api.${auth.site}/api/v2/app-builder/queries/preview-async`; | ||
| const displayName = formatRef(func); | ||
|
|
||
| log.debug(`Calling Datadog API: ${endpoint}`); | ||
|
|
||
| const body = JSON.stringify({ | ||
| data: { | ||
| type: 'queries', | ||
| attributes: { | ||
| query: { | ||
| id: randomUUID(), | ||
| name: displayName, | ||
| type: 'action', | ||
| properties: { | ||
| spec: { | ||
| fqn, | ||
| inputs, | ||
| }, | ||
| onlyTriggerManually: true, | ||
| }, | ||
| }, | ||
| template_params: {}, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const initialResult = await doAuthenticatedRequest<{ data?: { id?: string } }>({ | ||
| url: endpoint, | ||
| method: 'POST', | ||
| type: 'json', | ||
| getData: () => ({ | ||
| data: body, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }), | ||
| }); | ||
|
|
||
| const receiptId = initialResult.data?.id; | ||
|
|
||
| if (!receiptId) { | ||
| throw new Error('No receipt ID returned from Datadog API'); | ||
| } | ||
|
|
||
| log.debug(`Query execution started with receipt: ${receiptId}`); | ||
|
|
||
| return pollQueryExecution(receiptId, auth, doAuthenticatedRequest, log); | ||
| } | ||
| /** | ||
| * Execute a script via Datadog's app-builder queries API. | ||
| */ | ||
|
|
@@ -320,20 +380,25 @@ async function handleExecuteAction( | |
| log: Logger, | ||
| ): Promise<void> { | ||
| try { | ||
| const { func, code, args } = await validateAndBundle(req, functionsByName, bundle); | ||
| const { functionName, args = [] } = await parseRequestBody(req); | ||
| const func = functionsByName.get(functionName); | ||
|
|
||
| setExecuteActionImplementation(async (actionId, { inputs, connectionId }) => { | ||
| return executeAction({ | ||
| fqn: actionId, | ||
| inputs, | ||
| connectionId, | ||
| }); | ||
| }); | ||
| // Loading the module todos.backend.ts | ||
| const functions = await import(func?.absolutePath); | ||
|
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.
For the normal Useful? React with 👍 / 👎. |
||
| // Execute the addTodo() | ||
| const result = await functions[func?.name](...args); | ||
|
|
||
| const displayName = formatRef(func); | ||
|
|
||
| log.debug(`Executing action: ${displayName} with args`); | ||
|
|
||
| const result = await executeScriptViaDatadog( | ||
| code, | ||
| func, | ||
| args, | ||
| auth, | ||
| doAuthenticatedRequest, | ||
| log, | ||
| ); | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify({ success: true, result } satisfies ExecuteActionResponse)); | ||
|
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.
When a directly invoked backend function returns a normal value such as Useful? React with 👍 / 👎. |
||
|
|
||
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.
For a request with a missing or unknown
functionName, this lookup yieldsundefinedand the subsequent import/invocation is caught as a generic 500. PreviouslyvalidateAndBundleexplicitly returned 400 for an invalid name and 404 for an unknown function, and the existing execute-action tests rely on those responses. Validate the parsed name and lookup result before attempting the direct import.Useful? React with 👍 / 👎.