Skip to content

Commit

Permalink
feat: help is displayed on /help command
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Jun 17, 2024
1 parent 42a6e27 commit 7033aac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/github/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EmitterWebhookEvent } from "@octokit/webhooks";
import { GitHubContext } from "../github-context";
import { GitHubEventHandler } from "../github-event-handler";
import { getConfig } from "../utils/config";
import issueCommentCreated from "./issue-comment-created";
import { repositoryDispatch } from "./repository-dispatch";
import { dispatchWorker, dispatchWorkflow, getDefaultBranch } from "../utils/workflow-dispatch";
import { PluginInput } from "../types/plugin";
Expand All @@ -19,6 +20,7 @@ function tryCatchWrapper(fn: (event: EmitterWebhookEvent) => unknown) {

export function bindHandlers(eventHandler: GitHubEventHandler) {
eventHandler.on("repository_dispatch", repositoryDispatch);
eventHandler.on("issue_comment.created", issueCommentCreated);
eventHandler.onAny(tryCatchWrapper((event) => handleEvent(event, eventHandler))); // onAny should also receive GithubContext but the types in octokit/webhooks are weird
}

Expand Down
22 changes: 22 additions & 0 deletions src/github/handlers/issue-comment-created.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { GitHubContext } from "../github-context";
import { getConfig } from "../utils/config";

export default async function issueCommentCreated(context: GitHubContext<"issue_comment.created">) {
const body = context.payload.comment.body.trim();
if (/^\/help$/.test(body)) {
const comments = ["---", "| name | description | command | example |", "---"];
console.log(JSON.stringify(context, null, 2));
const configuration = await getConfig(context);
for (const pluginArray of Object.values(configuration.plugins)) {
for (const plugin of pluginArray) {
comments.push(`| ${plugin.name} | ${plugin.description} | ${plugin.command} | ${plugin.example}`);
}
}
await context.octokit.issues.createComment({
body: comments.join("\n"),
issue_number: 0,
owner: "",
repo: "",
});
}
}
51 changes: 51 additions & 0 deletions tests/events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { afterAll, afterEach, beforeAll, describe, it, mock } from "bun:test";
import { config } from "dotenv";
import { GitHubContext } from "../src/github/github-context";
import { GitHubEventHandler } from "../src/github/github-event-handler";
import issueCommentCreated from "../src/github/handlers/issue-comment-created";
import { server } from "./__mocks__/node";
import { WebhooksMocked } from "./__mocks__/webhooks";

void mock.module("@octokit/webhooks", () => ({
Webhooks: WebhooksMocked,
}));

config({ path: ".dev.vars" });

beforeAll(() => {
server.listen();
});
afterEach(() => {
server.resetHandlers();
});
afterAll(() => {
server.close();
});

describe("Event related tests", () => {
it("Should post the help menu when /help command is invoked", async () => {
await issueCommentCreated({
id: "",
key: "issue_comment.created",
octokit: {
rest: {
repos: {
getContent() {
return { data: null };
},
},
},
},
eventHandler: {} as GitHubEventHandler,
payload: {
repository: {
owner: { login: "ubiquity" },
name: "ubiquibot-kernel",
},
comment: {
body: "/help",
},
} as unknown as GitHubContext<"issue_comment.created">["payload"],
} as unknown as GitHubContext);
});
});

0 comments on commit 7033aac

Please sign in to comment.