forked from ubiquity-os-marketplace/command-start-stop
-
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 branch 'development' into fix/config-props
- Loading branch information
Showing
26 changed files
with
7,135 additions
and
9,772 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>; |
Oops, something went wrong.