Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added events for comments #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 12 additions & 26 deletions bun.lock

Large diffs are not rendered by default.

1,571 changes: 670 additions & 901 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ts-template",
"description": "ts-template for UbiquityOS plugins.",
"ubiquity:listeners": ["issue_comment.created"],
"ubiquity:listeners": ["issue_comment.created", "pull_request_review_comment.created"],
"commands": {
"hello": {
"ubiquity:example": "/hello argument",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"dependencies": {
"@sinclair/typebox": "0.34.15",
"@ubiquity-os/plugin-sdk": "^2.0.6",
"@ubiquity-os/plugin-sdk": "^3.0.0",
"@ubiquity-os/ubiquity-os-logger": "^1.4.0",
"dotenv": "16.4.7",
"hono": "^4.6.19",
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/hello-world.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { postComment } from "@ubiquity-os/plugin-sdk";
import { Context } from "../types";

/**
Expand All @@ -16,11 +15,12 @@ export async function helloWorld(context: Context) {
logger,
payload,
config: { configurableResponse, customStringsUrl },
commentHandler,
} = context;

const sender = payload.comment.user?.login;
const repo = payload.repository.name;
const issueNumber = payload.issue.number;
const issueNumber = "issue" in payload ? payload.issue.number : payload.pull_request.number;
const owner = payload.repository.owner.login;
const body = payload.comment.body;

Expand All @@ -32,10 +32,10 @@ export async function helloWorld(context: Context) {
logger.info("Hello, world!");
logger.debug(`Executing helloWorld:`, { sender, repo, issueNumber, owner });

await postComment(context, logger.ok(configurableResponse));
await commentHandler.postComment(context, logger.ok(configurableResponse));
if (customStringsUrl) {
const response = await fetch(customStringsUrl).then((value) => value.json());
await postComment(context, logger.ok(response.greeting));
await commentHandler.postComment(context, logger.ok(response.greeting));
}

// Throw errors get posted by the SDK if "postCommentOnError" is set to true.
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { helloWorld } from "./handlers/hello-world";
import { Context } from "./types";
import { isIssueCommentEvent } from "./types/typeguards";
import { isCommentEvent } from "./types/typeguards";

/**
* The main plugin function. Split for easier testing.
*/
export async function runPlugin(context: Context) {
const { logger, eventName } = context;

if (isIssueCommentEvent(context)) {
if (isCommentEvent(context)) {
return await helloWorld(context);
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import { PluginSettings } from "./plugin-input";
*
* ubiquity:listeners: ["issue_comment.created", ...]
*/
export type SupportedEvents = "issue_comment.created";
export type SupportedEvents = "issue_comment.created" | "pull_request_review_comment.created";

export type Context<T extends SupportedEvents = SupportedEvents> = PluginContext<PluginSettings, Env, null, T>;
4 changes: 2 additions & 2 deletions src/types/typeguards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import { Context } from "./context";
/**
* Restricts the scope of `context` to the `issue_comment.created` payload.
*/
export function isIssueCommentEvent(context: Context): context is Context<"issue_comment.created"> {
return context.eventName === "issue_comment.created";
export function isCommentEvent(context: Context): context is Context {
return context.eventName === "issue_comment.created" || context.eventName === "pull_request_review_comment.created";
}
14 changes: 8 additions & 6 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, jest } from "@jest/globals";
import { drop } from "@mswjs/data";
import { CommentHandler } from "@ubiquity-os/plugin-sdk";
import { customOctokit as Octokit } from "@ubiquity-os/plugin-sdk/octokit";
import { Logs } from "@ubiquity-os/ubiquity-os-logger";
import dotenv from "dotenv";
Expand Down Expand Up @@ -62,16 +63,16 @@ describe("Plugin tests", () => {
const { context } = createContext();
await runPlugin(context);
const comments = db.issueComments.getAll();
expect(comments.length).toBe(1);
expect(comments[0].body).toMatch(STRINGS.HELLO_WORLD);
expect(comments.length).toBe(2);
expect(comments[1].body).toMatch(STRINGS.HELLO_WORLD);
});

it("Should respond with `Hello, Code Reviewers` in response to /Hello", async () => {
const { context } = createContext(STRINGS.CONFIGURABLE_RESPONSE);
await runPlugin(context);
const comments = db.issueComments.getAll();
expect(comments.length).toBe(1);
expect(comments[0].body).toMatch(STRINGS.CONFIGURABLE_RESPONSE);
expect(comments.length).toBe(2);
expect(comments[1].body).toMatch(STRINGS.CONFIGURABLE_RESPONSE);
});

it("Should not respond to a comment that doesn't contain /Hello", async () => {
Expand Down Expand Up @@ -102,7 +103,7 @@ function createContext(
) {
const repo = db.repo.findFirst({ where: { id: { equals: repoId } } }) as unknown as Context["payload"]["repository"];
const sender = db.users.findFirst({ where: { id: { equals: payloadSenderId } } }) as unknown as Context["payload"]["sender"];
const issue1 = db.issue.findFirst({ where: { id: { equals: issueOne } } }) as unknown as Context["payload"]["issue"];
const issue1 = db.issue.findFirst({ where: { id: { equals: issueOne } } }) as unknown as Context<"issue_comment.created">["payload"]["issue"];

createComment(commentBody, commentId); // create it first then pull it from the DB and feed it to _createContext
const comment = db.issueComments.findFirst({ where: { id: { equals: commentId } } }) as unknown as Context["payload"]["comment"];
Expand Down Expand Up @@ -134,7 +135,7 @@ function createContext(
function createContextInner(
repo: Context["payload"]["repository"],
sender: Context["payload"]["sender"],
issue: Context["payload"]["issue"],
issue: Context<"issue_comment.created">["payload"]["issue"],
comment: Context["payload"]["comment"],
configurableResponse: string
) {
Expand All @@ -156,5 +157,6 @@ function createContextInner(
},
env: {} as Env,
octokit: octokit,
commentHandler: new CommentHandler(),
} as unknown as Context;
}