feat: Add 'and', 'or' and 'containsString' to handlebar helpers (#13341)

This commit is contained in:
Marina 2022-01-06 16:24:42 +01:00 committed by GitHub
parent 8aadfbce21
commit 7770888184
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 0 deletions

View file

@ -36,3 +36,23 @@ If you want to print pretty JSON with Handlebars you can use the built-in functi
`{{{stringToPrettyJSON myvar}}}`
In the example above `myvar` is a variable/field, that contains valid JSON.
### containsString
Returns `true` if a given string is a substring.
`{{#if (containsString depName 'python')}}Python{{else}}Other{{/if}}`
### and
Returns `true` only if all expressions are `true`.
`{{#if (and isMajor hasReleaseNotes)}}Backwards Incompatible release! Check out the Release notes.{{/if}}`
In the example above, it will only show a text if `isMajor=true` and `hasReleaseNotes=true`.
### or
Returns `true` if at least one expression is `true`.
`{{#if (or isPatch isSingleVersion}}Small update, safer to merge and release.{{else}}Check out the changelog for all versions before merging!{{/if}}`

View file

@ -18,6 +18,48 @@ describe('util/template/index', () => {
expect(output).toContain('github');
expect(output).not.toContain('123test');
});
it('containsString', () => {
const userTemplate =
"{{#if (containsString platform 'git')}}True{{else}}False{{/if}}";
const input = { platform: 'github' };
const output = template.compile(userTemplate, input);
expect(output).toContain('True');
});
it('not containsString', () => {
const userTemplate =
"{{#if (containsString platform 'hub')}}True{{else}}False{{/if}}";
const input = { platform: 'gitlab' };
const output = template.compile(userTemplate, input);
expect(output).toContain('False');
});
it('and returns true when all parameters are true', () => {
const userTemplate =
'{{#if (and isMajor isSingleVersion isReplacement)}}True{{else}}False{{/if}}';
const input = { isMajor: true, isSingleVersion: true, isReplacement: true };
const output = template.compile(userTemplate, input);
expect(output).toContain('True');
});
it('and returns false when at least one parameter is false', () => {
const userTemplate =
'{{#if (and isMajor isPatch isGithub)}}True{{else}}False{{/if}}';
const input = { isMajor: true, isPatch: false, isReplacement: true };
const output = template.compile(userTemplate, input);
expect(output).toContain('False');
});
it('or returns true when at least one is true', () => {
const userTemplate =
'{{#if (or isMajor isPatch isReplacement)}}True{{else}}False{{/if}}';
const input = { isMajor: false, isPatch: true, isReplacement: false };
const output = template.compile(userTemplate, input);
expect(output).toContain('True');
});
it('or returns false when all are false', () => {
const userTemplate =
'{{#if (or isMajor isPatch isReplacement)}}True{{else}}False{{/if}}';
const input = { isMajor: false, isPatch: false, isReplacement: false };
const output = template.compile(userTemplate, input);
expect(output).toContain('False');
});
it('string to pretty JSON ', () => {
const userTemplate =
'{{{ stringToPrettyJSON \'{"some":{"fancy":"json"}}\'}}}';

View file

@ -17,6 +17,25 @@ handlebars.registerHelper(
(context || '').replace(new RegExp(find, 'g'), replace) // TODO #12873
);
handlebars.registerHelper('containsString', (str, subStr, options) =>
str.includes(subStr)
);
handlebars.registerHelper({
and(...args) {
// Need to remove the 'options', as last parameter
// https://handlebarsjs.com/api-reference/helpers.html
args.pop();
return args.every(Boolean);
},
or(...args) {
// Need to remove the 'options', as last parameter
// https://handlebarsjs.com/api-reference/helpers.html
args.pop();
return args.some(Boolean);
},
});
export const exposedConfigOptions = [
'additionalBranchPrefix',
'addLabels',