From 64bcc2f287cb35b51cc24a42b2a13f6f72c3ebd2 Mon Sep 17 00:00:00 2001 From: Josh Gross Date: Wed, 25 Jan 2023 15:24:57 -0500 Subject: [PATCH] Skip event context validation for `workflow_call` --- .../src/complete.expressions.test.ts | 18 ++ .../src/context-providers/default.ts | 2 +- .../context-providers/events/eventPayloads.ts | 2 + .../events/workflow_call.json | 184 +++++++++--------- .../src/context-providers/github.ts | 13 +- .../src/validate.expressions.test.ts | 16 ++ 6 files changed, 139 insertions(+), 96 deletions(-) diff --git a/actions-languageservice/src/complete.expressions.test.ts b/actions-languageservice/src/complete.expressions.test.ts index 5e50813..5b4b576 100644 --- a/actions-languageservice/src/complete.expressions.test.ts +++ b/actions-languageservice/src/complete.expressions.test.ts @@ -605,6 +605,24 @@ jobs: // pull_request is part of the pull_request event payload expect(result.map(x => x.label)).toContain("pull_request"); }); + + it("includes event payload for workflow_call", async () => { + const input = ` + on: [workflow_call] + + jobs: + test: + runs-on: ubuntu-latest + steps: + - run: echo \${{ github.event.| }} + `; + + const result = await complete(...getPositionFromCursor(input), undefined, undefined); + + // We don't validate github.event for workflow_call, + // but there should still be auto-completion suggestions + expect(result.length).toBeGreaterThan(0); + }); }); describe("steps context", () => { diff --git a/actions-languageservice/src/context-providers/default.ts b/actions-languageservice/src/context-providers/default.ts index 1d38d99..cb31d2d 100644 --- a/actions-languageservice/src/context-providers/default.ts +++ b/actions-languageservice/src/context-providers/default.ts @@ -52,7 +52,7 @@ function getDefaultContext(name: string, workflowContext: WorkflowContext, mode: return getEnvContext(workflowContext); case "github": - return getGithubContext(workflowContext); + return getGithubContext(workflowContext, mode); case "inputs": return getInputsContext(workflowContext); diff --git a/actions-languageservice/src/context-providers/events/eventPayloads.ts b/actions-languageservice/src/context-providers/events/eventPayloads.ts index dcfeb69..a5286a8 100644 --- a/actions-languageservice/src/context-providers/events/eventPayloads.ts +++ b/actions-languageservice/src/context-providers/events/eventPayloads.ts @@ -47,6 +47,7 @@ import status from "./status.json"; import team from "./team.json"; import team_add from "./team_add.json"; import watch from "./watch.json"; +import workflow_call from "./workflow_call.json"; import workflow_dispatch from "./workflow_dispatch.json"; import workflow_run from "./workflow_run.json"; @@ -101,6 +102,7 @@ export const eventPayloads: {[key: string]: Object} = { team, team_add, watch, + workflow_call, workflow_dispatch, workflow_run }; diff --git a/actions-languageservice/src/context-providers/events/workflow_call.json b/actions-languageservice/src/context-providers/events/workflow_call.json index 52ec8f1..2a1c20e 100644 --- a/actions-languageservice/src/context-providers/events/workflow_call.json +++ b/actions-languageservice/src/context-providers/events/workflow_call.json @@ -1,115 +1,111 @@ { - "ref": "refs/tags/simple-tag", - "before": "6113728f27ae82c7b1a177c8d03f9e96e0adf246", - "after": "0000000000000000000000000000000000000000", - "created": false, - "deleted": true, - "forced": false, - "base_ref": null, - "compare": "https://github.com/Codertocat/Hello-World/compare/6113728f27ae...000000000000", - "commits": [], - "head_commit": null, + "action": "on-demand-test", "repository": { - "id": 186853002, - "node_id": "MDEwOlJlcG9zaXRvcnkxODY4NTMwMDI=", - "name": "Hello-World", - "full_name": "Codertocat/Hello-World", - "private": false, + "id": 17273051, + "node_id": "MDEwOlJlcG9zaXRvcnkxNzI3MzA1MQ==", + "name": "octo-repo", + "full_name": "octo-org/octo-repo", + "private": true, "owner": { - "name": "Codertocat", - "email": "21031067+Codertocat@users.noreply.github.com", - "login": "Codertocat", - "id": 21031067, - "node_id": "MDQ6VXNlcjIxMDMxMDY3", - "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "login": "octo-org", + "id": 6811672, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjY4MTE2NzI=", + "avatar_url": "https://avatars3.githubusercontent.com/u/6811672?v=4", "gravatar_id": "", - "url": "https://api.github.com/users/Codertocat", - "html_url": "https://github.com/Codertocat", - "followers_url": "https://api.github.com/users/Codertocat/followers", - "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", - "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", - "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", - "organizations_url": "https://api.github.com/users/Codertocat/orgs", - "repos_url": "https://api.github.com/users/Codertocat/repos", - "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", - "received_events_url": "https://api.github.com/users/Codertocat/received_events", - "type": "User", + "url": "https://api.github.com/users/octo-org", + "html_url": "https://github.com/octo-org", + "followers_url": "https://api.github.com/users/octo-org/followers", + "following_url": "https://api.github.com/users/octo-org/following{/other_user}", + "gists_url": "https://api.github.com/users/octo-org/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octo-org/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octo-org/subscriptions", + "organizations_url": "https://api.github.com/users/octo-org/orgs", + "repos_url": "https://api.github.com/users/octo-org/repos", + "events_url": "https://api.github.com/users/octo-org/events{/privacy}", + "received_events_url": "https://api.github.com/users/octo-org/received_events", + "type": "Organization", "site_admin": false }, - "html_url": "https://github.com/Codertocat/Hello-World", - "description": null, + "html_url": "https://github.com/octo-org/octo-repo", + "description": "My first repo on GitHub!", "fork": false, - "url": "https://github.com/Codertocat/Hello-World", - "forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks", - "keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}", - "collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}", - "teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams", - "hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks", - "issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}", - "events_url": "https://api.github.com/repos/Codertocat/Hello-World/events", - "assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}", - "branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}", - "tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags", - "blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}", - "git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}", - "git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}", - "trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}", - "statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}", - "languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages", - "stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers", - "contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors", - "subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers", - "subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription", - "commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}", - "git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}", - "comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}", - "issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}", - "contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}", - "compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}", - "merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges", - "archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}", - "downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads", - "issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}", - "pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}", - "milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}", - "notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}", - "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}", - "releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}", - "deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments", - "created_at": 1557933565, - "updated_at": "2019-05-15T15:20:41Z", - "pushed_at": 1557933657, - "git_url": "git://github.com/Codertocat/Hello-World.git", - "ssh_url": "git@github.com:Codertocat/Hello-World.git", - "clone_url": "https://github.com/Codertocat/Hello-World.git", - "svn_url": "https://github.com/Codertocat/Hello-World", - "homepage": null, - "size": 0, + "url": "https://api.github.com/repos/octo-org/octo-repo", + "forks_url": "https://api.github.com/repos/octo-org/octo-repo/forks", + "keys_url": "https://api.github.com/repos/octo-org/octo-repo/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/octo-org/octo-repo/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/octo-org/octo-repo/teams", + "hooks_url": "https://api.github.com/repos/octo-org/octo-repo/hooks", + "issue_events_url": "https://api.github.com/repos/octo-org/octo-repo/issues/events{/number}", + "events_url": "https://api.github.com/repos/octo-org/octo-repo/events", + "assignees_url": "https://api.github.com/repos/octo-org/octo-repo/assignees{/user}", + "branches_url": "https://api.github.com/repos/octo-org/octo-repo/branches{/branch}", + "tags_url": "https://api.github.com/repos/octo-org/octo-repo/tags", + "blobs_url": "https://api.github.com/repos/octo-org/octo-repo/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/octo-org/octo-repo/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/octo-org/octo-repo/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/octo-org/octo-repo/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/octo-org/octo-repo/statuses/{sha}", + "languages_url": "https://api.github.com/repos/octo-org/octo-repo/languages", + "stargazers_url": "https://api.github.com/repos/octo-org/octo-repo/stargazers", + "contributors_url": "https://api.github.com/repos/octo-org/octo-repo/contributors", + "subscribers_url": "https://api.github.com/repos/octo-org/octo-repo/subscribers", + "subscription_url": "https://api.github.com/repos/octo-org/octo-repo/subscription", + "commits_url": "https://api.github.com/repos/octo-org/octo-repo/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/octo-org/octo-repo/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/octo-org/octo-repo/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/octo-org/octo-repo/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/octo-org/octo-repo/contents/{+path}", + "compare_url": "https://api.github.com/repos/octo-org/octo-repo/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/octo-org/octo-repo/merges", + "archive_url": "https://api.github.com/repos/octo-org/octo-repo/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/octo-org/octo-repo/downloads", + "issues_url": "https://api.github.com/repos/octo-org/octo-repo/issues{/number}", + "pulls_url": "https://api.github.com/repos/octo-org/octo-repo/pulls{/number}", + "milestones_url": "https://api.github.com/repos/octo-org/octo-repo/milestones{/number}", + "notifications_url": "https://api.github.com/repos/octo-org/octo-repo/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/octo-org/octo-repo/labels{/name}", + "releases_url": "https://api.github.com/repos/octo-org/octo-repo/releases{/id}", + "deployments_url": "https://api.github.com/repos/octo-org/octo-repo/deployments", + "created_at": "2014-02-28T02:42:51Z", + "updated_at": "2018-10-10T15:58:51Z", + "pushed_at": "2018-10-10T15:58:47Z", + "git_url": "git://github.com/octo-org/octo-repo.git", + "ssh_url": "git@github.com:octo-org/octo-repo.git", + "clone_url": "https://github.com/octo-org/octo-repo.git", + "svn_url": "https://github.com/octo-org/octo-repo", + "homepage": "", + "size": 59, "stargazers_count": 0, "watchers_count": 0, - "language": "Ruby", + "language": "JavaScript", "has_issues": true, "has_projects": true, "has_downloads": true, "has_wiki": true, - "has_pages": true, + "has_pages": false, "forks_count": 1, "mirror_url": null, "archived": false, - "disabled": false, - "open_issues_count": 2, + "open_issues_count": 23, "license": null, "forks": 1, - "open_issues": 2, + "open_issues": 23, "watchers": 0, - "default_branch": "master", - "stargazers": 0, - "master_branch": "master" + "default_branch": "master" }, - "pusher": { - "name": "Codertocat", - "email": "21031067+Codertocat@users.noreply.github.com" + "organization": { + "login": "octo-org", + "id": 6811672, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjY4MTE2NzI=", + "url": "https://api.github.com/orgs/octo-org", + "repos_url": "https://api.github.com/orgs/octo-org/repos", + "events_url": "https://api.github.com/orgs/octo-org/events", + "hooks_url": "https://api.github.com/orgs/octo-org/hooks", + "issues_url": "https://api.github.com/orgs/octo-org/issues", + "members_url": "https://api.github.com/orgs/octo-org/members{/member}", + "public_members_url": "https://api.github.com/orgs/octo-org/public_members{/member}", + "avatar_url": "https://avatars3.githubusercontent.com/u/6811672?v=4", + "description": "Working better together!" }, "sender": { "login": "Codertocat", @@ -130,5 +126,9 @@ "received_events_url": "https://api.github.com/users/Codertocat/received_events", "type": "User", "site_admin": false + }, + "installation": { + "id": 375706, + "node_id": "MDIzOkludGVncmF0aW9uSW5zdGFsbGF0aW9uMzc1NzA2" } } \ No newline at end of file diff --git a/actions-languageservice/src/context-providers/github.ts b/actions-languageservice/src/context-providers/github.ts index df3d72e..2d6f5a5 100644 --- a/actions-languageservice/src/context-providers/github.ts +++ b/actions-languageservice/src/context-providers/github.ts @@ -1,11 +1,12 @@ import {data, DescriptionDictionary} from "@github/actions-expressions"; import {ExpressionData} from "@github/actions-expressions/data/expressiondata"; import {WorkflowContext} from "../context/workflow-context"; +import {Mode} from "./default"; import {getDescription} from "./descriptions"; import {eventPayloads} from "./events/eventPayloads"; import {getInputsContext} from "./inputs"; -export function getGithubContext(workflowContext: WorkflowContext): DescriptionDictionary { +export function getGithubContext(workflowContext: WorkflowContext, mode: Mode): DescriptionDictionary { // https://docs.github.com/en/actions/learn-github-actions/contexts#github-cwontext const keys = [ "action", @@ -51,7 +52,7 @@ export function getGithubContext(workflowContext: WorkflowContext): DescriptionD if (key == "event") { return { key, - value: getEventContext(workflowContext), + value: getEventContext(workflowContext, mode), description }; } @@ -65,7 +66,7 @@ export function getGithubContext(workflowContext: WorkflowContext): DescriptionD ); } -function getEventContext(workflowContext: WorkflowContext): ExpressionData { +function getEventContext(workflowContext: WorkflowContext, mode: Mode): ExpressionData { const d = new data.Dictionary(); const eventsConfig = workflowContext?.template?.events; @@ -73,6 +74,12 @@ function getEventContext(workflowContext: WorkflowContext): ExpressionData { return d; } + // For callable workflows, the event is inherited from the calling workflow + // Allow any value for this case + if (eventsConfig.workflow_call && mode == Mode.Validation) { + return new data.Null(); + } + const inputs = getInputsContext(workflowContext); if (inputs.values().length > 0) { d.add("inputs", inputs); diff --git a/actions-languageservice/src/validate.expressions.test.ts b/actions-languageservice/src/validate.expressions.test.ts index 57a0eaa..ce58ee7 100644 --- a/actions-languageservice/src/validate.expressions.test.ts +++ b/actions-languageservice/src/validate.expressions.test.ts @@ -1254,5 +1254,21 @@ jobs: expect(result).toEqual([]); }); + + it("allows any event property for workflow_call", async () => { + const input = ` +on: + workflow_call: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - run: echo \${{ github.event.anything }}`; + + const result = await validate(createDocument("wf.yaml", input)); + + expect(result).toEqual([]); + }); }); });