322 lines
10 KiB
Plaintext
322 lines
10 KiB
Plaintext
---
|
||
title: GitHub
|
||
description: 在 GitHub 问题和拉取请求中使用 OpenCode。
|
||
---
|
||
|
||
OpenCode 与您的 GitHub 工作流程集成。在评论中提及 `/opencode` 或 `/oc`,OpenCode 将在您的 GitHub Actions 运行器中执行任务。
|
||
|
||
---
|
||
|
||
## 特徵
|
||
|
||
- **分类问题**:要求 OpenCode 调查问题并向您解释。
|
||
- **修复和实施**:要求 OpenCode 修复问题或实施功能。将在一个新的分支中工作并提交包含所有更改的 PR。
|
||
- **安全**:OpenCode 在 GitHub 的运行器中运行。
|
||
|
||
---
|
||
|
||
## 安裝
|
||
|
||
在 GitHub 存储库中的项目中运行以下命令:
|
||
|
||
```bash
|
||
opencode github install
|
||
```
|
||
|
||
这将引导您完成安装 GitHub 应用程序、创建工作流程和设置机密。
|
||
|
||
---
|
||
|
||
### 手動設置
|
||
|
||
或者您可以手動設置。
|
||
|
||
1. 错误 500(服务器错误)!!1500。这是一个错误。出现错误。请稍后重试。我们只知道这些。
|
||
|
||
前往[**github.com/apps/opencode-agent**](https://github.com/apps/opencode-agent)。确保它已安装在目标存储库上。
|
||
|
||
2. **添加工作流程**
|
||
|
||
将以下工作流程文件添加到存储库中的`.github/workflows/opencode.yml`中。确保在`env`中设置适当的`model`和所需的API按键。
|
||
|
||
```yml title=".github/workflows/opencode.yml" {24,26}
|
||
name: opencode
|
||
|
||
on:
|
||
issue_comment:
|
||
types: [created]
|
||
pull_request_review_comment:
|
||
types: [created]
|
||
|
||
jobs:
|
||
opencode:
|
||
if: |
|
||
contains(github.event.comment.body, '/oc') ||
|
||
contains(github.event.comment.body, '/opencode')
|
||
runs-on: ubuntu-latest
|
||
permissions:
|
||
id-token: write
|
||
steps:
|
||
- name: Checkout repository
|
||
uses: actions/checkout@v6
|
||
with:
|
||
fetch-depth: 1
|
||
persist-credentials: false
|
||
|
||
- name: Run OpenCode
|
||
uses: anomalyco/opencode/github@latest
|
||
env:
|
||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||
with:
|
||
model: anthropic/claude-sonnet-4-20250514
|
||
# share: true
|
||
# github_token: xxxx
|
||
```
|
||
|
||
3. 错误 500(服务器错误)!!1500。这是一个错误。出现错误。请稍后重试。我们只知道这些。
|
||
|
||
在您的组织或项目的**设置**中,展开左侧的**秘密和变量**,然后选择**操作**。并添加所需的API钥匙。
|
||
|
||
---
|
||
|
||
## 配置
|
||
|
||
- `model`:与 OpenCode 一起使用的模型。采用 `provider/model` 格式。这是**必需的**。
|
||
- `agent`:要使用的代理。必须是一级代理。如果未找到,则从配置回退到 `default_agent` 或 `"build"`。
|
||
- `share`:是否共享OpenCode会话。对于公共存储库,默认为**true**。
|
||
- `prompt`:可选的自定义提示以覆盖默认行为。使用它来自定义 OpenCode 处理请求的方式。
|
||
- `token`:可选的 GitHub 访问令牌,用于执行创建评论、提交更改和打开拉取请求等操作。默认情况下,OpenCode 使用来自 OpenCode GitHub 应用程序的安装访问令牌,因此提交、评论和拉取请求显示为来自应用。
|
||
|
||
或者,您可以使用 GitHub Action 运行程序的[内置`GITHUB_TOKEN`](https://docs.github.com/en/actions/tutorials/authenticate-with-github_token),而无需安装 OpenCode GitHub 应用程序。只需确保在您的工作流程中所需的权限:
|
||
|
||
```yaml
|
||
permissions:
|
||
id-token: write
|
||
contents: write
|
||
pull-requests: write
|
||
issues: write
|
||
```
|
||
|
||
错误 500(服务器错误)!!1500。这是一个错误。出现错误。请稍后重试。我们只知道这些。
|
||
|
||
---
|
||
|
||
## 支持的活動
|
||
|
||
OpenCode 可以由以下 GitHub 事件触发:
|
||
|
||
|事件類型 |觸發者 |詳情 |
|
||
| ----------------------------- | -------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
|
||
| `issue_comment` |对问题或 PR 发表评论 |在评论中提及 `/opencode` 或 `/oc`。 OpenCode 读取上下文并可以创建分支、打开 PR 或回复。
|
||
| `pull_request_review_comment` |对 PR 中的特定代码行进行评论 |在检查代码时提及`/opencode` 或`/oc`。 OpenCode 接收文件路径、行号和 diff 上下文。 |
|
||
| `issues` |问题已打开或已编辑|创建或修改问题时自动触发OpenCode。需要`prompt` 输入。
|
||
| `pull_request` | PR 已开启或已更新 |当 PR 打开、同步或重新打开时自动触发 OpenCode。对于自动评论很有用。
|
||
| `schedule` | 基于 Cron 的计划 | 按计划运行 OpenCode。需要 `prompt` 输入。输出进入日志和 PR(没有可评论的问题)。
|
||
| `workflow_dispatch` | 从 GitHub UI 手动触发 |通过“操作”选项卡触发 OpenCode。需要 `prompt` 输入。输出进入日志和 PR。 |
|
||
|
||
### 時間表示例
|
||
|
||
按计划运行OpenCode以执行自动化任务:
|
||
|
||
```yaml title=".github/workflows/opencode-scheduled.yml"
|
||
name: Scheduled OpenCode Task
|
||
|
||
on:
|
||
schedule:
|
||
- cron: "0 9 * * 1" # Every Monday at 9am UTC
|
||
|
||
jobs:
|
||
opencode:
|
||
runs-on: ubuntu-latest
|
||
permissions:
|
||
id-token: write
|
||
contents: write
|
||
pull-requests: write
|
||
issues: write
|
||
steps:
|
||
- name: Checkout repository
|
||
uses: actions/checkout@v6
|
||
with:
|
||
persist-credentials: false
|
||
|
||
- name: Run OpenCode
|
||
uses: anomalyco/opencode/github@latest
|
||
env:
|
||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||
with:
|
||
model: anthropic/claude-sonnet-4-20250514
|
||
prompt: |
|
||
Review the codebase for any TODO comments and create a summary.
|
||
If you find issues worth addressing, open an issue to track them.
|
||
```
|
||
|
||
对于计划事件,`prompt` 输入是**必需的**,因为没有注释可以从中提取指令。希望计划工作流在没有用户上下文的情况下运行并进行权限检查,因此如果您 OpenCode 创建分支或 PR,工作流必须支持 `contents: write` 和 `pull-requests: write`。
|
||
|
||
---
|
||
|
||
### 拉取請求示例
|
||
|
||
或更新公关时间自动审核:
|
||
|
||
```yaml title=".github/workflows/opencode-review.yml"
|
||
name: opencode-review
|
||
|
||
on:
|
||
pull_request:
|
||
types: [opened, synchronize, reopened, ready_for_review]
|
||
|
||
jobs:
|
||
review:
|
||
runs-on: ubuntu-latest
|
||
permissions:
|
||
id-token: write
|
||
contents: read
|
||
pull-requests: read
|
||
issues: read
|
||
steps:
|
||
- uses: actions/checkout@v6
|
||
with:
|
||
persist-credentials: false
|
||
- uses: anomalyco/opencode/github@latest
|
||
env:
|
||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||
with:
|
||
model: anthropic/claude-sonnet-4-20250514
|
||
use_github_token: true
|
||
prompt: |
|
||
Review this pull request:
|
||
- Check for code quality issues
|
||
- Look for potential bugs
|
||
- Suggest improvements
|
||
```
|
||
|
||
对于`pull_request`事件,如果未提供`prompt`,OpenCode将默认审核拉取请求。
|
||
|
||
---
|
||
|
||
### 問題分類示例
|
||
|
||
自動分類新問題。此示例過濾超過 30 天的帳戶以減少垃圾郵件:
|
||
|
||
```yaml title=".github/workflows/opencode-triage.yml"
|
||
name: Issue Triage
|
||
|
||
on:
|
||
issues:
|
||
types: [opened]
|
||
|
||
jobs:
|
||
triage:
|
||
runs-on: ubuntu-latest
|
||
permissions:
|
||
id-token: write
|
||
contents: write
|
||
pull-requests: write
|
||
issues: write
|
||
steps:
|
||
- name: Check account age
|
||
id: check
|
||
uses: actions/github-script@v7
|
||
with:
|
||
script: |
|
||
const user = await github.rest.users.getByUsername({
|
||
username: context.payload.issue.user.login
|
||
});
|
||
const created = new Date(user.data.created_at);
|
||
const days = (Date.now() - created) / (1000 * 60 * 60 * 24);
|
||
return days >= 30;
|
||
result-encoding: string
|
||
|
||
- uses: actions/checkout@v6
|
||
if: steps.check.outputs.result == 'true'
|
||
with:
|
||
persist-credentials: false
|
||
|
||
- uses: anomalyco/opencode/github@latest
|
||
if: steps.check.outputs.result == 'true'
|
||
env:
|
||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||
with:
|
||
model: anthropic/claude-sonnet-4-20250514
|
||
prompt: |
|
||
Review this issue. If there's a clear fix or relevant docs:
|
||
- Provide documentation links
|
||
- Add error handling guidance for code examples
|
||
Otherwise, do not comment.
|
||
```
|
||
|
||
对于 `issues` 事件,`prompt` 输入是**必需的**,因为没有注释可足以提取指令。
|
||
|
||
---
|
||
|
||
## 自定義提示
|
||
|
||
覆盖默认提示,为您的工作流程自定义 OpenCode 的行为。
|
||
|
||
```yaml title=".github/workflows/opencode.yml"
|
||
- uses: anomalyco/opencode/github@latest
|
||
with:
|
||
model: anthropic/claude-sonnet-4-5
|
||
prompt: |
|
||
Review this pull request:
|
||
- Check for code quality issues
|
||
- Look for potential bugs
|
||
- Suggest improvements
|
||
```
|
||
|
||
這對於執行與您的項目相關的特定審查標準、編碼標准或重點領域非常有用。
|
||
|
||
---
|
||
|
||
## 示例
|
||
|
||
以下是如何在 GitHub 中使用 OpenCode 的一些示例。
|
||
|
||
- **解釋一個問題**
|
||
|
||
在 GitHub 问题中添加此评论。
|
||
|
||
```
|
||
/opencode explain this issue
|
||
```
|
||
|
||
OpenCode 将阅读整个线程,包括所有评论,并回复并提供语音解释。
|
||
|
||
- **解決問題**
|
||
|
||
在 GitHub 问题中,说:
|
||
|
||
```
|
||
/opencode fix this
|
||
```
|
||
|
||
OpenCode 将创建一个新分支,实施更改,并使用更改打开 PR。
|
||
|
||
- **审查 PR 并进行更改**
|
||
|
||
在 GitHub PR 上留下以下评论。
|
||
|
||
```
|
||
Delete the attachment from S3 when the note is removed /oc
|
||
```
|
||
|
||
错误 500(服务器错误)!!1500。这是一个错误。出现错误。请稍后重试。我们只知道这些。
|
||
|
||
- **查看特定代碼行**
|
||
|
||
直接在PR的“文件”选项卡中的代码行上留下评论。OpenCode自动检测文件、行号和差异上下文以提供准确的响应。
|
||
|
||
```
|
||
[Comment on specific lines in Files tab]
|
||
/oc add error handling here
|
||
```
|
||
|
||
错误 500(服务器错误)!!1500。这是一个错误。出现错误。请稍后重试。我们只知道这些。
|
||
- 正在審查的確切文件
|
||
- 具體代碼行
|
||
- 周围的差异上下文
|
||
- 行號信息
|
||
|
||
這允許更有針對性的請求,而無需手動指定文件路徑或行號。
|