From b1af463bfa724f96e38007ca6ca7275f587ce4f2 Mon Sep 17 00:00:00 2001 From: Laura Yu <60276246+lauraway@users.noreply.github.com> Date: Wed, 22 Feb 2023 13:29:03 -0800 Subject: [PATCH] Adding reusable workflow jobs to matrix and strategy contexts (#158) --- .../src/context-providers/default.ts | 3 +- .../src/context-providers/matrix.ts | 2 +- .../src/context-providers/strategy.ts | 2 +- .../src/validate.expressions.test.ts | 39 +++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/actions-languageservice/src/context-providers/default.ts b/actions-languageservice/src/context-providers/default.ts index cb31d2d..d8732e3 100644 --- a/actions-languageservice/src/context-providers/default.ts +++ b/actions-languageservice/src/context-providers/default.ts @@ -57,6 +57,7 @@ function getDefaultContext(name: string, workflowContext: WorkflowContext, mode: case "inputs": return getInputsContext(workflowContext); + case "reusableWorkflowJob": case "job": return getJobContext(workflowContext); @@ -114,5 +115,5 @@ function filterContextNames(contextNames: string[], workflowContext: WorkflowCon } function hasStrategy(workflowContext: WorkflowContext): boolean { - return workflowContext.job?.strategy !== undefined; + return workflowContext.job?.strategy !== undefined || workflowContext.reusableWorkflowJob?.strategy !== undefined; } diff --git a/actions-languageservice/src/context-providers/matrix.ts b/actions-languageservice/src/context-providers/matrix.ts index b233d57..c7e8138 100644 --- a/actions-languageservice/src/context-providers/matrix.ts +++ b/actions-languageservice/src/context-providers/matrix.ts @@ -8,7 +8,7 @@ import {ContextValue, Mode} from "./default"; export function getMatrixContext(workflowContext: WorkflowContext, mode: Mode): ContextValue { // https://docs.github.com/en/actions/learn-github-actions/contexts#matrix-context - const strategy = workflowContext.job?.strategy; + const strategy = workflowContext.job?.strategy ?? workflowContext.reusableWorkflowJob?.strategy; if (!strategy || !isMapping(strategy)) { return new DescriptionDictionary(); } diff --git a/actions-languageservice/src/context-providers/strategy.ts b/actions-languageservice/src/context-providers/strategy.ts index 0090936..d60b4a2 100644 --- a/actions-languageservice/src/context-providers/strategy.ts +++ b/actions-languageservice/src/context-providers/strategy.ts @@ -7,7 +7,7 @@ export function getStrategyContext(workflowContext: WorkflowContext): Descriptio // https://docs.github.com/en/actions/learn-github-actions/contexts#strategy-context const keys = ["fail-fast", "job-index", "job-total", "max-parallel"]; - const strategy = workflowContext.job?.strategy; + const strategy = workflowContext.job?.strategy ?? workflowContext.reusableWorkflowJob?.strategy; if (!strategy || !isMapping(strategy)) { return new DescriptionDictionary( ...keys.map(key => { diff --git a/actions-languageservice/src/validate.expressions.test.ts b/actions-languageservice/src/validate.expressions.test.ts index 3a5f54c..7ca5fcb 100644 --- a/actions-languageservice/src/validate.expressions.test.ts +++ b/actions-languageservice/src/validate.expressions.test.ts @@ -505,6 +505,45 @@ jobs: expect(result).toEqual([]); }); + it("reference strategy in reusable workflow", async () => { + const input = ` + on: push + + jobs: + test: + strategy: + fail-fast: true + matrix: + node: [14, 16] + uses: ./reusable-workflow-with-inputs.yaml + with: + username: User-\${{ strategy.fail-fast }} + `; + + const result = await validate(createDocument("wf.yaml", input)); + + expect(result).toEqual([]); + }); + + it("reference matrix in reusable workflow", async () => { + const input = ` + on: push + + jobs: + test: + strategy: + matrix: + node: [14, 16] + uses: ./reusable-workflow-with-inputs.yaml + with: + username: \${{ matrix.node }} + `; + + const result = await validate(createDocument("wf.yaml", input)); + + expect(result).toEqual([]); + }); + it("reference outside of a matrix job", async () => { const input = ` on: push