-
Notifications
You must be signed in to change notification settings - Fork 312
feat: add dispatch_repository safe-output type for repository_dispatch events (experimental)
#22315
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
+1,966
−1
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c3b9ece
Initial plan
Copilot 717e76f
feat: add dispatch_repository safe-output type for repository_dispatc…
Copilot 220c32e
test: add integration tests for dispatch_repository compilation in pk…
Copilot c6b5a47
test: add integration tests for GitHub Actions expressions in dispatc…
Copilot dcbff94
Merge branch 'main' into copilot/add-dispatch-repository-safe-output
pelikhan 2b1dc42
feat: mark dispatch_repository as experimental with warning on compile
Copilot 918b844
Merge branch 'main' into copilot/add-dispatch-repository-safe-output
pelikhan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // @ts-check | ||
| /// <reference types="@actions/github-script" /> | ||
|
|
||
| /** | ||
| * @typedef {import('./types/handler-factory').HandlerFactoryFunction} HandlerFactoryFunction | ||
| */ | ||
|
|
||
| /** @type {string} Safe output type handled by this module */ | ||
| const HANDLER_TYPE = "dispatch_repository"; | ||
|
|
||
| const { getErrorMessage } = require("./error_helpers.cjs"); | ||
| const { createAuthenticatedGitHubClient } = require("./handler_auth.cjs"); | ||
| const { parseRepoSlug, validateTargetRepo, parseAllowedRepos } = require("./repo_helpers.cjs"); | ||
| const { logStagedPreviewInfo } = require("./staged_preview.cjs"); | ||
| const { isStagedMode } = require("./safe_output_helpers.cjs"); | ||
|
|
||
| /** | ||
| * Main handler factory for dispatch_repository | ||
| * Returns a message handler function that processes individual dispatch_repository messages | ||
| * @type {HandlerFactoryFunction} | ||
| */ | ||
| async function main(config = {}) { | ||
| const tools = config.tools || {}; | ||
| const githubClient = await createAuthenticatedGitHubClient(config); | ||
| const isStaged = isStagedMode(config); | ||
|
|
||
| const contextRepoSlug = `${context.repo.owner}/${context.repo.repo}`; | ||
| core.info(`dispatch_repository handler initialized: tools=${Object.keys(tools).join(", ")}, context_repo=${contextRepoSlug}`); | ||
|
|
||
| // Per-tool dispatch counters for max enforcement | ||
| /** @type {Record<string, number>} */ | ||
| const dispatchCounts = {}; | ||
|
|
||
| /** | ||
| * Message handler function that processes a single dispatch_repository message | ||
| * @param {Object} message - The dispatch_repository message to process | ||
| * @param {Object} resolvedTemporaryIds - Map of temporary IDs to resolved values | ||
| * @returns {Promise<Object>} Result with success/error status | ||
| */ | ||
| return async function handleDispatchRepository(message, resolvedTemporaryIds) { | ||
| const toolName = message.tool_name; | ||
|
|
||
| if (!toolName || toolName.trim() === "") { | ||
| core.warning("dispatch_repository: tool_name is empty, skipping"); | ||
| return { | ||
| success: false, | ||
| error: "E001: tool_name is required", | ||
| }; | ||
| } | ||
|
|
||
| // Look up the tool configuration | ||
| const toolConfig = tools[toolName]; | ||
| if (!toolConfig) { | ||
| core.warning(`dispatch_repository: unknown tool "${toolName}", skipping`); | ||
| return { | ||
| success: false, | ||
| error: `E001: tool "${toolName}" is not configured in dispatch_repository`, | ||
| }; | ||
| } | ||
|
|
||
| const maxCount = typeof toolConfig.max === "number" ? toolConfig.max : parseInt(String(toolConfig.max || "1"), 10) || 1; | ||
| const currentCount = dispatchCounts[toolName] || 0; | ||
|
|
||
| if (currentCount >= maxCount) { | ||
| core.warning(`dispatch_repository: max count of ${maxCount} reached for tool "${toolName}", skipping`); | ||
| return { | ||
| success: false, | ||
| error: `E002: Max count of ${maxCount} reached for tool "${toolName}"`, | ||
| }; | ||
| } | ||
|
|
||
| // Resolve target repository | ||
| // Prefer message.repository > toolConfig.repository > first allowed_repository | ||
| const messageRepo = message.repository || ""; | ||
| const configuredRepo = toolConfig.repository || ""; | ||
| const allowedReposConfig = toolConfig.allowed_repositories || []; | ||
| const allowedRepos = parseAllowedRepos(allowedReposConfig); | ||
|
|
||
| let targetRepoSlug = messageRepo || configuredRepo; | ||
|
|
||
| if (!targetRepoSlug && allowedReposConfig.length > 0) { | ||
| // Default to first allowed repository if no specific target given | ||
| targetRepoSlug = allowedReposConfig[0]; | ||
| } | ||
|
|
||
| if (!targetRepoSlug) { | ||
| core.warning(`dispatch_repository: no target repository for tool "${toolName}"`); | ||
| return { | ||
| success: false, | ||
| error: `E001: No target repository configured for tool "${toolName}"`, | ||
| }; | ||
| } | ||
|
|
||
| // Validate cross-repo dispatch (SEC-005 pattern) | ||
| const isCrossRepo = targetRepoSlug !== contextRepoSlug; | ||
| if (isCrossRepo && allowedRepos.size > 0) { | ||
| const repoValidation = validateTargetRepo(targetRepoSlug, contextRepoSlug, allowedRepos); | ||
| if (!repoValidation.valid) { | ||
| core.warning(`dispatch_repository: cross-repo check failed for "${targetRepoSlug}": ${repoValidation.error}`); | ||
| return { | ||
| success: false, | ||
| error: `E004: ${repoValidation.error}`, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| const parsedRepo = parseRepoSlug(targetRepoSlug); | ||
| if (!parsedRepo) { | ||
| core.warning(`dispatch_repository: invalid repository slug "${targetRepoSlug}"`); | ||
| return { | ||
| success: false, | ||
| error: `E001: Invalid repository slug "${targetRepoSlug}" (expected "owner/repo")`, | ||
| }; | ||
| } | ||
|
|
||
| // Build client_payload from message inputs + workflow identifier | ||
| /** @type {Record<string, any>} */ | ||
| const clientPayload = { | ||
| workflow: toolConfig.workflow || "", | ||
| }; | ||
|
|
||
| if (message.inputs && typeof message.inputs === "object") { | ||
| for (const [key, value] of Object.entries(message.inputs)) { | ||
| clientPayload[key] = value; | ||
| } | ||
| } | ||
|
|
||
| const eventType = toolConfig.event_type || toolConfig.eventType || ""; | ||
| if (!eventType) { | ||
| core.warning(`dispatch_repository: tool "${toolName}" has no event_type configured`); | ||
| return { | ||
| success: false, | ||
| error: `E001: event_type is required for tool "${toolName}"`, | ||
| }; | ||
| } | ||
|
|
||
| core.info(`dispatch_repository: dispatching event_type="${eventType}" to ${targetRepoSlug} (workflow: ${toolConfig.workflow || "unspecified"})`); | ||
|
|
||
| // If in staged mode, preview without executing | ||
| if (isStaged || toolConfig.staged) { | ||
| logStagedPreviewInfo(`Would dispatch repository_dispatch event: event_type="${eventType}" to ${targetRepoSlug}, client_payload=${JSON.stringify(clientPayload)}`); | ||
| dispatchCounts[toolName] = currentCount + 1; | ||
| return { | ||
| success: true, | ||
| staged: true, | ||
| tool_name: toolName, | ||
| repository: targetRepoSlug, | ||
| event_type: eventType, | ||
| client_payload: clientPayload, | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| await githubClient.rest.repos.createDispatchEvent({ | ||
| owner: parsedRepo.owner, | ||
| repo: parsedRepo.repo, | ||
| event_type: eventType, | ||
| client_payload: clientPayload, | ||
| }); | ||
|
|
||
| dispatchCounts[toolName] = currentCount + 1; | ||
| core.info(`✓ Successfully dispatched repository_dispatch: event_type="${eventType}" to ${targetRepoSlug}`); | ||
|
|
||
| return { | ||
| success: true, | ||
| tool_name: toolName, | ||
| repository: targetRepoSlug, | ||
| event_type: eventType, | ||
| client_payload: clientPayload, | ||
| }; | ||
| } catch (error) { | ||
| const errorMessage = getErrorMessage(error); | ||
| core.error(`dispatch_repository: failed to dispatch event_type="${eventType}" to ${targetRepoSlug}: ${errorMessage}`); | ||
|
|
||
| return { | ||
| success: false, | ||
| error: `E099: Failed to dispatch repository_dispatch event "${eventType}" to ${targetRepoSlug}: ${errorMessage}`, | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| module.exports = { main }; | ||
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.
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.
There’s no JS unit test coverage for the new
dispatch_repositoryhandler, while similar handlers (e.g.dispatch_workflow.cjs) have dedicated tests covering max enforcement, cross-repo allowlists, staged mode, and error paths. Given the security-sensitive nature of cross-repo dispatch, adding adispatch_repository.test.cjs(and possibly MCP tool registration tests) would help prevent regressions.This issue also appears in the following locations of the same file: