generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 22
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 #86 from ubiquity-whilefoo/command-interface
SDK and command interface
- Loading branch information
Showing
26 changed files
with
7,114 additions
and
9,782 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 was deleted.
Oops, something went wrong.
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,27 @@ | ||
import type { Config } from "jest"; | ||
|
||
const cfg: Config = { | ||
transform: { | ||
"^.+\\.tsx?$": [ | ||
"ts-jest", | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], | ||
coveragePathIgnorePatterns: ["node_modules", "mocks"], | ||
collectCoverage: true, | ||
coverageReporters: ["json", "lcov", "text", "clover", "json-summary"], | ||
reporters: ["default", "jest-junit", "jest-md-dashboard"], | ||
coverageDirectory: "coverage", | ||
testTimeout: 20000, | ||
roots: ["<rootDir>", "tests"], | ||
extensionsToTreatAsEsm: [".ts"], | ||
moduleNameMapper: { | ||
"^(\\.{1,2}/.*)\\.js$": "$1", | ||
}, | ||
setupFilesAfterEnv: ["dotenv/config"], | ||
}; | ||
|
||
export default cfg; |
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
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,60 +1,32 @@ | ||
import { paginateGraphQL } from "@octokit/plugin-paginate-graphql"; | ||
import { Octokit } from "@octokit/rest"; | ||
import { createClient } from "@supabase/supabase-js"; | ||
import { LogReturn, Logs } from "@ubiquity-os/ubiquity-os-logger"; | ||
import { createAdapters } from "./adapters"; | ||
import { userPullRequest, userSelfAssign, userStartStop, userUnassigned } from "./handlers/user-start-stop"; | ||
import { Context, Env, PluginInputs } from "./types"; | ||
import { addCommentToIssue } from "./utils/issue"; | ||
import { commandHandler, userPullRequest, userSelfAssign, userStartStop, userUnassigned } from "./handlers/user-start-stop"; | ||
import { Context } from "./types"; | ||
import { listOrganizations } from "./utils/list-organizations"; | ||
import { HttpStatusCode } from "./handlers/result-types"; | ||
import { createAdapters } from "./adapters"; | ||
import { createClient } from "@supabase/supabase-js"; | ||
|
||
export async function startStopTask(inputs: PluginInputs, env: Env) { | ||
const customOctokit = Octokit.plugin(paginateGraphQL); | ||
const octokit = new customOctokit({ auth: inputs.authToken }); | ||
const supabase = createClient(env.SUPABASE_URL, env.SUPABASE_KEY); | ||
|
||
const context: Context = { | ||
eventName: inputs.eventName, | ||
payload: inputs.eventPayload, | ||
config: inputs.settings, | ||
organizations: [], | ||
octokit, | ||
env, | ||
logger: new Logs("info"), | ||
adapters: {} as ReturnType<typeof createAdapters>, | ||
}; | ||
|
||
context.adapters = createAdapters(supabase, context); | ||
|
||
try { | ||
const organizations = await listOrganizations(context); | ||
context.organizations = organizations; | ||
export async function startStopTask(context: Context) { | ||
context.adapters = createAdapters(createClient(context.env.SUPABASE_URL, context.env.SUPABASE_KEY), context as Context); | ||
const organizations = await listOrganizations(context); | ||
context.organizations = organizations; | ||
|
||
switch (context.eventName) { | ||
case "issue_comment.created": | ||
return await userStartStop(context); | ||
case "issues.assigned": | ||
return await userSelfAssign(context as Context<"issues.assigned">); | ||
case "pull_request.opened": | ||
return await userPullRequest(context as Context<"pull_request.opened">); | ||
case "pull_request.edited": | ||
return await userPullRequest(context as Context<"pull_request.edited">); | ||
case "issues.unassigned": | ||
return await userUnassigned(context as Context<"issues.unassigned">); | ||
default: | ||
context.logger.error(`Unsupported event: ${context.eventName}`); | ||
} | ||
} catch (err) { | ||
let errorMessage; | ||
if (err instanceof LogReturn) { | ||
errorMessage = err; | ||
await addCommentToIssue(context, `${errorMessage?.logMessage.diff}\n<!--\n${sanitizeMetadata(errorMessage?.metadata)}\n-->`); | ||
} else { | ||
context.logger.error("An error occurred", { err }); | ||
} | ||
if (context.command) { | ||
return await commandHandler(context); | ||
} | ||
} | ||
|
||
function sanitizeMetadata(obj: LogReturn["metadata"]): string { | ||
return JSON.stringify(obj, null, 2).replace(/</g, "<").replace(/>/g, ">").replace(/--/g, "--"); | ||
switch (context.eventName) { | ||
case "issue_comment.created": | ||
return await userStartStop(context as Context<"issue_comment.created">); | ||
case "issues.assigned": | ||
return await userSelfAssign(context as Context<"issues.assigned">); | ||
case "pull_request.opened": | ||
return await userPullRequest(context as Context<"pull_request.opened">); | ||
case "pull_request.edited": | ||
return await userPullRequest(context as Context<"pull_request.edited">); | ||
case "issues.unassigned": | ||
return await userUnassigned(context as Context<"issues.unassigned">); | ||
default: | ||
context.logger.error(`Unsupported event: ${context.eventName}`); | ||
return { status: HttpStatusCode.BAD_REQUEST }; | ||
} | ||
} |
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,17 @@ | ||
import { Type as T } from "@sinclair/typebox"; | ||
import { StaticDecode } from "@sinclair/typebox"; | ||
|
||
export const startCommandSchema = T.Object({ | ||
name: T.Literal("start"), | ||
parameters: T.Object({ | ||
teammates: T.Array(T.String()), | ||
}), | ||
}); | ||
|
||
export const stopCommandSchema = T.Object({ | ||
name: T.Literal("stop"), | ||
}); | ||
|
||
export const commandSchema = T.Union([startCommandSchema, stopCommandSchema]); | ||
|
||
export type Command = StaticDecode<typeof commandSchema>; |
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,28 +1,16 @@ | ||
import { paginateGraphQLInterface } from "@octokit/plugin-paginate-graphql"; | ||
import { Octokit } from "@octokit/rest"; | ||
import { EmitterWebhookEvent as WebhookEvent, EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; | ||
import { Logs } from "@ubiquity-os/ubiquity-os-logger"; | ||
import { Context as PluginContext } from "@ubiquity-os/plugin-sdk"; | ||
import { createAdapters } from "../adapters"; | ||
import { Env } from "./env"; | ||
import { PluginSettings } from "./plugin-input"; | ||
import { Command } from "./command"; | ||
|
||
export type SupportedEventsU = "issue_comment.created" | "issues.assigned" | "pull_request.opened" | "pull_request.edited" | "issues.unassigned"; | ||
|
||
export type SupportedEvents = { | ||
[K in SupportedEventsU]: K extends WebhookEventName ? WebhookEvent<K> : never; | ||
}; | ||
export type SupportedEvents = "issue_comment.created" | "issues.assigned" | "pull_request.opened" | "pull_request.edited" | "issues.unassigned"; | ||
|
||
export function isIssueCommentEvent(context: Context): context is Context<"issue_comment.created"> { | ||
return "issue" in context.payload; | ||
} | ||
|
||
export interface Context<T extends SupportedEventsU = SupportedEventsU, TU extends SupportedEvents[T] = SupportedEvents[T]> { | ||
eventName: T; | ||
payload: TU["payload"]; | ||
octokit: InstanceType<typeof Octokit> & paginateGraphQLInterface; | ||
export type Context<TEvents extends SupportedEvents = SupportedEvents> = PluginContext<PluginSettings, Env, Command, TEvents> & { | ||
adapters: ReturnType<typeof createAdapters>; | ||
config: PluginSettings; | ||
organizations: string[]; | ||
env: Env; | ||
logger: Logs; | ||
} | ||
}; |
Oops, something went wrong.