-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ubq-testing/workflows
Workflows
- Loading branch information
Showing
18 changed files
with
527 additions
and
147 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { getAppOctokit } from "#root/helpers/authenticated-octokit.js"; | ||
import { PluginContext } from "#root/utils/plugin-context-single.js"; | ||
import { Context } from "../types"; | ||
|
||
/** | ||
* Used by the worker instance to kick off workflows within it's own repository. | ||
* | ||
* These workflows are extensions of the worker allowing for more complex operations | ||
* to be performed outside of Cloudflare Workers' limitations. | ||
* | ||
* @param env The environment variables for the worker instance. These | ||
* will be taken from the repository's secrets. | ||
* @param args The arguments passed to the workflow. | ||
* | ||
*/ | ||
|
||
export async function repositoryDispatch(context: Context, workflow: string) { | ||
const inputs = PluginContext.getInstance().getInputs(); | ||
const { logger } = context; | ||
const repository = "telegram--bot"; | ||
const owner = "ubq-testing"; | ||
const branch = "workflows"; | ||
const app = await getAppOctokit(context); | ||
const installation = await app.octokit.rest.apps.getRepoInstallation({ owner, repo: repository }); | ||
|
||
// Set the installation id for the octokit instance | ||
|
||
const octokit = await app.getInstallationOctokit(installation.data.id); | ||
|
||
logger.info(`Dispatching workflow function: ${workflow}`); | ||
|
||
|
||
/** | ||
* We'll hit the main workflow entry and pass in the same inputs so | ||
* that it essentially runs on the same context as the worker. | ||
*/ | ||
|
||
Reflect.deleteProperty(inputs, "signature"); | ||
|
||
return await octokit.rest.actions.createWorkflowDispatch({ | ||
owner, | ||
repo: repository, | ||
workflow_id: "compute.yml", | ||
ref: branch, | ||
inputs: { | ||
...inputs, | ||
eventPayload: JSON.stringify(context.payload), | ||
settings: JSON.stringify(context.config), | ||
} | ||
}); | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { App } from "octokit"; | ||
import { Context } from "../types"; | ||
|
||
export async function getAppOctokit(context: Context) { | ||
const { env: { APP_ID, APP_PRIVATE_KEY } } = context; | ||
return new App({ appId: APP_ID, privateKey: APP_PRIVATE_KEY }); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Context, SupportedEvents, SupportedEventsU } from "./context"; | ||
|
||
export type CallbackResult = { status: 200 | 201 | 204 | 404 | 500, reason: string; content?: string | Record<string, any> }; | ||
|
||
/** | ||
* The `Context` type is a generic type defined as `Context<TEvent, TPayload>`, | ||
* where `TEvent` is a string representing the event name (e.g., "issues.labeled") | ||
* and `TPayload` is the webhook payload type for that event, derived from | ||
* the `SupportedEvents` type map. | ||
* | ||
* The `ProxyCallbacks` object is cast to allow optional callbacks | ||
* for each event type. This is useful because not all events may have associated callbacks. | ||
* As opposed to Partial<ProxyCallbacks> which could mean an undefined object. | ||
* | ||
* The expected function signature for callbacks looks like this: | ||
* | ||
* ```typescript | ||
* fn(context: Context<"issues.labeled", SupportedEvents["issues.labeled"]>): Promise<Result> | ||
* ``` | ||
*/ | ||
|
||
type ProxyTypeHelper = { | ||
[K in SupportedEventsU]: Array<(context: Context<K, SupportedEvents[K]>) => Promise<CallbackResult>>; | ||
}; | ||
export type ProxyCallbacks = ProxyTypeHelper; |
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,24 @@ | ||
import { LogReturn } from "@ubiquity-dao/ubiquibot-logger"; | ||
import { Context } from "../types"; | ||
import { addCommentToIssue } from "#root/handlers/github/utils/add-comment-to-issues.js"; | ||
|
||
export function handleUncaughtError(error: unknown) { | ||
console.error(error); | ||
const status = 500; | ||
return new Response(JSON.stringify({ error }), { status: status, headers: { "content-type": "application/json" } }); | ||
} | ||
export function sanitizeMetadata(obj: LogReturn["metadata"]): string { | ||
return JSON.stringify(obj, null, 2).replace(/</g, "<").replace(/>/g, ">").replace(/--/g, "--"); | ||
} | ||
|
||
export async function bubbleUpErrorComment(context: Context, err: unknown) { | ||
let errorMessage; | ||
if (err instanceof LogReturn) { | ||
errorMessage = err; | ||
} else if (err instanceof Error) { | ||
errorMessage = context.logger.error(err.message, { error: err }); | ||
} else { | ||
errorMessage = context.logger.error("An error occurred", { err }); | ||
} | ||
await addCommentToIssue(context, `${errorMessage?.logMessage.diff}\n<!--\n${sanitizeMetadata(errorMessage?.metadata)}\n-->`); | ||
} |
Oops, something went wrong.