← Back to Plugins
Tools

Github Pr Auto Fix

bjyounger By bjyounger 👁 9 views ▲ 0 votes

OpenClaw plugin for auto-fixing GitHub PR CI failures and review comments

GitHub

Install

npm install
npm

Configuration Example

{
  "plugins": {
    "github-pr-auto-fix": {
      "githubWebhookSecret": "your-webhook-secret",
      "githubToken": "your-github-token"
    }
  }
}

README

# GitHub PR Auto-Fix Plugin

自动修复 GitHub PR 的 CI 失败和 Review 评论的 OpenClaw 插件。

## 功能特性

### CI 失败自动修复
- 监听 GitHub `check_run` 事件
- 自动分析 CI 失败日志
- 支持多种错误类型识别:
  - **测试失败**: Jest, Vitest, Mocha, 等主流测试框架
  - **TypeScript 错误**: 类型错误、编译错误
  - **ESLint 错误**: 代码风格和静态检查问题
  - **依赖错误**: npm/yarn/pnpm 安装冲突
  - **构建错误**: 打包构建失败
  - **超时错误**: 测试或构建超时

### Review 评论智能处理
- 监听 `pull_request_review` 和 `pull_request_review_comment` 事件
- 智能分析评论意图:
  - **代码修改请求**: 需要修改代码
  - **问题询问**: 需要回答问题
  - **建议讨论**: 可选处理的建议
  - **认可批准**: 无需处理
- 自动优先级排序和安全确认

### 状态管理
- 完整的 PR 处理状态跟踪
- 幂等性保证:同一 PR 只处理一次
- 支持状态文件清理

## 安装

### 方式一:直接安装

将插件目录放置在 OpenClaw 的 plugins 目录下:

```bash
cp -r github-pr-auto-fix /root/.openclaw/plugins/
```

### 方式二:从源码构建

```bash
cd /root/.openclaw/plugins/github-pr-auto-fix
npm install
npm run build
```

## 配置

### 基础配置

在 `openclaw.json` 中添加插件配置:

```json
{
  "plugins": {
    "github-pr-auto-fix": {
      "githubWebhookSecret": "your-webhook-secret",
      "githubToken": "your-github-token"
    }
  }
}
```

### 配置项说明

| 配置项 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| `githubWebhookSecret` | string | 是 | GitHub App/Webhook 的 secret,用于验证 webhook 请求 |
| `githubToken` | string | 是 | GitHub Personal Access Token 或 App Token,用于 API 调用 |

### GitHub Token 权限要求

GitHub Token 需要以下权限:

- `repo` - 读取仓库内容和创建 PR 评论
- `read:org` - 读取组织信息(可选)
- `workflow` - 读取 workflow 运行状态

### GitHub Webhook 配置

1. 在 GitHub 仓库设置中,进入 **Settings > Webhooks**
2. 点击 **Add webhook**
3. 配置以下选项:
   - **Payload URL**: `http://your-openclaw-host/github-pr-auto-fix/webhook`
   - **Content type**: `application/json`
   - **Secret**: 与配置中 `githubWebhookSecret` 一致
   - **Events**: 选择以下事件
     - `Check runs`
     - `Pull request reviews`
     - `Pull request review comments`

## 使用示例

### 场景一:CI 测试失败自动修复

1. 开发者提交 PR
2. CI 运行测试,某个测试失败
3. 插件接收 `check_run` 事件
4. 自动分析错误日志,识别失败的测试
5. 派发修复任务到 subagent
6. 自动修复并推送代码

```
PR #123 created → CI runs → Test fails → Plugin detects
    → Analyzes error → "expect(result).toEqual(expected)"
    → Dispatches fix task → Fixes code → Pushes commit
```

### 场景二:Review 评论自动处理

1. Reviewer 提交 review,请求修改
2. 插件接收 `pull_request_review` 事件
3. 分析评论内容,识别修改请求
4. 自动处理评论或请求确认

```
Review submitted → "Please fix this function"
    → Plugin analyzes → Intent: code_change_request
    → Priority: medium → Dispatches fix task
```

## 目录结构

```
github-pr-auto-fix/
├── plugin.ts              # 主入口
├── package.json           # 依赖配置
├── config.example.json    # 配置示例
├── README.md              # 文档
├── handlers/
│   ├── index.ts          # 导出
│   └── webhook.ts        # Webhook 处理器
├── lib/
│   ├── index.ts          # 导出
│   ├── github.ts         # GitHub API 封装
│   ├── state.ts          # 状态管理
│   ├── ci-log.ts         # CI 日志获取
│   ├── error-classifier.ts    # 错误分类
│   ├── fix-dispatcher.ts      # 修复派发
│   ├── comment-analyzer.ts    # 评论意图分析
│   └── review-handler.ts      # Review 评论处理
└── tests/
    ├── fixtures/         # 测试数据
    ├── unit/             # 单元测试
    └── integration/      # 集成测试
```

## API 参考

### Webhook 端点

```
POST /github-pr-auto-fix/webhook
```

接收 GitHub webhook 事件。

### 支持的 GitHub 事件

#### check_run

触发条件:
- `status` = `completed`
- `conclusion` = `failure`
- 关联的 PR 存在

#### pull_request_review

触发条件:
- `state` = `changes_requested`

#### pull_request_review_comment

触发条件:
- `action` = `created`
- 非机器人评论

## 状态管理

插件在 workspace 目录下创建 `.auto-fix-pr/` 目录存储状态:

```
.auto-fix-pr/
├── pending/     # 待处理的 PR
├── processing/  # 正在处理的 PR
└── completed/   # 已完成的 PR
```

### 状态流转

```
pending → processing → success/failed
```

### 状态文件格式

```json
{
  "prKey": "owner/repo/123",
  "repo": "owner/repo",
  "prNumber": 123,
  "status": "processing",
  "trigger": "check_run",
  "triggerData": {
    "checkRunId": 456,
    "checkRunName": "CI",
    "conclusion": "failure"
  },
  "createdAt": 1234567890000,
  "updatedAt": 1234567890000,
  "fixCommit": "abc123"
}
```

## 错误分类

### 支持的错误类型

| 类型 | 说明 | 示例 |
|------|------|------|
| `test_failure` | 测试失败 | Jest/Vitest/Mocha 断言失败 |
| `type_error` | TypeScript 类型错误 | TS2322, TS2339 |
| `compile_error` | 编译错误 | 语法错误、缺少导入 |
| `lint_error` | ESLint 错误 | no-unused-vars, react-hooks/rules |
| `dependency_error` | 依赖安装错误 | 版本冲突、包不存在 |
| `build_error` | 构建错误 | Vite/Webpack 打包失败 |
| `timeout_error` | 超时错误 | 测试超时 |
| `unknown` | 未知错误 | 无法分类的错误 |

### 错误优先级

```
test_failure > type_error > compile_error > lint_error > dependency_error > build_error > timeout_error > unknown
```

## 评论意图分析

### 意图类型

| 类型 | 说明 | 示例 |
|------|------|------|
| `code_change_request` | 需要修改代码 | "Please fix this bug" |
| `question` | 需要回答问题 | "Why did you use this approach?" |
| `discussion` | 讨论/建议 | "Maybe we should consider..." |
| `approval` | 认可 | "LGTM!" |
| `unknown` | 无法判断 | 空评论或格式不清 |

### 优先级检测

- **high**: 包含 `critical`, `urgent`, `security`, `blocking` 等关键词
- **medium**: 默认的修改请求和问题
- **low**: 包含 `nit`, `minor`, `optional`, `nice to have` 等

### 需要人工确认的情况

- 涉及敏感关键词(password, secret, token 等)
- 包含删除操作(delete, drop, remove)
- 涉及重构或架构讨论
- 包含 @ 提及其他用户
- 评论内容过长(超过 1000 字符)

## 测试

### 运行测试

```bash
# 安装测试依赖
npm install

# 运行所有测试
npm test

# 运行单元测试
npm run test:unit

# 运行集成测试
npm run test:integration

# 生成覆盖率报告
npm run test:coverage
```

### 测试目录结构

```
tests/
├── fixtures/
│   └── sample-ci-logs.ts     # 测试用的 CI 日志样本
├── unit/
│   ├── error-classifier.test.ts    # 错误分类测试
│   ├── comment-analyzer.test.ts    # 评论分析测试
│   └── state.test.ts               # 状态管理测试
└── integration/
    ├── ci-fail-flow.test.ts        # CI 失败流程测试
    └── review-comment-flow.test.ts # Review 评论流程测试
```

## 故障排查

### 常见问题

#### 1. Webhook 签名验证失败

**症状**: 日志显示 "Webhook 签名验证失败"

**解决方案**:
- 检查 `githubWebhookSecret` 配置是否正确
- 确认 GitHub webhook 设置中的 Secret 与配置一致
- 确认请求头包含 `X-Hub-Signature-256`

#### 2. GitHub API 调用失败

**症状**: 日志显示 "GitHub API error: 401" 或 "403"

**解决方案**:
- 检查 `githubToken` 是否有效
- 确认 Token 有足够的权限(repo, workflow)
- 检查 API 速率限制

#### 3. CI 日志获取失败

**症状**: 错误分析返回空或 "Failed to fetch CI log"

**解决方案**:
- 确认服务器上安装了 `gh` CLI 工具
- 确认 `gh` CLI 已登录并有权限访问仓库
- 检查 check run 是否有可用的日志输出

#### 4. 重复处理同一 PR

**症状**: 同一 PR 被多次处理

**解决方案**:
- 检查 `.auto-fix-pr/` 目录下的状态文件
- 确认状态正确更新(pending → processing → completed)
- 如果需要重新处理,删除对应的状态文件

#### 5. Review 评论未触发处理

**症状**: 提交 review 后没有反应

**解决方案**:
- 确认 review 状态为 `changes_requested`
- 检查是否是机器人评论(机器人评论会被跳过)
- 确认 webhook 正确配置了 `Pull request reviews` 事件

### 调试模式

启用详细日志:

```json
{
  "plugins": {
    "github-pr-auto-fix": {
      "debug": true
    }
  }
}
```

### 日志位置

插件日志会输出到 OpenClaw 的主日志中,通常位于:

```
/root/.openclaw/logs/
```

## 开发

### 环境设置

```bash
# 克隆仓库
cd /root/.openclaw/plugins
git clone <repo-url> github-pr-auto-fix

# 安装依赖
cd github-pr-auto-fix
npm install

# 类型检查
npx tsc --noEmit
```

### 添加新的错误类型

1. 在 `lib/error-classifier.ts` 中添加新的错误类型到 `ErrorType`
2. 在 `ERROR_PATTERNS` 中添加匹配模式
3. 在 `classifySection` 中添加处理逻辑
4. 添加对应的单元测试

### 添加新的评论意图

1. 在 `lib/comment-analyzer.ts` 中添加新的意图类型到 `CommentIntent`
2. 在 `PATTERNS` 中添加匹配模式
3. 在 `analyzeIntent` 中添加检测逻辑
4. 添加对应的单元测试

## 许可证

MIT
tools

Comments

Sign in to leave a comment

Loading comments...