Merge pull request #41 from github/joshmgross/strategy-context-pt2
Filter `strategy` context to matrix jobs and add values
This commit is contained in:
@@ -337,7 +337,7 @@ jobs:
|
||||
});
|
||||
|
||||
describe("strategy context", () => {
|
||||
it.failing("strategy is not suggested when outside of a matrix job", async () => {
|
||||
it("strategy is not suggested when outside of a matrix job", async () => {
|
||||
const input = `
|
||||
on: push
|
||||
|
||||
@@ -381,15 +381,15 @@ jobs:
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
test-group: [1, 2]
|
||||
node: [14, 16]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm test > test-job-\${{ strategy.| }}.txt
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
test-group: [1, 2]
|
||||
node: [14, 16]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm test > test-job-\${{ strategy.| }}.txt
|
||||
`;
|
||||
|
||||
const result = await complete(...getPositionFromCursor(input), undefined, contextProviderConfig);
|
||||
|
||||
@@ -4,7 +4,7 @@ import {ContextProviderConfig} from "./config";
|
||||
import {getInputsContext} from "./inputs";
|
||||
import {getNeedsContext} from "./needs";
|
||||
import {getStepsContext} from "./steps";
|
||||
import {getStrategyContext} from "./strategy";
|
||||
import {allowStrategyContext, getStrategyContext} from "./strategy";
|
||||
|
||||
export async function getContext(
|
||||
names: string[],
|
||||
@@ -13,7 +13,8 @@ export async function getContext(
|
||||
): Promise<data.Dictionary> {
|
||||
const context = new data.Dictionary();
|
||||
|
||||
for (const contextName of names) {
|
||||
const filteredNames = filterContextNames(names, workflowContext);
|
||||
for (const contextName of filteredNames) {
|
||||
let value: data.Dictionary | undefined;
|
||||
|
||||
value = await getDefaultContext(contextName, workflowContext);
|
||||
@@ -53,7 +54,7 @@ async function getDefaultContext(name: string, workflowContext: WorkflowContext)
|
||||
return getStepsContext(workflowContext);
|
||||
|
||||
case "strategy":
|
||||
return getStrategyContext();
|
||||
return getStrategyContext(workflowContext);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
@@ -67,3 +68,13 @@ function objectToDictionary(object: {[key: string]: string}): data.Dictionary {
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
function filterContextNames(contextNames: string[], workflowContext: WorkflowContext): string[] {
|
||||
return contextNames.filter(name => {
|
||||
switch (name) {
|
||||
case "strategy":
|
||||
return allowStrategyContext(workflowContext);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,12 +1,44 @@
|
||||
import {data} from "@github/actions-expressions";
|
||||
import {isMapping, isScalar, isString} from "@github/actions-workflow-parser";
|
||||
import {WorkflowContext} from "../context/workflow-context";
|
||||
import {scalarToData} from "../utils/scalar-to-data";
|
||||
|
||||
export function getStrategyContext(): data.Dictionary {
|
||||
export function getStrategyContext(workflowContext: WorkflowContext): data.Dictionary {
|
||||
// https://docs.github.com/en/actions/learn-github-actions/contexts#strategy-context
|
||||
const keys = ["fail-fast", "job-index", "job-total", "max-parallel"];
|
||||
|
||||
return new data.Dictionary(
|
||||
...keys.map(key => {
|
||||
return {key, value: new data.Null()};
|
||||
})
|
||||
);
|
||||
const strategy = workflowContext.job?.strategy;
|
||||
if (!strategy || !isMapping(strategy)) {
|
||||
return new data.Dictionary(
|
||||
...keys.map(key => {
|
||||
return {key, value: new data.Null()};
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const strategyContext = new data.Dictionary();
|
||||
for (let i = 0; i < strategy.count; i++) {
|
||||
const pair = strategy.get(i);
|
||||
if (!isString(pair.key)) {
|
||||
continue;
|
||||
}
|
||||
if (!keys.includes(pair.key.value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const value = isScalar(pair.value) ? scalarToData(pair.value) : new data.Null();
|
||||
strategyContext.add(pair.key.value, value);
|
||||
}
|
||||
|
||||
for (const key of keys) {
|
||||
if (!strategyContext.get(key)) {
|
||||
strategyContext.add(key, new data.Null());
|
||||
}
|
||||
}
|
||||
|
||||
return strategyContext;
|
||||
}
|
||||
|
||||
export function allowStrategyContext(workflowContext: WorkflowContext): boolean {
|
||||
return workflowContext.job?.strategy !== undefined;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import {data} from "@github/actions-expressions";
|
||||
import {isBoolean, isNumber, isString} from "@github/actions-workflow-parser";
|
||||
import {ScalarToken} from "@github/actions-workflow-parser/templates/tokens/scalar-token";
|
||||
import {TokenType} from "@github/actions-workflow-parser/templates/tokens/types";
|
||||
|
||||
export function scalarToData(scalar: ScalarToken): data.ExpressionData {
|
||||
if (isNumber(scalar)) {
|
||||
return new data.NumberData(scalar.value);
|
||||
}
|
||||
|
||||
if (isString(scalar)) {
|
||||
return new data.StringData(scalar.value);
|
||||
}
|
||||
|
||||
if (isBoolean(scalar)) {
|
||||
return new data.BooleanData(scalar.value);
|
||||
}
|
||||
|
||||
if (scalar.templateTokenType === TokenType.Null) {
|
||||
return new data.Null();
|
||||
}
|
||||
|
||||
return new data.StringData(scalar.toDisplayString());
|
||||
}
|
||||
@@ -245,7 +245,7 @@ jobs:
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it.failing("reference outside of a matrix job", async () => {
|
||||
it("reference outside of a matrix job", async () => {
|
||||
const input = `
|
||||
on: push
|
||||
|
||||
|
||||
Generated
+6
-6
@@ -665,9 +665,9 @@
|
||||
"link": true
|
||||
},
|
||||
"node_modules/@github/actions-workflow-parser": {
|
||||
"version": "0.0.28",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.28/ad1da14904a4aa3482176614d454a85041f1e23b",
|
||||
"integrity": "sha512-x0P5FZ78ige6o03wU4BgfrfkzDOL+iCFbBHk6mTNL4ZX/dR4xCUn8mfhOb9JxCKhuO3UqN5y1+mitcgejGHyfg==",
|
||||
"version": "0.0.29",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.29/ecf3d1c9727e4f5f71e52863f456672b173b7096",
|
||||
"integrity": "sha512-VJLRv+GHD0E7S9uUCH89jIIA3iMdKRcqhrKwKq5TE5u/6Lap5nj1Juy48tBoCFAog07lhuqyqgWjP6FHHyAvPg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@github/actions-expressions": "*",
|
||||
@@ -10927,9 +10927,9 @@
|
||||
}
|
||||
},
|
||||
"@github/actions-workflow-parser": {
|
||||
"version": "0.0.28",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.28/ad1da14904a4aa3482176614d454a85041f1e23b",
|
||||
"integrity": "sha512-x0P5FZ78ige6o03wU4BgfrfkzDOL+iCFbBHk6mTNL4ZX/dR4xCUn8mfhOb9JxCKhuO3UqN5y1+mitcgejGHyfg==",
|
||||
"version": "0.0.29",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.29/ecf3d1c9727e4f5f71e52863f456672b173b7096",
|
||||
"integrity": "sha512-VJLRv+GHD0E7S9uUCH89jIIA3iMdKRcqhrKwKq5TE5u/6Lap5nj1Juy48tBoCFAog07lhuqyqgWjP6FHHyAvPg==",
|
||||
"requires": {
|
||||
"@github/actions-expressions": "*",
|
||||
"yaml": "^2.0.0-8"
|
||||
|
||||
Reference in New Issue
Block a user