From 2c15e7c44ea878221cce0afba4b93ffa3f4da067 Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Thu, 30 May 2024 14:27:03 +0900 Subject: [PATCH 1/8] fix: skip on issue close not completed status --- src/index.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0bcaf22b..b087309b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,17 +3,35 @@ import { IssueActivity } from "./issue-activity"; import program from "./parser/command-line"; import { Processor } from "./parser/processor"; import { parseGitHubUrl } from "./start"; +import { getOctokitInstance } from "./get-authentication-token.ts"; async function main() { - if (program.eventName === "issues.closed") { - const issue = parseGitHubUrl(program.eventPayload.issue.html_url); + const { eventPayload, eventName } = program; + if (eventName === "issues.closed") { + if (eventPayload.issue.state_reason !== "completed") { + const result = "# Issue was not closed as completed. Skipping."; + try { + await getOctokitInstance().issues.createComment({ + body: `\`\`\`${result}\`\`\``, + repo: eventPayload.repository.name, + owner: eventPayload.repository.owner.login, + issue_number: eventPayload.issue.number, + }); + } catch (e) { + console.error(e); + } + return result; + } + const issue = parseGitHubUrl(eventPayload.issue.html_url); const activity = new IssueActivity(issue); await activity.init(); const processor = new Processor(); await processor.run(activity); return processor.dump(); } else { - console.warn(`${program.eventName} is not supported, skipping.`); + const result = `${eventName} is not supported, skipping.`; + console.warn(result); + return result; } } From b0a11307710db5a41fee101af39510970fcbbd65 Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Thu, 30 May 2024 15:18:03 +0900 Subject: [PATCH 2/8] chore: added test for issue skip --- src/index.ts | 18 +++++++----------- tests/action.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 tests/action.test.ts diff --git a/src/index.ts b/src/index.ts index b087309b..bbc4da3d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,21 +5,17 @@ import { Processor } from "./parser/processor"; import { parseGitHubUrl } from "./start"; import { getOctokitInstance } from "./get-authentication-token.ts"; -async function main() { +export async function main() { const { eventPayload, eventName } = program; if (eventName === "issues.closed") { if (eventPayload.issue.state_reason !== "completed") { const result = "# Issue was not closed as completed. Skipping."; - try { - await getOctokitInstance().issues.createComment({ - body: `\`\`\`${result}\`\`\``, - repo: eventPayload.repository.name, - owner: eventPayload.repository.owner.login, - issue_number: eventPayload.issue.number, - }); - } catch (e) { - console.error(e); - } + await getOctokitInstance().issues.createComment({ + body: `\`\`\`${result}\`\`\``, + repo: eventPayload.repository.name, + owner: eventPayload.repository.owner.login, + issue_number: eventPayload.issue.number, + }); return result; } const issue = parseGitHubUrl(eventPayload.issue.html_url); diff --git a/tests/action.test.ts b/tests/action.test.ts new file mode 100644 index 00000000..cda91f9d --- /dev/null +++ b/tests/action.test.ts @@ -0,0 +1,36 @@ +/* eslint @typescript-eslint/no-var-requires: 0 */ +import "../src/parser/command-line"; +import { main } from "../src"; + +jest.mock("../src/parser/command-line", () => { + const cfg = require("./__mocks__/results/valid-configuration.json"); + const dotenv = require("dotenv"); + dotenv.config(); + return { + stateId: 1, + eventName: "issues.closed", + authToken: process.env.GITHUB_TOKEN, + ref: "", + eventPayload: { + issue: { + html_url: "https://github.com/ubiquibot/comment-incentives/issues/22", + number: 1, + state_reason: "not_planned", + }, + repository: { + name: "conversation-rewards", + owner: { + login: "ubiquibot", + }, + }, + }, + settings: JSON.stringify(cfg), + }; +}); + +describe("Action tests", () => { + it("Should skip when the issue is closed without the completed status", async () => { + const e = await main(); + expect(e).toEqual("# Issue was not closed as completed. Skipping."); + }); +}); From 753afb44c98628ddbbfb4929adf441c3569ccfa7 Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Thu, 30 May 2024 15:28:53 +0900 Subject: [PATCH 3/8] chore: fixed formatting for skipped task --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index bbc4da3d..c483ed93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ export async function main() { if (eventPayload.issue.state_reason !== "completed") { const result = "# Issue was not closed as completed. Skipping."; await getOctokitInstance().issues.createComment({ - body: `\`\`\`${result}\`\`\``, + body: `\`\`\`text\n${result}\`\`\``, repo: eventPayload.repository.name, owner: eventPayload.repository.owner.login, issue_number: eventPayload.issue.number, From d36dac0c2ce248e11fcc9884dda6ebd3bb58fe1e Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Thu, 30 May 2024 15:34:13 +0900 Subject: [PATCH 4/8] chore: fixed formatting for skipped task --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index c483ed93..d98aa859 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ export async function main() { if (eventPayload.issue.state_reason !== "completed") { const result = "# Issue was not closed as completed. Skipping."; await getOctokitInstance().issues.createComment({ - body: `\`\`\`text\n${result}\`\`\``, + body: `\`\`\`text\n${result}\n\`\`\``, repo: eventPayload.repository.name, owner: eventPayload.repository.owner.login, issue_number: eventPayload.issue.number, From f8c48ce8096ec62afbb3b1214f30fd29138baa92 Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Thu, 30 May 2024 15:41:21 +0900 Subject: [PATCH 5/8] chore: renamed variable for test --- tests/action.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/action.test.ts b/tests/action.test.ts index cda91f9d..402e937e 100644 --- a/tests/action.test.ts +++ b/tests/action.test.ts @@ -30,7 +30,7 @@ jest.mock("../src/parser/command-line", () => { describe("Action tests", () => { it("Should skip when the issue is closed without the completed status", async () => { - const e = await main(); - expect(e).toEqual("# Issue was not closed as completed. Skipping."); + const result = await main(); + expect(result).toEqual("# Issue was not closed as completed. Skipping."); }); }); From 795ed19e668f5085633d572ef184d5eb2fd5f47e Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Thu, 30 May 2024 15:47:07 +0900 Subject: [PATCH 6/8] chore: fix jest test --- tests/process.issue.test.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/process.issue.test.ts b/tests/process.issue.test.ts index 5f0e74aa..aedcb475 100644 --- a/tests/process.issue.test.ts +++ b/tests/process.issue.test.ts @@ -41,7 +41,17 @@ jest.mock("../src/parser/command-line", () => { authToken: process.env.GITHUB_TOKEN, ref: "", eventPayload: { - issue: { html_url: "https://github.com/ubiquibot/comment-incentives/issues/22" }, + issue: { + html_url: "https://github.com/ubiquibot/comment-incentives/issues/22", + number: 1, + state_reason: "completed", + }, + repository: { + name: "conversation-rewards", + owner: { + login: "ubiquibot", + }, + }, }, settings: JSON.stringify(cfg), }; From a84dea3577691ff482f4ed1ec4f5576943447d23 Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Thu, 30 May 2024 15:56:44 +0900 Subject: [PATCH 7/8] chore: split main code for easier testing --- src/index.ts | 34 ++-------------------------------- src/run.ts | 31 +++++++++++++++++++++++++++++++ tests/action.test.ts | 4 ++-- 3 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 src/run.ts diff --git a/src/index.ts b/src/index.ts index d98aa859..7bc8f787 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,37 +1,7 @@ import * as core from "@actions/core"; -import { IssueActivity } from "./issue-activity"; -import program from "./parser/command-line"; -import { Processor } from "./parser/processor"; -import { parseGitHubUrl } from "./start"; -import { getOctokitInstance } from "./get-authentication-token.ts"; +import { run } from "./run"; -export async function main() { - const { eventPayload, eventName } = program; - if (eventName === "issues.closed") { - if (eventPayload.issue.state_reason !== "completed") { - const result = "# Issue was not closed as completed. Skipping."; - await getOctokitInstance().issues.createComment({ - body: `\`\`\`text\n${result}\n\`\`\``, - repo: eventPayload.repository.name, - owner: eventPayload.repository.owner.login, - issue_number: eventPayload.issue.number, - }); - return result; - } - const issue = parseGitHubUrl(eventPayload.issue.html_url); - const activity = new IssueActivity(issue); - await activity.init(); - const processor = new Processor(); - await processor.run(activity); - return processor.dump(); - } else { - const result = `${eventName} is not supported, skipping.`; - console.warn(result); - return result; - } -} - -main() +run() .then((result) => { core?.setOutput("result", result); }) diff --git a/src/run.ts b/src/run.ts new file mode 100644 index 00000000..30766e76 --- /dev/null +++ b/src/run.ts @@ -0,0 +1,31 @@ +import { IssueActivity } from "./issue-activity"; +import program from "./parser/command-line"; +import { Processor } from "./parser/processor"; +import { parseGitHubUrl } from "./start"; +import { getOctokitInstance } from "./get-authentication-token.ts"; + +export async function run() { + const { eventPayload, eventName } = program; + if (eventName === "issues.closed") { + if (eventPayload.issue.state_reason !== "completed") { + const result = "# Issue was not closed as completed. Skipping."; + await getOctokitInstance().issues.createComment({ + body: `\`\`\`text\n${result}\n\`\`\``, + repo: eventPayload.repository.name, + owner: eventPayload.repository.owner.login, + issue_number: eventPayload.issue.number, + }); + return result; + } + const issue = parseGitHubUrl(eventPayload.issue.html_url); + const activity = new IssueActivity(issue); + await activity.init(); + const processor = new Processor(); + await processor.run(activity); + return processor.dump(); + } else { + const result = `${eventName} is not supported, skipping.`; + console.warn(result); + return result; + } +} diff --git a/tests/action.test.ts b/tests/action.test.ts index 402e937e..1deafb47 100644 --- a/tests/action.test.ts +++ b/tests/action.test.ts @@ -1,6 +1,6 @@ /* eslint @typescript-eslint/no-var-requires: 0 */ import "../src/parser/command-line"; -import { main } from "../src"; +import { run } from "../src/run"; jest.mock("../src/parser/command-line", () => { const cfg = require("./__mocks__/results/valid-configuration.json"); @@ -30,7 +30,7 @@ jest.mock("../src/parser/command-line", () => { describe("Action tests", () => { it("Should skip when the issue is closed without the completed status", async () => { - const result = await main(); + const result = await run(); expect(result).toEqual("# Issue was not closed as completed. Skipping."); }); }); From db3d753bbfa3d83469626c088e874e36d5a30b40 Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Thu, 30 May 2024 16:09:14 +0900 Subject: [PATCH 8/8] chore: mock for post comment --- tests/__mocks__/handlers.ts | 3 +++ tests/action.test.ts | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/tests/__mocks__/handlers.ts b/tests/__mocks__/handlers.ts index 6ecd525a..6adb29a0 100644 --- a/tests/__mocks__/handlers.ts +++ b/tests/__mocks__/handlers.ts @@ -114,4 +114,7 @@ export const handlers = [ const createdPermit = db.permits.create(data); return HttpResponse.json(createdPermit); }), + http.post("https://api.github.com/repos/:owner/:repo/issues/:id/comments", () => { + return HttpResponse.json({}); + }), ]; diff --git a/tests/action.test.ts b/tests/action.test.ts index 1deafb47..4f772e06 100644 --- a/tests/action.test.ts +++ b/tests/action.test.ts @@ -1,6 +1,11 @@ /* eslint @typescript-eslint/no-var-requires: 0 */ import "../src/parser/command-line"; import { run } from "../src/run"; +import { server } from "./__mocks__/node.ts"; + +beforeAll(() => server.listen()); +afterEach(() => server.resetHandlers()); +afterAll(() => server.close()); jest.mock("../src/parser/command-line", () => { const cfg = require("./__mocks__/results/valid-configuration.json");