Add a context provider for secrets
This commit is contained in:
@@ -4,13 +4,18 @@ import {ContextProviderConfig} from "./context-providers/config";
|
||||
import {getPositionFromCursor} from "./test-utils/cursor-position";
|
||||
|
||||
const contextProviderConfig: ContextProviderConfig = {
|
||||
getContext: (context: string) => {
|
||||
getContext: async (context: string) => {
|
||||
switch (context) {
|
||||
case "github":
|
||||
return new data.Dictionary({
|
||||
key: "event",
|
||||
value: new data.StringData("push")
|
||||
});
|
||||
case "secrets":
|
||||
return new data.Dictionary({
|
||||
key: "DEPLOY_KEY",
|
||||
value: new data.StringData("DEPLOY_KEY")
|
||||
});
|
||||
}
|
||||
|
||||
return undefined;
|
||||
@@ -125,4 +130,22 @@ jobs:
|
||||
expect(result.map(x => x.label)).toEqual(["event"]);
|
||||
});
|
||||
});
|
||||
|
||||
it("context inherited from parent", async () => {
|
||||
// The token definition is just a `string` and
|
||||
// the parent `step-with` holds the allowed context
|
||||
const input = `
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/deploy@v100
|
||||
with:
|
||||
deploy-key: \${{ secrets.|
|
||||
`;
|
||||
const result = await complete(...getPositionFromCursor(input), undefined, contextProviderConfig);
|
||||
|
||||
expect(result.map(x => x.label)).toEqual(["DEPLOY_KEY"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,6 +12,7 @@ import {ContextProviderConfig} from "./context-providers/config";
|
||||
import {getContext} from "./context-providers/default";
|
||||
import {getWorkflowContext, WorkflowContext} from "./context/workflow-context";
|
||||
import {nullTrace} from "./nulltrace";
|
||||
import {getAllowedContext} from "./utils/allowed-context";
|
||||
import {findToken} from "./utils/find-token";
|
||||
import {transform} from "./utils/transform";
|
||||
import {Value, ValueProviderConfig} from "./value-providers/config";
|
||||
@@ -80,7 +81,8 @@ export async function complete(
|
||||
|
||||
const expressionInput = (getExpressionInput(currentInput, relCharPos) || "").trim();
|
||||
|
||||
const context = getContext(token.definition?.readerContext || [], contextProviderConfig);
|
||||
const allowedContext = getAllowedContext(token, parent!);
|
||||
const context = await getContext(allowedContext, contextProviderConfig);
|
||||
|
||||
return completeExpression(expressionInput, context, []);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import {Dictionary} from "@github/actions-expressions/data/dictionary";
|
||||
import {ExpressionData, Pair} from "@github/actions-expressions/data/expressiondata";
|
||||
|
||||
export type ContextProviderConfig = {
|
||||
getContext: (name: string) => data.Dictionary | undefined;
|
||||
getContext: (name: string) => Promise<data.Dictionary | undefined>;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {data} from "@github/actions-expressions";
|
||||
import {ContextProviderConfig} from "./config";
|
||||
|
||||
export function getContext(names: string[], config: ContextProviderConfig | undefined): data.Dictionary {
|
||||
export async function getContext(names: string[], config: ContextProviderConfig | undefined): Promise<data.Dictionary> {
|
||||
const context = new data.Dictionary();
|
||||
|
||||
for (const contextName of names) {
|
||||
@@ -10,7 +10,7 @@ export function getContext(names: string[], config: ContextProviderConfig | unde
|
||||
value = getDefaultContext(contextName);
|
||||
|
||||
if (!value) {
|
||||
value = config?.getContext(contextName);
|
||||
value = await config?.getContext(contextName);
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
|
||||
|
||||
export function getAllowedContext(token: TemplateToken, parent: TemplateToken): string[] {
|
||||
// Workaround for https://github.com/github/c2c-actions-experience/issues/6876
|
||||
// Context is inherited from the parent
|
||||
const allowedContext = new Set<string>();
|
||||
for (const t of [token, parent]) {
|
||||
if (t.definition?.readerContext) {
|
||||
for (const context of t.definition.readerContext) {
|
||||
allowedContext.add(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(allowedContext);
|
||||
}
|
||||
@@ -116,7 +116,7 @@ async function additionalValidations(
|
||||
for (const token of TemplateToken.traverse(root)) {
|
||||
// If this is an expression, validate it
|
||||
if (isBasicExpression(token)) {
|
||||
validateExpression(diagnostics, token, contextProviderConfig);
|
||||
await validateExpression(diagnostics, token, contextProviderConfig);
|
||||
}
|
||||
|
||||
// Allowed values coming from the schema have already been validated. Only check if
|
||||
@@ -193,7 +193,7 @@ function getProviderContext(
|
||||
return getWorkflowContext(documentUri, template, path);
|
||||
}
|
||||
|
||||
function validateExpression(
|
||||
async function validateExpression(
|
||||
diagnostics: Diagnostic[],
|
||||
token: BasicExpressionToken,
|
||||
contextProviderConfig: ContextProviderConfig | undefined
|
||||
@@ -217,7 +217,7 @@ function validateExpression(
|
||||
}
|
||||
|
||||
try {
|
||||
const context = getContext(namedContexts, contextProviderConfig);
|
||||
const context = await getContext(namedContexts, contextProviderConfig);
|
||||
|
||||
const e = new Evaluator(expr, wrapDictionary(context));
|
||||
e.evaluate();
|
||||
|
||||
Reference in New Issue
Block a user