Skip to content

Commit 8e83042

Browse files
chore: refine worker dispatch logic, update dependencies, and clean up typings
1 parent 0037833 commit 8e83042

5 files changed

Lines changed: 19 additions & 9 deletions

File tree

‎bun.lock‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"@octokit/webhooks": "^14.2.0",
6565
"@octokit/webhooks-types": "^7.6.1",
6666
"@sinclair/typebox": "^0.34.30",
67-
"@ubiquity-os/plugin-sdk": "^3.5.2",
67+
"@ubiquity-os/plugin-sdk": "^3.5.3",
6868
"dotenv": "16.4.5",
6969
"hono": "^4.10.7",
7070
"js-yaml": "^4.1.1",

‎src/github/handlers/index.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,18 @@ async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceTyp
5656
const stateId = crypto.randomUUID();
5757
const manifest = await getManifest(context, plugin);
5858
const workerUrl = getWorkerUrlFromManifest(manifest);
59-
const ref = workerUrl ? workerUrl : plugin.ref ?? (await getDefaultBranch(context, plugin.owner, plugin.repo));
59+
const ref = workerUrl ? workerUrl : (plugin.ref ?? (await getDefaultBranch(context, plugin.owner, plugin.repo)));
6060
const token = await eventHandler.getToken(event.payload.installation.id);
6161
const inputs = new PluginInput(context.eventHandler, stateId, context.key, event.payload, settings?.with, token, ref, null);
6262

6363
// We wrap the dispatch so a failing plugin doesn't break the whole execution
6464
try {
6565
context.logger.debug({ plugin: pluginEntry.key, worker: Boolean(workerUrl) }, "Dispatching event");
6666
if (workerUrl) {
67-
await dispatchWorker(workerUrl, await inputs.getInputs());
67+
const res = await dispatchWorker(workerUrl, await inputs.getInputs());
68+
if (res.status >= 300) {
69+
context.logger.warn({ plugin: pluginEntry.key, response: await safeJson(res) }, "Error response on dispatch event");
70+
}
6871
} else {
6972
await dispatchWorkflow(context, {
7073
owner: plugin.owner,
@@ -80,3 +83,11 @@ async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceTyp
8083
}
8184
}
8285
}
86+
87+
async function safeJson(response: Response) {
88+
const contentType = response.headers.get("content-type");
89+
if (contentType && contentType.includes("application/json")) {
90+
return response.json();
91+
}
92+
return await response.text();
93+
}

‎src/github/utils/workflow-dispatch.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ export async function dispatchWorkflow(context: GitHubContext, options: Workflow
3333
}
3434

3535
export async function dispatchWorker(targetUrl: string, payload?: Record<string, unknown>) {
36-
const result = await fetch(targetUrl, {
36+
return await fetch(targetUrl, {
3737
body: JSON.stringify(payload),
3838
method: "POST",
3939
headers: {
4040
"Content-Type": "application/json",
4141
},
4242
});
43-
return result.json();
4443
}
4544

4645
export async function getDefaultBranch(context: GitHubContext, owner: string, repository: string) {

‎tsconfig.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
3232
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
3333
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
34-
"types": ["@cloudflare/workers-types/2023-07-01"] /* Specify type package names to be included without being referenced in a source file. */,
34+
"types": [] /* Specify type package names to be included without being referenced in a source file. */,
3535
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
3636
"resolveJsonModule": true /* Enable importing .json files */,
3737
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
@@ -48,7 +48,7 @@
4848
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
51-
"rootDir": ".",
51+
"rootDir": ".",
5252
// "removeComments": true, /* Disable emitting comments. */
5353
// "noEmit": true /* Disable emitting files from a compilation. */,
5454
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */

0 commit comments

Comments
 (0)