Skip to content

Commit

Permalink
fix: Deployment variable selection logic bug
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Oct 23, 2024
1 parent 0672eb4 commit 2899756
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ describe("job-variables-deployment", () => {
vi.mocked(utils.getTarget).mockResolvedValue(target);

vi.mocked(utils.getVariableValues).mockResolvedValue(variableValues);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(
variableValues[1]!,
);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(variableValues[1]);
vi.mocked(utils.getEnvironment).mockResolvedValue(undefined);

const result = await jobVariablesDeployment.determineVariablesForReleaseJob(
Expand Down Expand Up @@ -158,7 +156,7 @@ describe("job-variables-deployment", () => {
vi.mocked(utils.getTarget).mockResolvedValue(target);

vi.mocked(utils.getVariableValues).mockResolvedValue(variableValues);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(null);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(undefined);
vi.mocked(utils.getEnvironment).mockResolvedValue(undefined);

const result = await jobVariablesDeployment.determineVariablesForReleaseJob(
Expand Down Expand Up @@ -193,7 +191,7 @@ describe("job-variables-deployment", () => {
vi.mocked(utils.getTarget).mockResolvedValue(target);

vi.mocked(utils.getVariableValues).mockResolvedValue(variableValues);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(null);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(undefined);
vi.mocked(utils.getEnvironment).mockResolvedValue(undefined);

const result = await jobVariablesDeployment.determineVariablesForReleaseJob(
Expand Down Expand Up @@ -228,9 +226,7 @@ describe("job-variables-deployment", () => {
vi.mocked(utils.getTarget).mockResolvedValue(target);

vi.mocked(utils.getVariableValues).mockResolvedValue(variableValues);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(
variableValues[1]!,
);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(variableValues[1]);

vi.mocked(utils.getEnvironment).mockResolvedValue({
id: "0",
Expand Down Expand Up @@ -296,9 +292,7 @@ describe("job-variables-deployment", () => {
vi.mocked(utils.getTarget).mockResolvedValue(target);

vi.mocked(utils.getVariableValues).mockResolvedValue(variableValues);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(
variableValues[1]!,
);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(variableValues[1]);

vi.mocked(utils.getEnvironment).mockResolvedValue({
id: "0",
Expand Down Expand Up @@ -363,7 +357,7 @@ describe("job-variables-deployment", () => {
vi.mocked(utils.getTarget).mockResolvedValue(target);

vi.mocked(utils.getVariableValues).mockResolvedValue(variableValues);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(null);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(undefined);

vi.mocked(utils.getEnvironment).mockResolvedValue({
id: "0",
Expand Down Expand Up @@ -428,7 +422,7 @@ describe("job-variables-deployment", () => {
vi.mocked(utils.getTarget).mockResolvedValue(target);

vi.mocked(utils.getVariableValues).mockResolvedValue(variableValues);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(null);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(undefined);

vi.mocked(utils.getEnvironment).mockResolvedValue({
id: "0",
Expand Down Expand Up @@ -493,7 +487,7 @@ describe("job-variables-deployment", () => {
vi.mocked(utils.getTarget).mockResolvedValue(target);

vi.mocked(utils.getVariableValues).mockResolvedValue(variableValues);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(null);
vi.mocked(utils.getFirstMatchedTarget).mockResolvedValue(undefined);

vi.mocked(utils.getEnvironment).mockResolvedValue({
id: "0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Tx } from "@ctrlplane/db";
import { isPresent } from "ts-is-present";

import * as schema from "@ctrlplane/db/schema";

Expand Down Expand Up @@ -123,10 +124,14 @@ export const determineReleaseVariableValue = async (
(v) => v.id === defaultValueId,
);

const valuesWithFilter = deploymentVariableValues.filter((v) =>
isPresent(v.targetFilter),
);

const firstMatchedValue = await utils.getFirstMatchedTarget(
tx,
jobTarget.id,
deploymentVariableValues,
valuesWithFilter,
);

if (firstMatchedValue != null)
Expand Down
21 changes: 9 additions & 12 deletions packages/job-dispatch/src/job-variables-deployment/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Tx } from "@ctrlplane/db";
import type { TargetCondition } from "@ctrlplane/validators/targets";
import { isPresent } from "ts-is-present";

import { and, eq, takeFirstOrNull } from "@ctrlplane/db";
import * as SCHEMA from "@ctrlplane/db/schema";
Expand Down Expand Up @@ -67,15 +68,11 @@ export const getFirstMatchedTarget = (
tx: Tx,
targetId: string,
values: SCHEMA.DeploymentVariableValue[],
) => {
const promises = values.map(async (value) => {
const matchedTarget = await getMatchedTarget(
tx,
targetId,
value.targetFilter,
);
return matchedTarget != null ? value : null;
});

return Promise.all(promises).then(takeFirstOrNull);
};
) =>
Promise.all(
values.map((value) =>
getMatchedTarget(tx, targetId, value.targetFilter).then(
(matchedTarget) => (matchedTarget != null ? value : null),
),
),
).then((res) => res.find(isPresent));

0 comments on commit 2899756

Please sign in to comment.