Merge pull request #117 from github/cschleiden/accept-client
Extract octokit client creation and set user agent
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import {Octokit} from "@octokit/rest";
|
||||
|
||||
export function getClient(token: string, userAgent?: string): Octokit {
|
||||
return new Octokit({
|
||||
auth: token,
|
||||
userAgent: userAgent || `GitHub Actions Language Server`
|
||||
});
|
||||
}
|
||||
@@ -16,19 +16,20 @@ import {
|
||||
TextDocumentSyncKind
|
||||
} from "vscode-languageserver";
|
||||
import {TextDocument} from "vscode-languageserver-textdocument";
|
||||
import {getClient} from "./client";
|
||||
import {Commands} from "./commands";
|
||||
import {contextProviders} from "./context-providers";
|
||||
import {descriptionProvider} from "./description-provider";
|
||||
import {InitializationOptions, RepositoryContext} from "./initializationOptions";
|
||||
import {onCompletion} from "./on-completion";
|
||||
import {TTLCache} from "./utils/cache";
|
||||
import {valueProviders} from "./value-providers";
|
||||
import {getActionInputs} from "./value-providers/action-inputs";
|
||||
import {Commands} from "./commands";
|
||||
import {descriptionProvider} from "./description-provider";
|
||||
|
||||
export function initConnection(connection: Connection) {
|
||||
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
|
||||
|
||||
let sessionToken: string | undefined;
|
||||
let client: Octokit | undefined;
|
||||
let repos: RepositoryContext[] = [];
|
||||
const cache = new TTLCache();
|
||||
|
||||
@@ -49,7 +50,11 @@ export function initConnection(connection: Connection) {
|
||||
);
|
||||
|
||||
const options: InitializationOptions = params.initializationOptions;
|
||||
sessionToken = options.sessionToken;
|
||||
|
||||
if (options.sessionToken) {
|
||||
client = getClient(options.sessionToken, options.userAgent);
|
||||
}
|
||||
|
||||
if (options.repos) {
|
||||
repos = options.repos;
|
||||
}
|
||||
@@ -93,15 +98,13 @@ export function initConnection(connection: Connection) {
|
||||
const repoContext = repos.find(repo => textDocument.uri.startsWith(repo.workspaceUri));
|
||||
|
||||
const config: ValidationConfig = {
|
||||
valueProviderConfig: valueProviders(sessionToken, repoContext, cache),
|
||||
contextProviderConfig: contextProviders(sessionToken, repoContext, cache),
|
||||
valueProviderConfig: valueProviders(client, repoContext, cache),
|
||||
contextProviderConfig: contextProviders(client, repoContext, cache),
|
||||
getActionInputs: async action => {
|
||||
if (sessionToken) {
|
||||
const octokit = new Octokit({
|
||||
auth: sessionToken
|
||||
});
|
||||
return await getActionInputs(octokit, cache, action);
|
||||
if (client) {
|
||||
return await getActionInputs(client, cache, action);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
@@ -114,7 +117,7 @@ export function initConnection(connection: Connection) {
|
||||
return await onCompletion(
|
||||
position,
|
||||
documents.get(textDocument.uri)!,
|
||||
sessionToken,
|
||||
client,
|
||||
repos.find(repo => textDocument.uri.startsWith(repo.workspaceUri)),
|
||||
cache
|
||||
);
|
||||
@@ -122,7 +125,7 @@ export function initConnection(connection: Connection) {
|
||||
|
||||
connection.onHover(async ({position, textDocument}: HoverParams): Promise<Hover | null> => {
|
||||
return hover(documents.get(textDocument.uri)!, position, {
|
||||
descriptionProvider: descriptionProvider(sessionToken, cache)
|
||||
descriptionProvider: descriptionProvider(client, cache)
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -9,18 +9,14 @@ import {RepositoryContext} from "./initializationOptions";
|
||||
import {TTLCache} from "./utils/cache";
|
||||
|
||||
export function contextProviders(
|
||||
sessionToken: string | undefined,
|
||||
client: Octokit | undefined,
|
||||
repo: RepositoryContext | undefined,
|
||||
cache: TTLCache
|
||||
): ContextProviderConfig {
|
||||
if (!repo || !sessionToken) {
|
||||
if (!repo || !client) {
|
||||
return {getContext: (_: string) => Promise.resolve(undefined)};
|
||||
}
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: sessionToken
|
||||
});
|
||||
|
||||
const getContext = async (
|
||||
name: string,
|
||||
defaultContext: DescriptionDictionary | undefined,
|
||||
@@ -28,11 +24,11 @@ export function contextProviders(
|
||||
) => {
|
||||
switch (name) {
|
||||
case "secrets":
|
||||
return await getSecrets(workflowContext, octokit, cache, repo, defaultContext);
|
||||
return await getSecrets(workflowContext, client, cache, repo, defaultContext);
|
||||
case "vars":
|
||||
return await getVariables(workflowContext, octokit, cache, repo, defaultContext);
|
||||
return await getVariables(workflowContext, client, cache, repo, defaultContext);
|
||||
case "steps":
|
||||
return await getStepsContext(octokit, cache, defaultContext, workflowContext);
|
||||
return await getStepsContext(client, cache, defaultContext, workflowContext);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
import {DescriptionProvider} from "@github/actions-languageservice/hover";
|
||||
import {Octokit} from "@octokit/rest";
|
||||
import {TTLCache} from "./utils/cache";
|
||||
import {getActionInputDescription} from "./description-providers/action-input";
|
||||
import {TTLCache} from "./utils/cache";
|
||||
|
||||
export function descriptionProvider(sessionToken: string | undefined, cache: TTLCache): DescriptionProvider {
|
||||
const octokit =
|
||||
sessionToken &&
|
||||
new Octokit({
|
||||
auth: sessionToken
|
||||
});
|
||||
|
||||
export function descriptionProvider(client: Octokit | undefined, cache: TTLCache): DescriptionProvider {
|
||||
const getDescription: DescriptionProvider["getDescription"] = async (context, token, path) => {
|
||||
if (!octokit) {
|
||||
if (!client) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const parent = path[path.length - 1];
|
||||
if (context.step && parent.definition?.key === "step-with") {
|
||||
return await getActionInputDescription(octokit, cache, context.step, token);
|
||||
return await getActionInputDescription(client, cache, context.step, token);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -9,6 +9,11 @@ export interface InitializationOptions {
|
||||
*/
|
||||
sessionToken?: string;
|
||||
|
||||
/**
|
||||
* Optional user agent to use when making calls to github.com
|
||||
*/
|
||||
userAgent?: string;
|
||||
|
||||
/**
|
||||
* List of repositories that the language server should be aware of
|
||||
*/
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {complete} from "@github/actions-languageservice/complete";
|
||||
import {Octokit} from "@octokit/rest";
|
||||
import {CompletionItem, Position} from "vscode-languageserver";
|
||||
import {TextDocument} from "vscode-languageserver-textdocument";
|
||||
import {contextProviders} from "./context-providers";
|
||||
@@ -9,14 +10,14 @@ import {valueProviders} from "./value-providers";
|
||||
export async function onCompletion(
|
||||
position: Position,
|
||||
document: TextDocument,
|
||||
sessionToken: string | undefined,
|
||||
client: Octokit | undefined,
|
||||
repoContext: RepositoryContext | undefined,
|
||||
cache: TTLCache
|
||||
): Promise<CompletionItem[]> {
|
||||
return await complete(
|
||||
document,
|
||||
position,
|
||||
repoContext && valueProviders(sessionToken, repoContext, cache),
|
||||
repoContext && contextProviders(sessionToken, repoContext, cache)
|
||||
repoContext && valueProviders(client, repoContext, cache),
|
||||
repoContext && contextProviders(client, repoContext, cache)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -9,34 +9,30 @@ import {getEnvironments} from "./value-providers/job-environment";
|
||||
import {getRunnerLabels} from "./value-providers/runs-on";
|
||||
|
||||
export function valueProviders(
|
||||
sessionToken: string | undefined,
|
||||
client: Octokit | undefined,
|
||||
repo: RepositoryContext | undefined,
|
||||
cache: TTLCache
|
||||
): ValueProviderConfig {
|
||||
if (!repo || !sessionToken) {
|
||||
if (!repo || !client) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: sessionToken
|
||||
});
|
||||
|
||||
return {
|
||||
"job-environment": {
|
||||
kind: ValueProviderKind.AllowedValues,
|
||||
get: (_: WorkflowContext) => getEnvironments(octokit, cache, repo.owner, repo.name)
|
||||
get: (_: WorkflowContext) => getEnvironments(client, cache, repo.owner, repo.name)
|
||||
},
|
||||
"job-environment-name": {
|
||||
kind: ValueProviderKind.AllowedValues,
|
||||
get: (_: WorkflowContext) => getEnvironments(octokit, cache, repo.owner, repo.name)
|
||||
get: (_: WorkflowContext) => getEnvironments(client, cache, repo.owner, repo.name)
|
||||
},
|
||||
"runs-on": {
|
||||
kind: ValueProviderKind.SuggestedValues,
|
||||
get: (_: WorkflowContext) => getRunnerLabels(octokit, cache, repo.owner, repo.name)
|
||||
get: (_: WorkflowContext) => getRunnerLabels(client, cache, repo.owner, repo.name)
|
||||
},
|
||||
"step-with": {
|
||||
kind: ValueProviderKind.AllowedValues,
|
||||
get: (context: WorkflowContext) => getActionInputValues(octokit, cache, context)
|
||||
get: (context: WorkflowContext) => getActionInputValues(client, cache, context)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"moduleResolution": "node",
|
||||
"lib": ["es6", "webworker"]
|
||||
"lib": ["es6", "webworker"],
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"watchOptions": {
|
||||
"watchFile": "useFsEvents",
|
||||
|
||||
Reference in New Issue
Block a user