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

68 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-02-24 08:53:51 -08:00
import {data, DescriptionDictionary} from "@actions/expressions";
import {isMapping, isSequence} from "@actions/workflow-parser";
import {MappingToken} from "@actions/workflow-parser/templates/tokens/mapping-token";
2022-12-12 13:48:31 -08:00
import {WorkflowContext} from "../context/workflow-context";
2023-01-10 17:44:48 -08:00
export function getJobContext(workflowContext: WorkflowContext): DescriptionDictionary {
2022-12-12 13:48:31 -08:00
// https://docs.github.com/en/actions/learn-github-actions/contexts#job-context
2023-01-10 17:44:48 -08:00
const jobContext = new DescriptionDictionary();
2022-12-15 14:31:38 -08:00
const job = workflowContext.job;
if (!job) {
2022-12-16 14:16:08 -08:00
return jobContext;
2022-12-12 13:48:31 -08:00
}
2022-12-15 14:31:38 -08:00
// Container
const jobContainer = job.container;
if (jobContainer && isMapping(jobContainer)) {
2022-12-16 10:58:09 -08:00
const containerContext = createContainerContext(jobContainer, false);
2022-12-15 14:31:38 -08:00
jobContext.add("container", containerContext);
}
// Services
const jobServices = job.services;
if (jobServices && isMapping(jobServices)) {
2023-01-10 17:44:48 -08:00
const servicesContext = new DescriptionDictionary();
2022-12-15 14:31:38 -08:00
for (const service of jobServices) {
if (!isMapping(service.value)) {
2022-12-16 14:16:08 -08:00
continue;
2022-12-15 14:31:38 -08:00
}
2022-12-16 10:58:09 -08:00
const serviceContext = createContainerContext(service.value, true);
2022-12-15 14:31:38 -08:00
servicesContext.add(service.key.toString(), serviceContext);
}
jobContext.add("services", servicesContext);
}
// Status
jobContext.add("status", new data.Null());
2022-12-12 13:48:31 -08:00
return jobContext;
}
2022-12-15 14:31:38 -08:00
2022-12-15 15:13:29 -08:00
function createContainerContext(container: MappingToken, isServices: boolean): data.Dictionary {
2022-12-15 14:31:38 -08:00
const containerContext = new data.Dictionary();
2022-12-16 14:16:08 -08:00
for (const {key, value} of container) {
if (isSequence(value)) {
2022-12-15 15:13:29 -08:00
// service ports are the only thing that is part of the job context
2022-12-16 14:16:08 -08:00
if (key.toString() !== "ports") {
continue;
}
2022-12-16 14:16:08 -08:00
const ports = new data.Dictionary();
for (const item of value) {
// We can determine the context mapping fully only if the port is defined
// as a mapping (i.e. <port1>:<port2>), single ports are assigned randomly
2022-12-16 14:16:08 -08:00
const portParts = item.toString().split(":");
if (isServices && portParts.length === 2) {
ports.add(portParts[1], new data.StringData(portParts[0]));
2022-12-16 14:16:08 -08:00
} else {
2022-12-16 10:58:09 -08:00
// If the port isn't a mapping, just use null
ports.add(portParts[0], new data.Null());
}
2022-12-15 14:31:38 -08:00
}
2022-12-16 14:16:08 -08:00
containerContext.add(key.toString(), ports);
2022-12-15 14:31:38 -08:00
}
}
2022-12-15 15:13:29 -08:00
containerContext.add("id", new data.Null());
containerContext.add("network", new data.Null());
2022-12-15 14:31:38 -08:00
return containerContext;
}