Handle undefined templates in workflow template cache

This commit is contained in:
Josh Gross
2023-03-17 13:23:43 -04:00
parent a8a68a8028
commit 8942a05502
5 changed files with 51 additions and 31 deletions
+9 -3
View File
@@ -66,14 +66,20 @@ export async function complete(
}; };
const parsedWorkflow = fetchOrParseWorkflow(file, textDocument.uri); const parsedWorkflow = fetchOrParseWorkflow(file, textDocument.uri);
if (!parsedWorkflow) { if (!parsedWorkflow?.value) {
return []; return [];
} }
const template = await fetchOrConvertWorkflowTemplate(parsedWorkflow, textDocument.uri, config, { const template = await fetchOrConvertWorkflowTemplate(
parsedWorkflow.context,
parsedWorkflow.value,
textDocument.uri,
config,
{
fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0, fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0,
errorPolicy: ErrorPolicy.TryConversion errorPolicy: ErrorPolicy.TryConversion
}); }
);
const {token, keyToken, parent, path} = findToken(newPos, parsedWorkflow.value); const {token, keyToken, parent, path} = findToken(newPos, parsedWorkflow.value);
const workflowContext = getWorkflowContext(textDocument.uri, template, path); const workflowContext = getWorkflowContext(textDocument.uri, template, path);
+9 -3
View File
@@ -16,13 +16,19 @@ export async function documentLinks(document: TextDocument, workspace: string |
}; };
const parsedWorkflow = fetchOrParseWorkflow(file, document.uri); const parsedWorkflow = fetchOrParseWorkflow(file, document.uri);
if (!parsedWorkflow) { if (!parsedWorkflow?.value) {
return []; return [];
} }
const template = await fetchOrConvertWorkflowTemplate(parsedWorkflow, document.uri, undefined, { const template = await fetchOrConvertWorkflowTemplate(
parsedWorkflow.context,
parsedWorkflow.value,
document.uri,
undefined,
{
errorPolicy: ErrorPolicy.TryConversion errorPolicy: ErrorPolicy.TryConversion
}); }
);
const links: DocumentLink[] = []; const links: DocumentLink[] = [];
+9 -3
View File
@@ -44,14 +44,20 @@ export async function hover(document: TextDocument, position: Position, config?:
}; };
const parsedWorkflow = fetchOrParseWorkflow(file, document.uri); const parsedWorkflow = fetchOrParseWorkflow(file, document.uri);
if (!parsedWorkflow) { if (!parsedWorkflow?.value) {
return null; return null;
} }
const template = await fetchOrConvertWorkflowTemplate(parsedWorkflow, document.uri, config, { const template = await fetchOrConvertWorkflowTemplate(
parsedWorkflow.context,
parsedWorkflow.value,
document.uri,
config,
{
errorPolicy: ErrorPolicy.TryConversion, errorPolicy: ErrorPolicy.TryConversion,
fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0 fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0
}); }
);
const tokenResult = findToken(position, parsedWorkflow.value); const tokenResult = findToken(position, parsedWorkflow.value);
const {token, keyToken, parent} = tokenResult; const {token, keyToken, parent} = tokenResult;
+18 -16
View File
@@ -1,8 +1,14 @@
import {convertWorkflowTemplate, parseWorkflow} from "@github/actions-workflow-parser"; import {
import {WorkflowTemplateConverterOptions} from "@github/actions-workflow-parser/model/convert"; convertWorkflowTemplate,
parseWorkflow,
ParseWorkflowResult,
WorkflowTemplate
} from "@github/actions-workflow-parser";
import {File} from "@github/actions-workflow-parser/workflows/file"; import {File} from "@github/actions-workflow-parser/workflows/file";
import {ParseWorkflowResult} from "@github/actions-workflow-parser"; import {WorkflowTemplateConverterOptions} from "@github/actions-workflow-parser/model/convert";
import {WorkflowTemplate} from "@github/actions-workflow-parser"; import {TemplateContext} from "@github/actions-workflow-parser/templates/template-context";
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
import {nullTrace} from "../nulltrace"; import {nullTrace} from "../nulltrace";
import {CompletionConfig} from "../complete"; import {CompletionConfig} from "../complete";
@@ -30,21 +36,17 @@ export function fetchOrParseWorkflow(file: File, uri: string): ParseWorkflowResu
} }
export async function fetchOrConvertWorkflowTemplate( export async function fetchOrConvertWorkflowTemplate(
parsedWorkflow: ParseWorkflowResult, context: TemplateContext,
template: TemplateToken,
uri: string, uri: string,
config?: CompletionConfig, config?: CompletionConfig,
options?: WorkflowTemplateConverterOptions options?: WorkflowTemplateConverterOptions
): Promise<WorkflowTemplate> { ): Promise<WorkflowTemplate> {
let template = workflowTemplateCache.get(uri); const cachedTemplate = workflowTemplateCache.get(uri);
if (!template) { if (cachedTemplate) {
template = await convertWorkflowTemplate( return cachedTemplate;
parsedWorkflow.context,
// TODO: @joshmgross We can't assume that the value is non-null here
parsedWorkflow.value!, // eslint-disable-line @typescript-eslint/no-non-null-assertion
config?.fileProvider,
options
);
workflowTemplateCache.set(uri, template);
} }
return template; const workflowTemplate = await convertWorkflowTemplate(context, template, config?.fileProvider, options);
workflowTemplateCache.set(uri, workflowTemplate);
return workflowTemplate;
} }
+1 -1
View File
@@ -53,7 +53,7 @@ export async function validate(textDocument: TextDocument, config?: ValidationCo
if (result.value) { if (result.value) {
// Errors will be updated in the context. Attempt to do the conversion anyway in order to give the user more information // Errors will be updated in the context. Attempt to do the conversion anyway in order to give the user more information
const template = await fetchOrConvertWorkflowTemplate(result, textDocument.uri, config, { const template = await fetchOrConvertWorkflowTemplate(result.context, result.value, textDocument.uri, config, {
fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0, fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0,
errorPolicy: ErrorPolicy.TryConversion errorPolicy: ErrorPolicy.TryConversion
}); });