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