sotobot 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 James Garbutt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/lib/bot.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { App } from '@octokit/app';
2
+ import type { Octokit } from '@octokit/core';
3
+ import type { EmitterWebhookEvent } from '@octokit/webhooks/types';
4
+ type IssueCommentEvent = EmitterWebhookEvent<'issue_comment.created'> & {
5
+ octokit: Octokit;
6
+ };
7
+ export interface CommandContext {
8
+ octokit: Octokit;
9
+ payload: IssueCommentEvent['payload'];
10
+ args: string[];
11
+ }
12
+ export type CommandHandler = (context: CommandContext) => Promise<void>;
13
+ export interface BotOptions {
14
+ name: string;
15
+ }
16
+ export declare class Bot {
17
+ #private;
18
+ constructor(app: App, options: BotOptions);
19
+ addCommand(name: string | string[], fn: CommandHandler): void;
20
+ handleRequest(request: Request): Promise<Response>;
21
+ }
22
+ export {};
package/lib/bot.js ADDED
@@ -0,0 +1,57 @@
1
+ import { createWebMiddleware } from '@octokit/webhooks';
2
+ import { parseCommand } from './comments.js';
3
+ import { WEBHOOK_PATH } from './constants.js';
4
+ import { hasWriteAccess } from './octokit.js';
5
+ export class Bot {
6
+ #app;
7
+ #middleware;
8
+ #commands = new Map();
9
+ #options;
10
+ constructor(app, options) {
11
+ this.#options = options;
12
+ this.#app = app;
13
+ this.#app.webhooks.onError(this.#onError);
14
+ this.#app.webhooks.on('issue_comment.created', this.#onIssueComment);
15
+ this.#middleware = createWebMiddleware(this.#app.webhooks, {
16
+ path: WEBHOOK_PATH,
17
+ });
18
+ }
19
+ addCommand(name, fn) {
20
+ if (Array.isArray(name)) {
21
+ for (const n of name) {
22
+ this.#commands.set(n, fn);
23
+ }
24
+ }
25
+ else {
26
+ this.#commands.set(name, fn);
27
+ }
28
+ }
29
+ handleRequest(request) {
30
+ return this.#middleware(request);
31
+ }
32
+ #onIssueComment = async ({ octokit, payload, }) => {
33
+ if (!payload.issue.pull_request) {
34
+ return;
35
+ }
36
+ const command = parseCommand(this.#options.name, payload.comment.body);
37
+ if (!command) {
38
+ return;
39
+ }
40
+ const handler = this.#commands.get(command.name);
41
+ if (!handler) {
42
+ return;
43
+ }
44
+ const username = payload.comment.user?.login;
45
+ if (!username) {
46
+ return;
47
+ }
48
+ const allowed = await hasWriteAccess(octokit, payload.repository.owner.login, payload.repository.name, username);
49
+ if (!allowed) {
50
+ return;
51
+ }
52
+ await handler({ octokit, payload, args: command.args });
53
+ };
54
+ #onError = (error) => {
55
+ console.error('Webhook handler error', error);
56
+ };
57
+ }
@@ -0,0 +1,9 @@
1
+ import { Bot } from './bot.js';
2
+ export interface ServerOptions {
3
+ privateKey: string;
4
+ appId: string;
5
+ webhookSecret: string;
6
+ }
7
+ export declare function createServer(bot: Bot): {
8
+ fetch(request: Request): Promise<Response>;
9
+ };
@@ -0,0 +1,12 @@
1
+ import { WEBHOOK_PATH } from './constants.js';
2
+ export function createServer(bot) {
3
+ return {
4
+ async fetch(request) {
5
+ const { pathname } = new URL(request.url);
6
+ if (request.method === 'POST' && pathname === WEBHOOK_PATH) {
7
+ return bot.handleRequest(request);
8
+ }
9
+ return new Response('Not found', { status: 404 });
10
+ },
11
+ };
12
+ }
@@ -0,0 +1,5 @@
1
+ export interface ParsedCommand {
2
+ name: string;
3
+ args: string[];
4
+ }
5
+ export declare function parseCommand(botName: string, body: string): ParsedCommand | null;
@@ -0,0 +1,16 @@
1
+ export function parseCommand(botName, body) {
2
+ const mention = `!${botName}`;
3
+ for (const rawLine of body.split('\n')) {
4
+ const line = rawLine.trim();
5
+ if (!line.startsWith(mention)) {
6
+ continue;
7
+ }
8
+ const parts = line.slice(mention.length).trim().split(/\s+/);
9
+ const [name, ...args] = parts;
10
+ if (!name) {
11
+ continue;
12
+ }
13
+ return { name, args };
14
+ }
15
+ return null;
16
+ }
@@ -0,0 +1 @@
1
+ export declare const WEBHOOK_PATH = "/webhook";
@@ -0,0 +1 @@
1
+ export const WEBHOOK_PATH = '/webhook';
package/lib/main.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { Bot, BotOptions } from './bot.js';
2
+ export interface CreateBotOptions {
3
+ privateKey: string;
4
+ appId: string;
5
+ webhookSecret: string;
6
+ bot: BotOptions;
7
+ }
8
+ export declare function createBot(options: CreateBotOptions): Bot;
package/lib/main.js ADDED
@@ -0,0 +1,20 @@
1
+ import { App } from '@octokit/app';
2
+ import { Bot } from './bot.js';
3
+ const botCache = new WeakMap();
4
+ export function createBot(options) {
5
+ const cachedBot = botCache.get(options);
6
+ if (cachedBot) {
7
+ return cachedBot;
8
+ }
9
+ const { privateKey, appId, webhookSecret } = options;
10
+ const app = new App({
11
+ appId,
12
+ privateKey,
13
+ webhooks: {
14
+ secret: webhookSecret,
15
+ },
16
+ });
17
+ const bot = new Bot(app, options.bot);
18
+ botCache.set(options, bot);
19
+ return bot;
20
+ }
@@ -0,0 +1,2 @@
1
+ import type { Octokit } from '@octokit/core';
2
+ export declare function hasWriteAccess(octokit: Octokit, owner: string, repo: string, username: string): Promise<boolean>;
package/lib/octokit.js ADDED
@@ -0,0 +1,4 @@
1
+ export async function hasWriteAccess(octokit, owner, repo, username) {
2
+ const { data } = await octokit.request('GET /repos/{owner}/{repo}/collaborators/{username}/permission', { owner, repo, username });
3
+ return data.permission === 'admin' || data.permission === 'write';
4
+ }
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "sotobot",
3
+ "version": "0.2.0",
4
+ "description": "A serverless-focused GitHub bot framework.",
5
+ "keywords": [
6
+ "github",
7
+ "bot",
8
+ "probot",
9
+ "ci"
10
+ ],
11
+ "homepage": "https://github.com/43081j/sotobot#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/43081j/sotobot/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/43081j/sotobot.git"
18
+ },
19
+ "license": "MIT",
20
+ "author": "James Garbutt (https://github.com/43081j)",
21
+ "type": "module",
22
+ "main": "lib/main.js",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./lib/main.d.ts",
26
+ "default": "./lib/main.js"
27
+ },
28
+ "./cloudflare.js": {
29
+ "types": "./lib/cloudflare.d.ts",
30
+ "default": "./lib/cloudflare.js"
31
+ },
32
+ "./lib/cloudflare.js": {
33
+ "types": "./lib/cloudflare.d.ts",
34
+ "default": "./lib/cloudflare.js"
35
+ }
36
+ },
37
+ "files": [
38
+ "lib",
39
+ "!lib/**/*.test.js",
40
+ "!lib/**/*.test.d.ts"
41
+ ],
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "scripts": {
46
+ "build:clean": "node -e \"require('fs').rmSync('./lib', { recursive: true, force: true })\"",
47
+ "build": "npm run build:clean && tsgo",
48
+ "lint:js": "oxlint src",
49
+ "lint:format": "oxfmt --check src",
50
+ "lint": "npm run lint:js && npm run lint:format",
51
+ "format": "oxfmt --write src",
52
+ "test": "vitest run"
53
+ },
54
+ "devDependencies": {
55
+ "@typescript/native-preview": "^7.0.0-dev.20260615.1",
56
+ "@vitest/coverage-v8": "^4.1.9",
57
+ "oxfmt": "^0.54.0",
58
+ "oxlint": "^1.69.0",
59
+ "vitest": "^4.1.9"
60
+ },
61
+ "dependencies": {
62
+ "@octokit/app": "^16.1.2",
63
+ "@octokit/core": "^7.0.6",
64
+ "@octokit/webhooks": "^14.2.0"
65
+ }
66
+ }