Files
languageservices/languageservice/src/context-providers/strategy.ts
T

40 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-02-24 08:53:51 -08:00
import {data, DescriptionDictionary} from "@actions/expressions";
import {isMapping, isScalar, isString} from "@actions/workflow-parser";
import {WorkflowContext} from "../context/workflow-context";
import {scalarToData} from "../utils/scalar-to-data";
2022-12-06 18:02:54 -05:00
2023-01-10 17:44:48 -08:00
export function getStrategyContext(workflowContext: WorkflowContext): DescriptionDictionary {
2022-12-06 18:02:54 -05:00
// 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 ?? workflowContext.reusableWorkflowJob?.strategy;
if (!strategy || !isMapping(strategy)) {
2023-01-10 17:44:48 -08:00
return new DescriptionDictionary(
...keys.map(key => {
return {key, value: new data.Null()};
})
);
}
2023-01-10 17:44:48 -08:00
const strategyContext = new DescriptionDictionary();
2022-12-08 15:22:34 -05:00
for (const pair of strategy) {
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;
}