-
-
Notifications
You must be signed in to change notification settings - Fork 918
feat(webapp): Add support for resetting idempotency keys #2777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mpcgrid
wants to merge
3
commits into
main
Choose a base branch
from
feat/tri-6733-reset-an-idempotencykey
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+343
−10
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
apps/webapp/app/routes/api.v1.idempotencyKeys.$key.reset.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { json } from "@remix-run/server-runtime"; | ||
| import { z } from "zod"; | ||
| import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server"; | ||
| import { ResetIdempotencyKeyService } from "~/v3/services/resetIdempotencyKey.server"; | ||
|
|
||
| const ParamsSchema = z.object({ | ||
| key: z.string(), | ||
| }); | ||
|
|
||
| const BodySchema = z.object({ | ||
| taskIdentifier: z.string().min(1, "Task identifier is required"), | ||
| }); | ||
|
|
||
| export const { action } = createActionApiRoute( | ||
| { | ||
| params: ParamsSchema, | ||
| body: BodySchema, | ||
| allowJWT: true, | ||
| corsStrategy: "all", | ||
| authorization: { | ||
| action: "write", | ||
| resource: () => ({}), | ||
| superScopes: ["write:runs", "admin"], | ||
| }, | ||
| }, | ||
| async ({ params, body, authentication }) => { | ||
| const service = new ResetIdempotencyKeyService(); | ||
|
|
||
| try { | ||
| const result = await service.call(params.key, body.taskIdentifier, authentication.environment); | ||
| return json(result, { status: 200 }); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| return json({ error: error.message }, { status: 404 }); | ||
| } | ||
| return json({ error: "Internal Server Error" }, { status: 500 }); | ||
| } | ||
| } | ||
| ); | ||
133 changes: 133 additions & 0 deletions
133
...nizationSlug.projects.$projectParam.env.$envParam.runs.$runParam.idempotencyKey.reset.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { parse } from "@conform-to/zod"; | ||
| import { type ActionFunction, json } from "@remix-run/node"; | ||
| import { z } from "zod"; | ||
| import { prisma } from "~/db.server"; | ||
| import { jsonWithErrorMessage, jsonWithSuccessMessage } from "~/models/message.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
| import { requireUserId } from "~/services/session.server"; | ||
| import { ResetIdempotencyKeyService } from "~/v3/services/resetIdempotencyKey.server"; | ||
| import { v3RunParamsSchema } from "~/utils/pathBuilder"; | ||
| import { authenticateApiRequest } from "~/services/apiAuth.server"; | ||
| import { environment } from "effect/Differ"; | ||
mpcgrid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export const resetIdempotencyKeySchema = z.object({ | ||
| taskIdentifier: z.string().min(1, "Task identifier is required"), | ||
| }); | ||
|
|
||
| export const action: ActionFunction = async ({ request, params }) => { | ||
| const userId = await requireUserId(request); | ||
| const { projectParam, organizationSlug, envParam, runParam } = | ||
| v3RunParamsSchema.parse(params); | ||
|
|
||
| const formData = await request.formData(); | ||
| const submission = parse(formData, { schema: resetIdempotencyKeySchema }); | ||
|
|
||
| if (!submission.value) { | ||
| return json(submission); | ||
| } | ||
|
|
||
| try { | ||
| const { taskIdentifier } = submission.value; | ||
|
|
||
| const taskRun = await prisma.taskRun.findFirst({ | ||
| where: { | ||
| friendlyId: runParam, | ||
| project: { | ||
| slug: projectParam, | ||
| organization: { | ||
| slug: organizationSlug, | ||
| members: { | ||
| some: { | ||
| userId, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| runtimeEnvironment: { | ||
| slug: envParam, | ||
| }, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| idempotencyKey: true, | ||
| taskIdentifier: true, | ||
| runtimeEnvironmentId: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (!taskRun) { | ||
| submission.error = { runParam: ["Run not found"] }; | ||
| return json(submission); | ||
| } | ||
|
|
||
| if (!taskRun.idempotencyKey) { | ||
| return jsonWithErrorMessage( | ||
| submission, | ||
| request, | ||
| "This run does not have an idempotency key" | ||
| ); | ||
| } | ||
|
|
||
| if (taskRun.taskIdentifier !== taskIdentifier) { | ||
| submission.error = { taskIdentifier: ["Task identifier does not match this run"] }; | ||
| return json(submission); | ||
| } | ||
|
|
||
| const environment = await prisma.runtimeEnvironment.findUnique({ | ||
| where: { | ||
| id: taskRun.runtimeEnvironmentId, | ||
| }, | ||
| include: { | ||
| project: { | ||
| include: { | ||
| organization: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!environment) { | ||
| return jsonWithErrorMessage( | ||
| submission, | ||
| request, | ||
| "Environment not found" | ||
| ); | ||
| } | ||
|
|
||
| const service = new ResetIdempotencyKeyService(); | ||
|
|
||
| await service.call(taskRun.idempotencyKey, taskIdentifier, { | ||
| ...environment, | ||
| organizationId: environment.project.organizationId, | ||
| organization: environment.project.organization, | ||
| }); | ||
|
|
||
| return jsonWithSuccessMessage( | ||
| { success: true }, | ||
| request, | ||
| "Idempotency key reset successfully" | ||
| ); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| logger.error("Failed to reset idempotency key", { | ||
| error: { | ||
| name: error.name, | ||
| message: error.message, | ||
| stack: error.stack, | ||
| }, | ||
| }); | ||
| return jsonWithErrorMessage( | ||
| submission, | ||
| request, | ||
| `Failed to reset idempotency key: ${error.message}` | ||
| ); | ||
| } else { | ||
| logger.error("Failed to reset idempotency key", { error }); | ||
| return jsonWithErrorMessage( | ||
| submission, | ||
| request, | ||
| `Failed to reset idempotency key: ${JSON.stringify(error)}` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { AuthenticatedEnvironment } from "~/services/apiAuth.server"; | ||
| import { BaseService, ServiceValidationError } from "./baseService.server"; | ||
|
|
||
| export class ResetIdempotencyKeyService extends BaseService { | ||
| public async call( | ||
| idempotencyKey: string, | ||
| taskIdentifier: string, | ||
| authenticatedEnv: AuthenticatedEnvironment | ||
| ): Promise<{ id: string }> { | ||
| // Find all runs with this idempotency key and task identifier in the authenticated environment | ||
| const runs = await this._prisma.taskRun.findMany({ | ||
| where: { | ||
| idempotencyKey, | ||
| taskIdentifier, | ||
| runtimeEnvironmentId: authenticatedEnv.id, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (runs.length === 0) { | ||
| throw new ServiceValidationError( | ||
| `No runs found with idempotency key: ${idempotencyKey} and task: ${taskIdentifier}`, | ||
| 404 | ||
| ); | ||
| } | ||
|
|
||
| // Update all runs to clear the idempotency key | ||
| await this._prisma.taskRun.updateMany({ | ||
| where: { | ||
| idempotencyKey, | ||
| taskIdentifier, | ||
| runtimeEnvironmentId: authenticatedEnv.id, | ||
| }, | ||
| data: { | ||
| idempotencyKey: null, | ||
| idempotencyKeyExpiresAt: null, | ||
| }, | ||
| }); | ||
|
|
||
| return { id: idempotencyKey }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tighten error handling: don’t turn all errors into 404s or leak messages
The happy path is good, but the current
try/catch:has a few issues:
error.messageis returned to clients, potentially leaking internal details.ServiceValidationError.A safer pattern is to only treat
ServiceValidationErroras a 4xx and default everything else to 500 with a generic message. For example:(or, if
createActionApiRoutealready handlesServiceValidationErrorglobally, you can simply drop thetry/catchand let it bubble).This keeps client semantics accurate and avoids over‑exposing internal error messages.
🤖 Prompt for AI Agents