Skip to content
Closed
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
11 changes: 5 additions & 6 deletions packages/db/src/schema/deployment-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ export const createDeploymentVariableValue = createInsertSchema(
deploymentVariableValue,
{ resourceSelector: resourceCondition },
)
.omit({
id: true,
})
.extend({
default: z.boolean().optional(),
});
.omit({ id: true })
.extend({ default: z.boolean().optional() });
export type CreateDeploymentVariableValue = z.infer<
typeof createDeploymentVariableValue
>;
export const updateDeploymentVariableValue =
createDeploymentVariableValue.partial();

Expand Down
13 changes: 13 additions & 0 deletions packages/deployment-variables/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import baseConfig, { requireJsSuffix } from "@ctrlplane/eslint-config/base";

/** @type {import('typescript-eslint').Config} */
export default [
{
ignores: ["dist/**"],
rules: {
"@typescript-eslint/require-await": "off",
},
},
...requireJsSuffix,
...baseConfig,
];
37 changes: 37 additions & 0 deletions packages/deployment-variables/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@ctrlplane/rule-engine",
"private": true,
"version": "0.1.0",
"type": "module",
"exports": {
".": {
"types": "./src/index.ts",
"default": "./dist/index.js"
}
},
"license": "MIT",
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"test": "vitest",
"clean": "rm -rf .turbo node_modules",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@ctrlplane/db": "workspace:*",
"@ctrlplane/validators": "workspace:*",
"ts-is-present": "catalog:"
},
"devDependencies": {
"@ctrlplane/eslint-config": "workspace:*",
"@ctrlplane/prettier-config": "workspace:*",
"@ctrlplane/tsconfig": "workspace:*",
"@types/node": "catalog:node22",
"eslint": "catalog:",
"prettier": "catalog:",
"typescript": "catalog:"
},
"prettier": "@ctrlplane/prettier-config"
}
Comment on lines +1 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Critical naming conflict with an existing package

The pipeline is failing because there's already a package named @ctrlplane/rule-engine at packages/rule-engine/package.json. This package needs to be renamed to something else, likely @ctrlplane/deployment-variables to match the directory name.

-  "name": "@ctrlplane/rule-engine",
+  "name": "@ctrlplane/deployment-variables",

🏁 Script executed:

#!/bin/bash
# Check if there's already a package at packages/rule-engine
if [ -f "packages/rule-engine/package.json" ]; then
  echo "Verification: Found existing package at packages/rule-engine/package.json"
  grep -n "\"name\":" packages/rule-engine/package.json
fi

# Check if the directory name matches other package patterns
echo "Current directory structure suggests the package should be named:"
basename "$(dirname "$(pwd)/packages/deployment-variables")"

Length of output: 552


Critical Naming Conflict: Rename Package

The package in packages/deployment-variables/package.json is currently named "@ctrlplane/rule-engine", which conflicts with the existing package at packages/rule-engine/package.json that already uses that name. To resolve the pipeline failure and maintain naming consistency with the directory structure, please update the package name accordingly:

  • File to update: packages/deployment-variables/package.json
  • Action required: Change the package name from "@ctrlplane/rule-engine" to "@ctrlplane/deployment-variables".
-  "name": "@ctrlplane/rule-engine",
+  "name": "@ctrlplane/deployment-variables",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{
"name": "@ctrlplane/rule-engine",
"private": true,
"version": "0.1.0",
"type": "module",
"exports": {
".": {
"types": "./src/index.ts",
"default": "./dist/index.js"
}
},
"license": "MIT",
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"test": "vitest",
"clean": "rm -rf .turbo node_modules",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@ctrlplane/db": "workspace:*",
"@ctrlplane/validators": "workspace:*",
"ts-is-present": "catalog:"
},
"devDependencies": {
"@ctrlplane/eslint-config": "workspace:*",
"@ctrlplane/prettier-config": "workspace:*",
"@ctrlplane/tsconfig": "workspace:*",
"@types/node": "catalog:node22",
"eslint": "catalog:",
"prettier": "catalog:",
"typescript": "catalog:"
},
"prettier": "@ctrlplane/prettier-config"
}
{
"name": "@ctrlplane/deployment-variables",
"private": true,
"version": "0.1.0",
"type": "module",
"exports": {
".": {
"types": "./src/index.ts",
"default": "./dist/index.js"
}
},
"license": "MIT",
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"test": "vitest",
"clean": "rm -rf .turbo node_modules",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@ctrlplane/db": "workspace:*",
"@ctrlplane/validators": "workspace:*",
"ts-is-present": "catalog:"
},
"devDependencies": {
"@ctrlplane/eslint-config": "workspace:*",
"@ctrlplane/prettier-config": "workspace:*",
"@ctrlplane/tsconfig": "workspace:*",
"@types/node": "catalog:node22",
"eslint": "catalog:",
"prettier": "catalog:",
"typescript": "catalog:"
},
"prettier": "@ctrlplane/prettier-config"
}
🧰 Tools
🪛 GitHub Actions: CI

[error] 1-1: Failed to add workspace '@ctrlplane/rule-engine', it already exists at 'packages/rule-engine/package.json'.

146 changes: 146 additions & 0 deletions packages/deployment-variables/src/generate-releases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { and, asc, eq, isNotNull, takeFirstOrNull, Tx } from "@ctrlplane/db";
import * as SCHEMA from "@ctrlplane/db/schema";

/**
* Get the variable directly assigned to the resource
* @param db
* @param resourceId
* @param key
* @returns the variable assigned to the resource for this key
*/
const getResourceVariable = async (db: Tx, resourceId: string, key: string) =>
db
.select()
.from(SCHEMA.resourceVariable)
.where(
and(
eq(SCHEMA.resourceVariable.key, key),
eq(SCHEMA.resourceVariable.resourceId, resourceId),
),
)
.then(takeFirstOrNull);

/**
* Get the variable value assigned to the deployment
* To resolve conflicts, we sort the values by jsonb ascending and select the first
* @param db
* @param resourceId
* @param deploymentId
* @param key
*/
const getDeploymentVariableValue = async (
db: Tx,
resourceId: string,
deploymentId: string,
key: string,
) => {
const variable = await db
.select()
.from(SCHEMA.deploymentVariable)
.where(
and(
eq(SCHEMA.deploymentVariable.key, key),
eq(SCHEMA.deploymentVariable.deploymentId, deploymentId),
),
)
.then(takeFirstOrNull);

if (variable == null) return null;

const values = await db
.select()
.from(SCHEMA.deploymentVariableValue)
.where(eq(SCHEMA.deploymentVariableValue.variableId, variable.id))
.orderBy(asc(SCHEMA.deploymentVariableValue.value));

for (const value of values) {
const resource = await db
.select()
.from(SCHEMA.resource)
.where(
and(
eq(SCHEMA.resource.id, resourceId),
SCHEMA.resourceMatchesMetadata(db, value.resourceSelector),
),
)
.then(takeFirstOrNull);

if (resource != null) return value.value;
}

const defaultValue = values.find((v) => v.id === variable.defaultValueId);
return defaultValue?.value ?? null;
};

/**
* Get the variable value assigned to the environment via variable sets
* To resolve conflicts, we sort the values by jsonb ascending and select the first
* @param db
* @param environmentId
* @param key
*/
const getVariableSetValue = async (
db: Tx,
environmentId: string,
key: string,
) =>
db
.select()
.from(SCHEMA.variableSetValue)
.innerJoin(
SCHEMA.variableSetEnvironment,
eq(
SCHEMA.variableSetValue.variableSetId,
SCHEMA.variableSetEnvironment.variableSetId,
),
)
.where(
and(
eq(SCHEMA.variableSetEnvironment.environmentId, environmentId),
eq(SCHEMA.variableSetValue.key, key),
),
)
.orderBy(asc(SCHEMA.variableSetValue.value))
.limit(1)
.then(takeFirstOrNull);

/**
*
* @param db
* @param resourceId
* @param deploymentId
* @param environmentId
* @param key
* @returns the value assigned to the resource for this key
*
* This function will deterministically assign a value to the resource
* based on the following priority rule:
*
* 1. Check if there is a variable directly assigned to the resource
* 2. Check if there is a deployment variable value who's resource selector
* matches the resource
* - If there are multiple, we sort by jsonb ascending and select the first
* 3. Check if there is a value for this key in a variable set assigned to the environment
* - If there are multiple, we sort by jsonb ascending and select the first
*/
const getValueForResource = async (
db: Tx,
resourceId: string,
deploymentId: string,
environmentId: string,
key: string,
) => {
const resourceVariable = await getResourceVariable(db, resourceId, key);
if (resourceVariable != null) return resourceVariable.value;

const deploymentVariableValue = await getDeploymentVariableValue(
db,
resourceId,
deploymentId,
key,
);
if (deploymentVariableValue != null) return deploymentVariableValue;

const variableSetValue = await getVariableSetValue(db, environmentId, key);
return variableSetValue;
};
11 changes: 11 additions & 0 deletions packages/deployment-variables/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@ctrlplane/tsconfig/internal-package.json",
"compilerOptions": {
"outDir": "dist",
"baseUrl": ".",
"incremental": true,
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["*.ts", "src"],
"exclude": ["node_modules"]
}
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading