2023-02-24 08:53:51 -08:00
|
|
|
import {convertWorkflowTemplate, parseWorkflow, ParseWorkflowResult, WorkflowTemplate} from "@actions/workflow-parser";
|
|
|
|
|
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
|
|
|
|
2023-03-03 12:49:25 -08:00
|
|
|
import {CompletionConfig} from "../complete";
|
2023-02-24 08:53:51 -08:00
|
|
|
import {nullTrace} from "../nulltrace";
|
2023-03-03 12:28:29 -08:00
|
|
|
|
|
|
|
|
const parsedWorkflowCache = new Map<string, ParseWorkflowResult>();
|
|
|
|
|
const workflowTemplateCache = new Map<string, WorkflowTemplate>();
|
|
|
|
|
|
2023-03-06 15:16:26 -08:00
|
|
|
export function clearCacheEntry(uri: string) {
|
2023-03-03 12:28:29 -08:00
|
|
|
parsedWorkflowCache.delete(uri);
|
2023-03-21 15:52:25 -04:00
|
|
|
parsedWorkflowCache.delete(workflowKey(uri, true));
|
2023-03-03 12:28:29 -08:00
|
|
|
workflowTemplateCache.delete(uri);
|
2023-03-21 15:52:25 -04:00
|
|
|
workflowTemplateCache.delete(workflowKey(uri, true));
|
2023-03-03 12:28:29 -08:00
|
|
|
}
|
|
|
|
|
|
2023-03-06 15:16:26 -08:00
|
|
|
export function clearCache() {
|
|
|
|
|
parsedWorkflowCache.clear();
|
2023-03-03 12:28:29 -08:00
|
|
|
workflowTemplateCache.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 15:52:25 -04:00
|
|
|
/**
|
|
|
|
|
* Parses a workflow file and caches the result
|
|
|
|
|
* @param transformed Indicates whether the workflow has been transformed before parsing
|
|
|
|
|
* @returns the {@link ParseWorkflowResult}
|
|
|
|
|
*/
|
|
|
|
|
export function fetchOrParseWorkflow(file: File, uri: string, transformed = false): ParseWorkflowResult {
|
|
|
|
|
const key = workflowKey(uri, transformed);
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* Converts a workflow template and caches the result
|
|
|
|
|
* @param transformed Indicates whether the workflow has been transformed before parsing
|
|
|
|
|
* @returns the converted {@link WorkflowTemplate}
|
|
|
|
|
*/
|
2023-03-03 12:49:25 -08:00
|
|
|
export async function fetchOrConvertWorkflowTemplate(
|
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> {
|
2023-03-21 15:52:25 -04:00
|
|
|
const key = workflowKey(uri, transformed);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// Use a separate cache key for transformed workflows
|
|
|
|
|
function workflowKey(uri: string, transformed: boolean): string {
|
|
|
|
|
if (transformed) {
|
|
|
|
|
return `transformed-${uri}`;
|
|
|
|
|
}
|
|
|
|
|
return uri;
|
|
|
|
|
}
|