Merge pull request #29 from github/cschleiden/inputs-context-provider
Add `inputs` context-provider
This commit is contained in:
@@ -166,7 +166,7 @@ jobs:
|
|||||||
needs: [b]
|
needs: [b]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- run: echo "hello \${{ needs.|
|
- run: echo "hello \${{ needs.|
|
||||||
`;
|
`;
|
||||||
const result = await complete(...getPositionFromCursor(input), undefined, contextProviderConfig);
|
const result = await complete(...getPositionFromCursor(input), undefined, contextProviderConfig);
|
||||||
|
|
||||||
@@ -185,7 +185,7 @@ jobs:
|
|||||||
needs: [a]
|
needs: [a]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- run: echo "hello \${{ needs.a.|
|
- run: echo "hello \${{ needs.a.|
|
||||||
`;
|
`;
|
||||||
const result = await complete(...getPositionFromCursor(input), undefined, contextProviderConfig);
|
const result = await complete(...getPositionFromCursor(input), undefined, contextProviderConfig);
|
||||||
|
|
||||||
@@ -212,4 +212,32 @@ jobs:
|
|||||||
|
|
||||||
expect(result.map(x => x.label)).toEqual(["build_id"]);
|
expect(result.map(x => x.label)).toEqual(["build_id"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("inputs", async () => {
|
||||||
|
const input = `
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
default: some value
|
||||||
|
another-name:
|
||||||
|
type: string
|
||||||
|
jobs:
|
||||||
|
a:
|
||||||
|
outputs:
|
||||||
|
build_id: my-build-id
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: echo hello a
|
||||||
|
b:
|
||||||
|
needs: [a]
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: echo "hello \${{ inputs.|
|
||||||
|
`;
|
||||||
|
const result = await complete(...getPositionFromCursor(input), undefined, contextProviderConfig);
|
||||||
|
|
||||||
|
expect(result.map(x => x.label)).toEqual(["another-name", "name"]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {data} from "@github/actions-expressions";
|
import {data} from "@github/actions-expressions";
|
||||||
import {WorkflowContext} from "../context/workflow-context";
|
import {WorkflowContext} from "../context/workflow-context";
|
||||||
import {ContextProviderConfig} from "./config";
|
import {ContextProviderConfig} from "./config";
|
||||||
|
import {getInputsContext} from "./inputs";
|
||||||
import {getNeedsContext} from "./needs";
|
import {getNeedsContext} from "./needs";
|
||||||
|
|
||||||
export async function getContext(
|
export async function getContext(
|
||||||
@@ -13,7 +14,7 @@ export async function getContext(
|
|||||||
for (const contextName of names) {
|
for (const contextName of names) {
|
||||||
let value: data.Dictionary | undefined;
|
let value: data.Dictionary | undefined;
|
||||||
|
|
||||||
value = getDefaultContext(contextName, workflowContext);
|
value = await getDefaultContext(contextName, workflowContext);
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
value = await config?.getContext(contextName);
|
value = await config?.getContext(contextName);
|
||||||
@@ -29,7 +30,7 @@ export async function getContext(
|
|||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDefaultContext(name: string, workflowContext: WorkflowContext): data.Dictionary | undefined {
|
async function getDefaultContext(name: string, workflowContext: WorkflowContext): Promise<data.Dictionary | undefined> {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case "runner":
|
case "runner":
|
||||||
return objectToDictionary({
|
return objectToDictionary({
|
||||||
@@ -39,8 +40,12 @@ function getDefaultContext(name: string, workflowContext: WorkflowContext): data
|
|||||||
tool_cache: "/opt/hostedtoolcache",
|
tool_cache: "/opt/hostedtoolcache",
|
||||||
temp: "/home/runner/work/_temp"
|
temp: "/home/runner/work/_temp"
|
||||||
});
|
});
|
||||||
|
|
||||||
case "needs":
|
case "needs":
|
||||||
return getNeedsContext(workflowContext);
|
return getNeedsContext(workflowContext);
|
||||||
|
|
||||||
|
case "inputs":
|
||||||
|
return getInputsContext(workflowContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import {data} from "@github/actions-expressions";
|
||||||
|
import {WorkflowContext} from "../context/workflow-context";
|
||||||
|
|
||||||
|
export function getInputsContext(workflowContext: WorkflowContext): data.Dictionary {
|
||||||
|
const d = new data.Dictionary();
|
||||||
|
if (!workflowContext?.template?.events) {
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
const event = workflowContext.template.events["workflow_dispatch"];
|
||||||
|
if (!event) {
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
const inputs = event.inputs;
|
||||||
|
for (const inputName of Object.keys(inputs)) {
|
||||||
|
const input = inputs[inputName];
|
||||||
|
switch (input.type) {
|
||||||
|
case "choice":
|
||||||
|
if (input.default) {
|
||||||
|
d.add(inputName, new data.StringData(input.default as string));
|
||||||
|
} else {
|
||||||
|
// Default to the first input or an empty string
|
||||||
|
d.add(inputName, new data.StringData((input.options || [""])[0]));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "environment":
|
||||||
|
if (input.default) {
|
||||||
|
d.add(inputName, new data.StringData(input.default as string));
|
||||||
|
} else {
|
||||||
|
// For now default to an empty value if there is no default value. This will always be an environment, so
|
||||||
|
// we could also dynamically look up environments and default to the first one, but leaving this as a
|
||||||
|
// future enhancement for now.
|
||||||
|
d.add(inputName, new data.StringData(""));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "boolean":
|
||||||
|
d.add(inputName, new data.BooleanData((input.default as boolean) || false));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "string":
|
||||||
|
default:
|
||||||
|
d.add(inputName, new data.StringData((input.default as string) || inputName));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return d;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user