generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat: config event handler #18
Merged
Merged
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
89f7de7
feat: config event handler
whilefoo b17af14
feat: workflow dispatch
whilefoo 10887c3
fix: add missing events
whilefoo 084d1c1
feat: webhook type
Keyrxng 72fdeef
Update webhook-events.ts
Keyrxng 441ebbd
Update webhook-events.ts
Keyrxng 7c6043a
Update webhook-events.ts
Keyrxng 5f0e142
Update webhook-events.ts
Keyrxng ea1b1d7
Merge pull request #20 from Keyrxng/config
whilefoo 0979fd7
feat: updated delegated compute inputs
whilefoo 3c0b829
feat: remove our enum because we use octokit's types
whilefoo b224246
feat: pass auth token instead of installation id
whilefoo 295284a
fix: remove toString()
whilefoo b02d104
feat: ref
whilefoo 7bd5ff8
feat: plugin chain config and repo dispatch handler
whilefoo 54c29c8
feat: added instructions to deploy to Cloudflare Workers
whilefoo b5fd06d
fix: add env param
whilefoo d769841
feat: store event payload, inputs and outputs of plugins in chain state
whilefoo 92e48bf
feat: update instructions
whilefoo 0d83199
feat: support using output from previous plugins
whilefoo 387d33b
fix: refactor into smaller functions
whilefoo 597d9ce
fix: typescript too complex expression
whilefoo 37cb25c
feat: skip bot events
whilefoo c000369
Merge remote-tracking branch 'origin/development' into config
whilefoo 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 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 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
Binary file not shown.
This file contains 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 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 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 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,6 +1,79 @@ | ||
import { EmitterWebhookEvent } from "@octokit/webhooks"; | ||
import { GitHubEventHandler } from "../github-event-handler"; | ||
import { getConfig } from "../utils/config"; | ||
import { issueCommentCreated } from "./issue-comment/created"; | ||
import { repositoryDispatch } from "./repository-dispatch"; | ||
import { dispatchWorkflow, getDefaultBranch } from "../utils/workflow-dispatch"; | ||
import { DelegatedComputeInputs } from "../types/plugin"; | ||
|
||
export function bindHandlers(webhooks: GitHubEventHandler) { | ||
webhooks.on("issue_comment.created", issueCommentCreated); | ||
function tryCatchWrapper(fn: (event: EmitterWebhookEvent) => unknown) { | ||
return async (event: EmitterWebhookEvent) => { | ||
try { | ||
await fn(event); | ||
} catch (error) { | ||
console.error("Error in event handler", error); | ||
} | ||
}; | ||
} | ||
|
||
export function bindHandlers(eventHandler: GitHubEventHandler) { | ||
eventHandler.on("issue_comment.created", issueCommentCreated); | ||
eventHandler.on("repository_dispatch", repositoryDispatch); | ||
eventHandler.onAny(tryCatchWrapper((event) => handleEvent(event, eventHandler))); // onAny should also receive GithubContext but the types in octokit/webhooks are weird | ||
} | ||
|
||
async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceType<typeof GitHubEventHandler>) { | ||
const context = eventHandler.transformEvent(event); | ||
|
||
const config = await getConfig(context); | ||
|
||
if (!config) { | ||
console.log("No config found"); | ||
return; | ||
} | ||
|
||
if (!("installation" in event.payload) || event.payload.installation?.id === undefined) { | ||
console.log("No installation found"); | ||
return; | ||
} | ||
|
||
const pluginChains = config.plugins[context.key].concat(config.plugins["*"]); | ||
|
||
if (pluginChains.length === 0) { | ||
console.log(`No handler found for event ${event.name}`); | ||
return; | ||
} | ||
|
||
for (const pluginChain of pluginChains) { | ||
// invoke the first plugin in the chain | ||
const { plugin, with: settings } = pluginChain.uses[0]; | ||
console.log(`Calling handler for event ${event.name}`); | ||
|
||
const stateId = crypto.randomUUID(); | ||
|
||
const state = { | ||
eventId: context.id, | ||
eventName: context.key, | ||
eventPayload: event.payload, | ||
currentPlugin: 0, | ||
pluginChain: pluginChain.uses, | ||
outputs: new Array(pluginChain.uses.length), | ||
inputs: new Array(pluginChain.uses.length), | ||
}; | ||
|
||
const ref = plugin.ref ?? (await getDefaultBranch(context, plugin.owner, plugin.repo)); | ||
const token = await eventHandler.getToken(event.payload.installation.id); | ||
const inputs = new DelegatedComputeInputs(stateId, context.key, event.payload, settings, token, ref); | ||
|
||
state.inputs[0] = inputs; | ||
await eventHandler.pluginChainState.put(stateId, state); | ||
|
||
await dispatchWorkflow(context, { | ||
owner: plugin.owner, | ||
repository: plugin.repo, | ||
workflowId: plugin.workflowId, | ||
ref: plugin.ref, | ||
inputs: inputs.getInputs(), | ||
}); | ||
} | ||
} |
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.
So I assume that this is the kernel logic to generate the token with the intent to pass it along to the plugin.