2023-02-24 08:53:51 -08:00
|
|
|
import {data, DescriptionDictionary} from "@actions/expressions";
|
|
|
|
|
import {isMapping, isScalar, isString} from "@actions/workflow-parser";
|
2022-12-07 13:26:34 -05:00
|
|
|
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"];
|
|
|
|
|
|
2023-02-22 13:29:03 -08:00
|
|
|
const strategy = workflowContext.job?.strategy ?? workflowContext.reusableWorkflowJob?.strategy;
|
2022-12-07 13:26:34 -05:00
|
|
|
if (!strategy || !isMapping(strategy)) {
|
2023-01-10 17:44:48 -08:00
|
|
|
return new DescriptionDictionary(
|
2022-12-07 13:26:34 -05:00
|
|
|
...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) {
|
2022-12-07 13:26:34 -05:00
|
|
|
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;
|
|
|
|
|
}
|