From 1a2e9bb9d17ae9eb756145728060a5c38299f1bb Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:37:25 +0100 Subject: [PATCH] chore: logger, fix tests --- src/handlers/comment-created-callback.ts | 17 +++++++++-------- src/helpers/errors.ts | 2 +- src/plugin.ts | 2 +- tests/main.test.ts | 8 ++++---- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/handlers/comment-created-callback.ts b/src/handlers/comment-created-callback.ts index 644b83a..d49f385 100644 --- a/src/handlers/comment-created-callback.ts +++ b/src/handlers/comment-created-callback.ts @@ -12,19 +12,20 @@ export async function issueCommentCreatedCallback( env: { UBIQUITY_OS_APP_NAME }, } = context; const question = context.payload.comment.body.trim(); - if (!question || question.length === 0) { - return { status: 204, reason: logger.info("Comment is empty. Skipping.").logMessage.raw }; - } - const slugRegex = new RegExp(`@${UBIQUITY_OS_APP_NAME} `, "gi"); - if (!question.match(slugRegex)) { + const slugRegex = new RegExp(`@${UBIQUITY_OS_APP_NAME}`, "gi"); + + if (!slugRegex.test(question)) { return { status: 204, reason: logger.info("Comment does not mention the app. Skipping.").logMessage.raw }; } + + if (!question.length || question.replace(slugRegex, "").trim().length === 0) { + return { status: 204, reason: logger.info("No question provided. Skipping.").logMessage.raw }; + } + if (context.payload.comment.user?.type === "Bot") { return { status: 204, reason: logger.info("Comment is from a bot. Skipping.").logMessage.raw }; } - if (question.replace(slugRegex, "").trim().length === 0) { - return { status: 204, reason: logger.info("Comment is empty. Skipping.").logMessage.raw }; - } + logger.info(`Asking question: ${question}`); try { diff --git a/src/helpers/errors.ts b/src/helpers/errors.ts index 84da985..52dae30 100644 --- a/src/helpers/errors.ts +++ b/src/helpers/errors.ts @@ -1,4 +1,4 @@ -import { LogReturn, Logs } from "@ubiquity-dao/ubiquibot-logger"; +import { LogReturn, Logs } from "@ubiquity-os/ubiquity-os-logger"; import { Context } from "../types"; import { addCommentToIssue } from "../handlers/add-comment"; export const logger = new Logs("debug"); diff --git a/src/plugin.ts b/src/plugin.ts index 50e422a..284b3cf 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,7 +1,7 @@ import { Octokit } from "@octokit/rest"; import { PluginInputs } from "./types"; import { Context } from "./types"; -import { LogLevel, Logs } from "@ubiquity-dao/ubiquibot-logger"; +import { LogLevel, Logs } from "@ubiquity-os/ubiquity-os-logger"; import { Env } from "./types/env"; import { createAdapters } from "./adapters"; import { createClient } from "@supabase/supabase-js"; diff --git a/tests/main.test.ts b/tests/main.test.ts index 935a113..67ee804 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -2,7 +2,7 @@ import { db } from "./__mocks__/db"; import { server } from "./__mocks__/node"; import usersGet from "./__mocks__/users-get.json"; import { expect, describe, beforeAll, beforeEach, afterAll, afterEach, it } from "@jest/globals"; -import { Logs } from "@ubiquity-dao/ubiquibot-logger"; +import { Logs } from "@ubiquity-os/ubiquity-os-logger"; import { Context, SupportedEventsU } from "../src/types"; import { drop } from "@mswjs/data"; import issueTemplate from "./__mocks__/issue-template"; @@ -93,7 +93,7 @@ describe("Ask plugin tests", () => { createComments([transformCommentTemplate(1, 1, TEST_QUESTION, "ubiquity", "test-repo", true)]); await runPlugin(ctx); - expect(infoSpy).toHaveBeenCalledWith("Comment is empty. Skipping."); + expect(infoSpy).toHaveBeenCalledWith("No question provided. Skipping."); }); it("Should throw if OPENAI_API_KEY is not defined", () => { const settings = {}; @@ -168,7 +168,7 @@ describe("Ask plugin tests", () => { `; const normalizedExpected = normalizeString(prompt); - const normalizedReceived = normalizeString(infoSpy.mock.calls[1][0]); + const normalizedReceived = normalizeString(infoSpy.mock.calls[1][0] as string); expect(normalizedReceived).toEqual(normalizedExpected); }); @@ -187,7 +187,7 @@ function transformCommentTemplate(commentId: number, issueNumber: number, body: login: "ubiquity", type: "User", }, - body: TEST_QUESTION, + body: body, url: "https://api.github.com/repos/ubiquity/test-repo/issues/comments/1", html_url: "https://www.github.com/ubiquity/test-repo/issues/1", owner: "ubiquity",