Files
languageservices/languageservice/src/document-links.ts
T

52 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-12-21 13:34:10 +01:00
import {ErrorPolicy} from "@github/actions-workflow-parser/model/convert";
import {isJob} from "@github/actions-workflow-parser/model/type-guards";
2022-12-21 13:34:10 +01:00
import {File} from "@github/actions-workflow-parser/workflows/file";
import {TextDocument} from "vscode-languageserver-textdocument";
import {DocumentLink} from "vscode-languageserver-types";
import {actionUrl, parseActionReference} from "./action";
2022-12-21 13:34:10 +01:00
import {mapRange} from "./utils/range";
2023-03-03 12:39:40 -08:00
import {fetchOrParseWorkflow, fetchOrConvertWorkflowTemplate} from "./utils/workflow-cache";
2022-12-21 13:34:10 +01:00
2023-01-04 11:23:57 -08:00
export async function documentLinks(document: TextDocument): Promise<DocumentLink[]> {
2022-12-21 13:34:10 +01:00
const file: File = {
name: document.uri,
content: document.getText()
};
2023-03-03 12:39:40 -08:00
const parsedWorkflow = fetchOrParseWorkflow(file, document.uri);
2023-03-03 12:28:29 -08:00
if (!parsedWorkflow) {
2022-12-21 13:34:10 +01:00
return [];
}
2023-03-06 11:33:28 -08:00
const template = await fetchOrConvertWorkflowTemplate(parsedWorkflow, document.uri, undefined, {
errorPolicy: ErrorPolicy.TryConversion
});
2022-12-21 13:34:10 +01:00
// Add links to referenced actions
const actionLinks: DocumentLink[] = [];
for (const job of template?.jobs || []) {
if (!job || !isJob(job)) {
continue;
}
for (const step of job.steps || []) {
2022-12-21 13:34:10 +01:00
if ("uses" in step) {
const actionRef = parseActionReference(step.uses.value);
if (!actionRef) {
continue;
}
const url = actionUrl(actionRef);
2022-12-21 13:34:10 +01:00
actionLinks.push({
range: mapRange(step.uses.range),
target: url,
tooltip: `Open action on GitHub`
});
}
}
}
return [...actionLinks];
}