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
+11 -5
View File
@@ -66,14 +66,20 @@ export async function complete(
};
const parsedWorkflow = fetchOrParseWorkflow(file, textDocument.uri);
if (!parsedWorkflow) {
if (!parsedWorkflow?.value) {
return [];
}
const template = await fetchOrConvertWorkflowTemplate(parsedWorkflow, textDocument.uri, config, {
fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0,
errorPolicy: ErrorPolicy.TryConversion
});
const template = await fetchOrConvertWorkflowTemplate(
parsedWorkflow.context,
parsedWorkflow.value,
textDocument.uri,
config,
{
fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0,
errorPolicy: ErrorPolicy.TryConversion
}
);
const {token, keyToken, parent, path} = findToken(newPos, parsedWorkflow.value);
const workflowContext = getWorkflowContext(textDocument.uri, template, path);
+10 -4
View File
@@ -16,13 +16,19 @@ export async function documentLinks(document: TextDocument, workspace: string |
};
const parsedWorkflow = fetchOrParseWorkflow(file, document.uri);
if (!parsedWorkflow) {
if (!parsedWorkflow?.value) {
return [];
}
const template = await fetchOrConvertWorkflowTemplate(parsedWorkflow, document.uri, undefined, {
errorPolicy: ErrorPolicy.TryConversion
});
const template = await fetchOrConvertWorkflowTemplate(
parsedWorkflow.context,
parsedWorkflow.value,
document.uri,
undefined,
{
errorPolicy: ErrorPolicy.TryConversion
}
);
const links: DocumentLink[] = [];
+11 -5
View File
@@ -44,14 +44,20 @@ export async function hover(document: TextDocument, position: Position, config?:
};
const parsedWorkflow = fetchOrParseWorkflow(file, document.uri);
if (!parsedWorkflow) {
if (!parsedWorkflow?.value) {
return null;
}
const template = await fetchOrConvertWorkflowTemplate(parsedWorkflow, document.uri, config, {
errorPolicy: ErrorPolicy.TryConversion,
fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0
});
const template = await fetchOrConvertWorkflowTemplate(
parsedWorkflow.context,
parsedWorkflow.value,
document.uri,
config,
{
errorPolicy: ErrorPolicy.TryConversion,
fetchReusableWorkflowDepth: config?.fileProvider ? 1 : 0
}
);
const tokenResult = findToken(position, parsedWorkflow.value);
const {token, keyToken, parent} = tokenResult;
+18 -16
View File
@@ -1,8 +1,14 @@
import {convertWorkflowTemplate, parseWorkflow} from "@github/actions-workflow-parser";
import {WorkflowTemplateConverterOptions} from "@github/actions-workflow-parser/model/convert";
import {
convertWorkflowTemplate,
parseWorkflow,
ParseWorkflowResult,
WorkflowTemplate
} from "@github/actions-workflow-parser";
import {File} from "@github/actions-workflow-parser/workflows/file";
import {ParseWorkflowResult} from "@github/actions-workflow-parser";
import {WorkflowTemplate} from "@github/actions-workflow-parser";
import {WorkflowTemplateConverterOptions} from "@github/actions-workflow-parser/model/convert";
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 {CompletionConfig} from "../complete";
@@ -30,21 +36,17 @@ export function fetchOrParseWorkflow(file: File, uri: string): ParseWorkflowResu
}
export async function fetchOrConvertWorkflowTemplate(
parsedWorkflow: ParseWorkflowResult,
context: TemplateContext,
template: TemplateToken,
uri: string,
config?: CompletionConfig,
options?: WorkflowTemplateConverterOptions
): Promise<WorkflowTemplate> {
let template = workflowTemplateCache.get(uri);
if (!template) {
template = await convertWorkflowTemplate(
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);
const cachedTemplate = workflowTemplateCache.get(uri);
if (cachedTemplate) {
return cachedTemplate;
}
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) {
// 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,
errorPolicy: ErrorPolicy.TryConversion
});