|
| 1 | +import { type ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { |
| 3 | + type GenerateRegistryCredentialsResponseBody, |
| 4 | + ProgressDeploymentRequestBody, |
| 5 | + tryCatch, |
| 6 | +} from "@trigger.dev/core/v3"; |
| 7 | +import { z } from "zod"; |
| 8 | +import { authenticateRequest } from "~/services/apiAuth.server"; |
| 9 | +import { logger } from "~/services/logger.server"; |
| 10 | +import { DeploymentService } from "~/v3/services/deployment.server"; |
| 11 | + |
| 12 | +const ParamsSchema = z.object({ |
| 13 | + deploymentId: z.string(), |
| 14 | +}); |
| 15 | + |
| 16 | +export async function action({ request, params }: ActionFunctionArgs) { |
| 17 | + if (request.method.toUpperCase() !== "POST") { |
| 18 | + return json({ error: "Method Not Allowed" }, { status: 405 }); |
| 19 | + } |
| 20 | + |
| 21 | + const parsedParams = ParamsSchema.safeParse(params); |
| 22 | + |
| 23 | + if (!parsedParams.success) { |
| 24 | + return json({ error: "Invalid params" }, { status: 400 }); |
| 25 | + } |
| 26 | + |
| 27 | + const authenticationResult = await authenticateRequest(request, { |
| 28 | + apiKey: true, |
| 29 | + organizationAccessToken: false, |
| 30 | + personalAccessToken: false, |
| 31 | + }); |
| 32 | + |
| 33 | + if (!authenticationResult || !authenticationResult.result.ok) { |
| 34 | + logger.info("Invalid or missing api key", { url: request.url }); |
| 35 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 36 | + } |
| 37 | + |
| 38 | + const { environment: authenticatedEnv } = authenticationResult.result; |
| 39 | + const { deploymentId } = parsedParams.data; |
| 40 | + |
| 41 | + const [, rawBody] = await tryCatch(request.json()); |
| 42 | + const body = ProgressDeploymentRequestBody.safeParse(rawBody ?? {}); |
| 43 | + |
| 44 | + if (!body.success) { |
| 45 | + return json({ error: "Invalid request body", issues: body.error.issues }, { status: 400 }); |
| 46 | + } |
| 47 | + |
| 48 | + const deploymentService = new DeploymentService(); |
| 49 | + |
| 50 | + return await deploymentService.generateRegistryCredentials(authenticatedEnv, deploymentId).match( |
| 51 | + (result) => { |
| 52 | + return json( |
| 53 | + { |
| 54 | + username: result.username, |
| 55 | + password: result.password, |
| 56 | + expiresAt: result.expiresAt.toISOString(), |
| 57 | + repositoryUri: result.repositoryUri, |
| 58 | + } satisfies GenerateRegistryCredentialsResponseBody, |
| 59 | + { status: 200 } |
| 60 | + ); |
| 61 | + }, |
| 62 | + (error) => { |
| 63 | + switch (error.type) { |
| 64 | + case "deployment_not_found": |
| 65 | + return json({ error: "Deployment not found" }, { status: 404 }); |
| 66 | + case "deployment_has_no_image_reference": |
| 67 | + logger.error( |
| 68 | + "Failed to generate registry credentials: deployment_has_no_image_reference", |
| 69 | + { deploymentId } |
| 70 | + ); |
| 71 | + return json({ error: "Deployment has no image reference" }, { status: 409 }); |
| 72 | + case "deployment_is_already_final": |
| 73 | + return json( |
| 74 | + { error: "Failed to generate registry credentials: deployment_is_already_final" }, |
| 75 | + { status: 409 } |
| 76 | + ); |
| 77 | + case "missing_registry_credentials": |
| 78 | + logger.error("Failed to generate registry credentials: missing_registry_credentials", { |
| 79 | + deploymentId, |
| 80 | + }); |
| 81 | + return json({ error: "Missing registry credentials" }, { status: 409 }); |
| 82 | + case "registry_not_supported": |
| 83 | + logger.error("Failed to generate registry credentials: registry_not_supported", { |
| 84 | + deploymentId, |
| 85 | + }); |
| 86 | + return json({ error: "Registry not supported" }, { status: 409 }); |
| 87 | + case "registry_region_not_supported": |
| 88 | + logger.error("Failed to generate registry credentials: registry_region_not_supported", { |
| 89 | + deploymentId, |
| 90 | + }); |
| 91 | + return json({ error: "Registry region not supported" }, { status: 409 }); |
| 92 | + case "other": |
| 93 | + default: |
| 94 | + error.type satisfies "other"; |
| 95 | + logger.error("Failed to generate registry credentials", { error: error.cause }); |
| 96 | + return json({ error: "Internal server error" }, { status: 500 }); |
| 97 | + } |
| 98 | + } |
| 99 | + ); |
| 100 | +} |
0 commit comments