2026-01-02 10:38:52 -06:00
|
|
|
import {convertWorkflowTemplate, parseWorkflow, TemplateParseResult, WorkflowTemplate} from "@actions/workflow-parser";
|
|
|
|
|
import {parseAction} from "@actions/workflow-parser/actions/action-parser";
|
|
|
|
|
import {
|
|
|
|
|
ActionTemplate,
|
|
|
|
|
ActionTemplateConverterOptions,
|
|
|
|
|
convertActionTemplate
|
|
|
|
|
} from "@actions/workflow-parser/actions/action-template";
|
2023-02-24 08:53:51 -08:00
|
|
|
import {WorkflowTemplateConverterOptions} from "@actions/workflow-parser/model/convert";
|
|
|
|
|
import {TemplateContext} from "@actions/workflow-parser/templates/template-context";
|
|
|
|
|
import {TemplateToken} from "@actions/workflow-parser/templates/tokens/template-token";
|
|
|
|
|
import {File} from "@actions/workflow-parser/workflows/file";
|
2023-03-17 13:23:43 -04:00
|
|
|
|
2025-12-18 13:35:48 -06:00
|
|
|
import {CompletionConfig} from "../complete.js";
|
|
|
|
|
import {nullTrace} from "../nulltrace.js";
|
2023-03-03 12:28:29 -08:00
|
|
|
|
2026-01-02 10:38:52 -06:00
|
|
|
const parsedWorkflowCache = new Map<string, TemplateParseResult>();
|
|
|
|
|
const parsedActionCache = new Map<string, TemplateParseResult>();
|
2023-03-03 12:28:29 -08:00
|
|
|
const workflowTemplateCache = new Map<string, WorkflowTemplate>();
|
2026-01-02 10:38:52 -06:00
|
|
|
const actionTemplateCache = new Map<string, ActionTemplate>();
|
2023-03-03 12:28:29 -08:00
|
|
|
|
2023-03-06 15:16:26 -08:00
|
|
|
export function clearCacheEntry(uri: string) {
|
2023-03-03 12:28:29 -08:00
|
|
|
parsedWorkflowCache.delete(uri);
|
2026-01-02 10:38:52 -06:00
|
|
|
parsedWorkflowCache.delete(cacheKey(uri, true));
|
|
|
|
|
parsedActionCache.delete(uri);
|
|
|
|
|
parsedActionCache.delete(cacheKey(uri, true));
|
2023-03-03 12:28:29 -08:00
|
|
|
workflowTemplateCache.delete(uri);
|
2026-01-02 10:38:52 -06:00
|
|
|
workflowTemplateCache.delete(cacheKey(uri, true));
|
|
|
|
|
actionTemplateCache.delete(uri);
|
|
|
|
|
actionTemplateCache.delete(cacheKey(uri, true));
|
2023-03-03 12:28:29 -08:00
|
|
|
}
|
|
|
|
|
|
2023-03-06 15:16:26 -08:00
|
|
|
export function clearCache() {
|
|
|
|
|
parsedWorkflowCache.clear();
|
2026-01-02 10:38:52 -06:00
|
|
|
parsedActionCache.clear();
|
2023-03-03 12:28:29 -08:00
|
|
|
workflowTemplateCache.clear();
|
2026-01-02 10:38:52 -06:00
|
|
|
actionTemplateCache.clear();
|
2023-03-03 12:28:29 -08:00
|
|
|
}
|
|
|
|
|
|
2023-03-21 15:52:25 -04:00
|
|
|
/**
|
2026-01-02 10:38:52 -06:00
|
|
|
* Parses a workflow file, returning cached result if available
|
2023-03-21 15:52:25 -04:00
|
|
|
* @param transformed Indicates whether the workflow has been transformed before parsing
|
2026-01-02 10:38:52 -06:00
|
|
|
* @returns the {@link TemplateParseResult}
|
2023-03-21 15:52:25 -04:00
|
|
|
*/
|
2026-01-02 10:38:52 -06:00
|
|
|
export function getOrParseWorkflow(file: File, uri: string, transformed = false): TemplateParseResult {
|
|
|
|
|
const key = cacheKey(uri, transformed);
|
2023-03-21 15:52:25 -04:00
|
|
|
const cachedResult = parsedWorkflowCache.get(key);
|
2023-03-14 11:43:28 -04:00
|
|
|
if (cachedResult) {
|
|
|
|
|
return cachedResult;
|
2023-03-03 12:28:29 -08:00
|
|
|
}
|
2023-03-14 11:43:28 -04:00
|
|
|
const result = parseWorkflow(file, nullTrace);
|
2023-03-21 15:52:25 -04:00
|
|
|
parsedWorkflowCache.set(key, result);
|
2023-03-03 12:28:29 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 15:52:25 -04:00
|
|
|
/**
|
2026-01-02 10:38:52 -06:00
|
|
|
* Parses an action file, returning cached result if available
|
|
|
|
|
* @param transformed Indicates whether the action has been transformed before parsing
|
|
|
|
|
* @returns the {@link TemplateParseResult}
|
|
|
|
|
*/
|
|
|
|
|
export function getOrParseAction(file: File, uri: string, transformed = false): TemplateParseResult {
|
|
|
|
|
const key = cacheKey(uri, transformed);
|
|
|
|
|
const cachedResult = parsedActionCache.get(key);
|
|
|
|
|
if (cachedResult) {
|
|
|
|
|
return cachedResult;
|
|
|
|
|
}
|
|
|
|
|
const result = parseAction(file, nullTrace);
|
|
|
|
|
parsedActionCache.set(key, result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts a workflow template, returning cached result if available
|
2023-03-21 15:52:25 -04:00
|
|
|
* @param transformed Indicates whether the workflow has been transformed before parsing
|
|
|
|
|
* @returns the converted {@link WorkflowTemplate}
|
|
|
|
|
*/
|
2026-01-02 10:38:52 -06:00
|
|
|
export async function getOrConvertWorkflowTemplate(
|
2023-03-17 13:23:43 -04:00
|
|
|
context: TemplateContext,
|
|
|
|
|
template: TemplateToken,
|
2023-03-03 12:49:25 -08:00
|
|
|
uri: string,
|
|
|
|
|
config?: CompletionConfig,
|
2023-03-21 15:52:25 -04:00
|
|
|
options?: WorkflowTemplateConverterOptions,
|
|
|
|
|
transformed = false
|
2023-03-03 12:49:25 -08:00
|
|
|
): Promise<WorkflowTemplate> {
|
2026-01-02 10:38:52 -06:00
|
|
|
const key = cacheKey(uri, transformed);
|
2023-03-21 15:52:25 -04:00
|
|
|
const cachedTemplate = workflowTemplateCache.get(key);
|
2023-03-17 13:23:43 -04:00
|
|
|
if (cachedTemplate) {
|
|
|
|
|
return cachedTemplate;
|
2023-03-03 12:28:29 -08:00
|
|
|
}
|
2023-03-17 13:23:43 -04:00
|
|
|
const workflowTemplate = await convertWorkflowTemplate(context, template, config?.fileProvider, options);
|
2023-03-21 15:52:25 -04:00
|
|
|
workflowTemplateCache.set(key, workflowTemplate);
|
2023-03-17 13:23:43 -04:00
|
|
|
return workflowTemplate;
|
2023-03-03 12:49:25 -08:00
|
|
|
}
|
2023-03-21 15:52:25 -04:00
|
|
|
|
2026-01-02 10:38:52 -06:00
|
|
|
/**
|
|
|
|
|
* Converts an action template, returning cached result if available
|
|
|
|
|
* @param transformed Indicates whether the action has been transformed before parsing
|
|
|
|
|
* @returns the converted {@link ActionTemplate}
|
|
|
|
|
*/
|
|
|
|
|
export function getOrConvertActionTemplate(
|
|
|
|
|
context: TemplateContext,
|
|
|
|
|
template: TemplateToken,
|
|
|
|
|
uri: string,
|
|
|
|
|
options?: ActionTemplateConverterOptions,
|
|
|
|
|
transformed = false
|
|
|
|
|
): ActionTemplate {
|
|
|
|
|
const key = cacheKey(uri, transformed);
|
|
|
|
|
const cachedTemplate = actionTemplateCache.get(key);
|
|
|
|
|
if (cachedTemplate) {
|
|
|
|
|
return cachedTemplate;
|
|
|
|
|
}
|
|
|
|
|
const actionTemplate = convertActionTemplate(context, template, options);
|
|
|
|
|
actionTemplateCache.set(key, actionTemplate);
|
|
|
|
|
return actionTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use a separate cache key for transformed documents
|
|
|
|
|
function cacheKey(uri: string, transformed: boolean): string {
|
2023-03-21 15:52:25 -04:00
|
|
|
if (transformed) {
|
|
|
|
|
return `transformed-${uri}`;
|
|
|
|
|
}
|
|
|
|
|
return uri;
|
|
|
|
|
}
|