Skip to content

Commit 9798587

Browse files
alexluckettmokhlddavidjamesstonenayijAndrew-Folga
authored
Backport: runner-v2 (events and templates) (#745)
* Add version to release workflow * Chore: Backport forms runner V2 (#728) * PLATFORM: Add support for page events, custom views and injectable controllers (#13) * Add support for page onLoad event and providing a custom view * Inject controllers to plugin registration * Temporarily supress custom controller TS error * Pass controller through to FormModel (#16) * PLATFORM: Feature/page events payload (#22) * Add support for TerminalPageController * Update start page logic when using page conditions engine * Resolve page condition in controller constructor * Add page condition routing logic * Bump @defra/forms-model dep to 3.0.395 * Make engine optional on FormModel class * Add TerminalPageController test * Add QuetionPageController V2 tests * Refactor getFormContext to reduce cognitive complexity * Change logic for when the form Continue button is displayed for V2 * Support for injectable forms services * Use injected forms services in page controllers * Add defaultServices file * Simplify type where value is not used * fix: prevent 500s from malformed requests in crumb generation (#676) * fix: prevent 500s from malformed requests in crumb generation * test: added test for crumb state checks * refactor: split the check into helper function with tests * refactor: made helper function clearer * Structured emails for machine ingestion (#679) * extract notification service to a abstract service * machine output * Move v1 formatter into formatters folder * Move output formatters into separate folder * add form def to JSON output * switch outputFormatter based on condition * Refactor tests to support new formatters and injectable service * fix import path * Add tests for new macine output * simplify functions in machine formatter * Allow unused vars if they begin with underscore * Resolve jest-extended tsc errors * #504353 Update expiry in the notify email since it's been increased (#685) * Update expiry in the notify email since it's been increased * Update test * Support machine emails via form definition flag (#684) * use output format from form def and encode Notify JSON as base64 * Categorise outputs into main and repeaters * Add files to machine v1 output * Use 3.0.398 of model * use forms-designer path from config * Fix typo * Replace POST payload with structured data * Update page event payload to use the machine output format * Remove test page event from example grant * Temporarily suppress TS error --------- Co-authored-by: Alex Luckett <alexgluckett@gmail.com> Co-authored-by: Alex Luckett <alexluckett@users.noreply.github.com> Co-authored-by: Mohammed Khalid <emailmokhld@gmail.com> Co-authored-by: Jignesh Nayi <47786869+nayij@users.noreply.github.com> * PLATFORM: feature/templates (#33) * Remove toLowerCase from component schema message titles as they could contain interpolated variables * Add convenience maps onto FormContext * Add liquid templates * Add convenience maps onto FormContext type * Add liquid helpers to Nunjucks environment * Use liquid helpers in Nunjucks templates * Add types * Fix tests * Add templating documentation * Add Liquid helper tests * Add Liquid helper test definition file * Add TS comment exceptions * Remove page and field liquid filters * Add test showing liquid evaluation can use values from `context.data` * Update template README * Evaluate templates in check answers summary rows * Update nunjucks liquid filter * Add template journey test * Include summary rows title checks in template journey tests * Update Templates README * Add missing types * Enable tracing (#36) * Remove ts incremental build cache file and add to gitignore (#37) * PLATFORM: Add href, page, field and answer filters to nunjucks (#46) * added href and answer as nunjucks filters Signed-off-by: David Barker <david.barker@equalexperts.com> * added filters Signed-off-by: David Barker <david.barker@equalexperts.com> * modified index to include filters Signed-off-by: David Barker <david.barker@equalexperts.com> * Update nunjucks filters * Update documentation --------- Signed-off-by: David Barker <david.barker@equalexperts.com> Co-authored-by: davidjamesstone <davidjamesstone@gmail.com> * test: tests for answer filter * test: tests enviroment.js checkErrorTemplates * test: field.js test suite * test: href filter * refactor(test) * test: PageController coverage * test: test cases for when pageDef.view is present * test: test suite for page filter * test: test suite for logger-options * PLATFORM: Change tracing so it doesn't overwrite existing headers (#54) * Change tracing so it doesn't overwrite existing headers * Add tests to ensure additional headers are preserved * Merge headers inside applyTraceHeaders utility function * Replace merge with assign * Remove unused JSDoc declaration --------- Co-authored-by: Andrew Folga <andrew.folga@equalexperts.com> * test: evaluate and href filters * test: add coverage for falsy tracing header config * refactor: improve testability of applyTraceHeaders * test: add coverage for HTML component string content check * test: add coverage for Nunjucks evaluate function and component label handling * refactor(sonar) * fix: Add type assertion for custom page controllers --------- Signed-off-by: David Barker <david.barker@equalexperts.com> Co-authored-by: David Stone <davidjamesstone@gmail.com> Co-authored-by: Alex Luckett <alexgluckett@gmail.com> Co-authored-by: Alex Luckett <alexluckett@users.noreply.github.com> Co-authored-by: Jignesh Nayi <47786869+nayij@users.noreply.github.com> Co-authored-by: Andrew-Folga-EE <173707519+Andrew-Folga@users.noreply.github.com> Co-authored-by: David Barker <david.barker@equalexperts.com> Co-authored-by: Andrew Folga <andrew.folga@equalexperts.com> --------- Signed-off-by: David Barker <david.barker@equalexperts.com> Co-authored-by: Mohammed Khalid <emailmokhld@gmail.com> Co-authored-by: David Stone <davidjamesstone@gmail.com> Co-authored-by: Jignesh Nayi <47786869+nayij@users.noreply.github.com> Co-authored-by: Andrew-Folga-EE <173707519+Andrew-Folga@users.noreply.github.com> Co-authored-by: David Barker <david.barker@equalexperts.com> Co-authored-by: Andrew Folga <andrew.folga@equalexperts.com>
1 parent 65b9de2 commit 9798587

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2429
-105
lines changed

.github/workflows/publish.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ on:
1515
- '!**/*.test.*'
1616

1717
workflow_dispatch:
18+
inputs:
19+
version:
20+
description: 'Version number manual override'
21+
required: false
1822

1923
concurrency:
2024
group: publish
@@ -41,3 +45,4 @@ jobs:
4145
uses: DEFRA/cdp-build-action/build@main
4246
with:
4347
github-token: ${{ secrets.GITHUB_TOKEN }}
48+
version: ${{ inputs.version }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ coverage
99
.server
1010
.cache
1111
.env
12+
tsconfig.tsbuildinfo

jest.config.cjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ module.exports = {
4545
`node_modules/(?!${[
4646
'@defra/forms-model/.*',
4747
'nanoid', // Supports ESM only
48-
'slug' // Supports ESM only
48+
'slug', // Supports ESM only
49+
'@defra/hapi-tracing' // Supports ESM only|
4950
].join('|')}/)`
5051
],
5152
testTimeout: 10000,

package-lock.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"license": "SEE LICENSE IN LICENSE",
4242
"dependencies": {
4343
"@defra/forms-model": "^3.0.409",
44+
"@defra/hapi-tracing": "^1.0.0",
4445
"@elastic/ecs-pino-format": "^1.5.0",
4546
"@hapi/boom": "^10.0.1",
4647
"@hapi/catbox": "^12.1.1",
@@ -74,6 +75,7 @@
7475
"humanize-duration": "^3.32.1",
7576
"ioredis": "^5.4.2",
7677
"joi": "^17.13.3",
78+
"liquidjs": "^10.20.3",
7779
"lodash": "^4.17.21",
7880
"marked": "^15.0.6",
7981
"nunjucks": "^3.2.3",

src/config/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ export const config = convict({
152152
env: 'REDIS_KEY_PREFIX'
153153
} as SchemaObj<string>
154154
},
155+
tracing: {
156+
header: {
157+
doc: 'Tracing header name',
158+
format: String,
159+
default: 'x-cdp-request-id',
160+
env: 'TRACING_HEADER'
161+
} as SchemaObj<string>
162+
},
155163

156164
/**
157165
* Email outputs
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { getTraceId } from '@defra/hapi-tracing'
2+
3+
import { loggerOptions } from '~/src/server/common/helpers/logging/logger-options.js'
4+
5+
jest.mock('@defra/hapi-tracing', () => ({
6+
getTraceId: jest.fn()
7+
}))
8+
9+
describe('logger-options', () => {
10+
beforeEach(() => {
11+
jest.clearAllMocks()
12+
})
13+
14+
describe('configuration', () => {
15+
it('has the expected properties', () => {
16+
expect(loggerOptions).toHaveProperty('enabled')
17+
expect(loggerOptions).toHaveProperty('ignorePaths')
18+
expect(loggerOptions).toHaveProperty('redact')
19+
expect(loggerOptions).toHaveProperty('level')
20+
})
21+
22+
it('ignores health endpoint', () => {
23+
expect(loggerOptions.ignorePaths).toContain('/health')
24+
})
25+
26+
it('has redaction configuration', () => {
27+
expect(loggerOptions.redact).toBeDefined()
28+
})
29+
})
30+
31+
describe('mixin function', () => {
32+
it('returns an empty object when no trace ID is available', () => {
33+
jest.mocked(getTraceId).mockReturnValue(null)
34+
35+
const result = loggerOptions.mixin()
36+
expect(result).toEqual({})
37+
})
38+
39+
it('includes trace ID when available', () => {
40+
jest.mocked(getTraceId).mockReturnValue('some-trace-id')
41+
42+
const result = loggerOptions.mixin()
43+
expect(result).toEqual({
44+
trace: {
45+
id: 'some-trace-id'
46+
}
47+
})
48+
})
49+
})
50+
})

src/server/common/helpers/logging/logger-options.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getTraceId } from '@defra/hapi-tracing'
12
import { ecsFormat } from '@elastic/ecs-pino-format'
23
import { type Options } from 'hapi-pino'
34
import { type LoggerOptions, type TransportSingleOptions } from 'pino'
@@ -33,5 +34,13 @@ export const loggerOptions = {
3334
remove: true
3435
},
3536
level: logConfig.level,
36-
...formatters[logConfig.format]
37+
...formatters[logConfig.format],
38+
mixin() {
39+
const mixinValues: { trace?: { id: string } } = {}
40+
const traceId = getTraceId()
41+
if (traceId) {
42+
mixinValues.trace = { id: traceId }
43+
}
44+
return mixinValues
45+
}
3746
} satisfies Options
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { tracing } from '@defra/hapi-tracing'
2+
3+
import { config } from '~/src/config/index.js'
4+
5+
// Explicitly declare the expected type
6+
7+
export const requestTracing = {
8+
plugin: tracing,
9+
options: { tracingHeader: config.get('tracing').header }
10+
}

src/server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ProxyAgent } from 'proxy-agent'
1414

1515
import { config } from '~/src/config/index.js'
1616
import { requestLogger } from '~/src/server/common/helpers/logging/request-logger.js'
17+
import { requestTracing } from '~/src/server/common/helpers/logging/request-tracing.js'
1718
import { buildRedisClient } from '~/src/server/common/helpers/redis-client.js'
1819
import { configureBlankiePlugin } from '~/src/server/plugins/blankie.js'
1920
import { configureCrumbPlugin } from '~/src/server/plugins/crumb.js'
@@ -124,6 +125,7 @@ export async function createServer(routeConfig?: RouteConfig) {
124125
await server.register(pluginRouter)
125126
await server.register(pluginErrorPages)
126127
await server.register(blipp)
128+
await server.register(requestTracing)
127129

128130
server.state('cookieConsent', {
129131
ttl: 365 * 24 * 60 * 60 * 1000, // 1 year in ms

0 commit comments

Comments
 (0)