跳到內容

GitHub

在 GitHub Issues 和拉取請求中使用 opencode。

opencode 與您的 GitHub 工作流程整合。在評論中提及 /opencode/oc,opencode 將在您的 GitHub Actions Runner 中執行任務。


功能

  • 分類問題:要求 opencode 調查問題並向您解釋。
  • 修復和實作:要求 opencode 修復問題或實作功能。它將在一個新分支中工作並提交包含所有變更的 PR。
  • 安全:opencode 在 GitHub 的 Runner 中執行。

安裝

在 GitHub 儲存庫中的專案中執行以下指令:

Terminal window
opencode github install

這將引導您完成安裝 GitHub 應用程式、建立工作流程和設定 Secrets。


手動設定

或者您可以手動設定。

  1. 安裝 GitHub 應用程式

    前往 github.com/apps/opencode-agent。確保它已安裝在目標儲存庫上。

  2. 新增工作流程

    將以下工作流程檔案新增到儲存庫中的 .github/workflows/opencode.yml 中。確保在 env 中設定適當的 model 和所需的 API 金鑰。

    .github/workflows/opencode.yml
    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. 設定 Secrets

    在您的組織或專案的 Settings 中,展開左側的 Secrets and variables,然後選擇 Actions。並新增所需的 API 金鑰。


設定

  • model:與 opencode 一起使用的模型。採用 provider/model 格式。這是必需的

  • agent: 使用的代理。必須是主要代理。如果未找到,則從設定退回到 default_agent"build"

  • share:是否分享 opencode 工作階段。對於公共儲存庫,預設為 true

  • prompt:可選的自定義提示以覆寫預設行為。使用它來自定義 opencode 處理請求的方式。

  • token:可選的 GitHub 存取權杖,用於執行建立評論、提交變更和打開拉取請求等操作。預設情況下,opencode 使用來自 opencode GitHub 應用程式的安裝存取權杖,因此提交、評論和拉取請求顯示為來自應用程式。

    或者,您可以使用 GitHub Action Runner 的 內建 GITHUB_TOKEN,而無需安裝 opencode GitHub 應用程式。只需確保在您的工作流程中授予所需的權限:

    permissions:
    id-token: write
    contents: write
    pull-requests: write
    issues: write

    注意:使用 GITHUB_TOKEN 時,opencode 執行的操作(如提交和評論)將不會觸發其他工作流程。


支援的事件

opencode 可以由以下 GitHub 事件觸發:

事件類型觸發條件詳情
issue_comment對問題或 PR 發表評論在評論中提及 /opencode/oc。 opencode 讀取上下文並可以建立分支、打開 PR 或回覆。
pull_request_review_comment對 PR 中的特定程式碼行進行評論在檢查程式碼時提及 /opencode/oc。 opencode 接收檔案路徑、行號和 diff 上下文。
issues問題已打開或已編輯建立或修改問題時自動觸發 opencode。需要 prompt 輸入。
pull_requestPR 已開啟或已更新當 PR 被開啟、同步或重新開啟時自動觸發 opencode。對於自動評論很有用。
schedule基於 Cron 的排程按排程執行 opencode。需要 prompt 輸入。輸出進入日誌和 PR(沒有可評論的問題)。
workflow_dispatch從 GitHub UI 手動觸發透過「Actions」標籤按需觸發 opencode。需要 prompt 輸入。輸出進入日誌和 PR。

排程範例

按排程執行 opencode 以執行自動化任務:

.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: writepull-requests: write


拉取請求範例

打開或更新 PR 時自動審閱:

.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 天的帳號以減少垃圾訊息:

.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 的行為。

.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 Issue 中添加此評論。

    /opencode explain this issue

    opencode 將閱讀整個討論串,包括所有評論,並回覆並提供清晰的解釋。

  • 解決問題

    在 GitHub Issue 中,說:

    /opencode fix this

    opencode 將建立一個新分支,實作變更,並使用變更打開 PR。

  • 審閱 PR 並進行變更

    在 GitHub PR 上留下以下評論。

    Delete the attachment from S3 when the note is removed /oc

    opencode 將實作請求的變更並將其推送到分支。

  • 查看特定程式碼行

    直接在 PR 的「Files」標籤中的程式碼行上留下評論。 opencode 自動檢測檔案、行號和 diff 上下文以提供精確的回應。

    [Comment on specific lines in Files tab]
    /oc add error handling here

    opencode 將查看:

    • 正在審閱的確切檔案
    • 具體程式碼行
    • 周圍的 diff 上下文
    • 行號資訊

    這允許更有針對性的請求,而無需手動指定檔案路徑或行號。