Skip to content

Commit

Permalink
Add closeIssue()
Browse files Browse the repository at this point in the history
  • Loading branch information
yykamei committed Mar 28, 2024
1 parent 78b7dfc commit 35b4590
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
18 changes: 18 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29181,6 +29181,21 @@ class GitHubAPIClient {
body: response.data.body ?? "",
});
}
async closeIssue(owner, repo, issue) {
const response = await this.client.request("PATCH /repos/{owner}/{repo}/issues/{issue_number}", {
owner,
repo,
issue_number: issue.parameters.number,
state: "closed",
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
});
return new GitHubIssue({
...response.data,
body: response.data.body ?? "",
});
}
}

;// CONCATENATED MODULE: ./src/GitHubIssueContent.ts
Expand Down Expand Up @@ -29225,6 +29240,9 @@ class GitHubRepository {
async createIssue(issueContent) {
return this.apiClient.createIssue(this.owner, this.repo, issueContent);
}
async closeIssue(issue) {
return this.apiClient.closeIssue(this.owner, this.repo, issue);
}
}

;// CONCATENATED MODULE: ./src/Input.ts
Expand Down
5 changes: 5 additions & 0 deletions src/APIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ export interface APIClient {
repo: string,
issueContent: GitHubIssueContent,
): Promise<GitHubIssue>;
closeIssue(
owner: string,
repo: string,
issue: GitHubIssue,
): Promise<GitHubIssue>;
}
22 changes: 22 additions & 0 deletions src/GitHubAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,26 @@ export class GitHubAPIClient implements APIClient {
body: response.data.body ?? "",
});
}
async closeIssue(
owner: string,
repo: string,
issue: GitHubIssue,
): Promise<GitHubIssue> {
const response = await this.client.request(
"PATCH /repos/{owner}/{repo}/issues/{issue_number}",
{
owner,
repo,
issue_number: issue.parameters.number,
state: "closed",
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
},
);
return new GitHubIssue({
...response.data,
body: response.data.body ?? "",
});
}
}
3 changes: 3 additions & 0 deletions src/GitHubRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ export class GitHubRepository {
async createIssue(issueContent: GitHubIssueContent): Promise<GitHubIssue> {
return this.apiClient.createIssue(this.owner, this.repo, issueContent);
}
async closeIssue(issue: GitHubIssue): Promise<GitHubIssue> {
return this.apiClient.closeIssue(this.owner, this.repo, issue);
}
}
15 changes: 15 additions & 0 deletions src/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,19 @@ export class TestClient implements APIClient {
body: "body",
});
}

async closeIssue(
_owner: string,
_repo: string,
_issue: GitHubIssue,
): Promise<GitHubIssue> {
return new GitHubIssue({
id: 234,
url: "https://github.com/yykamei/test-repo/issue/234",
number: 2340,
state: "open",
title: "test",
body: "body",
});
}
}
39 changes: 39 additions & 0 deletions tests/GitHubAPIClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { GitHubAPIClient } from "../src/GitHubAPIClient";
import { GitHubIssue } from "../src/GitHubIssue";
import { GitHubIssueContent } from "../src/GitHubIssueContent";
import { GitHubWorkflow } from "../src/GitHubWorkflow";

Expand Down Expand Up @@ -204,3 +205,41 @@ describe("GitHubAPIClient.createIssue()", () => {
expect(issue).toHaveProperty("parameters");
});
});
describe("GitHubAPIClient.closeIssue()", () => {
afterEach(() => {
vi.restoreAllMocks();
});

it("should close an issue on GitHub", async () => {
const client = new GitHubAPIClient("token");
const spy = vi.spyOn(client.client, "request");
spy.mockResolvedValueOnce({
data: {
id: 234,
url: "https://github.com/yykamei/test-repo/issue/234",
number: 2340,
state: "closed",
title: "test",
body: "body",
},
headers: {},
url: "https://example.com",
status: 200,
});

const issue = await client.closeIssue(
"yykamei",
"test-repo",
new GitHubIssue({
id: 234,
url: "https://github.com/yykamei/test-repo/issue/234",
number: 2340,
state: "closed",
title: "test",
body: "body",
}),
);
expect(spy).toHaveBeenCalledTimes(1);
expect(issue).toHaveProperty("parameters");
});
});
11 changes: 11 additions & 0 deletions tests/GitHubRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, it } from "vitest";
import { GitHubIssue } from "../src/GitHubIssue";
import { GitHubIssueContent } from "../src/GitHubIssueContent";
import { GitHubRepository } from "../src/GitHubRepository";
import { GitHubWorkflow } from "../src/GitHubWorkflow";
Expand All @@ -12,5 +13,15 @@ describe("GitHubRepository", () => {
repository.getWorkflow("abc.yml");
repository.getWorkflowRuns(new GitHubWorkflow(8, "Eight", "eight.yml"));
repository.createIssue(new GitHubIssueContent([], "title"));
repository.closeIssue(
new GitHubIssue({
id: 1,
number: 1,
body: "b",
url: "url",
title: "t",
state: "open",
}),
);
});
});

0 comments on commit 35b4590

Please sign in to comment.