-
Notifications
You must be signed in to change notification settings - Fork 18
feat: grad rollouts v2 #587
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00549e2
init
adityachoudhari26 454fac4
first take
adityachoudhari26 157cfcd
e2e tests
adityachoudhari26 5d5b9cb
approval releated tests
adityachoudhari26 801d36c
cleanup
adityachoudhari26 cd18d50
rabbit cleanup
adityachoudhari26 288b549
add uuid casting
adityachoudhari26 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
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
84 changes: 84 additions & 0 deletions
84
...deployment-versions/[deploymentVersionId]/environments/[environmentId]/rollout/openapi.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,84 @@ | ||
| import type { Swagger } from "atlassian-openapi"; | ||
|
|
||
| export const openapi: Swagger.SwaggerV3 = { | ||
| openapi: "3.0.0", | ||
| info: { title: "Ctrlplane API", version: "1.0.0" }, | ||
| paths: { | ||
| "/v1/deployment-versions/{deploymentVersionId}/environments/{environmentId}/rollout": | ||
| { | ||
| get: { | ||
| summary: | ||
| "Get the rollout information across all release targets for a given deployment version and environment", | ||
| operationId: "getRolloutInfo", | ||
| parameters: [ | ||
| { | ||
| name: "deploymentVersionId", | ||
| in: "path", | ||
| required: true, | ||
| schema: { type: "string", format: "uuid" }, | ||
| description: "The deployment version ID", | ||
| }, | ||
| { | ||
| name: "environmentId", | ||
| in: "path", | ||
| required: true, | ||
| schema: { type: "string", format: "uuid" }, | ||
| description: "The environment ID", | ||
| }, | ||
| ], | ||
| responses: { | ||
| 200: { | ||
| description: "The rollout information", | ||
| content: { | ||
| "application/json": { | ||
| schema: { | ||
| type: "array", | ||
| items: { | ||
| allOf: [ | ||
| { $ref: "#/components/schemas/ReleaseTarget" }, | ||
| { | ||
| type: "object", | ||
| properties: { | ||
| rolloutTime: { | ||
| type: "string", | ||
| format: "date-time", | ||
| nullable: true, | ||
| }, | ||
| rolloutPosition: { type: "number" }, | ||
| }, | ||
| required: ["rolloutTime", "rolloutPosition"], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| 404: { | ||
| description: | ||
| "The deployment version or environment was not found", | ||
| content: { | ||
| "application/json": { | ||
| schema: { | ||
| type: "object", | ||
| properties: { error: { type: "string" } }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| 500: { | ||
| description: "Internal server error", | ||
| content: { | ||
| "application/json": { | ||
| schema: { | ||
| type: "object", | ||
| properties: { error: { type: "string" } }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; |
143 changes: 143 additions & 0 deletions
143
...1/deployment-versions/[deploymentVersionId]/environments/[environmentId]/rollout/route.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,143 @@ | ||
| import type { Tx } from "@ctrlplane/db"; | ||
| import { NextResponse } from "next/server"; | ||
| import { INTERNAL_SERVER_ERROR, NOT_FOUND } from "http-status"; | ||
|
|
||
| import { and, eq, takeFirstOrNull } from "@ctrlplane/db"; | ||
| import * as schema from "@ctrlplane/db/schema"; | ||
| import { logger } from "@ctrlplane/logger"; | ||
| import { | ||
| getRolloutInfoForReleaseTarget, | ||
| mergePolicies, | ||
| } from "@ctrlplane/rule-engine"; | ||
| import { getApplicablePoliciesWithoutResourceScope } from "@ctrlplane/rule-engine/db"; | ||
| import { Permission } from "@ctrlplane/validators/auth"; | ||
|
|
||
| import { authn, authz } from "~/app/api/v1/auth"; | ||
| import { request } from "~/app/api/v1/middleware"; | ||
|
|
||
| const log = logger.child({ | ||
| route: | ||
| "/v1/deployment-versions/{deploymentVersionId}/environments/{environmentId}/rollout", | ||
| }); | ||
|
|
||
| const getDeploymentVersion = async (db: Tx, deploymentVersionId: string) => | ||
| db | ||
| .select() | ||
| .from(schema.deploymentVersion) | ||
| .where(eq(schema.deploymentVersion.id, deploymentVersionId)) | ||
| .then(takeFirstOrNull); | ||
|
|
||
| const getEnvironment = async (db: Tx, environmentId: string) => | ||
| db | ||
| .select() | ||
| .from(schema.environment) | ||
| .where(eq(schema.environment.id, environmentId)) | ||
| .then(takeFirstOrNull); | ||
|
|
||
| const getReleaseTargets = async ( | ||
| db: Tx, | ||
| deploymentId: string, | ||
| environmentId: string, | ||
| ) => | ||
| db | ||
| .select() | ||
| .from(schema.releaseTarget) | ||
| .innerJoin( | ||
| schema.deployment, | ||
| eq(schema.releaseTarget.deploymentId, schema.deployment.id), | ||
| ) | ||
| .innerJoin( | ||
| schema.environment, | ||
| eq(schema.releaseTarget.environmentId, schema.environment.id), | ||
| ) | ||
| .innerJoin( | ||
| schema.resource, | ||
| eq(schema.releaseTarget.resourceId, schema.resource.id), | ||
| ) | ||
| .where( | ||
| and( | ||
| eq(schema.releaseTarget.deploymentId, deploymentId), | ||
| eq(schema.releaseTarget.environmentId, environmentId), | ||
| ), | ||
| ) | ||
| .then((rows) => | ||
| rows.map((row) => ({ | ||
| ...row.release_target, | ||
| deployment: row.deployment, | ||
| environment: row.environment, | ||
| resource: row.resource, | ||
| })), | ||
| ); | ||
|
|
||
| export const GET = request() | ||
| .use(authn) | ||
| .use( | ||
| authz(({ can, params }) => | ||
| can.perform(Permission.DeploymentVersionGet).on({ | ||
| type: "deploymentVersion", | ||
| id: params.deploymentVersionId ?? "", | ||
| }), | ||
| ), | ||
| ) | ||
| .handle< | ||
| { db: Tx }, | ||
| { params: Promise<{ deploymentVersionId: string; environmentId: string }> } | ||
| >(async ({ db }, { params }) => { | ||
| try { | ||
| const { deploymentVersionId, environmentId } = await params; | ||
|
|
||
| const deploymentVersion = await getDeploymentVersion( | ||
| db, | ||
| deploymentVersionId, | ||
| ); | ||
| if (deploymentVersion == null) | ||
| return NextResponse.json( | ||
| { error: "Deployment version not found" }, | ||
| { status: NOT_FOUND }, | ||
| ); | ||
|
|
||
| const environment = await getEnvironment(db, environmentId); | ||
| if (environment == null) | ||
| return NextResponse.json( | ||
| { error: "Environment not found" }, | ||
| { status: NOT_FOUND }, | ||
| ); | ||
|
|
||
| const releaseTargets = await getReleaseTargets( | ||
| db, | ||
| deploymentVersion.deploymentId, | ||
| environmentId, | ||
| ); | ||
|
|
||
| const policies = await getApplicablePoliciesWithoutResourceScope( | ||
| db, | ||
| environmentId, | ||
| deploymentVersion.deploymentId, | ||
| ); | ||
| const policy = mergePolicies(policies); | ||
|
|
||
| const releaseTargetsWithRolloutInfo = await Promise.all( | ||
| releaseTargets.map((releaseTarget) => | ||
| getRolloutInfoForReleaseTarget( | ||
| db, | ||
| releaseTarget, | ||
| policy, | ||
| deploymentVersion, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| const releaseTargetsSortedByRolloutPosition = | ||
| releaseTargetsWithRolloutInfo.sort( | ||
| (a, b) => a.rolloutPosition - b.rolloutPosition, | ||
| ); | ||
|
|
||
| return NextResponse.json(releaseTargetsSortedByRolloutPosition); | ||
| } catch (error) { | ||
| log.error("Error getting rollout info", { error }); | ||
| return NextResponse.json( | ||
| { error: "Internal server error" }, | ||
| { status: INTERNAL_SERVER_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
Oops, something went wrong.
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.
🛠️ Refactor suggestion
Validate & coerce
approvedAtinto aDateupfrontz.string().datetime()merely validates the ISO-8601 string but you still need to turn it into aDate.Doing the coercion in the schema avoids repeated
new Date()logic and guarantees you never store an Invalid Date.The rest of the handler can then use
ctx.body.approvedAt ?? new Date()without additional parsing.📝 Committable suggestion
🤖 Prompt for AI Agents