Testing is working

This commit is contained in:
Matt Aitken
2022-12-29 10:39:24 +00:00
parent c2801cf8d6
commit 9de6ec1d54
6 changed files with 61 additions and 51 deletions
@@ -30,8 +30,26 @@ export const { commitSession, getSession } = createCookieSessionStorage({
},
});
export async function getRuntimeEnvironmentFromSession(
session: Session
export async function getRuntimeEnvironmentFromRequest(
request: Request
): Promise<string> {
return session.get("environment") ?? "development";
const environmentSession = await getSession(request.headers.get("cookie"));
return environmentSession.get("environment") ?? "development";
}
export async function getRuntimeEnvironment({
organizationId,
slug,
}: {
organizationId: string;
slug: string;
}) {
return prisma.runtimeEnvironment.findUnique({
where: {
organizationId_slug: {
organizationId,
slug,
},
},
});
}
@@ -12,7 +12,7 @@ import {
import { getConnectedApiConnectionsForOrganizationSlug } from "~/models/apiConnection.server";
import {
commitSession,
getRuntimeEnvironmentFromSession,
getRuntimeEnvironmentFromRequest,
getSession,
} from "~/models/runtimeEnvironment.server";
import { getWorkflowFromSlugs } from "~/models/workflow.server";
@@ -39,9 +39,8 @@ export const loader = async ({ request, params }: LoaderArgs) => {
trigger: TriggerMetadataSchema.parse(r.trigger),
}));
const environmentSession = await getSession(request.headers.get("cookie"));
const currentEnvironmentSlug = await getRuntimeEnvironmentFromSession(
environmentSession
const currentEnvironmentSlug = await getRuntimeEnvironmentFromRequest(
request
);
const allConnections = await getConnectedApiConnectionsForOrganizationSlug({
@@ -58,14 +57,11 @@ export const loader = async ({ request, params }: LoaderArgs) => {
integration: integrations.find((i) => i.key === c.service),
}));
return typedjson(
{
workflow: { ...workflow, rules },
currentEnvironmentSlug,
connectionSlots,
},
{ headers: { "Set-Cookie": await commitSession(environmentSession) } }
);
return typedjson({
workflow: { ...workflow, rules },
currentEnvironmentSlug,
connectionSlots,
});
};
export default function Page() {
@@ -1,4 +1,4 @@
import { Form, useFetcher } from "@remix-run/react";
import { useFetcher } from "@remix-run/react";
import { useCallback, useState } from "react";
import invariant from "tiny-invariant";
import { JSONEditor } from "~/components/code/JSONEditor";
@@ -15,7 +15,6 @@ import { useConnectionSlots } from "~/hooks/useConnectionSlots";
import { useCurrentEnvironment } from "~/hooks/useEnvironments";
import { useCurrentOrganization } from "~/hooks/useOrganizations";
import { useCurrentWorkflow } from "~/hooks/useWorkflows";
import { RuntimeEnvironment } from "~/models/runtimeEnvironment.server";
export default function Page() {
const organization = useCurrentOrganization();
@@ -27,14 +26,10 @@ export default function Page() {
const environment = useCurrentEnvironment();
invariant(environment, "Environment not found");
const testFetcher = useFetcher();
const eventRule = workflow.rules.find(
(r) => r.environmentId === environment.id
);
const [testContent, setTestContent] = useState<string>("");
return (
<>
<Header1 className="mb-4">Overview</Header1>
@@ -77,7 +72,6 @@ export default function Page() {
<Tester
organizationSlug={organization.slug}
workflowSlug={workflow.slug}
environment={environment}
/>
</Panel>
)}
@@ -88,26 +82,20 @@ export default function Page() {
function Tester({
organizationSlug,
workflowSlug,
environment,
}: {
organizationSlug: string;
workflowSlug: string;
environment: RuntimeEnvironment;
}) {
const testFetcher = useFetcher();
const [testContent, setTestContent] = useState<string>("");
const submit = useCallback(() => {
console.log({
environmentId: environment.id,
apiKey: environment.apiKey,
data: testContent,
payload: testContent,
});
testFetcher.submit(
{
environmentId: environment.id,
apiKey: environment.apiKey,
payload: testContent,
},
{
@@ -115,14 +103,7 @@ function Tester({
action: `/resources/run/${organizationSlug}/test/${workflowSlug}`,
}
);
}, [
environment.apiKey,
environment.id,
organizationSlug,
testContent,
testFetcher,
workflowSlug,
]);
}, [organizationSlug, testContent, testFetcher, workflowSlug]);
return (
<div className="flex flex-col gap-2">
@@ -1,17 +1,18 @@
import type { ActionArgs } from "@remix-run/server-runtime";
import { TriggerMetadataSchema } from "@trigger.dev/common-schemas";
import invariant from "tiny-invariant";
import { ulid } from "ulid";
import { z } from "zod";
import { getOrganizationFromSlug } from "~/models/organization.server";
import {
getRuntimeEnvironment,
getRuntimeEnvironmentFromRequest,
} from "~/models/runtimeEnvironment.server";
import { getWorkflowFromSlugs } from "~/models/workflow.server";
import { IngestEvent } from "~/services/events/ingest.server";
import { requireUserId } from "~/services/session.server";
const requestSchema = z.object({
environmentId: z.string(),
apiKey: z.string(),
payload: z.any(),
payload: z.string(),
});
export const action = async ({ request, params }: ActionArgs) => {
@@ -31,7 +32,9 @@ export const action = async ({ request, params }: ActionArgs) => {
try {
const formData = await request.formData();
const body = Object.fromEntries(formData.entries());
const { payload, apiKey, environmentId } = requestSchema.parse(body);
const { payload } = requestSchema.parse(body);
const jsonPayload = JSON.parse(payload);
const workflow = await getWorkflowFromSlugs({
userId,
@@ -46,25 +49,31 @@ export const action = async ({ request, params }: ActionArgs) => {
});
invariant(organization, "organization is required");
const activeEventRule = workflow.rules.find(
(r) => r.environmentId === environmentId
);
const eventRule = TriggerMetadataSchema.parse(activeEventRule);
const environmentSlug = await getRuntimeEnvironmentFromRequest(request);
const environment = await getRuntimeEnvironment({
organizationId: organization.id,
slug: environmentSlug,
});
invariant(environment, "environment is required");
//todo choose event name from form dropdown
const ingestService = new IngestEvent();
await ingestService.call(
{
id: ulid(),
name: eventRule.name,
name: workflow.eventNames[0],
type: workflow.type,
service: eventRule.service,
payload: payload,
service: workflow.service,
payload: jsonPayload,
context: {},
apiKey: apiKey,
apiKey: environment.apiKey,
isTest: true,
},
organization
);
//todo redirect to the runs page
return null;
} catch (error: any) {
console.error(error);
throw new Response(error.message, { status: 400 });
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "Workflow" ADD COLUMN "eventNames" TEXT[],
ADD COLUMN "service" TEXT NOT NULL DEFAULT 'trigger';
+3
View File
@@ -120,6 +120,9 @@ model Workflow {
runs WorkflowRun[]
rules EventRule[]
service String @default("trigger")
eventNames String[]
@@unique([organizationId, slug])
}