From 57a77551b0f9e0d3140577aadb0865479d84738b Mon Sep 17 00:00:00 2001 From: flmeyer Date: Mon, 8 May 2023 17:00:05 +0200 Subject: [PATCH 1/4] Enable support for GitHub Enterprise Server --- languageserver/src/client.ts | 5 +++-- languageserver/src/connection.ts | 2 +- languageserver/src/initializationOptions.ts | 5 +++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/languageserver/src/client.ts b/languageserver/src/client.ts index de54554..ac0aa61 100644 --- a/languageserver/src/client.ts +++ b/languageserver/src/client.ts @@ -1,8 +1,9 @@ import {Octokit} from "@octokit/rest"; -export function getClient(token: string, userAgent?: string): Octokit { +export function getClient(token: string, userAgent?: string, apiUrl?: string): Octokit { return new Octokit({ auth: token, - userAgent: userAgent || `GitHub Actions Language Server` + userAgent: userAgent || `GitHub Actions Language Server`, + baseUrl: apiUrl }); } diff --git a/languageserver/src/connection.ts b/languageserver/src/connection.ts index 274923a..c5c5f5f 100644 --- a/languageserver/src/connection.ts +++ b/languageserver/src/connection.ts @@ -51,7 +51,7 @@ export function initConnection(connection: Connection) { const options = params.initializationOptions as InitializationOptions; if (options.sessionToken) { - client = getClient(options.sessionToken, options.userAgent); + client = getClient(options.sessionToken, options.userAgent, options.githubApiUrl); } if (options.repos) { diff --git a/languageserver/src/initializationOptions.ts b/languageserver/src/initializationOptions.ts index abc1547..00e23fc 100644 --- a/languageserver/src/initializationOptions.ts +++ b/languageserver/src/initializationOptions.ts @@ -23,6 +23,11 @@ export interface InitializationOptions { * Desired log level */ logLevel?: LogLevel; + + /** + * If a GitHub Enterprise Server should be used, the URL of the API endpoint, eg "https://ghe.my-company.com/api/v3" + */ + githubApiUrl?: string; } export interface RepositoryContext { From 468b68840b16cfd319c35a2879eb34e6f359cad6 Mon Sep 17 00:00:00 2001 From: flmeyer Date: Fri, 12 May 2023 22:42:09 +0200 Subject: [PATCH 2/4] Add try-catch to avoid failing requests On GHES servers below version 3.8, the variables context is unavailable, resulting in 404 errors when calling the corresponding endpoint. --- .../src/context-providers/variables.ts | 87 ++++++++++--------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/languageserver/src/context-providers/variables.ts b/languageserver/src/context-providers/variables.ts index 92e5d4c..345bb85 100644 --- a/languageserver/src/context-providers/variables.ts +++ b/languageserver/src/context-providers/variables.ts @@ -2,9 +2,10 @@ import {data, DescriptionDictionary} from "@actions/expressions"; import {Pair} from "@actions/expressions/data/expressiondata"; import {StringData} from "@actions/expressions/data/index"; import {WorkflowContext} from "@actions/languageservice/context/workflow-context"; -import {warn} from "@actions/languageservice/log"; +import {log, warn} from "@actions/languageservice/log"; import {isMapping, isString} from "@actions/workflow-parser"; import {Octokit} from "@octokit/rest"; +import {RequestError} from "@octokit/types"; import {RepositoryContext} from "../initializationOptions"; import {TTLCache} from "../utils/cache"; @@ -42,50 +43,58 @@ export async function getVariables( } const variablesContext = defaultContext || new DescriptionDictionary(); - const variables = await getRemoteVariables(octokit, cache, repo, environmentName); + try { + const variables = await getRemoteVariables(octokit, cache, repo, environmentName); - // Build combined map of variables - const variablesMap = new Map< - string, - { - key: string; - value: data.StringData; - description?: string; - } - >(); + // Build combined map of variables + const variablesMap = new Map< + string, + { + key: string; + value: data.StringData; + description?: string; + } + >(); - variables.organizationVariables.forEach(variable => - variablesMap.set(variable.key.toLowerCase(), { - key: variable.key, - value: new data.StringData(variable.value.coerceString()), - description: `${variable.value.coerceString()} - Organization variable` - }) - ); + variables.organizationVariables.forEach(variable => + variablesMap.set(variable.key.toLowerCase(), { + key: variable.key, + value: new data.StringData(variable.value.coerceString()), + description: `${variable.value.coerceString()} - Organization variable` + }) + ); - // Override org variables with repo variables - variables.repoVariables.forEach(variable => - variablesMap.set(variable.key.toLowerCase(), { - key: variable.key, - value: new data.StringData(variable.value.coerceString()), - description: `${variable.value.coerceString()} - Repository variable` - }) - ); + // Override org variables with repo variables + variables.repoVariables.forEach(variable => + variablesMap.set(variable.key.toLowerCase(), { + key: variable.key, + value: new data.StringData(variable.value.coerceString()), + description: `${variable.value.coerceString()} - Repository variable` + }) + ); - // Override repo variables with environment veriables (if defined) - variables.environmentVariables.forEach(variable => - variablesMap.set(variable.key.toLowerCase(), { - key: variable.key, - value: new data.StringData(variable.value.coerceString()), - description: `${variable.value.coerceString()} - Variable for environment \`${environmentName || ""}\`` - }) - ); + // Override repo variables with environment veriables (if defined) + variables.environmentVariables.forEach(variable => + variablesMap.set(variable.key.toLowerCase(), { + key: variable.key, + value: new data.StringData(variable.value.coerceString()), + description: `${variable.value.coerceString()} - Variable for environment \`${environmentName || ""}\`` + }) + ); - // Sort variables by key and add to context - Array.from(variablesMap.values()) - .sort((a, b) => a.key.localeCompare(b.key)) - .forEach(variable => variablesContext?.add(variable.key, variable.value, variable.description)); + // Sort variables by key and add to context + Array.from(variablesMap.values()) + .sort((a, b) => a.key.localeCompare(b.key)) + .forEach(variable => variablesContext?.add(variable.key, variable.value, variable.description)); - return variablesContext; + return variablesContext; + } catch (e: any) { + const requestError: RequestError = e; + if (requestError.name == "HttpError" && requestError.status == 404) { + log("Failure to request variables. Ignore if you're using GitHub Enterprise Server below version 3.8"); + return variablesContext; + } else throw e; + } } export async function getRemoteVariables( From 41436c657094f18fa3c34c46bc2bbeee88403d0d Mon Sep 17 00:00:00 2001 From: flmeyer Date: Fri, 19 May 2023 16:34:58 +0200 Subject: [PATCH 3/4] Use correct RequestError class --- languageserver/src/context-providers/variables.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/languageserver/src/context-providers/variables.ts b/languageserver/src/context-providers/variables.ts index 345bb85..ef32c78 100644 --- a/languageserver/src/context-providers/variables.ts +++ b/languageserver/src/context-providers/variables.ts @@ -5,7 +5,7 @@ import {WorkflowContext} from "@actions/languageservice/context/workflow-context import {log, warn} from "@actions/languageservice/log"; import {isMapping, isString} from "@actions/workflow-parser"; import {Octokit} from "@octokit/rest"; -import {RequestError} from "@octokit/types"; +import {RequestError} from "@octokit/request-error"; import {RepositoryContext} from "../initializationOptions"; import {TTLCache} from "../utils/cache"; @@ -88,9 +88,9 @@ export async function getVariables( .forEach(variable => variablesContext?.add(variable.key, variable.value, variable.description)); return variablesContext; - } catch (e: any) { - const requestError: RequestError = e; - if (requestError.name == "HttpError" && requestError.status == 404) { + } catch (e) { + if (!(e instanceof RequestError)) throw e; + if (e.name == "HttpError" && e.status == 404) { log("Failure to request variables. Ignore if you're using GitHub Enterprise Server below version 3.8"); return variablesContext; } else throw e; From b912482163779d561c5d624d3013a3fb7f011752 Mon Sep 17 00:00:00 2001 From: Olfi01 Date: Thu, 25 May 2023 00:57:30 +0200 Subject: [PATCH 4/4] Apply suggestions from code review Changed parameter naming to match general pattern Co-authored-by: Christopher Schleiden --- languageserver/src/connection.ts | 2 +- languageserver/src/initializationOptions.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/languageserver/src/connection.ts b/languageserver/src/connection.ts index c5c5f5f..90b139b 100644 --- a/languageserver/src/connection.ts +++ b/languageserver/src/connection.ts @@ -51,7 +51,7 @@ export function initConnection(connection: Connection) { const options = params.initializationOptions as InitializationOptions; if (options.sessionToken) { - client = getClient(options.sessionToken, options.userAgent, options.githubApiUrl); + client = getClient(options.sessionToken, options.userAgent, options.gitHubApiUrl); } if (options.repos) { diff --git a/languageserver/src/initializationOptions.ts b/languageserver/src/initializationOptions.ts index 00e23fc..59ef462 100644 --- a/languageserver/src/initializationOptions.ts +++ b/languageserver/src/initializationOptions.ts @@ -27,7 +27,7 @@ export interface InitializationOptions { /** * If a GitHub Enterprise Server should be used, the URL of the API endpoint, eg "https://ghe.my-company.com/api/v3" */ - githubApiUrl?: string; + gitHubApiUrl?: string; } export interface RepositoryContext {