Implemented seeding the cloud based on env vars

This commit is contained in:
Eric Allam
2023-06-22 12:00:07 +01:00
parent 95787fcc66
commit b62f00dfd7
6 changed files with 446 additions and 427 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ import { z } from "zod";
export type PrismaTransactionClient = Omit<
PrismaClient,
"$connect" | "$disconnect" | "$on" | "$transaction" | "$use"
"$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends"
>;
export type PrismaClientOrTransaction = PrismaClient | PrismaTransactionClient;
+3 -3
View File
@@ -16,7 +16,7 @@
"test:e2e:dev": "start-server-and-test dev http://localhost:3000 \"npx cypress open\"",
"test:e2e:run": "cross-env PORT=8811 start-server-and-test start:mocks http://localhost:8811 \"npx cypress run\"",
"test:e2e:ci": "pnpx cypress run",
"typecheck": "tsc -b && tsc -b cypress",
"typecheck": "tsc -b",
"db:seed": "ts-node prisma/seed.ts",
"generate:sourcemaps": "remix build --sourcemap",
"sentry-upload": "pnpm run generate:sourcemaps && sentry-upload-sourcemaps",
@@ -216,8 +216,8 @@
"nodemon": "^2.0.19",
"npm-run-all": "^4.1.5",
"postcss": "^8.4.21",
"prettier": "^2.6.2",
"prettier-plugin-tailwindcss": "^0.1.13",
"prettier": "^2.8.8",
"prettier-plugin-tailwindcss": "^0.3.0",
"prop-types": "^15.8.1",
"react-date-range": "^1.4.0",
"rimraf": "^3.0.2",
+7 -417
View File
@@ -1,12 +1,10 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import { PrismaClient } from "@trigger.dev/database";
import { integrationCatalog } from "../app/services/externalApis/integrationCatalog.server";
import { seedCloud } from "./seedCloud";
const prisma = new PrismaClient();
const GITHUB_CONNECTION_KEY = "github-seed-key";
const SLACK_CONNECTION_KEY = "slack-seed-key";
async function seedIntegrationAuthMethods() {
for (const [identifier, integration] of Object.entries(
integrationCatalog.getIntegrations()
@@ -67,420 +65,12 @@ async function seedIntegrationAuthMethods() {
async function seed() {
await seedIntegrationAuthMethods();
// TODO: extract this into a separate file and put it behind some kind of env var
// Create a user, organization, and project
const user = await prisma.user.upsert({
where: {
email: "eric@trigger.dev",
},
create: {
email: "eric@trigger.dev",
name: "Eric",
authenticationMethod: "MAGIC_LINK",
},
update: {},
});
const organization = await prisma.organization.upsert({
where: {
slug: "seed-org-123",
},
create: {
title: "Personal Workspace",
slug: "seed-org-123",
members: {
create: {
userId: user.id,
role: "ADMIN",
},
},
projects: {
create: {
name: "My Project",
slug: "my-project-123",
},
},
},
update: {},
include: {
members: true,
projects: true,
},
});
const adminMember = organization.members[0];
const defaultProject = organization.projects[0];
const devEnv = await prisma.runtimeEnvironment.upsert({
where: {
apiKey: "tr_dev_bNaLxayOXqoj",
},
create: {
apiKey: "tr_dev_bNaLxayOXqoj",
slug: "dev",
type: "DEVELOPMENT",
project: {
connect: {
id: defaultProject.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
orgMember: {
connect: {
id: adminMember.id,
},
},
},
update: {},
});
await prisma.runtimeEnvironment.upsert({
where: {
apiKey: "tr_prod_bNaLxayOXqoj",
},
create: {
apiKey: "tr_prod_bNaLxayOXqoj",
slug: "prod",
type: "PRODUCTION",
project: {
connect: {
id: defaultProject.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
},
update: {},
});
// Now we need to create a couple of Integrations
const slackIntegration = await prisma.integration.upsert({
where: {
organizationId_slug: {
organizationId: organization.id,
slug: "my-slack-new",
},
},
create: {
organization: {
connect: {
id: organization.id,
},
},
definition: {
connect: {
id: "slack",
},
},
slug: "my-slack-new",
title: "My Slack",
scopes: ["chat:write"],
authSource: "HOSTED",
connectionType: "DEVELOPER",
authMethod: {
connect: {
definitionId_key: {
definitionId: "slack",
key: "oauth2Bot",
},
},
},
},
update: {},
});
const githubIntegration = await prisma.integration.upsert({
where: {
organizationId_slug: {
organizationId: organization.id,
slug: "github",
},
},
create: {
organization: {
connect: {
id: organization.id,
},
},
definition: {
connect: {
id: "github",
},
},
slug: "github",
title: "GitHub",
scopes: ["admin:repo_hook", "public_repo"],
authSource: "HOSTED",
connectionType: "DEVELOPER",
authMethod: {
connect: {
definitionId_key: {
definitionId: "github",
key: "oauth2",
},
},
},
},
update: {},
});
await prisma.integrationConnection.upsert({
where: {
id: "clhkhsvx20000rmdy9u9d25e7",
},
create: {
id: "clhkhsvx20000rmdy9u9d25e7",
metadata: {},
integration: {
connect: {
id: githubIntegration.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
connectionType: "DEVELOPER",
dataReference: {
connectOrCreate: {
where: {
key: GITHUB_CONNECTION_KEY,
},
create: {
key: GITHUB_CONNECTION_KEY,
provider: "DATABASE",
},
},
},
},
update: {},
});
await prisma.integrationConnection.upsert({
where: {
id: "clhkigzf90000rmdyfuiec6ew",
},
create: {
id: "clhkigzf90000rmdyfuiec6ew",
metadata: { account: "Trigger.dev" },
integration: {
connect: {
id: slackIntegration.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
connectionType: "DEVELOPER",
dataReference: {
connectOrCreate: {
where: {
key: SLACK_CONNECTION_KEY,
},
create: {
key: SLACK_CONNECTION_KEY,
provider: "DATABASE",
},
},
},
},
update: {},
});
await prisma.secretStore.upsert({
where: {
key: GITHUB_CONNECTION_KEY,
},
create: {
key: GITHUB_CONNECTION_KEY,
value: {
raw: {
scope: "admin:repo_hook,public_repo",
token_type: "bearer",
access_token: process.env.SEED_GITHUB_ACCESS_TOKEN,
},
type: "oauth2",
scopes: ["admin:repo_hook,public_repo"],
accessToken: process.env.SEED_GITHUB_ACCESS_TOKEN,
},
},
update: {},
});
await prisma.secretStore.upsert({
where: {
key: SLACK_CONNECTION_KEY,
},
create: {
key: SLACK_CONNECTION_KEY,
value: {
raw: {
ok: true,
team: { id: "T84AW8RBP", name: "Trigger.dev" },
scope:
"chat:write,channels:read,channels:manage,im:write,channels:join,chat:write.customize,bookmarks:read",
app_id: "A04H149K884",
enterprise: null,
token_type: "bot",
authed_user: { id: "U8590FPB9" },
bot_user_id: "U04H0UUQPHR",
access_token: process.env.SEED_SLACK_ACCESS_TOKEN,
is_enterprise_install: false,
},
type: "oauth2",
scopes: [
"chat:write",
"channels:read",
"channels:manage",
"im:write",
"channels:join",
"chat:write.customize",
"bookmarks:read",
],
accessToken: process.env.SEED_SLACK_ACCESS_TOKEN,
},
},
update: {},
});
const userGithubIntegration = await prisma.integration.upsert({
where: {
organizationId_slug: {
organizationId: organization.id,
slug: "github-user",
},
},
create: {
definition: {
connect: {
id: "github",
},
},
slug: "github-user",
title: "GitHub User",
scopes: ["admin:repo_hook", "public_repo"],
authSource: "HOSTED",
connectionType: "EXTERNAL",
authMethod: {
connect: {
definitionId_key: {
definitionId: "github",
key: "oauth2",
},
},
},
organization: {
connect: {
id: organization.id,
},
},
},
update: {},
});
const externalAccount1Identifier = "eric1234";
const externalAccount1 = await prisma.externalAccount.upsert({
where: {
environmentId_identifier: {
environmentId: devEnv.id,
identifier: externalAccount1Identifier,
},
},
create: {
organizationId: organization.id,
environmentId: devEnv.id,
identifier: externalAccount1Identifier,
metadata: { foo: "bar" },
},
update: {},
});
const externalAccount2Identifier = "matt1234";
const externalAccount2 = await prisma.externalAccount.upsert({
where: {
environmentId_identifier: {
environmentId: devEnv.id,
identifier: externalAccount2Identifier,
},
},
create: {
organizationId: organization.id,
environmentId: devEnv.id,
identifier: externalAccount2Identifier,
metadata: { bar: "baz" },
},
update: {},
});
await prisma.integrationConnection.upsert({
where: {
id: "cli1qcroy0000b4dy084m2jsr",
},
create: {
id: "cli1qcroy0000b4dy084m2jsr",
externalAccount: {
connect: {
id: externalAccount1.id,
},
},
metadata: {},
integration: {
connect: {
id: userGithubIntegration.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
connectionType: "EXTERNAL",
dataReference: {
connectOrCreate: {
where: {
key: `${externalAccount1Identifier}-github`,
},
create: {
key: `${externalAccount1Identifier}-github`,
provider: "DATABASE",
},
},
},
},
update: {},
});
await prisma.secretStore.upsert({
where: {
key: `${externalAccount1Identifier}-github`,
},
create: {
key: `${externalAccount1Identifier}-github`,
value: {
raw: {
scope: "admin:repo_hook,public_repo",
token_type: "bearer",
access_token: process.env.SEED_USER_GITHUB_ACCESS_TOKEN,
},
type: "oauth2",
scopes: ["admin:repo_hook,public_repo"],
accessToken: process.env.SEED_USER_GITHUB_ACCESS_TOKEN,
},
},
update: {},
});
if (
process.env.NODE_ENV === "development" &&
process.env.SEED_CLOUD === "enabled"
) {
await seedCloud(prisma);
}
}
seed()
+430
View File
@@ -0,0 +1,430 @@
import { PrismaClient } from "@trigger.dev/database";
const GITHUB_CONNECTION_KEY = "github-seed-key";
const SLACK_CONNECTION_KEY = "slack-seed-key";
export async function seedCloud(prisma: PrismaClient) {
if (!process.env.SEED_CLOUD_EMAIL) {
return;
}
const name = process.env.SEED_CLOUD_EMAIL.split("@")[0];
// Create a user, organization, and project
const user = await prisma.user.upsert({
where: {
email: process.env.SEED_CLOUD_EMAIL,
},
create: {
email: process.env.SEED_CLOUD_EMAIL,
name,
authenticationMethod: "MAGIC_LINK",
},
update: {},
});
const organization = await prisma.organization.upsert({
where: {
slug: "seed-org-123",
},
create: {
title: "Personal Workspace",
slug: "seed-org-123",
members: {
create: {
userId: user.id,
role: "ADMIN",
},
},
projects: {
create: {
name: "My Project",
slug: "my-project-123",
},
},
},
update: {},
include: {
members: true,
projects: true,
},
});
const adminMember = organization.members[0];
const defaultProject = organization.projects[0];
const devEnv = await prisma.runtimeEnvironment.upsert({
where: {
apiKey: "tr_dev_bNaLxayOXqoj",
},
create: {
apiKey: "tr_dev_bNaLxayOXqoj",
slug: "dev",
type: "DEVELOPMENT",
project: {
connect: {
id: defaultProject.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
orgMember: {
connect: {
id: adminMember.id,
},
},
},
update: {},
});
await prisma.runtimeEnvironment.upsert({
where: {
apiKey: "tr_prod_bNaLxayOXqoj",
},
create: {
apiKey: "tr_prod_bNaLxayOXqoj",
slug: "prod",
type: "PRODUCTION",
project: {
connect: {
id: defaultProject.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
},
update: {},
});
// Now we need to create a couple of Integrations
const slackIntegration = await prisma.integration.upsert({
where: {
organizationId_slug: {
organizationId: organization.id,
slug: "my-slack-new",
},
},
create: {
organization: {
connect: {
id: organization.id,
},
},
definition: {
connect: {
id: "slack",
},
},
slug: "my-slack-new",
title: "My Slack",
scopes: ["chat:write"],
authSource: "HOSTED",
connectionType: "DEVELOPER",
authMethod: {
connect: {
definitionId_key: {
definitionId: "slack",
key: "oauth2Bot",
},
},
},
},
update: {},
});
const githubIntegration = await prisma.integration.upsert({
where: {
organizationId_slug: {
organizationId: organization.id,
slug: "github",
},
},
create: {
organization: {
connect: {
id: organization.id,
},
},
definition: {
connect: {
id: "github",
},
},
slug: "github",
title: "GitHub",
scopes: ["admin:repo_hook", "public_repo"],
authSource: "HOSTED",
connectionType: "DEVELOPER",
authMethod: {
connect: {
definitionId_key: {
definitionId: "github",
key: "oauth2",
},
},
},
},
update: {},
});
if (process.env.SEED_CLOUD_GITHUB_ACCESS_TOKEN) {
await prisma.integrationConnection.upsert({
where: {
id: "clhkhsvx20000rmdy9u9d25e7",
},
create: {
id: "clhkhsvx20000rmdy9u9d25e7",
metadata: {},
integration: {
connect: {
id: githubIntegration.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
connectionType: "DEVELOPER",
dataReference: {
connectOrCreate: {
where: {
key: GITHUB_CONNECTION_KEY,
},
create: {
key: GITHUB_CONNECTION_KEY,
provider: "DATABASE",
},
},
},
},
update: {},
});
await prisma.secretStore.upsert({
where: {
key: GITHUB_CONNECTION_KEY,
},
create: {
key: GITHUB_CONNECTION_KEY,
value: {
raw: {
scope: "admin:repo_hook,public_repo",
token_type: "bearer",
access_token: process.env.SEED_CLOUD_GITHUB_ACCESS_TOKEN,
},
type: "oauth2",
scopes: ["admin:repo_hook,public_repo"],
accessToken: process.env.SEED_CLOUD_GITHUB_ACCESS_TOKEN,
},
},
update: {},
});
}
if (process.env.SEED_CLOUD_SLACK_ACCESS_TOKEN) {
await prisma.integrationConnection.upsert({
where: {
id: "clhkigzf90000rmdyfuiec6ew",
},
create: {
id: "clhkigzf90000rmdyfuiec6ew",
metadata: { account: "Trigger.dev" },
integration: {
connect: {
id: slackIntegration.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
connectionType: "DEVELOPER",
dataReference: {
connectOrCreate: {
where: {
key: SLACK_CONNECTION_KEY,
},
create: {
key: SLACK_CONNECTION_KEY,
provider: "DATABASE",
},
},
},
},
update: {},
});
await prisma.secretStore.upsert({
where: {
key: SLACK_CONNECTION_KEY,
},
create: {
key: SLACK_CONNECTION_KEY,
value: {
raw: {
ok: true,
team: { id: "T84AW8RBP", name: "Trigger.dev" },
scope:
"chat:write,channels:read,channels:manage,im:write,channels:join,chat:write.customize,bookmarks:read",
app_id: "A04H149K884",
enterprise: null,
token_type: "bot",
authed_user: { id: "U8590FPB9" },
bot_user_id: "U04H0UUQPHR",
access_token: process.env.SEED_CLOUD_SLACK_ACCESS_TOKEN,
is_enterprise_install: false,
},
type: "oauth2",
scopes: [
"chat:write",
"channels:read",
"channels:manage",
"im:write",
"channels:join",
"chat:write.customize",
"bookmarks:read",
],
accessToken: process.env.SEED_CLOUD_SLACK_ACCESS_TOKEN,
},
},
update: {},
});
}
const userGithubIntegration = await prisma.integration.upsert({
where: {
organizationId_slug: {
organizationId: organization.id,
slug: "github-user",
},
},
create: {
definition: {
connect: {
id: "github",
},
},
slug: "github-user",
title: "GitHub User",
scopes: ["admin:repo_hook", "public_repo"],
authSource: "HOSTED",
connectionType: "EXTERNAL",
authMethod: {
connect: {
definitionId_key: {
definitionId: "github",
key: "oauth2",
},
},
},
organization: {
connect: {
id: organization.id,
},
},
},
update: {},
});
const externalAccount1Identifier = `${name}1234`;
const externalAccount1 = await prisma.externalAccount.upsert({
where: {
environmentId_identifier: {
environmentId: devEnv.id,
identifier: externalAccount1Identifier,
},
},
create: {
organizationId: organization.id,
environmentId: devEnv.id,
identifier: externalAccount1Identifier,
metadata: { foo: "bar" },
},
update: {},
});
const externalAccount2Identifier = "other1234";
const externalAccount2 = await prisma.externalAccount.upsert({
where: {
environmentId_identifier: {
environmentId: devEnv.id,
identifier: externalAccount2Identifier,
},
},
create: {
organizationId: organization.id,
environmentId: devEnv.id,
identifier: externalAccount2Identifier,
metadata: { bar: "baz" },
},
update: {},
});
await prisma.integrationConnection.upsert({
where: {
id: "cli1qcroy0000b4dy084m2jsr",
},
create: {
id: "cli1qcroy0000b4dy084m2jsr",
externalAccount: {
connect: {
id: externalAccount1.id,
},
},
metadata: {},
integration: {
connect: {
id: userGithubIntegration.id,
},
},
organization: {
connect: {
id: organization.id,
},
},
connectionType: "EXTERNAL",
dataReference: {
connectOrCreate: {
where: {
key: `${externalAccount1Identifier}-github`,
},
create: {
key: `${externalAccount1Identifier}-github`,
provider: "DATABASE",
},
},
},
},
update: {},
});
await prisma.secretStore.upsert({
where: {
key: `${externalAccount1Identifier}-github`,
},
create: {
key: `${externalAccount1Identifier}-github`,
value: {
raw: {
scope: "admin:repo_hook,public_repo",
token_type: "bearer",
access_token: process.env.SEED_USER_GITHUB_ACCESS_TOKEN,
},
type: "oauth2",
scopes: ["admin:repo_hook,public_repo"],
accessToken: process.env.SEED_USER_GITHUB_ACCESS_TOKEN,
},
},
update: {},
});
}
+3 -3
View File
@@ -5,15 +5,15 @@
"main": "./src/index.ts",
"types": "./src/index.ts",
"dependencies": {
"@prisma/client": "latest",
"@prisma/client": "^4.16.0",
"typescript": "^4.8.4"
},
"devDependencies": {
"prisma": "latest"
"prisma": "^4.16.0"
},
"scripts": {
"db:generate": "prisma generate",
"db:migrate:dev": "prisma migrate dev",
"db:migrate:deploy": "prisma migrate deploy"
}
}
}
+2 -3
View File
@@ -4,9 +4,8 @@ datasource db {
}
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-1.1.x"]
previewFeatures = ["orderByNulls", "filteredRelationCount"]
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-1.1.x"]
}
model User {