Skip to content
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

feat(jsonata): validation, caching, better logging #31832

Merged
merged 14 commits into from
Oct 8, 2024
33 changes: 29 additions & 4 deletions lib/modules/datasource/custom/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('modules/datasource/custom/index', () => {
expect(result).toEqual(expected);
});

it('returns null if transformation using jsonata rules fail', async () => {
it('returns null if transformation compilation using jsonata fails', async () => {
httpMock
.scope('https://example.com')
.get('/v1')
Expand All @@ -248,9 +248,34 @@ describe('modules/datasource/custom/index', () => {
},
});
expect(result).toBeNull();
expect(logger.debug).toHaveBeenCalledWith(
{ err: expect.any(Object), transformTemplate: '$[.name = "Alice" and' },
'Error while transforming response',
expect(logger.once.warn).toHaveBeenCalledWith(
{ errorMessage: 'The symbol "." cannot be used as a unary operator' },
'Error while compiling JSONata expression: $[.name = "Alice" and',
);
});

it('returns null if jsonata expression evaluation fails', async () => {
httpMock
.scope('https://example.com')
.get('/v1')
.reply(200, '1.0.0 \n2.0.0 \n 3.0.0 ', {
'Content-Type': 'text/plain',
});
const result = await getPkgReleases({
datasource: `${CustomDatasource.id}.foo`,
packageName: 'myPackage',
customDatasources: {
foo: {
defaultRegistryUrlTemplate: 'https://example.com/v1',
transformTemplates: ['$notafunction()'],
format: 'plain',
},
},
});
expect(result).toBeNull();
expect(logger.once.warn).toHaveBeenCalledWith(
{ err: expect.any(Object) },
'Error while evaluating JSONata expression: $notafunction()',
);
});

Expand Down
19 changes: 14 additions & 5 deletions lib/modules/datasource/custom/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import is from '@sindresorhus/is';
import jsonata from 'jsonata';
import { logger } from '../../../logger';
import * as jsonata from '../../../util/jsonata';
import { Datasource } from '../datasource';
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
import { fetchers } from './formats';
Expand Down Expand Up @@ -46,13 +46,22 @@ export class CustomDatasource extends Datasource {
logger.trace({ data }, `Custom manager fetcher '${format}' returned data.`);

for (const transformTemplate of transformTemplates) {
const expression = jsonata.getExpression(transformTemplate);

if (expression instanceof Error) {
logger.once.warn(
{ errorMessage: expression.message },
`Error while compiling JSONata expression: ${transformTemplate}`,
);
return null;
}

try {
const expression = jsonata(transformTemplate);
data = await expression.evaluate(data);
} catch (err) {
rarkins marked this conversation as resolved.
Show resolved Hide resolved
logger.debug(
{ err, transformTemplate },
'Error while transforming response',
logger.once.warn(
{ err },
`Error while evaluating JSONata expression: ${transformTemplate}`,
);
return null;
}
Expand Down
19 changes: 19 additions & 0 deletions lib/util/jsonata.ts
rarkins marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import jsonata from 'jsonata';
import * as memCache from './cache/memory';

export function getExpression(input: string): jsonata.Expression | Error {
rarkins marked this conversation as resolved.
Show resolved Hide resolved
const cacheKey = `jsonata:${input}`;
rarkins marked this conversation as resolved.
Show resolved Hide resolved
const cachedExpression = memCache.get(cacheKey);
if (cachedExpression) {
return cachedExpression;

Check warning on line 8 in lib/util/jsonata.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/jsonata.ts#L8

Added line #L8 was not covered by tests
}
let result: jsonata.Expression | Error;
try {
result = jsonata(input);
} catch (err) {
// JSONata errors aren't detected as TypeOf Error
result = new Error(err.message ?? 'Unknown JSONata error');
}
memCache.set(cacheKey, result);
return result;
}
Loading