Merge pull request #53 from github/elbrenn/github-context

Add basic github context provider
This commit is contained in:
Beth Brennan
2022-12-12 19:36:35 -05:00
committed by GitHub
3 changed files with 78 additions and 10 deletions
@@ -36,7 +36,7 @@ describe("expressions", () => {
describe("top-level auto-complete", () => {
it("single region", async () => {
const input = "run-name: ${{ | }}";
const result = await complete(...getPositionFromCursor(input), undefined, contextProviderConfig);
const result = await complete(...getPositionFromCursor(input), undefined, undefined);
expect(result.map(x => x.label)).toEqual([
"github",
@@ -320,6 +320,23 @@ jobs:
expect(result).toEqual([]);
});
it("github context includes expected keys", async () => {
const input = `
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: echo \${{ github.| }}
`;
const result = await complete(...getPositionFromCursor(input), undefined, undefined);
expect(result.map(x => x.label)).toContain("actor");
});
describe("steps context", () => {
it("includes defined step IDs", async () => {
const input = `
@@ -2,6 +2,7 @@ import {data} from "@github/actions-expressions";
import {Kind} from "@github/actions-expressions/data/expressiondata";
import {WorkflowContext} from "../context/workflow-context";
import {ContextProviderConfig} from "./config";
import {getGithubContext} from "./github";
import {getInputsContext} from "./inputs";
import {getMatrixContext} from "./matrix";
import {getNeedsContext} from "./needs";
@@ -44,6 +45,18 @@ export async function getContext(
function getDefaultContext(name: string, workflowContext: WorkflowContext, mode: Mode): ContextValue | undefined {
switch (name) {
case "github":
return getGithubContext();
case "inputs":
return getInputsContext(workflowContext);
case "matrix":
return getMatrixContext(workflowContext, mode);
case "needs":
return getNeedsContext(workflowContext);
case "runner":
return objectToDictionary({
os: "Linux",
@@ -53,12 +66,6 @@ function getDefaultContext(name: string, workflowContext: WorkflowContext, mode:
temp: "/home/runner/work/_temp"
});
case "needs":
return getNeedsContext(workflowContext);
case "inputs":
return getInputsContext(workflowContext);
case "secrets":
return objectToDictionary({GITHUB_TOKEN: "***"});
@@ -67,9 +74,6 @@ function getDefaultContext(name: string, workflowContext: WorkflowContext, mode:
case "strategy":
return getStrategyContext(workflowContext);
case "matrix":
return getMatrixContext(workflowContext, mode);
}
return undefined;
@@ -0,0 +1,47 @@
import {data} from "@github/actions-expressions";
export function getGithubContext(): data.Dictionary {
// https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
const keys = [
"action",
"action_path",
"action_ref",
"action_repository",
"action_status",
"actor",
"api_url",
"base_ref",
"env",
"event",
"event_name",
"event_path",
"graphql_url",
"head_ref",
"job",
"ref",
"ref_name",
"ref_protected",
"ref_type",
"path",
"repository",
"repository_owner",
"repositoryUrl",
"retention_days",
"run_id",
"run_number",
"run_attempt",
"secret_source",
"server_url",
"sha",
"token",
"triggering_actor",
"workflow",
"workspace"
];
return new data.Dictionary(
...keys.map(key => {
return {key, value: new data.Null()};
})
);
}