@contember/cli 2.2.0-alpha.15 → 2.2.0-alpha.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/development/src/dic.cjs +2 -0
  2. package/dist/development/src/dic.cjs.map +1 -1
  3. package/dist/development/src/dic.js +2 -0
  4. package/dist/development/src/dic.js.map +1 -1
  5. package/dist/development/src/lib/actions/UndefinedActionsVariablesChecker.cjs +39 -0
  6. package/dist/development/src/lib/actions/UndefinedActionsVariablesChecker.cjs.map +1 -0
  7. package/dist/development/src/lib/actions/UndefinedActionsVariablesChecker.js +39 -0
  8. package/dist/development/src/lib/actions/UndefinedActionsVariablesChecker.js.map +1 -0
  9. package/dist/development/src/lib/migrations/MigrationExecutionFacade.cjs +6 -1
  10. package/dist/development/src/lib/migrations/MigrationExecutionFacade.cjs.map +1 -1
  11. package/dist/development/src/lib/migrations/MigrationExecutionFacade.js +6 -1
  12. package/dist/development/src/lib/migrations/MigrationExecutionFacade.js.map +1 -1
  13. package/dist/production/src/dic.cjs +2 -0
  14. package/dist/production/src/dic.cjs.map +1 -1
  15. package/dist/production/src/dic.js +2 -0
  16. package/dist/production/src/dic.js.map +1 -1
  17. package/dist/production/src/lib/actions/UndefinedActionsVariablesChecker.cjs +39 -0
  18. package/dist/production/src/lib/actions/UndefinedActionsVariablesChecker.cjs.map +1 -0
  19. package/dist/production/src/lib/actions/UndefinedActionsVariablesChecker.js +39 -0
  20. package/dist/production/src/lib/actions/UndefinedActionsVariablesChecker.js.map +1 -0
  21. package/dist/production/src/lib/migrations/MigrationExecutionFacade.cjs +6 -1
  22. package/dist/production/src/lib/migrations/MigrationExecutionFacade.cjs.map +1 -1
  23. package/dist/production/src/lib/migrations/MigrationExecutionFacade.js +6 -1
  24. package/dist/production/src/lib/migrations/MigrationExecutionFacade.js.map +1 -1
  25. package/dist/types/dic.d.ts.map +1 -1
  26. package/dist/types/lib/actions/UndefinedActionsVariablesChecker.d.ts +17 -0
  27. package/dist/types/lib/actions/UndefinedActionsVariablesChecker.d.ts.map +1 -0
  28. package/dist/types/lib/migrations/MigrationExecutionFacade.d.ts +3 -1
  29. package/dist/types/lib/migrations/MigrationExecutionFacade.d.ts.map +1 -1
  30. package/dist/types/tsconfig.tsbuildinfo +1 -1
  31. package/package.json +14 -14
  32. package/src/dic.ts +2 -0
  33. package/src/lib/actions/UndefinedActionsVariablesChecker.ts +52 -0
  34. package/src/lib/migrations/MigrationExecutionFacade.ts +6 -0
  35. package/tests/cases/unit/MigrationExecutionFacade.test.ts +44 -2
  36. package/tests/cases/unit/UndefinedActionsVariablesChecker.test.ts +78 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/cli",
3
- "version": "2.2.0-alpha.15",
3
+ "version": "2.2.0-alpha.16",
4
4
  "license": "Apache-2.0",
5
5
  "bin": {
6
6
  "contember": "./dist/production/run.js"
@@ -36,19 +36,19 @@
36
36
  "tailwindcss": "^4.2.1"
37
37
  },
38
38
  "dependencies": {
39
- "@contember/cli-common": "2.2.0-alpha.15",
40
- "@contember/config-loader": "2.2.0-alpha.15",
41
- "@contember/database": "2.2.0-alpha.15",
42
- "@contember/dic": "2.2.0-alpha.15",
43
- "@contember/engine-common": "2.2.0-alpha.15",
44
- "@contember/engine-content-api": "2.2.0-alpha.15",
45
- "@contember/graphql-client": "2.2.0-alpha.15",
46
- "@contember/graphql-client-tenant": "2.2.0-alpha.15",
47
- "@contember/migrations-client": "2.2.0-alpha.15",
48
- "@contember/schema": "2.2.0-alpha.15",
49
- "@contember/schema-migrations": "2.2.0-alpha.15",
50
- "@contember/schema-utils": "2.2.0-alpha.15",
51
- "@contember/typesafe": "2.2.0-alpha.15",
39
+ "@contember/cli-common": "2.2.0-alpha.16",
40
+ "@contember/config-loader": "2.2.0-alpha.16",
41
+ "@contember/database": "2.2.0-alpha.16",
42
+ "@contember/dic": "2.2.0-alpha.16",
43
+ "@contember/engine-common": "2.2.0-alpha.16",
44
+ "@contember/engine-content-api": "2.2.0-alpha.16",
45
+ "@contember/graphql-client": "2.2.0-alpha.16",
46
+ "@contember/graphql-client-tenant": "2.2.0-alpha.16",
47
+ "@contember/migrations-client": "2.2.0-alpha.16",
48
+ "@contember/schema": "2.2.0-alpha.16",
49
+ "@contember/schema-migrations": "2.2.0-alpha.16",
50
+ "@contember/schema-utils": "2.2.0-alpha.16",
51
+ "@contember/typesafe": "2.2.0-alpha.16",
52
52
  "@graphql-tools/schema": "^10.0.31",
53
53
  "chalk": "^5.6.2",
54
54
  "chalk-table": "^1.0.2",
package/src/dic.ts CHANGED
@@ -47,6 +47,7 @@ import {
47
47
  WorkspaceUpdateApiCommand,
48
48
  } from './commands/index.js'
49
49
  import { MigrationExecutionFacade } from './lib/migrations/MigrationExecutionFacade.js'
50
+ import { UndefinedActionsVariablesChecker } from './lib/actions/UndefinedActionsVariablesChecker.js'
50
51
  import { MigrationPrinter } from './lib/migrations/MigrationPrinter.js'
51
52
  import { MigrationSnapshotFacade } from './lib/migrations/MigrationSnapshotFacade.js'
52
53
  import { MigrationsStatusFacade } from './lib/migrations/MigrationsStatusFacade.js'
@@ -258,6 +259,7 @@ export const createContainer = ({ env, version, runtime, workspace, output }: {
258
259
  migrationsStatusFacade,
259
260
  schemaStateManager,
260
261
  migrationSnapshotFacade,
262
+ new UndefinedActionsVariablesChecker(output),
261
263
  output,
262
264
  ),
263
265
  )
@@ -0,0 +1,52 @@
1
+ import { Output } from '@contember/cli-common'
2
+ import { Actions } from '@contember/schema'
3
+ import { getWebhookTargetVariableNames } from '@contember/schema-utils'
4
+ import { RemoteProject } from '../project/RemoteProject.js'
5
+ import { ActionsApi, ActionsClient } from './ActionsClient.js'
6
+
7
+ export type ActionsVariablesClientFactory = (project: RemoteProject) => Pick<ActionsApi, 'listVariables'>
8
+
9
+ const createActionsClient: ActionsVariablesClientFactory = project => ActionsClient.create(project.endpoint, project.name, project.token)
10
+
11
+ /**
12
+ * Warns, never fails, about webhook targets that interpolate a variable the engine does not define:
13
+ * the engine holds such a target's events back until the variable is set.
14
+ */
15
+ export class UndefinedActionsVariablesChecker {
16
+ constructor(
17
+ private readonly output: Output = new Output(),
18
+ private readonly createClient: ActionsVariablesClientFactory = createActionsClient,
19
+ ) {
20
+ }
21
+
22
+ /** `loadActions` builds the migrated schema's actions; a failure there is reported like a failed lookup. */
23
+ public async check(project: RemoteProject, loadActions: () => Promise<Actions.Schema>): Promise<void> {
24
+ let referencing: { name: string; variables: string[] }[]
25
+ let defined: Set<string>
26
+ try {
27
+ referencing = Object.entries((await loadActions()).targets)
28
+ .map(([name, target]) => ({ name, variables: getWebhookTargetVariableNames(target) }))
29
+ .filter(it => it.variables.length > 0)
30
+ if (referencing.length === 0) {
31
+ return
32
+ }
33
+ const variables = await this.createClient(project).listVariables()
34
+ // the engine treats an empty value as unset; an environment value is not returned at all (null)
35
+ defined = new Set(variables.filter(it => it.value !== '').map(it => it.name))
36
+ } catch (error) {
37
+ const reason = error instanceof Error ? error.message : String(error)
38
+ this.output.warn(`Warning: could not check the variables webhook targets reference: ${reason}`)
39
+ return
40
+ }
41
+
42
+ for (const { name, variables } of referencing) {
43
+ const undefinedVariables = variables.filter(it => !defined.has(it))
44
+ if (undefinedVariables.length > 0) {
45
+ this.output.warn(
46
+ `Warning: webhook target ${name} references undefined variables: ${undefinedVariables.join(', ')}. `
47
+ + 'Its events wait until they are set (actions set-variables, or the engine environment).',
48
+ )
49
+ }
50
+ }
51
+ }
52
+ }
@@ -18,6 +18,7 @@ import { RemoteProjectProvider } from '../project/RemoteProjectProvider.js'
18
18
  import { CliError, ExitCode, Output } from '@contember/cli-common'
19
19
  import { GraphQlClientError } from '@contember/graphql-client'
20
20
  import { promptSelect } from '../prompt/index.js'
21
+ import { UndefinedActionsVariablesChecker } from '../actions/UndefinedActionsVariablesChecker.js'
21
22
 
22
23
  export class MigrationExecutionFacade {
23
24
  constructor(
@@ -30,6 +31,7 @@ export class MigrationExecutionFacade {
30
31
  private readonly migrationStatusFacade: MigrationsStatusFacade,
31
32
  private readonly schemaStateManager: SchemaStateManager,
32
33
  private readonly migrationSnapshotFacade: MigrationSnapshotFacade,
34
+ private readonly undefinedActionsVariablesChecker: UndefinedActionsVariablesChecker,
33
35
  private readonly output: Output = new Output(),
34
36
  ) {
35
37
  }
@@ -179,6 +181,10 @@ export class MigrationExecutionFacade {
179
181
  }
180
182
  throw e
181
183
  }
184
+ await this.undefinedActionsVariablesChecker.check(
185
+ project,
186
+ async () => (await this.schemaVersionBuilder.buildSchema(until ? MigrationVersionHelper.extractVersion(until) : undefined)).actions,
187
+ )
182
188
  return true
183
189
  }
184
190
 
@@ -41,6 +41,8 @@ import { TenantApiTransport } from '../../../src/lib/tenant/TenantApiTransport.j
41
41
  import { TenantCreateProjectDetails, TenantCreateProjectResult, TenantProjectClient } from '../../../src/lib/tenant/clients/TenantProjectClient.js'
42
42
  import { createTestOutput } from '../../../../cli-common/tests/lib/testOutput.js'
43
43
  import prompts from 'prompts'
44
+ import { Actions, Schema } from '@contember/schema'
45
+ import { UndefinedActionsVariablesChecker } from '../../../src/lib/actions/UndefinedActionsVariablesChecker.js'
44
46
 
45
47
  type Event =
46
48
  | { type: 'create-project'; details: TenantCreateProjectDetails | undefined }
@@ -49,6 +51,7 @@ type Event =
49
51
  | { type: 'snapshot-build' }
50
52
  | { type: 'snapshot-apply' }
51
53
  | { type: 'execute-migrations'; schemaState: SchemaState | undefined }
54
+ | { type: 'list-variables' }
52
55
 
53
56
  class RecordingSystemClient extends SystemClient {
54
57
  constructor(
@@ -164,8 +167,21 @@ afterEach(async () => {
164
167
  await fs.rm(workDir, { recursive: true, force: true })
165
168
  })
166
169
 
170
+ const withTargets = (schema: Schema, targets: Actions.Schema['targets']): Schema => ({
171
+ ...schema,
172
+ actions: { ...schema.actions, targets: { ...schema.actions.targets, ...targets } },
173
+ })
174
+
167
175
  const createHarness = async (
168
- { state = false, migration = false, snapshot = false, remoteExecuted = false, remoteMissing = false, stdinTty = false } = {},
176
+ { state = false, migration = false, snapshot = false, remoteExecuted = false, remoteMissing = false, stdinTty = false, targets = {} }: {
177
+ state?: boolean
178
+ migration?: boolean
179
+ snapshot?: boolean
180
+ remoteExecuted?: boolean
181
+ remoteMissing?: boolean
182
+ stdinTty?: boolean
183
+ targets?: Actions.Schema['targets']
184
+ } = {},
169
185
  ) => {
170
186
  const events: Event[] = []
171
187
  const migrationsDir = path.join(workDir, 'migrations')
@@ -176,7 +192,10 @@ const createHarness = async (
176
192
  if (migration) {
177
193
  await filesManager.createDirIfNotExist()
178
194
  await filesManager.createFile(
179
- JSON.stringify({ formatVersion: VERSION_LATEST, modifications: schemaDiffer.diffSchemas(emptySchema, createSchema(ExecutionModel)) }),
195
+ JSON.stringify({
196
+ formatVersion: VERSION_LATEST,
197
+ modifications: schemaDiffer.diffSchemas(emptySchema, withTargets(createSchema(ExecutionModel), targets)),
198
+ }),
180
199
  '2024-01-01-120000-init',
181
200
  )
182
201
  }
@@ -241,6 +260,12 @@ const createHarness = async (
241
260
  statusFacade,
242
261
  stateManager,
243
262
  snapshotFacade,
263
+ new UndefinedActionsVariablesChecker(io.output, () => ({
264
+ listVariables: async () => {
265
+ events.push({ type: 'list-variables' })
266
+ return []
267
+ },
268
+ })),
244
269
  io.output,
245
270
  )
246
271
  return { events, facade, io }
@@ -366,3 +391,20 @@ test('notifies confirmation once after interactive execution is accepted', async
366
391
 
367
392
  expect(confirmations).toBe(1)
368
393
  })
394
+
395
+ test('warns after executing migrations whose webhook target references an undefined variable', async () => {
396
+ const { facade, events, io } = await createHarness({
397
+ migration: true,
398
+ targets: { hook: { type: 'webhook', name: 'hook', url: 'https://example.com/{{TOKEN}}' } },
399
+ })
400
+
401
+ const result = await facade.execute({ requireConfirmation: false })
402
+
403
+ expect(result).toBe(true)
404
+ const types = events.map(event => event.type)
405
+ expect(types.indexOf('list-variables')).toBeGreaterThan(types.indexOf('execute-migrations'))
406
+ expect(io.stderr.lines).toContain(
407
+ 'Warning: webhook target hook references undefined variables: TOKEN. '
408
+ + 'Its events wait until they are set (actions set-variables, or the engine environment).',
409
+ )
410
+ })
@@ -0,0 +1,78 @@
1
+ import { expect, test } from 'bun:test'
2
+ import { Actions } from '@contember/schema'
3
+ import { Variable } from '../../../src/lib/actions/ActionsClient.js'
4
+ import { UndefinedActionsVariablesChecker } from '../../../src/lib/actions/UndefinedActionsVariablesChecker.js'
5
+ import { RemoteProject } from '../../../src/lib/project/RemoteProject.js'
6
+ import { createTestOutput } from '../../../../cli-common/tests/lib/testOutput.js'
7
+
8
+ const project = new RemoteProject('blog', 'http://api.test', 'token')
9
+
10
+ const createActions = (targets: Record<string, Pick<Actions.WebhookTarget, 'url' | 'headers'>>): Actions.Schema => ({
11
+ triggers: {},
12
+ targets: Object.fromEntries(Object.entries(targets).map(([name, target]) => [name, { type: 'webhook', name, ...target }])),
13
+ })
14
+
15
+ const createChecker = (listVariables: () => Promise<Variable[]>) => {
16
+ const io = createTestOutput()
17
+ let requests = 0
18
+ const checker = new UndefinedActionsVariablesChecker(io.output, () => ({
19
+ listVariables: () => {
20
+ requests++
21
+ return listVariables()
22
+ },
23
+ }))
24
+ return { checker, io, requests: () => requests }
25
+ }
26
+
27
+ test('warns about each target with variables the engine does not define', async () => {
28
+ const { checker, io } = createChecker(async () => [
29
+ { name: 'HOST', value: 'example.com', source: 'DATABASE' },
30
+ { name: 'TOKEN', value: null, source: 'ENVIRONMENT' },
31
+ { name: 'EMPTY', value: '', source: 'DATABASE' },
32
+ ])
33
+
34
+ await checker.check(
35
+ project,
36
+ async () =>
37
+ createActions({
38
+ defined: { url: 'https://{{HOST}}/hook', headers: { Authorization: 'Bearer {{TOKEN}}' } },
39
+ partly: { url: 'https://{{HOST}}/{{MISSING}}', headers: { 'X-Empty': '{{EMPTY}}', 'X-Again': '{{MISSING}}' } },
40
+ plain: { url: 'https://example.com/hook' },
41
+ }),
42
+ )
43
+
44
+ expect(io.stderr.lines).toStrictEqual([
45
+ 'Warning: webhook target partly references undefined variables: MISSING, EMPTY. '
46
+ + 'Its events wait until they are set (actions set-variables, or the engine environment).',
47
+ ])
48
+ })
49
+
50
+ test('makes no request when no target references a variable', async () => {
51
+ const { checker, io, requests } = createChecker(async () => [])
52
+
53
+ await checker.check(project, async () => createActions({ plain: { url: 'https://example.com/hook' } }))
54
+
55
+ expect(requests()).toBe(0)
56
+ expect(io.stderr.lines).toStrictEqual([])
57
+ })
58
+
59
+ test('a failed variables request is a warning, not a failure', async () => {
60
+ const { checker, io } = createChecker(async () => {
61
+ throw new Error('Forbidden')
62
+ })
63
+
64
+ await checker.check(project, async () => createActions({ target: { url: 'https://{{HOST}}/hook' } }))
65
+
66
+ expect(io.stderr.lines).toStrictEqual(['Warning: could not check the variables webhook targets reference: Forbidden'])
67
+ })
68
+
69
+ test('a schema that cannot be built is a warning, not a failure', async () => {
70
+ const { checker, io, requests } = createChecker(async () => [])
71
+
72
+ await checker.check(project, async () => {
73
+ throw new Error('Unknown modification')
74
+ })
75
+
76
+ expect(requests()).toBe(0)
77
+ expect(io.stderr.lines).toStrictEqual(['Warning: could not check the variables webhook targets reference: Unknown modification'])
78
+ })