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

56 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-12-21 13:34:10 +01:00
import {convertWorkflowTemplate, parseWorkflow} from "@github/actions-workflow-parser";
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 {parseActionReference} from "./action";
import {nullTrace} from "./nulltrace";
import {mapRange} from "./utils/range";
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-02-06 11:15:15 -05:00
const result = parseWorkflow(file, nullTrace);
2022-12-21 13:34:10 +01:00
if (!result.value) {
return [];
}
2023-02-06 15:10:34 -05:00
const template = await convertWorkflowTemplate(result.context, result.value!, ErrorPolicy.TryConversion);
2022-12-21 13:34:10 +01:00
// Add links to referenced actions
const actionLinks: DocumentLink[] = [];
// TODO: Support base uri for GHES
const gitHubBaseUri = "https://www.github.com/";
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 = `${gitHubBaseUri}${actionRef.owner}/${actionRef.name}/tree/${actionRef.ref}/${
actionRef.path || ""
}`;
actionLinks.push({
range: mapRange(step.uses.range),
target: url,
tooltip: `Open action on GitHub`
});
}
}
}
return [...actionLinks];
}