-
Notifications
You must be signed in to change notification settings - Fork 157
Feature - Environment Variable "Updated" field and pending update endpoint #4006
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b028be0
Adds updated column in env_vars
172a034
Adds some logic to calculate the outstanding updates
358efb0
Adds some explanatory text
1ee1630
Adds source of variable
a150e86
Update services/api/src/resources/environment/environment_redeploy.ts
bomoko 68ef0ef
Update services/api/src/resources/environment/environment_redeploy.ts
bomoko 8fbc8ce
Update services/api/src/typeDefs.js
bomoko 7a28ddd
Fixes env var updated query
f2aad51
Fixes env var updated query
8a0a377
Better index use in query
5991acb
Update services/api/src/resources/environment/environment_redeploy.ts
bomoko 16d13b7
Update services/api/src/resources/environment/environment_redeploy.ts
bomoko 98f5df8
Update services/api/src/resources/environment/environment_redeploy.ts
bomoko c32d4d2
Adds slightly stricter typing to pending changes
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
25 changes: 25 additions & 0 deletions
25
services/api/database/migrations/20251019203621_add_updated_to_env_vars.js
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,25 @@ | ||
| /** | ||
| * @param { import("knex").Knex } knex | ||
| * @returns { Promise<void> } | ||
| */ | ||
| exports.up = function(knex) { | ||
| return knex.schema | ||
| .alterTable('env_vars', (table) => { | ||
| table.datetime('updated').notNullable().defaultTo(knex.fn.now()); | ||
| }) | ||
| .raw("UPDATE env_vars SET updated='1970-01-01 00:00:00'"); | ||
| // Note, we do the above update so that all _existing_ env vars are | ||
| // not picked up as needing to be deployed (since we're using 'updated' to track new/updated vars) | ||
| // but any newly created items will get the _current_ date/time | ||
| }; | ||
|
|
||
| /** | ||
| * @param { import("knex").Knex } knex | ||
| * @returns { Promise<void> } | ||
| */ | ||
| exports.down = function(knex) { | ||
| return knex.schema | ||
| .alterTable('env_vars', (table) => { | ||
| table.dropColumn('updated'); | ||
| }) | ||
| }; |
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
73 changes: 73 additions & 0 deletions
73
services/api/src/resources/environment/environment_redeploy.ts
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,73 @@ | ||
| // This file contains the logic to determine whether an environment requires a redeploy | ||
|
|
||
| import * as R from 'ramda'; | ||
| import { sendToLagoonLogs } from '@lagoon/commons/dist/logs/lagoon-logger'; | ||
| import { createRemoveTask, seedNamespace } from '@lagoon/commons/dist/tasks'; | ||
| import { ResolverFn } from '..'; | ||
| import { logger } from '../../loggers/logger'; | ||
| import { isPatchEmpty, query, knex } from '../../util/db'; | ||
| import { convertDateToMYSQLDateFormat } from '../../util/convertDateToMYSQLDateTimeFormat'; | ||
| import { Helpers } from './helpers'; | ||
| import { Sql } from './sql'; | ||
| import { Sql as projectSql } from '../project/sql'; | ||
| import { Helpers as projectHelpers } from '../project/helpers'; | ||
| import { Helpers as openshiftHelpers } from '../openshift/helpers'; | ||
| import { Helpers as organizationHelpers } from '../organization/helpers'; | ||
| import { getFactFilteredEnvironmentIds } from '../fact/resolvers'; | ||
| import { getUserProjectIdsFromRoleProjectIds } from '../../util/auth'; | ||
| import { RemoveData, DeployType, AuditType } from '@lagoon/commons/dist/types'; | ||
| import { AuditLog } from '../audit/types'; | ||
|
|
||
|
|
||
| export const getPendingChangesByEnvironmentId: ResolverFn = async( | ||
| { | ||
| id | ||
| }, | ||
| _, | ||
| { sqlClientPool, hasPermission }, | ||
| ) => { | ||
| // Note: as it stands, the only pending changes we have now have to do | ||
| // with env vars, but anything can be added in the form | ||
| // {type:"string", details:"string"} | ||
| let pendingChanges = await getPendingEnvVarChanges(sqlClientPool, id); | ||
| return pendingChanges; | ||
| } | ||
|
|
||
| const getPendingEnvVarChanges = async(sqlClientPool, envId) => { | ||
| const sql = ` | ||
| SELECT DISTINCT | ||
| ev.name, | ||
| ev.updated, | ||
| e.id, | ||
| e.name as env_name, | ||
| COALESCE( | ||
| IF(ev.environment IS NULL,NULL,'Environment'), | ||
| IF(ev.project IS NULL,NULL,'Project'), | ||
| IF(ev.organization IS NULL,NULL,'Organization') | ||
| ) as varsource, | ||
| COALESCE(MAX(d.created) OVER (PARTITION BY e.id), '1970-01-01') as last_deployment | ||
| FROM environment as e | ||
| LEFT JOIN deployment as d ON e.id = d.environment | ||
| INNER JOIN project as p ON p.id = e.project | ||
| LEFT JOIN organization as o ON o.id = p.organization | ||
| LEFT JOIN env_vars as ev ON ( | ||
| ev.environment = e.id OR | ||
| ev.project = p.id OR | ||
| ev.organization = o.id | ||
| ) | ||
| WHERE ev.name IS NOT NULL AND e.id = ? | ||
shreddedbacon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| `; | ||
|
|
||
| const results = await query(sqlClientPool, sql, [envId]); | ||
|
|
||
| // Filter in memory for env vars updated after last deployment | ||
| const pendingChanges = results.filter(row => { | ||
| const updated = new Date(row.updated); | ||
| const lastDeployment = new Date(row.last_deployment); | ||
bomoko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return updated > lastDeployment || true; | ||
bomoko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }).map(row => { | ||
| return {type:`Environment Variable - ${row.varsource} level`, details: row.name}; | ||
shreddedbacon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| return pendingChanges; | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.