Initial code import

This commit is contained in:
Christopher Schleiden
2022-11-08 17:00:59 -08:00
parent 7352cda0cf
commit 2e1652515e
38 changed files with 1354 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
This wraps the GitHub Actions language service and makes it available via the [language server protocol](https://microsoft.github.io/language-server-protocol/) (LSP).
+16
View File
@@ -0,0 +1,16 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: "ts-jest/presets/default-esm",
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
useESM: true,
},
],
},
moduleFileExtensions: ["ts", "js"],
};
+1 -1
View File
@@ -36,7 +36,7 @@
"watch": "tsc --build tsconfig.build.json --watch"
},
"dependencies": {
"@github/actions-languageservice": "^0.1.0",
"@github/actions-languageservice": "*",
"@octokit/rest": "^19.0.5",
"vscode-languageserver": "^8.0.2",
"vscode-languageserver-textdocument": "^1.0.7"
+11
View File
@@ -0,0 +1,11 @@
import { validate } from "@github/actions-languageservice";
import { TextDocument } from "vscode-languageserver-textdocument";
describe("simple test", () => {
it("should work", async () => {
const doc = TextDocument.create("uri", "workflow", 1, "on: push");
const r = await validate(doc);
expect(r).not.toBeNull();
});
});
+130
View File
@@ -0,0 +1,130 @@
import {
CompletionItem,
createConnection,
Hover,
HoverParams,
InitializeParams,
InitializeResult,
ProposedFeatures,
TextDocumentPositionParams,
TextDocuments,
TextDocumentSyncKind,
} from "vscode-languageserver/node";
import { hover, validate } from "@github/actions-languageservice";
import { TextDocument } from "vscode-languageserver-textdocument";
import {
InitializationOptions,
RepositoryContext,
} from "./initializationOptions";
import { onCompletion } from "./on-completion";
// Create a connection for the server, using Node's IPC as a transport.
// Also include all preview / proposed LSP features.
const connection = createConnection(ProposedFeatures.all);
// Create a simple text document manager.
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
let sessionToken: string | undefined;
let repos: RepositoryContext[] = [];
let repoWorkspaceMap = new Map<string, RepositoryContext>();
let hasConfigurationCapability = false;
let hasWorkspaceFolderCapability = false;
let hasDiagnosticRelatedInformationCapability = false;
connection.onInitialize((params: InitializeParams) => {
const capabilities = params.capabilities;
hasWorkspaceFolderCapability = !!(
capabilities.workspace && !!capabilities.workspace.workspaceFolders
);
hasDiagnosticRelatedInformationCapability = !!(
capabilities.textDocument &&
capabilities.textDocument.publishDiagnostics &&
capabilities.textDocument.publishDiagnostics.relatedInformation
);
const options: InitializationOptions = params.initializationOptions;
sessionToken = options.sessionToken;
if (options.repos) {
repos = options.repos;
for (const repo of repos) {
repoWorkspaceMap.set(repo.workspaceUri, repo);
}
}
const result: InitializeResult = {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Incremental,
completionProvider: {
resolveProvider: false,
},
hoverProvider: true,
},
};
if (hasWorkspaceFolderCapability) {
result.capabilities.workspace = {
workspaceFolders: {
supported: true,
},
};
}
return result;
});
connection.onInitialized(() => {
if (hasWorkspaceFolderCapability) {
connection.workspace.onDidChangeWorkspaceFolders((_event) => {
connection.console.log("Workspace folder change event received.");
});
}
});
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent((change) => {
validateTextDocument(change.document);
});
async function validateTextDocument(textDocument: TextDocument): Promise<void> {
const result = await validate(textDocument);
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: result });
}
connection.onDidChangeWatchedFiles((_change) => {
// Monitored files have change in VSCode
connection.console.log("We received an file change event");
});
// This handler provides the initial list of the completion items.
connection.onCompletion(
async ({
position,
textDocument,
}: TextDocumentPositionParams): Promise<CompletionItem[]> => {
return await onCompletion(
position,
textDocument.uri,
documents.get(textDocument.uri)!,
sessionToken,
repoWorkspaceMap
);
}
);
connection.onHover(
async ({ position, textDocument }: HoverParams): Promise<Hover | null> => {
const r = await hover(documents.get(textDocument.uri)!, position);
return r;
}
);
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);
// Listen on the connection
connection.listen();
@@ -0,0 +1,12 @@
export interface InitializationOptions {
sessionToken?: string;
repos?: RepositoryContext[];
}
export interface RepositoryContext {
id: number;
owner: string;
name: string;
workspaceUri: string;
}
@@ -0,0 +1,47 @@
import { complete } from "@github/actions-languageservice/complete";
import {
ValueProviderConfig,
WorkflowContext,
} from "@github/actions-languageservice/value-providers/config";
import { Octokit } from "@octokit/rest";
import { CompletionItem, DocumentUri, Position } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import { RepositoryContext } from "./initializationOptions";
import { getRunnerLabels } from "./value-providers/runs-on";
export async function onCompletion(
position: Position,
uri: DocumentUri,
document: TextDocument,
sessionToken: string | undefined,
repoWorkflowMap: Map<string, RepositoryContext>
): Promise<CompletionItem[]> {
const config: ValueProviderConfig = {
getCustomValues: async (key: string, context: WorkflowContext) =>
getCustomValues(key, context, sessionToken, repoWorkflowMap),
};
return await complete(document, position, config);
}
async function getCustomValues(
key: string,
context: WorkflowContext,
sessionToken: string | undefined,
repoWorkspaceMap: Map<string, RepositoryContext>
) {
if (!sessionToken) {
return;
}
// TODO: Parse workflow URI and look up repo for workspace
const [repo] = repoWorkspaceMap.values();
if (!repo) {
return;
}
if (key === "runs-on") {
const octokit = new Octokit({
auth: sessionToken,
});
return await getRunnerLabels(octokit, repo.owner, repo.name);
}
}
@@ -0,0 +1,41 @@
import { Value } from "@github/actions-languageservice/value-providers/config";
import { Octokit } from "@octokit/rest";
export async function getRunnerLabels(
client: Octokit,
owner: string,
name: string
): Promise<Value[]> {
const labels = new Set<string>([
"ubuntu-22.04",
"ubuntu-latest",
"ubuntu-20.04",
"ubuntu-18.04",
"windows-latest",
"windows-2022",
"windows-2019",
"windows-2016",
"macos-latest",
"macos-12",
"macos-11",
"macos-10.15",
"self-hosted",
]);
try {
const response = await client.actions.listSelfHostedRunnersForRepo({
owner,
repo: name,
});
for (const runner of response.data.runners) {
for (const label of runner.labels) {
labels.add(label.name);
}
}
} catch (e) {
console.log(e);
}
return Array.from(labels).map((label) => ({ label }));
}
@@ -0,0 +1,10 @@
{
"exclude": ["./src/**/*.test.ts"],
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"noEmit": false,
"outDir": "./dist"
}
}
+18
View File
@@ -0,0 +1,18 @@
{
"include": ["src"],
"compilerOptions": {
"module": "esnext",
"target": "ES2020",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "node"
},
"watchOptions": {
"watchFile": "useFsEvents",
"watchDirectory": "useFsEvents",
"synchronousWatchDirectory": true,
"excludeDirectories": ["**/node_modules", "dist"],
}
}