Files
cnb/agent/routes/issues/comments.ts
2026-03-11 16:53:01 +08:00

178 lines
5.6 KiB
TypeScript

import { createSkill, tool } from '@kevisual/router';
import { app, cnbManager } from '../../app.ts';
import { useKey } from '@kevisual/context';
// 查询 Issue 评论列表
app.route({
path: 'cnb',
key: 'list-issue-comments',
description: '查询 Issue 评论列表, 参数 repo, issueNumber, page, page_size',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'list-issue-comments',
title: '查询 Issue 评论列表',
args: {
repo: tool.schema.string().optional().describe('代码仓库名称, 如 my-user/my-repo'),
issueNumber: tool.schema.number().describe('Issue 编号'),
page: tool.schema.number().optional().describe('分页页码,默认: 1'),
page_size: tool.schema.number().optional().describe('分页每页大小,默认: 30'),
},
summary: '查询 Issue 评论列表',
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
let repo = ctx.query?.repo || useKey('CNB_REPO_SLUG_LOWERCASE');
const issueNumber = ctx.query?.issueNumber;
const page = ctx.query?.page ? Number(ctx.query.page) : undefined;
const page_size = ctx.query?.page_size ? Number(ctx.query.page_size) : undefined;
if (!repo) {
ctx.throw(400, '缺少参数 repo');
}
if (!issueNumber) {
ctx.throw(400, '缺少参数 issueNumber');
}
const params: Record<string, any> = {};
if (page) params.page = page;
if (page_size) params.page_size = page_size;
const res = await cnb.issue.getCommentList(repo, issueNumber, params);
ctx.forward(res);
}).addTo(app);
// 创建 Issue 评论
app.route({
path: 'cnb',
key: 'create-issue-comment',
description: '创建 Issue 评论, 参数 repo, issueNumber, body',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'create-issue-comment',
title: '创建 Issue 评论',
args: {
repo: tool.schema.string().optional().describe('代码仓库名称, 如 my-user/my-repo'),
issueNumber: tool.schema.number().describe('Issue 编号'),
body: tool.schema.string().describe('评论内容'),
clearAt: tool.schema.boolean().optional().describe('是否清除评论内容中的 @ 提及,默认: true'),
},
summary: '创建 Issue 评论',
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
let repo = ctx.query?.repo || useKey('CNB_REPO_SLUG_LOWERCASE');
const issueNumber = ctx.query?.issueNumber;
let body = ctx.query?.body;
const clearAt = ctx.query?.clearAt ?? true;
if (!repo) {
ctx.throw(400, '缺少参数 repo');
}
if (!issueNumber) {
ctx.throw(400, '缺少参数 issueNumber');
}
if (!body) {
ctx.throw(400, '缺少参数 body');
}
if (clearAt && body) {
// 清除评论内容中的 @ 提及
body = body.replace(/@/g, '');
}
const res = await cnb.issue.createComment(repo, issueNumber, body);
ctx.forward(res);
}).addTo(app);
// 获取 Issue 指定评论
app.route({
path: 'cnb',
key: 'get-issue-comment',
description: '获取 Issue 指定评论, 参数 repo, issueNumber, commentId',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'get-issue-comment',
title: '获取 Issue 评论',
args: {
repo: tool.schema.string().optional().describe('代码仓库名称, 如 my-user/my-repo'),
issueNumber: tool.schema.number().describe('Issue 编号'),
commentId: tool.schema.number().describe('评论 ID'),
},
summary: '获取 Issue 评论',
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
let repo = ctx.query?.repo || useKey('CNB_REPO_SLUG_LOWERCASE');
const issueNumber = ctx.query?.issueNumber;
const commentId = ctx.query?.commentId;
if (!repo) {
ctx.throw(400, '缺少参数 repo');
}
if (!issueNumber) {
ctx.throw(400, '缺少参数 issueNumber');
}
if (!commentId) {
ctx.throw(400, '缺少参数 commentId');
}
const res = await cnb.issue.getComment(repo, issueNumber, commentId);
ctx.forward(res);
}).addTo(app);
// 修改 Issue 评论
app.route({
path: 'cnb',
key: 'update-issue-comment',
description: '修改 Issue 评论, 参数 repo, issueNumber, commentId, body',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'update-issue-comment',
title: '修改 Issue 评论',
args: {
repo: tool.schema.string().optional().describe('代码仓库名称, 如 my-user/my-repo'),
issueNumber: tool.schema.number().describe('Issue 编号'),
commentId: tool.schema.number().describe('评论 ID'),
body: tool.schema.string().describe('评论内容'),
clearAt: tool.schema.boolean().optional().describe('是否清除评论内容中的 @ 提及,默认: true'),
},
summary: '修改 Issue 评论',
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
let repo = ctx.query?.repo || useKey('CNB_REPO_SLUG_LOWERCASE');
const issueNumber = ctx.query?.issueNumber;
const commentId = ctx.query?.commentId;
let body = ctx.query?.body;
const clearAt = ctx.query?.clearAt ?? true;
if (!repo) {
ctx.throw(400, '缺少参数 repo');
}
if (!issueNumber) {
ctx.throw(400, '缺少参数 issueNumber');
}
if (!commentId) {
ctx.throw(400, '缺少参数 commentId');
}
if (!body) {
ctx.throw(400, '缺少参数 body');
}
if (clearAt && body) {
// 清除评论内容中的 @ 提及
body = body.replace(/@/g, '');
}
const res = await cnb.issue.updateComment(repo, issueNumber, commentId, body);
ctx.forward(res);
}).addTo(app);