Skip to content

Commit

Permalink
Add only option
Browse files Browse the repository at this point in the history
  • Loading branch information
yykamei committed Mar 28, 2024
1 parent ebef971 commit 76ca162
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: ./
with:
only: ci.yml
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: GitHub Workflow Metrics
description: TODO
inputs:
only:
description: Only the specified workflows will be measured. This is supposed to be comma-separated list
required: false
default: ""
label:
description: The label for GitHub issues that the GitHub Action creates
required: false
Expand Down
19 changes: 16 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29241,8 +29241,14 @@ class GitHubRepository {
this.repo = repo;
this.apiClient = apiClient;
}
async getWorkflows() {
return this.apiClient.getWorkflows(this.owner, this.repo);
async getWorkflows(only) {
const workflows = await this.apiClient.getWorkflows(this.owner, this.repo);
if (only) {
return workflows.filter((w) => {
return only.some((o) => w.path.endsWith(o));
});
}
return workflows;
}
async getWorkflow(path) {
return this.apiClient.getWorkflow(this.owner, this.repo, path);
Expand Down Expand Up @@ -29280,6 +29286,13 @@ class Input {
get label() {
return this.getInputFn("label");
}
get only() {
const only = this.getInputFn("only");
if (only.length === 0) {
return null;
}
return only.split(/\s*,\s*/);
}
get token() {
return this.getInputFn("token", { required: true });
}
Expand Down Expand Up @@ -29328,7 +29341,7 @@ const main = async () => {
const input = new Input();
const apiClient = new GitHubAPIClient(input.token);
const repository = new GitHubRepository(input.owner, input.repo, apiClient);
const workflows = await repository.getWorkflows();
const workflows = await repository.getWorkflows(input.only);
const charts = await Promise.all(workflows.map(async (w) => {
const runs = await repository.getWorkflowRuns(w);
return new MermaidXYChart(w, runs);
Expand Down
10 changes: 8 additions & 2 deletions src/GitHubRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ export class GitHubRepository {
private readonly apiClient: APIClient,
) {}

async getWorkflows(): Promise<GitHubWorkflow[]> {
return this.apiClient.getWorkflows(this.owner, this.repo);
async getWorkflows(only?: string[] | null): Promise<GitHubWorkflow[]> {
const workflows = await this.apiClient.getWorkflows(this.owner, this.repo);
if (only) {
return workflows.filter((w) => {
return only.some((o) => w.path.endsWith(o));
});
}
return workflows;
}

async getWorkflow(path: string): Promise<GitHubWorkflow> {
Expand Down
8 changes: 8 additions & 0 deletions src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export class Input {
return this.getInputFn("label");
}

get only(): string[] | null {
const only = this.getInputFn("only");
if (only.length === 0) {
return null;
}
return only.split(/\s*,\s*/);
}

get token(): string {
return this.getInputFn("token", { required: true });
}
Expand Down
5 changes: 4 additions & 1 deletion src/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { GitHubWorkflowRun } from "./GitHubWorkflowRun";

export class TestClient implements APIClient {
async getWorkflows(_owner: string, _repo: string): Promise<GitHubWorkflow[]> {
return [new GitHubWorkflow(1234, "My Workflow", "my_workflow.yml")];
return [
new GitHubWorkflow(1234, "My Workflow", "my_workflow.yml"),
new GitHubWorkflow(1235, "XYZ", "xyz.yml"),
];
}
async getWorkflow(
_owner: string,
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const main = async () => {
const input = new Input();
const apiClient = new GitHubAPIClient(input.token);
const repository = new GitHubRepository(input.owner, input.repo, apiClient);
const workflows = await repository.getWorkflows();
const workflows = await repository.getWorkflows(input.only);
const charts = await Promise.all(
workflows.map(async (w) => {
const runs = await repository.getWorkflowRuns(w);
Expand Down
26 changes: 18 additions & 8 deletions tests/GitHubRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { GitHubIssue } from "../src/GitHubIssue";
import { GitHubIssueContent } from "../src/GitHubIssueContent";
import { GitHubRepository } from "../src/GitHubRepository";
import { GitHubWorkflow } from "../src/GitHubWorkflow";
import { TestClient } from "../src/TestClient";

describe("GitHubRepository", () => {
it("should call functions declared in APIClient", () => {
it("should call functions declared in APIClient", async () => {
const client = new TestClient();
const repository = new GitHubRepository("owner", "repo", client);
repository.getWorkflows();
repository.getWorkflow("abc.yml");
repository.getWorkflowRuns(new GitHubWorkflow(8, "Eight", "eight.yml"));
repository.getIssues([]);
repository.createIssue(new GitHubIssueContent([], "title"));
repository.closeIssue(
await repository.getWorkflows();
await repository.getWorkflow("abc.yml");
await repository.getWorkflowRuns(
new GitHubWorkflow(8, "Eight", "eight.yml"),
);
await repository.getIssues([]);
await repository.createIssue(new GitHubIssueContent([], "title"));
await repository.closeIssue(
new GitHubIssue({
id: 1,
number: 1,
Expand All @@ -25,4 +27,12 @@ describe("GitHubRepository", () => {
}),
);
});

it("should filter workflows with only", async () => {
const client = new TestClient();
const repository = new GitHubRepository("owner", "repo", client);
const workflows = await repository.getWorkflows(["xyz.yml"]);
expect(workflows.length).toEqual(1);
expect(workflows.map((w) => w.path)).toEqual(["xyz.yml"]);
});
});
20 changes: 20 additions & 0 deletions tests/Input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe("Input", () => {
return "my-token";
case "label":
return "github-workflows-metrics";
case "only":
return "";
default:
throw new Error("Unsupported key");
}
Expand All @@ -27,6 +29,24 @@ describe("Input", () => {
expect(input.owner).toEqual("yykamei");
expect(input.repo).toEqual("test-repo");
expect(input.label).toEqual("github-workflows-metrics");
expect(input.only).toBeNull();
expect(input.token).toEqual("my-token");
});
it("should return an Array for only when specified", () => {
const context = new Context();
vi.spyOn(context, "repo", "get").mockReturnValue({
owner: "yykamei",
repo: "test-repo",
});
const getInput = vi.fn((key: string) => {
switch (key) {
case "only":
return "a.yml , b.yml,c.yml";
default:
throw new Error("Unsupported key");
}
});
const input = new Input(context, getInput);
expect(input.only).toEqual(["a.yml", "b.yml", "c.yml"]);
});
});

0 comments on commit 76ca162

Please sign in to comment.