Separate reusable jobs in the workflow context

This commit is contained in:
Josh Gross
2023-02-03 17:52:48 -05:00
parent 3d5b006138
commit 986ce1bfe4
9 changed files with 37 additions and 37 deletions
@@ -1,6 +1,5 @@
import {data, DescriptionDictionary} from "@github/actions-expressions";
import {isScalar, isString} from "@github/actions-workflow-parser";
import {isJob} from "@github/actions-workflow-parser/model/type-guards";
import {MappingToken} from "@github/actions-workflow-parser/templates/tokens/mapping-token";
import {WorkflowContext} from "../context/workflow-context";
@@ -13,9 +12,8 @@ export function getEnvContext(workflowContext: WorkflowContext): DescriptionDict
}
//job env
const job = workflowContext.job;
if (job && isJob(job) && job.env) {
envContext(job.env, d);
if (workflowContext.job && workflowContext.job.env) {
envContext(workflowContext.job.env, d);
}
//workflow env
@@ -1,6 +1,5 @@
import {data, DescriptionDictionary} from "@github/actions-expressions";
import {isMapping, isSequence} from "@github/actions-workflow-parser";
import {isReusableWorkflowJob} from "@github/actions-workflow-parser/model/type-guards";
import {MappingToken} from "@github/actions-workflow-parser/templates/tokens/mapping-token";
import {WorkflowContext} from "../context/workflow-context";
@@ -8,7 +7,7 @@ export function getJobContext(workflowContext: WorkflowContext): DescriptionDict
// https://docs.github.com/en/actions/learn-github-actions/contexts#job-context
const jobContext = new DescriptionDictionary();
const job = workflowContext.job;
if (!job || isReusableWorkflowJob(job)) {
if (!job) {
return jobContext;
}
@@ -25,14 +25,15 @@ function needsJobContext(job?: WorkflowJob): DescriptionDictionary {
if (job && isJob(job)) {
d.add("outputs", jobOutputs(job));
}
// Can be "success", "failure", "cancelled", or "skipped"
d.add("result", new data.Null());
return d;
}
function jobOutputs(job: Job): DescriptionDictionary {
function jobOutputs(job?: Job): DescriptionDictionary {
const d = new DescriptionDictionary();
if (!job.outputs) {
if (!job?.outputs) {
return d;
}
@@ -1,12 +1,11 @@
import {data, DescriptionDictionary} from "@github/actions-expressions";
import {isReusableWorkflowJob} from "@github/actions-workflow-parser/model/type-guards";
import {Step} from "@github/actions-workflow-parser/model/workflow-template";
import {WorkflowContext} from "../context/workflow-context";
import {getDescription} from "./descriptions";
export function getStepsContext(workflowContext: WorkflowContext): DescriptionDictionary {
const d = new DescriptionDictionary();
if (!workflowContext.job || isReusableWorkflowJob(workflowContext.job)) {
if (!workflowContext.job?.steps) {
return d;
}
@@ -1,6 +1,6 @@
import {isMapping, isSequence, WorkflowTemplate} from "@github/actions-workflow-parser";
import {isJob, isReusableWorkflowJob} from "@github/actions-workflow-parser/model/type-guards";
import {Step, WorkflowJob} from "@github/actions-workflow-parser/model/workflow-template";
import {Step, Job, ReusableWorkflowJob} from "@github/actions-workflow-parser/model/workflow-template";
import {MappingToken} from "@github/actions-workflow-parser/templates/tokens/mapping-token";
import {SequenceToken} from "@github/actions-workflow-parser/templates/tokens/sequence-token";
import {StringToken} from "@github/actions-workflow-parser/templates/tokens/string-token";
@@ -11,8 +11,11 @@ export interface WorkflowContext {
template: WorkflowTemplate | undefined;
/** If the context is for a position within a job, this will be the job */
job?: WorkflowJob;
/** If the context is for a position within a regular job, this will be the job */
job?: Job;
/** If the context is for a position within a reusable workflow job, this will be the reusable workflow job */
reusableWorkflowJob?: ReusableWorkflowJob;
/** If the context is for a position within a step, this will be the step */
step?: Step;
@@ -36,7 +39,15 @@ export function getWorkflowContext(
switch (token.definition?.key) {
case "job": {
const jobID = (token as StringToken).value;
context.job = template.jobs.find(job => job.id.value === jobID);
const job = template.jobs.find(job => job.id.value === jobID);
if (!job) {
break;
}
if (isJob(job)) {
context.job = job;
} else if (isReusableWorkflowJob(job)) {
context.reusableWorkflowJob = job;
}
break;
}
case "steps": {