Merge pull request #18 from github/joshmgross/vp-caching
Cache API responses for custom value providers
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
|||||||
RepositoryContext,
|
RepositoryContext,
|
||||||
} from "./initializationOptions";
|
} from "./initializationOptions";
|
||||||
import { onCompletion } from "./on-completion";
|
import { onCompletion } from "./on-completion";
|
||||||
|
import { TTLCache } from "./utils/cache";
|
||||||
|
|
||||||
// Create a connection for the server, using Node's IPC as a transport.
|
// Create a connection for the server, using Node's IPC as a transport.
|
||||||
// Also include all preview / proposed LSP features.
|
// Also include all preview / proposed LSP features.
|
||||||
@@ -28,6 +29,7 @@ const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
|
|||||||
|
|
||||||
let sessionToken: string | undefined;
|
let sessionToken: string | undefined;
|
||||||
let repos: RepositoryContext[] = [];
|
let repos: RepositoryContext[] = [];
|
||||||
|
const cache = new TTLCache();
|
||||||
|
|
||||||
let hasConfigurationCapability = false;
|
let hasConfigurationCapability = false;
|
||||||
let hasWorkspaceFolderCapability = false;
|
let hasWorkspaceFolderCapability = false;
|
||||||
@@ -108,7 +110,8 @@ connection.onCompletion(
|
|||||||
position,
|
position,
|
||||||
documents.get(textDocument.uri)!,
|
documents.get(textDocument.uri)!,
|
||||||
sessionToken,
|
sessionToken,
|
||||||
repos.find((repo) => textDocument.uri.startsWith(repo.workspaceUri))
|
repos.find((repo) => textDocument.uri.startsWith(repo.workspaceUri)),
|
||||||
|
cache,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { complete } from "@github/actions-languageservice/complete";
|
import { complete } from "@github/actions-languageservice/complete";
|
||||||
import { WorkflowContext } from "@github/actions-languageservice/context/workflow-context";
|
import { WorkflowContext } from "@github/actions-languageservice/context/workflow-context";
|
||||||
import { ValueProviderConfig } from "@github/actions-languageservice/value-providers/config";
|
import { Value, ValueProviderConfig } from "@github/actions-languageservice/value-providers/config";
|
||||||
import { Octokit } from "@octokit/rest";
|
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 { RepositoryContext } from "./initializationOptions";
|
import { RepositoryContext } from "./initializationOptions";
|
||||||
|
import { TTLCache } from "./utils/cache";
|
||||||
import { getEnvironments } from "./value-providers/job-environment";
|
import { getEnvironments } from "./value-providers/job-environment";
|
||||||
import { getRunnerLabels } from "./value-providers/runs-on";
|
import { getRunnerLabels } from "./value-providers/runs-on";
|
||||||
|
|
||||||
@@ -12,21 +13,24 @@ export async function onCompletion(
|
|||||||
position: Position,
|
position: Position,
|
||||||
document: TextDocument,
|
document: TextDocument,
|
||||||
sessionToken: string | undefined,
|
sessionToken: string | undefined,
|
||||||
repoContext: RepositoryContext | undefined
|
repoContext: RepositoryContext | undefined,
|
||||||
|
cache: TTLCache,
|
||||||
): Promise<CompletionItem[]> {
|
): Promise<CompletionItem[]> {
|
||||||
const config: ValueProviderConfig = {
|
const config: ValueProviderConfig = {
|
||||||
getCustomValues: async (key: string, context: WorkflowContext) =>
|
getCustomValues: async (key: string, context: WorkflowContext) =>
|
||||||
getCustomValues(key, context, sessionToken, repoContext),
|
getCustomValues(key, context, sessionToken, repoContext, cache),
|
||||||
};
|
};
|
||||||
return await complete(document, position, config);
|
return await complete(document, position, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function getCustomValues(
|
async function getCustomValues(
|
||||||
key: string,
|
key: string,
|
||||||
context: WorkflowContext,
|
_: WorkflowContext,
|
||||||
sessionToken: string | undefined,
|
sessionToken: string | undefined,
|
||||||
repo: RepositoryContext | undefined,
|
repo: RepositoryContext | undefined,
|
||||||
) {
|
cache: TTLCache,
|
||||||
|
): Promise<Value[] | undefined> {
|
||||||
if (!sessionToken || !repo) {
|
if (!sessionToken || !repo) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -37,11 +41,11 @@ async function getCustomValues(
|
|||||||
|
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case "job-environment": {
|
case "job-environment": {
|
||||||
return await getEnvironments(octokit, repo.owner, repo.name);
|
return await getEnvironments(octokit, cache, repo.owner, repo.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
case "runs-on": {
|
case "runs-on": {
|
||||||
return await getRunnerLabels(octokit, repo.owner, repo.name);
|
return await getRunnerLabels(octokit, cache, repo.owner, repo.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
// From https://github.com/cschleiden/github-actions-parser/blob/a81dec9b7462dbcff08fbad0792f5ad549d9de7d/src/lib/workflowschema/workflowSchema.ts
|
||||||
|
interface CacheEntry<T> {
|
||||||
|
cachedAt: number;
|
||||||
|
content: T;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TTLCache {
|
||||||
|
private cache = new Map<string, CacheEntry<unknown>>();
|
||||||
|
|
||||||
|
constructor(private defaultTTLinMS: number = 10 * 60 * 1000) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param key Key to cache value under
|
||||||
|
* @param ttlInMS How long is the content valid. If optional, default value will be used
|
||||||
|
* @param getter Function to retrieve content if not in cache
|
||||||
|
*/
|
||||||
|
async get<T>(
|
||||||
|
key: string,
|
||||||
|
ttlInMS: number | undefined,
|
||||||
|
getter: () => Promise<T>
|
||||||
|
): Promise<T> {
|
||||||
|
const hasEntry = this.cache.has(key);
|
||||||
|
const e = hasEntry && this.cache.get(key);
|
||||||
|
if (
|
||||||
|
hasEntry &&
|
||||||
|
e &&
|
||||||
|
e.cachedAt > Date.now() - (ttlInMS || this.defaultTTLinMS)
|
||||||
|
) {
|
||||||
|
return e.content as T;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const content = await getter();
|
||||||
|
|
||||||
|
this.cache.set(key, {
|
||||||
|
cachedAt: Date.now(),
|
||||||
|
content,
|
||||||
|
});
|
||||||
|
|
||||||
|
return content;
|
||||||
|
} catch (e) {
|
||||||
|
this.cache.delete(key);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,22 @@
|
|||||||
import { Value } from "@github/actions-languageservice/value-providers/config";
|
import { Value } from "@github/actions-languageservice/value-providers/config";
|
||||||
import { Octokit } from "@octokit/rest";
|
import { Octokit } from "@octokit/rest";
|
||||||
|
import { TTLCache } from "../utils/cache";
|
||||||
|
|
||||||
export async function getEnvironments(
|
export async function getEnvironments(
|
||||||
client: Octokit,
|
client: Octokit,
|
||||||
|
cache: TTLCache,
|
||||||
owner: string,
|
owner: string,
|
||||||
name: string
|
name: string
|
||||||
): Promise<Value[]> {
|
): Promise<Value[]> {
|
||||||
|
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<string[]> {
|
||||||
let environments: string[] = [];
|
let environments: string[] = [];
|
||||||
try {
|
try {
|
||||||
const response = await client.repos.getAllEnvironments({
|
const response = await client.repos.getAllEnvironments({
|
||||||
@@ -20,5 +31,5 @@ export async function getEnvironments(
|
|||||||
console.log("Failure to retrieve environments: ", e);
|
console.log("Failure to retrieve environments: ", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Array.from(environments).map((env) => ({ label: env }));
|
return environments;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
import { Value } from "@github/actions-languageservice/value-providers/config";
|
import { Value } from "@github/actions-languageservice/value-providers/config";
|
||||||
import { Octokit } from "@octokit/rest";
|
import { Octokit } from "@octokit/rest";
|
||||||
|
import { TTLCache } from "../utils/cache";
|
||||||
|
|
||||||
export async function getRunnerLabels(
|
export async function getRunnerLabels(
|
||||||
client: Octokit,
|
client: Octokit,
|
||||||
|
cache: TTLCache,
|
||||||
owner: string,
|
owner: string,
|
||||||
name: string
|
name: string
|
||||||
): Promise<Value[]> {
|
): Promise<Value[]> {
|
||||||
const labels = new Set<string>([
|
const defaultLabels = [
|
||||||
"ubuntu-22.04",
|
"ubuntu-22.04",
|
||||||
"ubuntu-latest",
|
"ubuntu-latest",
|
||||||
"ubuntu-20.04",
|
"ubuntu-20.04",
|
||||||
@@ -20,8 +22,21 @@ export async function getRunnerLabels(
|
|||||||
"macos-11",
|
"macos-11",
|
||||||
"macos-10.15",
|
"macos-10.15",
|
||||||
"self-hosted",
|
"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<Set<string>> {
|
||||||
|
const labels = new Set<string>();
|
||||||
try {
|
try {
|
||||||
const response = await client.actions.listSelfHostedRunnersForRepo({
|
const response = await client.actions.listSelfHostedRunnersForRepo({
|
||||||
owner,
|
owner,
|
||||||
@@ -37,5 +52,5 @@ export async function getRunnerLabels(
|
|||||||
console.log("Failure to retrieve runner labels: ", e);
|
console.log("Failure to retrieve runner labels: ", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Array.from(labels).map((label) => ({ label }));
|
return labels;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user