feat: [#8920] Add nunjucksFilters option for user-provided filters
Signed-off-by: David Zemon <david@zemon.name>
This commit is contained in:
@@ -93,6 +93,9 @@ export interface SecureTemplaterOptions {
|
||||
|
||||
/* Enables jinja compatibility and the "jsonify" filter */
|
||||
cookiecutterCompat?: boolean;
|
||||
|
||||
/* Extra user-provided nunjucks filters */
|
||||
additionalFilters?: Record<string, (data: any) => any>;
|
||||
}
|
||||
|
||||
export type SecureTemplateRenderer = (
|
||||
@@ -102,7 +105,7 @@ export type SecureTemplateRenderer = (
|
||||
|
||||
export class SecureTemplater {
|
||||
static async loadRenderer(options: SecureTemplaterOptions = {}) {
|
||||
const { parseRepoUrl, cookiecutterCompat } = options;
|
||||
const { parseRepoUrl, cookiecutterCompat, additionalFilters } = options;
|
||||
let sandbox = undefined;
|
||||
|
||||
if (parseRepoUrl) {
|
||||
@@ -110,6 +113,10 @@ export class SecureTemplater {
|
||||
parseRepoUrl: (url: string) => JSON.stringify(parseRepoUrl(url)),
|
||||
};
|
||||
}
|
||||
sandbox = {
|
||||
...sandbox,
|
||||
...additionalFilters,
|
||||
};
|
||||
|
||||
const vm = new VM({ sandbox });
|
||||
|
||||
|
||||
@@ -53,9 +53,16 @@ export const createBuiltinActions = (options: {
|
||||
catalogClient: CatalogApi;
|
||||
containerRunner?: ContainerRunner;
|
||||
config: Config;
|
||||
nunjucksFilters?: Record<string, (data: any) => any>;
|
||||
}) => {
|
||||
const { reader, integrations, containerRunner, catalogClient, config } =
|
||||
options;
|
||||
const {
|
||||
reader,
|
||||
integrations,
|
||||
containerRunner,
|
||||
catalogClient,
|
||||
config,
|
||||
nunjucksFilters,
|
||||
} = options;
|
||||
const githubCredentialsProvider: GithubCredentialsProvider =
|
||||
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
|
||||
|
||||
@@ -67,6 +74,7 @@ export const createBuiltinActions = (options: {
|
||||
createFetchTemplateAction({
|
||||
integrations,
|
||||
reader,
|
||||
nunjucksFilters,
|
||||
}),
|
||||
createPublishGithubAction({
|
||||
integrations,
|
||||
|
||||
@@ -44,8 +44,9 @@ export type FetchTemplateInput = {
|
||||
export function createFetchTemplateAction(options: {
|
||||
reader: UrlReader;
|
||||
integrations: ScmIntegrations;
|
||||
nunjucksFilters?: Record<string, (data: any) => any>;
|
||||
}) {
|
||||
const { reader, integrations } = options;
|
||||
const { reader, integrations, nunjucksFilters } = options;
|
||||
|
||||
return createTemplateAction<FetchTemplateInput>({
|
||||
id: 'fetch:template',
|
||||
@@ -182,6 +183,7 @@ export function createFetchTemplateAction(options: {
|
||||
|
||||
const renderTemplate = await SecureTemplater.loadRenderer({
|
||||
cookiecutterCompat: ctx.input.cookiecutterCompat,
|
||||
additionalFilters: nunjucksFilters,
|
||||
});
|
||||
|
||||
for (const location of allEntriesInTemplate) {
|
||||
|
||||
@@ -44,6 +44,7 @@ type NunjucksWorkflowRunnerOptions = {
|
||||
actionRegistry: TemplateActionRegistry;
|
||||
integrations: ScmIntegrations;
|
||||
logger: winston.Logger;
|
||||
additionalFilters?: Record<string, (data: any) => any>;
|
||||
};
|
||||
|
||||
type TemplateContext = {
|
||||
@@ -190,6 +191,7 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
|
||||
parseRepoUrl(url: string) {
|
||||
return parseRepoUrl(url, integrations);
|
||||
},
|
||||
additionalFilters: this.options.additionalFilters,
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
@@ -46,6 +46,7 @@ export type CreateWorkerOptions = {
|
||||
integrations: ScmIntegrations;
|
||||
workingDirectory: string;
|
||||
logger: Logger;
|
||||
nunjucksFilters?: Record<string, (data: any) => any>;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -63,6 +64,7 @@ export class TaskWorker {
|
||||
actionRegistry,
|
||||
integrations,
|
||||
workingDirectory,
|
||||
nunjucksFilters,
|
||||
} = options;
|
||||
|
||||
const legacyWorkflowRunner = new HandlebarsWorkflowRunner({
|
||||
@@ -77,6 +79,7 @@ export class TaskWorker {
|
||||
integrations,
|
||||
logger,
|
||||
workingDirectory,
|
||||
additionalFilters: nunjucksFilters,
|
||||
});
|
||||
|
||||
return new TaskWorker({
|
||||
|
||||
@@ -57,6 +57,7 @@ export interface RouterOptions {
|
||||
taskWorkers?: number;
|
||||
containerRunner?: ContainerRunner;
|
||||
taskBroker?: TaskBroker;
|
||||
nunjucksFilters?: Record<string, (data: any) => any>;
|
||||
}
|
||||
|
||||
function isSupportedTemplate(
|
||||
@@ -83,6 +84,7 @@ export async function createRouter(
|
||||
actions,
|
||||
containerRunner,
|
||||
taskWorkers,
|
||||
nunjucksFilters,
|
||||
} = options;
|
||||
|
||||
const logger = parentLogger.child({ plugin: 'scaffolder' });
|
||||
@@ -110,6 +112,7 @@ export async function createRouter(
|
||||
integrations,
|
||||
logger,
|
||||
workingDirectory,
|
||||
nunjucksFilters,
|
||||
});
|
||||
workers.push(worker);
|
||||
}
|
||||
@@ -122,6 +125,7 @@ export async function createRouter(
|
||||
containerRunner,
|
||||
reader,
|
||||
config,
|
||||
nunjucksFilters,
|
||||
});
|
||||
|
||||
actionsToRegister.forEach(action => actionRegistry.register(action));
|
||||
|
||||
Reference in New Issue
Block a user