Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Oct 24, 2024
1 parent 15083dc commit f99730c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ const releaseForm = z.object({
systemId: z.string().uuid(),
deploymentId: z.string().uuid(),
version: z.string().min(1).max(255),
releaseDependencies: z.array(releaseDependency),
releaseDependencies: z.array(releaseDependency).refine((deps) => {
const deploymentIds = deps.map((d) => d.deploymentId);
return new Set(deploymentIds).size === deploymentIds.length;
}, "Cannot reuse a deployment in multiple release dependencies"),
});

export const CreateReleaseDialog: React.FC<{
Expand Down Expand Up @@ -129,10 +132,7 @@ export const CreateReleaseDialog: React.FC<{
numOfReleaseJobTriggers === 0
? `No targets to deploy release too.`
: `Dispatching ${release.releaseJobTriggers.length} job configuration${release.releaseJobTriggers.length > 1 ? "s" : ""}.`,
{
dismissible: true,
duration: 2_000,
},
{ dismissible: true, duration: 2_000 },
);

props.onClose?.();
Expand All @@ -144,6 +144,8 @@ export const CreateReleaseDialog: React.FC<{
name: "releaseDependencies",
});

const formErrors = form.formState.errors.releaseDependencies ?? null;

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>{children}</DialogTrigger>
Expand Down Expand Up @@ -239,31 +241,36 @@ export const CreateReleaseDialog: React.FC<{
</span>

{fields.map((_, index) => (
<div className="flex items-center gap-2">
<div key={index} className="flex items-center gap-2">
<FormField
control={form.control}
name={`releaseDependencies.${index}.deploymentId`}
render={({ field: { value, onChange } }) => (
<FormItem>
<Select value={value} onValueChange={onChange}>
<SelectTrigger className="w-32 text-sm">
<SelectValue placeholder="Deployment" key={value} />
</SelectTrigger>
<SelectContent>
<SelectGroup>
{globalDeployments.data
?.filter((d) => d.id !== deploymentId)
.map((deployment) => (
<SelectItem
key={deployment.id}
value={deployment.id}
>
{deployment.name}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
<FormControl>
<Select value={value} onValueChange={onChange}>
<SelectTrigger className="w-32 text-sm">
<SelectValue
placeholder="Deployment"
key={value}
/>
</SelectTrigger>
<SelectContent>
<SelectGroup>
{globalDeployments.data
?.filter((d) => d.id !== deploymentId)
.map((deployment) => (
<SelectItem
key={deployment.id}
value={deployment.id}
>
{deployment.name}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
</FormControl>
</FormItem>
)}
/>
Expand All @@ -273,19 +280,21 @@ export const CreateReleaseDialog: React.FC<{
name={`releaseDependencies.${index}.releaseFilter`}
render={({ field: { value, onChange } }) => (
<FormItem>
<ReleaseConditionDialog
condition={value}
onChange={onChange}
>
<Button variant="ghost" size="icon">
{isEmptyCondition(value) && (
<IconFilterExclamation className="h-4 w-4" />
)}
{!isEmptyCondition(value) && (
<IconFilterFilled className="h-4 w-4" />
)}
</Button>
</ReleaseConditionDialog>
<FormControl>
<ReleaseConditionDialog
condition={value}
onChange={onChange}
>
<Button variant="ghost" size="icon">
{isEmptyCondition(value) && (
<IconFilterExclamation className="h-4 w-4" />
)}
{!isEmptyCondition(value) && (
<IconFilterFilled className="h-4 w-4" />
)}
</Button>
</ReleaseConditionDialog>
</FormControl>
</FormItem>
)}
/>
Expand Down Expand Up @@ -319,6 +328,12 @@ export const CreateReleaseDialog: React.FC<{
</Button>
</div>

{formErrors?.root?.message && (
<div className="text-sm text-red-500">
{formErrors.root.message}
</div>
)}

<DialogFooter>
<Button type="submit">Create</Button>
</DialogFooter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export const TargetDiagramDependencies: React.FC<{
relationships: Array<schema.TargetRelationship>;
targets: Array<schema.Target>;
releaseDependencies: (schema.ReleaseDependency & {
deploymentName: string;
target?: string;
})[];
}> = ({ targetId, relationships, targets, releaseDependencies }) => {
Expand Down Expand Up @@ -385,12 +386,12 @@ export const TargetDiagramDependencies: React.FC<{
}}
>
<SelectTrigger>
<SelectValue placeholder="Select a release" />
<SelectValue placeholder="Select a dependency" />
</SelectTrigger>
<SelectContent>
{releaseDependencies.map((rd) => (
<SelectItem key={rd.id} value={rd.id}>
{rd.deploymentId}
{rd.deploymentName}
</SelectItem>
))}
</SelectContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,13 @@ const TargetDiagram: React.FC<{
})),
);
const [edges, __, onEdgesChange] = useEdgesState(
relationships.map((t) => {
return {
id: `${t.sourceId}-${t.targetId}`,
source: t.sourceId,
target: t.targetId,
markerEnd: { type: MarkerType.Arrow, color: colors.neutral[700] },
style: { stroke: colors.neutral[700] },
};
}),
relationships.map((t) => ({
id: `${t.sourceId}-${t.targetId}`,
source: t.sourceId,
target: t.targetId,
markerEnd: { type: MarkerType.Arrow, color: colors.neutral[700] },
style: { stroke: colors.neutral[700] },
})),
);
const onLayout = useOnLayout();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const bodySchema = z.array(
)
.optional()
.refine(
(vars) => new Set(vars?.map((v) => v.key)).size === vars?.length,
(vars) => vars?.length ?? 0 === new Set(vars?.map((v) => v.key)).size,
"Duplicate variable keys are not allowed",
),
}),
Expand Down
24 changes: 23 additions & 1 deletion packages/api/src/router/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const processReleaseJobTriggerWithAdditionalDataRows = (
environment_policy_release_window: EnvironmentPolicyReleaseWindow | null;
user?: User | null;
release_dependency?: ReleaseDependency | null;
deployment_name?: { deploymentName: string; deploymentId: string } | null;
}>,
) =>
_.chain(rows)
Expand All @@ -99,7 +100,16 @@ const processReleaseJobTriggerWithAdditionalDataRows = (
target: v[0]!.target,
release: { ...v[0]!.release, deployment: v[0]!.deployment },
environment: v[0]!.environment,
releaseDependencies: v.map((r) => r.release_dependency).filter(isPresent),
releaseDependencies: v
.map((r) =>
r.release_dependency != null
? {
...r.release_dependency,
deploymentName: r.deployment_name!.deploymentName,
}
: null,
)
.filter(isPresent),
rolloutDate:
v[0]!.environment_policy != null
? rolloutDateFromReleaseJobTrigger(
Expand Down Expand Up @@ -294,6 +304,14 @@ const releaseJobTriggerRouter = createTRPCRouter({
canUser.perform(Permission.JobGet).on({ type: "job", id: input }),
})
.query(async ({ ctx, input }) => {
const deploymentName = ctx.db
.select({
deploymentName: deployment.name,
deploymentId: deployment.id,
})
.from(deployment)
.as("deployment_name");

const data = await releaseJobTriggerQuery(ctx.db)
.leftJoin(user, eq(releaseJobTrigger.causedById, user.id))
.leftJoin(jobMetadata, eq(jobMetadata.jobId, job.id))
Expand All @@ -309,6 +327,10 @@ const releaseJobTriggerRouter = createTRPCRouter({
releaseDependency,
eq(releaseDependency.releaseId, release.id),
)
.leftJoin(
deploymentName,
eq(deploymentName.deploymentId, releaseDependency.deploymentId),
)
.where(eq(job.id, input))
.then(processReleaseJobTriggerWithAdditionalDataRows)
.then(takeFirst);
Expand Down
1 change: 1 addition & 0 deletions packages/job-dispatch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from "./policies/gradual-rollout.js";
export * from "./policies/manual-approval.js";
export * from "./policies/release-sequencing.js";
export * from "./policies/success-rate-criteria-passing.js";
export * from "./policies/release-dependency.js";
export * from "./policies/release-string-check.js";
export * from "./policies/concurrency-policy.js";
export * from "./policies/release-window.js";
7 changes: 6 additions & 1 deletion packages/job-dispatch/src/new-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Tx } from "@ctrlplane/db";
import { dispatchReleaseJobTriggers } from "./job-dispatch.js";
import { isPassingLockingPolicy } from "./lock-checker.js";
import { isPassingApprovalPolicy } from "./policies/manual-approval.js";
import { isPassingReleaseDependencyPolicy } from "./policies/release-dependency.js";
import { createReleaseJobTriggers } from "./release-job-trigger.js";

/**
Expand All @@ -20,7 +21,11 @@ export async function dispatchJobsForNewTargets(
if (releaseJobTriggers.length === 0) return;

await dispatchReleaseJobTriggers(db)
.filter(isPassingLockingPolicy, isPassingApprovalPolicy)
.filter(
isPassingLockingPolicy,
isPassingApprovalPolicy,
isPassingReleaseDependencyPolicy,
)
.releaseTriggers(releaseJobTriggers)
.dispatch();
}

0 comments on commit f99730c

Please sign in to comment.