internal-providers has the schemas for integrations

This commit is contained in:
Matt Aitken
2023-01-09 14:28:33 +00:00
parent 11c040faaf
commit 8aee590ab4
20 changed files with 71 additions and 82 deletions
@@ -7,7 +7,15 @@ export async function getAccessToken(
): Promise<string | null | undefined> {
switch (connection.authenticationMethod) {
case "OAUTH": {
return await pizzly.accessToken(connection.apiIdentifier, connection.id);
const accessToken = await pizzly.accessToken(
connection.apiIdentifier,
connection.id
);
if (accessToken == null) {
return undefined;
}
//todo if it's an OAuth1 API then this will fail, as Pizzly returns an object
return accessToken as string;
}
case "API_KEY": {
const parsed = apiKeyConfigSchema.safeParse(
@@ -4,10 +4,8 @@ import crypto from "node:crypto";
import type { PrismaClient } from "~/db.server";
import { prisma } from "~/db.server";
import { env } from "~/env.server";
import { apiKeyConfigSchema } from "~/models/apiConnection.server";
import { findExternalSourceById } from "~/models/externalSource.server";
import { getAccessToken } from "../accessToken.server";
import { pizzly } from "../pizzly.server";
export class RegisterExternalSource {
#prismaClient: PrismaClient;
@@ -49,6 +47,10 @@ export class RegisterExternalSource {
connection: APIConnection
) {
const accessToken = await getAccessToken(connection);
if (accessToken == null) {
throw new Error("No access token found for webhook");
}
const secret = crypto.randomBytes(32).toString("hex");
const webhookUrl = `${env.APP_ORIGIN}/api/v1/internal/webhooks/${connection.apiIdentifier}/${externalSource.id}`;
@@ -1,4 +0,0 @@
import Mergent from "mergent";
import { env } from "~/env.server";
export const mergent = new Mergent(env.MERGENT_KEY);
+1 -1
View File
@@ -7,7 +7,7 @@ const trigger = new Trigger({
apiKey: "trigger_dev_zC25mKNn6c0q",
endpoint: "ws://localhost:8889/ws",
logLevel: "debug",
on: github.triggers.repoIssueEvent({ repo: "triggerdotdev/trigger.dev" }),
on: github.events.repoIssueEvent({ repo: "triggerdotdev/trigger.dev" }),
run: async (event, ctx) => {
await ctx.logger.info(
"Inside the github-webhook workflow, received event",
+4 -1
View File
@@ -18,7 +18,10 @@ const trigger = new Trigger({
myDate: new Date(),
});
await ctx.fireEvent({ name: "smoke.test", payload: { baz: "banana" } });
await ctx.fireEvent("start-fire", {
name: "smoke.test",
payload: { baz: "banana" },
});
return { foo: "bar" };
},
+2 -1
View File
@@ -7,6 +7,7 @@
"types": "./src/index.ts",
"devDependencies": {
"@trigger.dev/tsconfig": "workspace:*",
"internal-providers": "workspace:*",
"@types/debug": "^4.1.7",
"@types/node": "^18.11.9",
"typescript": "^4.9.4"
@@ -17,4 +18,4 @@
"debug": "^4.3.4",
"zod": "^3.20.2"
}
}
}
@@ -5,13 +5,8 @@ import {
WebhookIntegration,
} from "../types";
import { Webhooks } from "@octokit/webhooks";
import {
WebhookSourceSchema,
IssueEventSchema,
WebhookRepoSource,
WebhookOrganizationSource,
} from "./schemas";
import { z } from "zod";
import { github } from "internal-providers";
export class GitHubWebhookIntegration implements WebhookIntegration {
keyForSource(source: unknown): string {
@@ -115,14 +110,10 @@ export class GitHubWebhookIntegration implements WebhookIntegration {
}
export const webhooks = new GitHubWebhookIntegration();
export const schemas = {
IssueEventSchema,
WebhookSourceSchema,
};
async function registerRepositoryWebhook(
config: WebhookConfig,
source: WebhookRepoSource
source: z.infer<typeof github.schemas.WebhookRepoSourceSchema>
) {
// Create the webhook in github
const response = await fetch(
@@ -160,7 +151,7 @@ async function registerRepositoryWebhook(
async function registerOrganizationWebhook(
config: WebhookConfig,
source: WebhookOrganizationSource
source: z.infer<typeof github.schemas.WebhookOrganizationSourceSchema>
) {
// Create the webhook in github
const response = await fetch(
@@ -197,7 +188,7 @@ async function registerOrganizationWebhook(
}
function parseWebhookSource(source: unknown) {
return WebhookSourceSchema.parse(source);
return github.schemas.WebhookSourceSchema.parse(source);
}
function omit<T extends Record<string, unknown>, K extends keyof T>(
@@ -6,20 +6,7 @@ import {
PerformRequestOptions,
RequestIntegration,
} from "../types";
import {
PostMessageSuccessResponseSchema,
PostMessageResponseSchema,
PostMessageBodySchema,
JoinConversationResponseSchema,
JoinConversationBodySchema,
ListConversationsResponseSchema,
} from "./schemas";
export const schemas = {
PostMessageSuccessResponseSchema,
PostMessageResponseSchema,
PostMessageBodySchema,
};
import { slack } from "internal-providers";
import debug from "debug";
@@ -27,25 +14,25 @@ const log = debug("trigger:integrations:slack");
class SlackRequestIntegration implements RequestIntegration {
#joinChannelEndpoint = new HttpEndpoint<
typeof JoinConversationResponseSchema,
typeof JoinConversationBodySchema
typeof slack.schemas.JoinConversationResponseSchema,
typeof slack.schemas.JoinConversationBodySchema
>({
response: JoinConversationResponseSchema,
response: slack.schemas.JoinConversationResponseSchema,
method: "POST",
path: "/conversations.join",
});
#listConversationsEndpoint = new HttpEndpoint({
response: ListConversationsResponseSchema,
response: slack.schemas.ListConversationsResponseSchema,
method: "GET",
path: "/conversations.list",
});
#postMessageEndpoint = new HttpEndpoint<
typeof PostMessageResponseSchema,
typeof PostMessageBodySchema
typeof slack.schemas.PostMessageResponseSchema,
typeof slack.schemas.PostMessageBodySchema
>({
response: PostMessageResponseSchema,
response: slack.schemas.PostMessageResponseSchema,
method: "POST",
path: "/chat.postMessage",
});
@@ -91,7 +78,7 @@ class SlackRequestIntegration implements RequestIntegration {
params: any,
cache?: CacheService
): Promise<PerformedRequestResponse> {
const parsedParams = PostMessageBodySchema.parse(params);
const parsedParams = slack.schemas.PostMessageBodySchema.parse(params);
log("chat.postMessage %O", parsedParams);
+4 -1
View File
@@ -1,6 +1,9 @@
{
"extends": "@trigger.dev/tsconfig/node18.json",
"include": ["./src/**/*.ts"],
"include": [
"./src/**/*.ts",
"../internal-providers/src/providers/slack/schemas.ts"
],
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
+13 -9
View File
@@ -1,16 +1,20 @@
import { githubProvider } from "./providers/github";
import { slackProvider } from "./providers/slack";
import { Provider, ProviderCatalog } from "./types";
import { github } from "./providers/github";
import { slack } from "./providers/slack";
import { Provider } from "./types";
export type {
Provider,
ProviderCatalog,
APIKeyAuthentication,
OAuthAuthentication,
} from "./types";
const providerCatalog = {
providers: { github, slack },
};
export function getProviders(isAdmin: boolean): Provider[] {
return catalog.providers.filter((provider) => {
const providers = Object.values(providerCatalog.providers);
return providers.filter((provider) => {
switch (provider.enabledFor) {
case "all":
return true;
@@ -18,10 +22,10 @@ export function getProviders(isAdmin: boolean): Provider[] {
return isAdmin;
case "none":
return false;
default:
return false;
}
});
}) as Provider[];
}
const catalog: ProviderCatalog = {
providers: [githubProvider, slackProvider],
};
export { github, slack };
@@ -1,6 +1,6 @@
import { Provider } from "../types";
import * as schemas from "./schemas";
export const githubProvider: Provider = {
export const github = {
name: "GitHub",
slug: "github",
icon: "/integrations/github.png",
@@ -17,4 +17,5 @@ export const githubProvider: Provider = {
},
},
},
schemas,
};
@@ -7,8 +7,6 @@ export const WebhookRepoSourceSchema = z.object({
events: z.array(z.string()),
});
export type WebhookRepoSource = z.infer<typeof WebhookRepoSourceSchema>;
export const WebhookOrganizationSourceSchema = z.object({
subresource: z.literal("organization"),
scopes: z.array(z.string()),
@@ -16,10 +14,6 @@ export const WebhookOrganizationSourceSchema = z.object({
events: z.array(z.string()),
});
export type WebhookOrganizationSource = z.infer<
typeof WebhookOrganizationSourceSchema
>;
export const WebhookSourceSchema = z.union([
WebhookRepoSourceSchema,
WebhookOrganizationSourceSchema,
@@ -1,6 +1,6 @@
import { Provider } from "../types";
import * as schemas from "./schemas";
export const slackProvider: Provider = {
export const slack = {
name: "Slack",
slug: "slack",
icon: "/integrations/slack.png",
@@ -17,4 +17,5 @@ export const slackProvider: Provider = {
},
},
},
schemas,
};
+4 -1
View File
@@ -1,9 +1,12 @@
import { ZodTypeAny } from "zod";
export type Provider = {
name: string;
slug: string;
icon: string;
enabledFor: "all" | "admins" | "none";
authentication: OAuthAuthentication | APIKeyAuthentication;
schemas: Record<string, ZodTypeAny>;
};
export type OAuthAuthentication = {
@@ -20,5 +23,5 @@ export type APIKeyAuthentication = {
};
export type ProviderCatalog = {
providers: Provider[];
providers: Record<string, Provider>;
};
+2 -2
View File
@@ -9,7 +9,7 @@
],
"devDependencies": {
"@trigger.dev/tsconfig": "workspace:*",
"internal-integrations": "workspace:*",
"internal-providers": "workspace:*",
"@types/node": "^18.11.9",
"rimraf": "^3.0.2",
"tsup": "^6.5.0"
@@ -24,4 +24,4 @@
"zod": "^3.20.2",
"@trigger.dev/sdk": "workspace:*"
}
}
}
@@ -1,5 +1,5 @@
import { TriggerEvent } from "@trigger.dev/sdk";
import { github } from "internal-integrations";
import { github } from "internal-providers";
export function repoIssueEvent(params: {
repo: string;
@@ -1,3 +1,3 @@
import * as triggers from "./triggers";
import * as events from "./events";
export { triggers };
export { events };
@@ -1,6 +1,6 @@
import { getTriggerRun } from "@trigger.dev/sdk";
import { z } from "zod";
import { slack } from "internal-integrations";
import { slack } from "internal-providers";
export type PostMessageOptions = z.infer<
typeof slack.schemas.PostMessageBodySchema
+4 -9
View File
@@ -584,6 +584,7 @@ importers:
'@types/debug': ^4.1.7
'@types/node': ^18.11.9
debug: ^4.3.4
internal-providers: workspace:*
typescript: ^4.9.4
zod: ^3.20.2
dependencies:
@@ -594,6 +595,7 @@ importers:
'@trigger.dev/tsconfig': link:../../config-packages/tsconfig
'@types/debug': 4.1.7
'@types/node': 18.11.15
internal-providers: link:../internal-providers
typescript: 4.9.4
packages/internal-platform:
@@ -623,11 +625,9 @@ importers:
'@types/node': ^18.11.9
tiny-invariant: ^1.2.0
typescript: ^4.9.4
yaml: ^2.2.0
zod: ^3.20.2
dependencies:
tiny-invariant: 1.3.1
yaml: 2.2.0
zod: 3.20.2
devDependencies:
'@trigger.dev/tsconfig': link:../../config-packages/tsconfig
@@ -664,7 +664,7 @@ importers:
'@trigger.dev/sdk': workspace:*
'@trigger.dev/tsconfig': workspace:*
'@types/node': ^18.11.9
internal-integrations: workspace:*
internal-providers: workspace:*
rimraf: ^3.0.2
tsup: ^6.5.0
zod: ^3.20.2
@@ -674,7 +674,7 @@ importers:
devDependencies:
'@trigger.dev/tsconfig': link:../../config-packages/tsconfig
'@types/node': 18.11.11
internal-integrations: link:../internal-integrations
internal-providers: link:../internal-providers
rimraf: 3.0.2
tsup: 6.5.0
@@ -17019,11 +17019,6 @@ packages:
resolution: {integrity: sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==}
engines: {node: '>= 6'}
/yaml/2.2.0:
resolution: {integrity: sha512-auf7Gi6QwO7HW//GA9seGvTXVGWl1CM/ADWh1+RxtXr6XOxnT65ovDl9fTi4e0monEyJxCHqDpF6QnFDXmJE4g==}
engines: {node: '>= 14'}
dev: false
/yargs-parser/20.2.9:
resolution: {integrity: sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==}
engines: {node: '>=10'}