-
Notifications
You must be signed in to change notification settings - Fork 2
feat(github): deliver webhook events to Mesh via trigger callbacks #332
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
+121
−183
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1bb576b
feat(github): deliver webhook events to Mesh via trigger callbacks
viktormarinho 34fa50a
refactor(github): use @decocms/runtime triggers SDK instead of manual…
viktormarinho 0a1a64b
fix: format trigger-store and tools
viktormarinho c4145f4
fix: resolve TypeScript errors in trigger SDK integration
viktormarinho 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
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 |
|---|---|---|
| @@ -1,112 +1,105 @@ | ||
| /** | ||
| * Trigger Store | ||
| * | ||
| * In-memory trigger configuration storage and static trigger definitions | ||
| * for the Mesh automations system. | ||
| */ | ||
| import { createTriggers } from "@decocms/runtime/triggers"; | ||
| import { StudioKV } from "@decocms/runtime/trigger-storage"; | ||
| import { z } from "zod"; | ||
|
|
||
| import type { TriggerDefinition } from "@decocms/bindings/trigger"; | ||
| const storage = | ||
| process.env.MESH_URL && process.env.MESH_API_KEY | ||
| ? new StudioKV({ | ||
| url: process.env.MESH_URL, | ||
| apiKey: process.env.MESH_API_KEY, | ||
| }) | ||
| : undefined; | ||
|
|
||
| interface TriggerConfig { | ||
| type: string; | ||
| params: Record<string, string>; | ||
| enabled: boolean; | ||
| connectionId: string; | ||
| } | ||
|
|
||
| const REPO_PARAM_SCHEMA: TriggerDefinition["paramsSchema"] = { | ||
| repo: { type: "string", description: "Repository full name e.g. owner/repo" }, | ||
| }; | ||
|
|
||
| export const GITHUB_TRIGGER_DEFINITIONS = [ | ||
| { | ||
| type: "github.push", | ||
| description: "Code pushed to a branch", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| { | ||
| type: "github.pull_request.opened", | ||
| description: "Pull request opened", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| { | ||
| type: "github.pull_request.closed", | ||
| description: "Pull request closed or merged", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| { | ||
| type: "github.pull_request.review_requested", | ||
| description: "Review requested on a PR", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| { | ||
| type: "github.issues.opened", | ||
| description: "Issue opened", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| { | ||
| type: "github.issues.closed", | ||
| description: "Issue closed", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| { | ||
| type: "github.issue_comment.created", | ||
| description: "Comment on issue or PR", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| { | ||
| type: "github.pull_request_review.submitted", | ||
| description: "PR review submitted", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| { | ||
| type: "github.release.published", | ||
| description: "Release published", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| { | ||
| type: "github.workflow_run.completed", | ||
| description: "Actions workflow completed", | ||
| paramsSchema: REPO_PARAM_SCHEMA, | ||
| }, | ||
| ] satisfies TriggerDefinition[]; | ||
|
|
||
| const triggerConfigs = new Map<string, TriggerConfig>(); | ||
|
|
||
| const KNOWN_TYPES = new Set(GITHUB_TRIGGER_DEFINITIONS.map((d) => d.type)); | ||
|
|
||
| export function listTriggerDefinitions(): TriggerDefinition[] { | ||
| return GITHUB_TRIGGER_DEFINITIONS; | ||
| } | ||
|
|
||
| export function configureTrigger( | ||
| type: string, | ||
| params: Record<string, string>, | ||
| enabled: boolean, | ||
| connectionId: string, | ||
| ): void { | ||
| if (!KNOWN_TYPES.has(type)) { | ||
| throw new Error(`Unknown trigger type: ${type}`); | ||
| } | ||
| const key = `${connectionId}::${type}::${params.repo ?? "*"}`; | ||
| if (enabled) { | ||
| triggerConfigs.set(key, { type, params, enabled, connectionId }); | ||
| } else { | ||
| triggerConfigs.delete(key); | ||
| } | ||
| } | ||
|
|
||
| export function hasMatchingTrigger( | ||
| eventType: string, | ||
| repo: string | undefined, | ||
| connectionId: string, | ||
| ): boolean { | ||
| for (const config of triggerConfigs.values()) { | ||
| if (!config.enabled) continue; | ||
| if (config.connectionId !== connectionId) continue; | ||
| if (config.type !== eventType) continue; | ||
| if (config.params.repo && repo && config.params.repo !== repo) continue; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| export const triggers = createTriggers({ | ||
| definitions: [ | ||
| { | ||
| type: "github.push", | ||
| description: "Triggered when code is pushed to a repository", | ||
| params: z.object({ | ||
| repo: z | ||
| .string() | ||
| .describe( | ||
| "Repository full name (owner/repo). Leave empty for all repos.", | ||
| ), | ||
| }), | ||
| }, | ||
| { | ||
| type: "github.pull_request.opened", | ||
| description: "Triggered when a pull request is opened", | ||
| params: z.object({ | ||
| repo: z | ||
| .string() | ||
| .describe( | ||
| "Repository full name (owner/repo). Leave empty for all repos.", | ||
| ), | ||
| }), | ||
| }, | ||
| { | ||
| type: "github.pull_request.closed", | ||
| description: "Triggered when a pull request is closed or merged", | ||
| params: z.object({ | ||
| repo: z | ||
| .string() | ||
| .describe( | ||
| "Repository full name (owner/repo). Leave empty for all repos.", | ||
| ), | ||
| }), | ||
| }, | ||
| { | ||
| type: "github.issues.opened", | ||
| description: "Triggered when an issue is opened", | ||
| params: z.object({ | ||
| repo: z | ||
| .string() | ||
| .describe( | ||
| "Repository full name (owner/repo). Leave empty for all repos.", | ||
| ), | ||
| }), | ||
| }, | ||
| { | ||
| type: "github.issues.closed", | ||
| description: "Triggered when an issue is closed", | ||
| params: z.object({ | ||
| repo: z | ||
| .string() | ||
| .describe( | ||
| "Repository full name (owner/repo). Leave empty for all repos.", | ||
| ), | ||
| }), | ||
| }, | ||
| { | ||
| type: "github.issue_comment.created", | ||
| description: "Triggered when a comment is added to an issue or PR", | ||
| params: z.object({ | ||
| repo: z | ||
| .string() | ||
| .describe( | ||
| "Repository full name (owner/repo). Leave empty for all repos.", | ||
| ), | ||
| }), | ||
| }, | ||
| { | ||
| type: "github.pull_request_review.submitted", | ||
| description: "Triggered when a pull request review is submitted", | ||
| params: z.object({ | ||
| repo: z | ||
| .string() | ||
| .describe( | ||
| "Repository full name (owner/repo). Leave empty for all repos.", | ||
| ), | ||
| }), | ||
| }, | ||
| { | ||
| type: "github.workflow_run.completed", | ||
| description: "Triggered when a GitHub Actions workflow run completes", | ||
| params: z.object({ | ||
| repo: z | ||
| .string() | ||
| .describe( | ||
| "Repository full name (owner/repo). Leave empty for all repos.", | ||
| ), | ||
| }), | ||
| }, | ||
| ], | ||
| storage, | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: The trigger definitions list drops the previously supported "github.pull_request.review_requested" and "github.release.published" events. Existing automations for those events will stop being configurable or matched after this change.
Prompt for AI agents