Skip to content

Commit

Permalink
Add range input to narrow data range
Browse files Browse the repository at this point in the history
  • Loading branch information
yykamei committed Apr 3, 2024
1 parent 8da7c62 commit 14f113a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: GitHub Workflow Metrics
description: TODO
inputs:
range:
description: |
The range of time to measure the workflows.
This can be:
7days, 14days, 30days
required: false
default: 30days
only:
description: Only the specified workflows will be measured. This is supposed to be comma-separated list
required: false
Expand Down
38 changes: 38 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29174,6 +29174,7 @@ class GitHubAPIClient {
page,
exclude_pull_requests: options?.excludePullRequests,
status: options?.status,
created: options?.created && rangeToCreated(options.created),
});
link = response.headers.link || "";
page += 1;
Expand Down Expand Up @@ -29254,6 +29255,29 @@ class GitHubAPIClient {
});
}
}
const rangeToCreated = (range) => {
const now = new Date();
switch (range) {
case "7days": {
now.setDate(now.getDate() - 7);
break;
}
case "14days": {
now.setDate(now.getDate() - 14);
break;
}
case "30days": {
now.setDate(now.getDate() - 30);
break;
}
}
const date = now.toLocaleDateString("en-CA", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
return `>=${date}`;
};

;// CONCATENATED MODULE: ./src/GitHubIssueContent.ts
class GitHubIssueContent {
Expand Down Expand Up @@ -29375,6 +29399,19 @@ class Input {
return undefined;
}
}
get range() {
const s = this.getInputFn("range");
switch (s) {
case "7days":
return s;
case "14days":
return s;
case "30days":
return s;
default:
throw new Error("range must be one of 7days, 14days, or 30days");
}
}
get token() {
return this.getInputFn("token", { required: true });
}
Expand Down Expand Up @@ -29457,6 +29494,7 @@ const main = async () => {
const charts = await Promise.all(workflows.map(async (w) => {
const runs = await repository.getWorkflowRuns(w, {
status: input.status,
created: input.range,
});
return new MermaidXYChart(w, runs, input);
}));
Expand Down
2 changes: 2 additions & 0 deletions src/APIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { GitHubIssue } from "./GitHubIssue";
import type { GitHubIssueContent } from "./GitHubIssueContent";
import type { GitHubWorkflow } from "./GitHubWorkflow";
import type { GitHubWorkflowRun } from "./GitHubWorkflowRun";
import type { RangeString } from "./Input";
import type { Usage } from "./Usage";

export type WorkflowStatus =
Expand All @@ -23,6 +24,7 @@ export type WorkflowStatus =
export type GetWorkflowRunsOptions = {
readonly excludePullRequests?: boolean;
readonly status?: WorkflowStatus;
readonly created?: RangeString;
};

export interface APIClient {
Expand Down
26 changes: 26 additions & 0 deletions src/GitHubAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GitHubIssue } from "./GitHubIssue";
import type { GitHubIssueContent } from "./GitHubIssueContent";
import { GitHubWorkflow } from "./GitHubWorkflow";
import { GitHubWorkflowRun } from "./GitHubWorkflowRun";
import type { RangeString } from "./Input";
import { Usage } from "./Usage";

const GITHUB_LINK_REL_REXT = 'rel="next"';
Expand Down Expand Up @@ -102,6 +103,7 @@ export class GitHubAPIClient implements APIClient {
page,
exclude_pull_requests: options?.excludePullRequests,
status: options?.status,
created: options?.created && rangeToCreated(options.created),
},
);
link = response.headers.link || "";
Expand Down Expand Up @@ -217,3 +219,27 @@ export class GitHubAPIClient implements APIClient {
});
}
}

const rangeToCreated = (range: RangeString) => {
const now = new Date();
switch (range) {
case "7days": {
now.setDate(now.getDate() - 7);
break;
}
case "14days": {
now.setDate(now.getDate() - 14);
break;
}
case "30days": {
now.setDate(now.getDate() - 30);
break;
}
}
const date = now.toLocaleDateString("en-CA", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
return `>=${date}`;
};
15 changes: 15 additions & 0 deletions src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { context } from "@actions/github";
import type { Context } from "@actions/github/lib/context";
import type { WorkflowStatus } from "./APIClient";

export type RangeString = "7days" | "14days" | "30days";
export class Input {
constructor(
private readonly ctx: Context = context,
Expand Down Expand Up @@ -68,6 +69,20 @@ export class Input {
}
}

get range(): RangeString {
const s = this.getInputFn("range");
switch (s) {
case "7days":
return s;
case "14days":
return s;
case "30days":
return s;
default:
throw new Error("range must be one of 7days, 14days, or 30days");
}
}

get token(): string {
return this.getInputFn("token", { required: true });
}
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const main = async () => {
workflows.map(async (w) => {
const runs = await repository.getWorkflowRuns(w, {
status: input.status,
created: input.range,
});
return new MermaidXYChart(w, runs, input);
}),
Expand Down
14 changes: 14 additions & 0 deletions tests/Input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe("Input", () => {
return "";
case "status":
return "failure";
case "range":
return "30days";
default:
throw new Error("Unsupported key");
}
Expand All @@ -33,6 +35,7 @@ describe("Input", () => {
expect(input.label).toEqual("github-workflows-metrics");
expect(input.only).toBeNull();
expect(input.status).toEqual("failure");
expect(input.range).toEqual("30days");
expect(input.token).toEqual("my-token");
});

Expand Down Expand Up @@ -85,4 +88,15 @@ describe("Input", () => {
const input = new Input(context, getInput);
expect(input.status).toBeUndefined();
});

it("should throw an error with the unsupported range", () => {
const context = new Context();
vi.spyOn(context, "repo", "get").mockReturnValue({
owner: "yykamei",
repo: "test-repo",
});
const getInput = vi.fn(() => "unknown");
const input = new Input(context, getInput);
expect(() => input.range).toThrowError;
});
});

0 comments on commit 14f113a

Please sign in to comment.