* add helper functions for generating Open API path/query params with defaults * tweak Open API schema - make description optional - allow null example + allowEmptyValue (for boolean query params) * convert examples --> openApi in amo * convert examples --> openApi in ansible * convert examples --> openApi in appveyor build/job * add re-usable Open API query param for test-results badges we can use these for all the 'test results' badges * convert examples --> openApi in appveyor tests * DRY up existing dynamic/endpoint param definitions * DRY up queryParam * allow enum param in serviceDefinition schema * improve misleading param name * check route and openApi are consistent on service load * fix mistake in ansible role route * documentation --> description * add pathParams and queryParams helpers +docstrings * give everything a search-friendly summary, check for duplicate summary * prettier fixup
126 lines
2.7 KiB
JavaScript
126 lines
2.7 KiB
JavaScript
import Joi from 'joi'
|
|
import { queryParams } from './index.js'
|
|
|
|
const testResultQueryParamSchema = Joi.object({
|
|
compact_message: Joi.equal(''),
|
|
passed_label: Joi.string(),
|
|
failed_label: Joi.string(),
|
|
skipped_label: Joi.string(),
|
|
}).required()
|
|
|
|
const testResultOpenApiQueryParams = queryParams(
|
|
{
|
|
name: 'compact_message',
|
|
example: null,
|
|
schema: { type: 'boolean' },
|
|
},
|
|
{ name: 'passed_label', example: 'good' },
|
|
{ name: 'failed_label', example: 'bad' },
|
|
{ name: 'skipped_label', example: 'n/a' },
|
|
)
|
|
|
|
function renderTestResultMessage({
|
|
passed,
|
|
failed,
|
|
skipped,
|
|
total,
|
|
passedLabel,
|
|
failedLabel,
|
|
skippedLabel,
|
|
isCompact,
|
|
}) {
|
|
const labels = { passedLabel, failedLabel, skippedLabel }
|
|
if (total === 0) {
|
|
return 'no tests'
|
|
} else if (isCompact) {
|
|
;({ passedLabel = '✔', failedLabel = '✘', skippedLabel = '➟' } = labels)
|
|
return [
|
|
`${passedLabel} ${passed}`,
|
|
failed > 0 && `${failedLabel} ${failed}`,
|
|
skipped > 0 && `${skippedLabel} ${skipped}`,
|
|
]
|
|
.filter(Boolean)
|
|
.join(' | ')
|
|
} else {
|
|
;({
|
|
passedLabel = 'passed',
|
|
failedLabel = 'failed',
|
|
skippedLabel = 'skipped',
|
|
} = labels)
|
|
return [
|
|
`${passed} ${passedLabel}`,
|
|
failed > 0 && `${failed} ${failedLabel}`,
|
|
skipped > 0 && `${skipped} ${skippedLabel}`,
|
|
]
|
|
.filter(Boolean)
|
|
.join(', ')
|
|
}
|
|
}
|
|
|
|
function renderTestResultBadge({
|
|
passed,
|
|
failed,
|
|
skipped,
|
|
total,
|
|
passedLabel,
|
|
failedLabel,
|
|
skippedLabel,
|
|
isCompact,
|
|
}) {
|
|
const message = renderTestResultMessage({
|
|
passed,
|
|
failed,
|
|
skipped,
|
|
total,
|
|
passedLabel,
|
|
failedLabel,
|
|
skippedLabel,
|
|
isCompact,
|
|
})
|
|
|
|
let color
|
|
if (total === 0) {
|
|
color = 'yellow'
|
|
} else if (failed > 0) {
|
|
color = 'red'
|
|
} else if (skipped > 0 && passed > 0) {
|
|
color = 'green'
|
|
} else if (skipped > 0) {
|
|
color = 'yellow'
|
|
} else {
|
|
color = 'brightgreen'
|
|
}
|
|
|
|
return { message, color }
|
|
}
|
|
|
|
const documentation = `
|
|
<p>
|
|
You may change the "passed", "failed" and "skipped" text on this badge by supplying query parameters <code>&passed_label=</code>, <code>&failed_label=</code> and <code>&skipped_label=</code> respectively.
|
|
</p>
|
|
|
|
<p>
|
|
For example, if you want to use a different terminology:
|
|
<br />
|
|
<code>?passed_label=good&failed_label=bad&skipped_label=n%2Fa</code>
|
|
</p>
|
|
|
|
<p>
|
|
Or symbols:
|
|
<br />
|
|
<code>?compact_message&passed_label=💃&failed_label=🤦♀️&skipped_label=🤷</code>
|
|
</p>
|
|
|
|
<p>
|
|
There is also a <code>&compact_message</code> query parameter, which will default to displaying ✔, ✘ and ➟, separated by a horizontal bar |.
|
|
</p>
|
|
`
|
|
|
|
export {
|
|
testResultQueryParamSchema,
|
|
testResultOpenApiQueryParams,
|
|
renderTestResultMessage,
|
|
renderTestResultBadge,
|
|
documentation,
|
|
}
|