-
Notifications
You must be signed in to change notification settings - Fork 11
init value selection #425
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
Closed
Closed
init value selection #425
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| 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, | ||
| ]; |
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,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" | ||
| } | ||
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,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; | ||
| }; |
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,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"] | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
💡 Verification agent
🧩 Analysis chain
Critical naming conflict with an existing package
The pipeline is failing because there's already a package named
@ctrlplane/rule-engineatpackages/rule-engine/package.json. This package needs to be renamed to something else, likely@ctrlplane/deployment-variablesto match the directory name.🏁 Script executed:
Length of output: 552
Critical Naming Conflict: Rename Package
The package in
packages/deployment-variables/package.jsonis currently named"@ctrlplane/rule-engine", which conflicts with the existing package atpackages/rule-engine/package.jsonthat already uses that name. To resolve the pipeline failure and maintain naming consistency with the directory structure, please update the package name accordingly:packages/deployment-variables/package.json"@ctrlplane/rule-engine"to"@ctrlplane/deployment-variables".📝 Committable suggestion
🧰 Tools
🪛 GitHub Actions: CI
[error] 1-1: Failed to add workspace '@ctrlplane/rule-engine', it already exists at 'packages/rule-engine/package.json'.