Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,38 @@ import {
ComparisonOperator,
FilterType,
} from "@ctrlplane/validators/conditions";
import { JobStatus } from "@ctrlplane/validators/jobs";

import { useReleaseChannelDrawer } from "~/app/[workspaceSlug]/(app)/_components/release-channel-drawer/useReleaseChannelDrawer";
import { api } from "~/trpc/react";
import { DeployButton } from "./DeployButton";
import { Release } from "./TableCells";

type Environment = RouterOutputs["environment"]["bySystemId"][number];
type BlockedEnv = RouterOutputs["release"]["blocked"][number];

type ReleaseEnvironmentCellProps = {
environment: Environment;
deployment: SCHEMA.Deployment;
release: { id: string; version: string; createdAt: Date };
blockedEnv?: BlockedEnv;
};

const ReleaseEnvironmentCell: React.FC<ReleaseEnvironmentCellProps> = ({
environment,
deployment,
release,
blockedEnv,
}) => {
const { workspaceSlug, systemSlug } = useParams<{
workspaceSlug: string;
systemSlug: string;
}>();

const { data: blockedEnvsResult, isLoading: isBlockedEnvsLoading } =
api.release.blocked.useQuery([release.id]);

const blockedEnv = blockedEnvsResult?.find(
(b) => b.environmentId === environment.id,
);

const { data: workspace, isLoading: isWorkspaceLoading } =
api.workspace.bySlug.useQuery(workspaceSlug);
const workspaceId = workspace?.id ?? "";
Expand Down Expand Up @@ -69,7 +74,10 @@ const ReleaseEnvironmentCell: React.FC<ReleaseEnvironmentCellProps> = ({
const { setReleaseChannelId } = useReleaseChannelDrawer();

const isLoading =
isWorkspaceLoading || isStatusesLoading || isResourcesLoading;
isWorkspaceLoading ||
isStatusesLoading ||
isResourcesLoading ||
isBlockedEnvsLoading;
if (isLoading)
return <p className="text-xs text-muted-foreground">Loading...</p>;

Expand All @@ -79,58 +87,72 @@ const ReleaseEnvironmentCell: React.FC<ReleaseEnvironmentCellProps> = ({
const hasJobAgent = deployment.jobAgentId != null;
const isBlockedByReleaseChannel = blockedEnv != null;

const showRelease = isAlreadyDeployed;
const showBlockedByReleaseChannel =
isBlockedByReleaseChannel &&
!statuses?.some((s) => s.job.status === JobStatus.InProgress);

const showRelease = isAlreadyDeployed && !showBlockedByReleaseChannel;
const showDeployButton =
!isAlreadyDeployed &&
hasJobAgent &&
hasResources &&
!isBlockedByReleaseChannel;

if (showRelease)
return (
<Release
workspaceSlug={workspaceSlug}
systemSlug={systemSlug}
deploymentSlug={deployment.slug}
releaseId={release.id}
version={release.version}
environment={environment}
name={release.version}
deployedAt={release.createdAt}
statuses={statuses.map((s) => s.job.status)}
/>
);

if (showDeployButton)
return (
<DeployButton releaseId={release.id} environmentId={environment.id} />
);

if (showBlockedByReleaseChannel)
return (
<div className="text-center text-xs text-muted-foreground/70">
Blocked by{" "}
<Button
variant="link"
size="sm"
onClick={() =>
setReleaseChannelId(blockedEnv.releaseChannelId ?? null)
}
className="px-0 text-muted-foreground/70"
>
release channel
</Button>
</div>
);

if (!hasJobAgent)
return (
<div className="text-center text-xs text-muted-foreground/70">
No job agent
</div>
);

if (!hasResources)
return (
<div className="text-center text-xs text-muted-foreground/70">
No resources
</div>
);

return (
<>
{showRelease && (
<Release
workspaceSlug={workspaceSlug}
systemSlug={systemSlug}
deploymentSlug={deployment.slug}
releaseId={release.id}
version={release.version}
environment={environment}
name={release.version}
deployedAt={release.createdAt}
statuses={statuses.map((s) => s.job.status)}
/>
)}

{showDeployButton && (
<DeployButton releaseId={release.id} environmentId={environment.id} />
)}

{!isAlreadyDeployed && (
<div className="text-center text-xs text-muted-foreground/70">
{isBlockedByReleaseChannel && (
<>
Blocked by{" "}
<Button
variant="link"
size="sm"
onClick={() =>
setReleaseChannelId(blockedEnv.releaseChannelId ?? null)
}
className="px-0 text-muted-foreground/70"
>
release channel
</Button>
</>
)}
{!isBlockedByReleaseChannel && !hasJobAgent && "No job agent"}
{!isBlockedByReleaseChannel &&
hasJobAgent &&
!hasResources &&
"No resources"}
</div>
)}
</>
<div className="text-center text-xs text-muted-foreground/70">
Release not deployed
</div>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ export const DeploymentPageContent: React.FC<DeploymentPageContentProps> = ({
);

const releaseIds = releases.data?.items.map((r) => r.id) ?? [];
const blockedEnvByRelease = api.release.blocked.useQuery(releaseIds, {
enabled: releaseIds.length > 0,
});

const loading = releases.isLoading;
const router = useRouter();
Expand Down Expand Up @@ -303,9 +300,6 @@ export const DeploymentPageContent: React.FC<DeploymentPageContentProps> = ({
environment={env}
deployment={deployment}
release={release}
blockedEnv={blockedEnvByRelease.data?.find(
(b) => b.environmentId === env.id,
)}
/>
</TableCell>
))}
Expand Down
30 changes: 30 additions & 0 deletions packages/api/src/router/environment-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import {
cancelOldReleaseJobTriggersOnJobDispatch,
dispatchReleaseJobTriggers,
handleEnvironmentPolicyReleaseChannelUpdate,
isPassingAllPolicies,
} from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";
Expand Down Expand Up @@ -377,6 +378,14 @@ export const policyRouter = createTRPCRouter({
.then(takeFirst);

if (releaseChannels != null) {
const prevReleaseChannels = await ctx.db
.select({
deploymentId: environmentPolicyReleaseChannel.deploymentId,
channelId: environmentPolicyReleaseChannel.channelId,
})
.from(environmentPolicyReleaseChannel)
.where(eq(environmentPolicyReleaseChannel.policyId, input.id));

const [nulled, set] = _.partition(
Object.entries(releaseChannels),
([_, channelId]) => channelId == null,
Expand Down Expand Up @@ -415,6 +424,27 @@ export const policyRouter = createTRPCRouter({
),
});
});

const newReleaseChannels = await ctx.db
.select({
deploymentId: environmentPolicyReleaseChannel.deploymentId,
channelId: environmentPolicyReleaseChannel.channelId,
})
.from(environmentPolicyReleaseChannel)
.where(eq(environmentPolicyReleaseChannel.policyId, input.id));

const prevMap = Object.fromEntries(
prevReleaseChannels.map((r) => [r.deploymentId, r.channelId]),
);
const newMap = Object.fromEntries(
newReleaseChannels.map((r) => [r.deploymentId, r.channelId]),
);

await handleEnvironmentPolicyReleaseChannelUpdate(
input.id,
prevMap,
newMap,
);
}

if (releaseWindows != null) {
Expand Down
30 changes: 28 additions & 2 deletions packages/api/src/router/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import {
dispatchJobsForAddedResources,
getEventsForEnvironmentDeleted,
handleEnvironmentReleaseChannelUpdate,
handleEvent,
} from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";
Expand Down Expand Up @@ -340,7 +341,15 @@ export const environmentRouter = createTRPCRouter({
.perform(Permission.EnvironmentUpdate)
.on({ type: "environment", id: input.id }),
})
.mutation(({ ctx, input }) => {
.mutation(async ({ ctx, input }) => {
const prevReleaseChannels = await ctx.db
.select({
deploymentId: environmentReleaseChannel.deploymentId,
channelId: environmentReleaseChannel.channelId,
})
.from(environmentReleaseChannel)
.where(eq(environmentReleaseChannel.environmentId, input.id));

const [nulled, set] = _.partition(
Object.entries(input.releaseChannels),
([_, channelId]) => channelId == null,
Expand All @@ -353,7 +362,7 @@ export const environmentRouter = createTRPCRouter({
channelId: channelId!,
}));

return ctx.db.transaction(async (db) => {
await ctx.db.transaction(async (db) => {
await db
.delete(environmentReleaseChannel)
.where(
Expand All @@ -377,6 +386,23 @@ export const environmentRouter = createTRPCRouter({
]),
});
});

const newReleaseChannels = await ctx.db
.select({
deploymentId: environmentReleaseChannel.deploymentId,
channelId: environmentReleaseChannel.channelId,
})
.from(environmentReleaseChannel)
.where(eq(environmentReleaseChannel.environmentId, input.id));

const prevMap = Object.fromEntries(
prevReleaseChannels.map((r) => [r.deploymentId, r.channelId]),
);
const newMap = Object.fromEntries(
newReleaseChannels.map((r) => [r.deploymentId, r.channelId]),
);

await handleEnvironmentReleaseChannelUpdate(input.id, prevMap, newMap);
}),

delete: protectedProcedure
Expand Down
Loading
Loading