|
| 1 | +import { type ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { StartDeploymentRequestBody } from "@trigger.dev/core/v3"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { authenticateRequest } from "~/services/apiAuth.server"; |
| 5 | +import { logger } from "~/services/logger.server"; |
| 6 | +import { DeploymentService } from "~/v3/services/deployment.server"; |
| 7 | + |
| 8 | +const ParamsSchema = z.object({ |
| 9 | + deploymentId: z.string(), |
| 10 | +}); |
| 11 | + |
| 12 | +export async function action({ request, params }: ActionFunctionArgs) { |
| 13 | + if (request.method.toUpperCase() !== "POST") { |
| 14 | + return json({ error: "Method Not Allowed" }, { status: 405 }); |
| 15 | + } |
| 16 | + |
| 17 | + const parsedParams = ParamsSchema.safeParse(params); |
| 18 | + |
| 19 | + if (!parsedParams.success) { |
| 20 | + return json({ error: "Invalid params" }, { status: 400 }); |
| 21 | + } |
| 22 | + |
| 23 | + const authenticationResult = await authenticateRequest(request, { |
| 24 | + apiKey: true, |
| 25 | + organizationAccessToken: false, |
| 26 | + personalAccessToken: false, |
| 27 | + }); |
| 28 | + |
| 29 | + if (!authenticationResult || !authenticationResult.result.ok) { |
| 30 | + logger.info("Invalid or missing api key", { url: request.url }); |
| 31 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 32 | + } |
| 33 | + |
| 34 | + const { environment: authenticatedEnv } = authenticationResult.result; |
| 35 | + const { deploymentId } = parsedParams.data; |
| 36 | + |
| 37 | + const rawBody = await request.json(); |
| 38 | + const body = StartDeploymentRequestBody.safeParse(rawBody); |
| 39 | + |
| 40 | + if (!body.success) { |
| 41 | + return json({ error: "Invalid request body", issues: body.error.issues }, { status: 400 }); |
| 42 | + } |
| 43 | + |
| 44 | + const deploymentService = new DeploymentService(); |
| 45 | + |
| 46 | + const result = await deploymentService.startDeployment(authenticatedEnv, deploymentId, body.data); |
| 47 | + return result.match( |
| 48 | + () => { |
| 49 | + return json(null, { status: 204 }); |
| 50 | + }, |
| 51 | + (error) => { |
| 52 | + switch (error.type) { |
| 53 | + case "deployment_not_found": |
| 54 | + return json({ error: "Deployment not found" }, { status: 404 }); |
| 55 | + case "deployment_not_pending": |
| 56 | + return json({ error: "Deployment is not pending" }, { status: 409 }); |
| 57 | + case "other": |
| 58 | + default: |
| 59 | + error.type satisfies "other"; |
| 60 | + return json({ error: "Internal server error" }, { status: 500 }); |
| 61 | + } |
| 62 | + } |
| 63 | + ); |
| 64 | +} |
0 commit comments