Skip to content

Commit 40b912d

Browse files
authored
Refactor (#78)
2 parents e22aed2 + d158239 commit 40b912d

8 files changed

+267
-202
lines changed

deno.lock

+49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/post.js

+16-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/format_util.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { format } from "npm:date-fns@3.2.0";
2+
import type { ganttJob, ganttStep, StepConclusion } from "./types.ts";
3+
4+
export const diffSec = (
5+
start?: string | Date | null,
6+
end?: string | Date | null,
7+
): number => {
8+
if (!start || !end) return 0;
9+
const startDate = new Date(start);
10+
const endDate = new Date(end);
11+
12+
return (endDate.getTime() - startDate.getTime()) / 1000;
13+
};
14+
15+
// Sec to elapsed format time like HH:mm:ss (ex. 70sec -> 00:01:10)
16+
export const formatElapsedTime = (sec: number): string => {
17+
const date = new Date(sec * 1000);
18+
const offsetMinute = date.getTimezoneOffset();
19+
const timezonreIgnoredDate = new Date(sec * 1000 + offsetMinute * 60 * 1000);
20+
return format(timezonreIgnoredDate, "HH:mm:ss");
21+
};
22+
23+
// Sec to elapsed short format time like 1h2m3s (ex. 70sec -> 1m10s)
24+
export const formatShortElapsedTime = (sec: number): string => {
25+
const date = new Date(sec * 1000);
26+
const offsetMinute = date.getTimezoneOffset();
27+
const timezonreIgnoredDate = new Date(sec * 1000 + offsetMinute * 60 * 1000);
28+
if (sec < 60) {
29+
return format(timezonreIgnoredDate, "s's'");
30+
} else if (sec < 60 * 60) {
31+
return format(timezonreIgnoredDate, "m'm's's'");
32+
} else {
33+
return format(timezonreIgnoredDate, "H'h'm'm's's'");
34+
}
35+
};
36+
37+
export const formatStep = (step: ganttStep): string => {
38+
switch (step.status) {
39+
case "":
40+
return `${step.name} :${step.id}, ${step.position}, ${step.sec}s`;
41+
default:
42+
return `${step.name} :${step.status}, ${step.id}, ${step.position}, ${step.sec}s`;
43+
}
44+
};
45+
46+
export const formatName = (name: string, sec: number): string => {
47+
return `${escapeName(name)} (${formatShortElapsedTime(sec)})`;
48+
};
49+
50+
export const escapeName = (name: string): string => {
51+
return name.replaceAll(":", "");
52+
};
53+
54+
export function formatSection(job: ganttJob): string {
55+
return [
56+
`section ${job.section}`,
57+
...job.steps.map((step) => formatStep(step)),
58+
].join("\n");
59+
}
60+
61+
export const convertStepToStatus = (
62+
conclusion: StepConclusion,
63+
): ganttStep["status"] => {
64+
switch (conclusion) {
65+
case "success":
66+
return "";
67+
case "failure":
68+
return "crit";
69+
case "cancelled":
70+
case "skipped":
71+
case "timed_out":
72+
return "done";
73+
case "neutral":
74+
case "action_required":
75+
case null:
76+
return "active";
77+
default:
78+
return "active";
79+
}
80+
};

src/post.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { setTimeout } from "node:timers/promises";
22
import process from "node:process";
33
import { debug, getInput, info, summary } from "npm:@actions/core@1.10.1";
44
import * as github from "npm:@actions/github@6.0.0";
5-
import { createGantt } from "./workflow_gantt.ts";
5+
import { createMermaid } from "./workflow_gantt.ts";
66
import {
77
createOctokit,
88
fetchWorkflow,
@@ -41,7 +41,7 @@ const main = async () => {
4141
debug(JSON.stringify(workflowJobs, null, 2));
4242

4343
info("Create gantt mermaid diagram...");
44-
const gantt = createGantt(workflow, workflowJobs);
44+
const gantt = createMermaid(workflow, workflowJobs);
4545
await summary.addRaw(gantt).write();
4646
debug(gantt);
4747

src/types.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export type ganttJob = {
2+
section: string;
3+
steps: ganttStep[];
4+
};
5+
6+
export type ganttStep = {
7+
name: string;
8+
id: `job${number}-${number}`;
9+
status: "" | "done" | "active" | "crit";
10+
position: string;
11+
sec: number;
12+
};
13+
14+
// ref: https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#get-a-job-for-a-workflow-run
15+
export type StepConclusion =
16+
| "success"
17+
| "failure"
18+
| "neutral"
19+
| "cancelled"
20+
| "skipped"
21+
| "timed_out"
22+
| "action_required"
23+
| null;

0 commit comments

Comments
 (0)