Add a context provider for secrets

This commit is contained in:
Josh Gross
2022-11-29 16:56:20 -05:00
parent a67069da17
commit 72c7fbd032
10 changed files with 105 additions and 9 deletions
@@ -0,0 +1,32 @@
import {data} from "@github/actions-expressions";
import {ContextProviderConfig} from "@github/actions-languageservice/.";
import {Octokit} from "@octokit/rest";
import {getSecrets} from "./context-providers/secrets";
import {RepositoryContext} from "./initializationOptions";
import {TTLCache} from "./utils/cache";
export function contextProviders(
sessionToken: string | undefined,
repo: RepositoryContext | undefined,
cache: TTLCache
): ContextProviderConfig {
if (!repo || !sessionToken) {
return {getContext: (_: string) => Promise.resolve(undefined)};
}
const octokit = new Octokit({
auth: sessionToken
});
const getContext = async (name: string) => {
switch (name) {
case "secrets":
const secrets = await getSecrets(octokit, cache, repo.owner, repo.name);
const secretContext = new data.Dictionary();
secrets.forEach(secret => secretContext.add(secret.value, secret));
return secretContext;
}
};
return {getContext};
}
@@ -0,0 +1,22 @@
import {StringData} from "@github/actions-expressions/data/string";
import {Octokit} from "@octokit/rest";
import {TTLCache} from "../utils/cache";
export function getSecrets(octokit: Octokit, cache: TTLCache, owner: string, name: string): Promise<StringData[]> {
return cache.get(`${owner}/${name}/secrets`, undefined, () => fetchSecrets(octokit, owner, name));
}
async function fetchSecrets(octokit: Octokit, owner: string, name: string): Promise<StringData[]> {
try {
const response = await octokit.actions.listRepoSecrets({
owner,
repo: name
});
return response.data.secrets.map(secret => new StringData(secret.name));
} catch (e) {
console.log("Failure to retrieve secrets: ", e);
}
return [];
}
+3 -1
View File
@@ -1,6 +1,7 @@
import { complete } from "@github/actions-languageservice/complete";
import { CompletionItem, Position } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import { contextProviders } from "./context-providers";
import { RepositoryContext } from "./initializationOptions";
import { TTLCache } from "./utils/cache";
import { valueProviders } from "./value-providers";
@@ -15,6 +16,7 @@ export async function onCompletion(
return await complete(
document,
position,
repoContext && valueProviders(sessionToken, repoContext, cache)
repoContext && valueProviders(sessionToken, repoContext, cache),
repoContext && contextProviders(sessionToken, repoContext, cache)
);
}