Skip to content

Commit

Permalink
Add showWaitingRunner option to createGanttJobs())
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesin11 committed Jul 2, 2024
1 parent 5cebcdf commit 3b79144
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const workflowJobs = await fetchWorkflowRunJobs(
runUrl.runId,
runAttempt,
);
const gantt = createMermaid(workflow, workflowJobs);
const gantt = createMermaid(workflow, workflowJobs, {});

if (options.output) {
await Deno.writeTextFile(options.output, gantt);
Expand Down
113 changes: 113 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/post.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const main = async () => {
debug(JSON.stringify(workflowJobs, null, 2));

info("Create gantt mermaid diagram...");
const gantt = createMermaid(workflow, workflowJobs);
const gantt = createMermaid(workflow, workflowJobs, {});
await summary.addRaw(gantt).write();
debug(gantt);

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export type StepConclusion =
| "timed_out"
| "action_required"
| null;

export type GanttOptions = {
showWaitingRunner?: boolean;
};
66 changes: 44 additions & 22 deletions src/workflow_gantt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
formatName,
formatSection,
} from "./format_util.ts";
import type { ganttJob, ganttStep, StepConclusion } from "./types.ts";
import type {
ganttJob,
GanttOptions,
ganttStep,
StepConclusion,
} from "./types.ts";

// ref: MAX_TEXTLENGTH https://github.com/mermaid-js/mermaid/blob/develop/packages/mermaid/src/mermaidAPI.ts
const MERMAID_MAX_CHAR = 50_000;
Expand All @@ -29,29 +34,16 @@ const createWaitingRunnerStep = (
workflow: Workflow,
job: WorkflowJobs[0],
jobIndex: number,
): ganttStep => {
): ganttStep | undefined => {
const status: ganttStep["status"] = "active";

// job.created_at does not exist in < GHES v3.9.
// So it is not possible to calculate the elapsed time the runner is waiting for a job, is not supported instead of the elapsed time.
// Also, it is not possible to create an exact job start time position. So use job.started_at instead of job.created_at.
if (job.created_at === undefined) {
const startJobElapsedSec = diffSec(
workflow.run_started_at,
job.started_at,
);
// dummy sec for gantt look and feel
const waitingRunnerElapsedSec = startJobElapsedSec;
return {
name: `Waiting for a runner (not supported < GHES v3.9)`,
id: `job${jobIndex}-0`,
status,
// dummy position for gantt look and feel
position: formatElapsedTime(0),
sec: waitingRunnerElapsedSec,
};
// >= GHES v3.9 or GitHub.com
return undefined;
} else {
// >= GHES v3.9 or GitHub.com
const startJobElapsedSec = diffSec(
workflow.run_started_at,
job.created_at,
Expand All @@ -70,17 +62,42 @@ const createWaitingRunnerStep = (
export const createGanttJobs = (
workflow: Workflow,
workflowJobs: WorkflowJobs,
showWaitingRunner = true,
): ganttJob[] => {
return filterJobs(workflowJobs).map(
(job, jobIndex, _jobs): ganttJob => {
(job, jobIndex, _jobs): ganttJob | undefined => {
if (job.steps === undefined) return undefined;

const section = escapeName(job.name);
let firstStep: ganttStep;

const waitingRunnerStep = createWaitingRunnerStep(
workflow,
job,
jobIndex,
);
if (!showWaitingRunner || waitingRunnerStep === undefined) {
const rawFirstStep = job.steps.shift();
if (rawFirstStep === undefined) return undefined;

const steps = filterSteps(job.steps ?? []).map(
const stepElapsedSec = diffSec(
rawFirstStep.started_at,
rawFirstStep.completed_at,
);
firstStep = {
name: formatName(rawFirstStep.name, stepElapsedSec),
id: `job${jobIndex}-0`,
status: convertStepToStatus(
rawFirstStep.conclusion as StepConclusion,
),
position: formatElapsedTime(stepElapsedSec),
sec: stepElapsedSec,
};
} else {
firstStep = waitingRunnerStep;
}

const steps = filterSteps(job.steps).map(
(step, stepIndex, _steps): ganttStep => {
const stepElapsedSec = diffSec(step.started_at, step.completed_at);
return {
Expand All @@ -93,9 +110,9 @@ export const createGanttJobs = (
},
);

return { section, steps: [waitingRunnerStep, ...steps] };
return { section, steps: [firstStep, ...steps] };
},
);
).filter((gantJobs): gantJobs is ganttJob => gantJobs !== undefined);
};

export const createGanttDiagrams = (
Expand Down Expand Up @@ -135,8 +152,13 @@ axisFormat %H:%M:%S
export const createMermaid = (
workflow: Workflow,
workflowJobs: WorkflowJobs,
options: GanttOptions,
): string => {
const title = workflow.name ?? "";
const jobs = createGanttJobs(workflow, workflowJobs);
const jobs = createGanttJobs(
workflow,
workflowJobs,
options.showWaitingRunner,
);
return createGanttDiagrams(title, jobs).join("\n");
};
6 changes: 3 additions & 3 deletions tests/workflow_gantt_1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ${workflowJobs[0].steps![6].name} (0s) :job0-7, after job0-6, 0s
${workflowJobs[0].steps![7].name} (0s) :job0-8, after job0-7, 0s
\`\`\``;

assertEquals(createMermaid(workflow, workflowJobs), expect);
assertEquals(createMermaid(workflow, workflowJobs, {}), expect);
});

await t.step("job has skipped and failure steps", () => {
Expand Down Expand Up @@ -192,7 +192,7 @@ ${workflowJobs[0].steps![2].name} (0s) :job0-3, after job0-2, 0s
${workflowJobs[0].steps![3].name} (0s) :job0-4, after job0-3, 0s
\`\`\``;

assertEquals(createMermaid(workflow, workflowJobs), expect);
assertEquals(createMermaid(workflow, workflowJobs, {}), expect);
});

await t.step("Hide not completed steps", () => {
Expand Down Expand Up @@ -258,6 +258,6 @@ Waiting for a runner (6s) :active, job0-0, 00:04:31, 6s
${workflowJobs[0].steps![0].name} (1s) :job0-1, after job0-0, 1s
\`\`\``;

assertEquals(createMermaid(workflow, workflowJobs), expect);
assertEquals(createMermaid(workflow, workflowJobs, {}), expect);
});
});
4 changes: 2 additions & 2 deletions tests/workflow_gantt_2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ ${workflowJobs[1].steps![5].name} (0s) :job1-6, after job1-5, 0s
${workflowJobs[1].steps![6].name} (0s) :job1-7, after job1-6, 0s
\`\`\``;

assertEquals(createMermaid(workflow, workflowJobs), expect);
assertEquals(createMermaid(workflow, workflowJobs, {}), expect);
});

await t.step("Hide skipped jobs", () => {
Expand Down Expand Up @@ -277,6 +277,6 @@ ${workflowJobs[0].steps![1].name} (0s) :job0-2, after job0-1, 0s
${workflowJobs[0].steps![2].name} (0s) :job0-3, after job0-2, 0s
\`\`\``;

assertEquals(createMermaid(workflow, workflowJobs), expect);
assertEquals(createMermaid(workflow, workflowJobs, {}), expect);
});
});
Loading

0 comments on commit 3b79144

Please sign in to comment.