diff --git a/actions-languageserver/src/on-completion.ts b/actions-languageserver/src/on-completion.ts index 89e45c3..7d5ff07 100644 --- a/actions-languageserver/src/on-completion.ts +++ b/actions-languageserver/src/on-completion.ts @@ -18,44 +18,34 @@ export async function onCompletion( ): Promise { const config: ValueProviderConfig = { getCustomValues: async (key: string, context: WorkflowContext) => - getCustomValuesWithCache(key, context, sessionToken, repoContext, cache), + getCustomValues(key, context, sessionToken, repoContext, cache), }; return await complete(document, position, config); } -async function getCustomValuesWithCache( - key: string, - context: WorkflowContext, - sessionToken: string | undefined, - repo: RepositoryContext | undefined, - cache: TTLCache, - ): Promise { - if (!sessionToken || !repo) { - return; - } - - const cacheKey = `${repo.owner}/${repo.name}/${key}`; - return cache.get(cacheKey, undefined, async () => await getCustomValues(key, context, sessionToken, repo)); -} - async function getCustomValues( key: string, _: WorkflowContext, - sessionToken: string, - repo: RepositoryContext, + sessionToken: string | undefined, + repo: RepositoryContext | undefined, + cache: TTLCache, ): Promise { + if (!sessionToken || !repo) { + return; + } + const octokit = new Octokit({ auth: sessionToken, }); switch (key) { case "job-environment": { - return await getEnvironments(octokit, repo.owner, repo.name); + return await getEnvironments(octokit, cache, repo.owner, repo.name); } case "runs-on": { - return await getRunnerLabels(octokit, repo.owner, repo.name); + return await getRunnerLabels(octokit, cache, repo.owner, repo.name); } } } diff --git a/actions-languageserver/src/value-providers/job-environment.ts b/actions-languageserver/src/value-providers/job-environment.ts index 287368e..9f20011 100644 --- a/actions-languageserver/src/value-providers/job-environment.ts +++ b/actions-languageserver/src/value-providers/job-environment.ts @@ -1,11 +1,22 @@ import { Value } from "@github/actions-languageservice/value-providers/config"; import { Octokit } from "@octokit/rest"; +import { TTLCache } from "../utils/cache"; export async function getEnvironments( client: Octokit, + cache: TTLCache, owner: string, name: string ): Promise { + const environments = await cache.get(`${owner}/${name}/environments`, undefined, () => fetchEnvironments(client, owner, name)); + return Array.from(environments).map((env) => ({ label: env })); +} + +async function fetchEnvironments( + client: Octokit, + owner: string, + name: string +): Promise { let environments: string[] = []; try { const response = await client.repos.getAllEnvironments({ @@ -20,5 +31,5 @@ export async function getEnvironments( console.log("Failure to retrieve environments: ", e); } - return Array.from(environments).map((env) => ({ label: env })); + return environments; } diff --git a/actions-languageserver/src/value-providers/runs-on.ts b/actions-languageserver/src/value-providers/runs-on.ts index f4e12a1..4fda670 100644 --- a/actions-languageserver/src/value-providers/runs-on.ts +++ b/actions-languageserver/src/value-providers/runs-on.ts @@ -1,12 +1,14 @@ import { Value } from "@github/actions-languageservice/value-providers/config"; import { Octokit } from "@octokit/rest"; +import { TTLCache } from "../utils/cache"; export async function getRunnerLabels( client: Octokit, + cache: TTLCache, owner: string, name: string ): Promise { - const labels = new Set([ + const defaultLabels = [ "ubuntu-22.04", "ubuntu-latest", "ubuntu-20.04", @@ -20,8 +22,21 @@ export async function getRunnerLabels( "macos-11", "macos-10.15", "self-hosted", - ]); + ]; + const repoLabels = await cache.get(`${owner}/${name}/runner-labels`, undefined, () => fetchRunnerLabels(client, owner, name)); + for (const label of defaultLabels) { + repoLabels.add(label); + } + return Array.from(repoLabels).map((label) => ({ label })); +} + +async function fetchRunnerLabels( + client: Octokit, + owner: string, + name: string +): Promise> { + const labels = new Set(); try { const response = await client.actions.listSelfHostedRunnersForRepo({ owner, @@ -37,5 +52,5 @@ export async function getRunnerLabels( console.log("Failure to retrieve runner labels: ", e); } - return Array.from(labels).map((label) => ({ label })); + return labels; }