iT邦幫忙

2026 iThome 鐵人賽

DAY 19
0
AI Engineering

[ opencode ] 開源 AI coding agent系列 第 19

19-opencode | GitHub 與 GitLab 整合:PR review 自動化

  • 分享至 

  • xImage
  •  

! 本篇文章將會介紹 opencode 的 GitHub / GitLab 整合,讓 AI 隊友直接住在你的 repo 裡,一聲令下就開工 :D

TL;DR: https://dev.benben.me/slides/s/ironman-19-github-gitlab

本篇目標

讀完這篇你會學到:

  • 安裝與設定 GitHub 整合(opencode github install
  • 在 issue / PR 用 /opencode/oc 發號施令
  • GitLab 的對應做法與差異

https://ithelp.ithome.com.tw/upload/images/20260919/20162883L3VCNyVSjc.jpg

如果成功的話,就會像這樣

概念:AI 隊友住進 repo

到目前為止,AI 都待在你的 terminal。GitHub 整合反過來——你在 issue 或 PR 留言,AI 在 GitHub Actions runner 上開工:

  • 不用開 terminal、不用 clone repo
  • 它能讀整個討論串、建 branch、改 code、開 PR
  • 跑在你自己的 runner 上,code 不落地第三方機器

GitHub:五分鐘安裝

在 repo 目錄下執行:

opencode github install

這個精靈會帶你完成三件事:裝 GitHub App、建 workflow、設 secrets。想手動的話:

  1. 安裝 github.com/apps/opencode-agent 到目標 repo
  2. 加 workflow .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:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 1
          persist-credentials: false
      - uses: anomalyco/opencode/github@latest
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        with:
          model: anthropic/claude-sonnet-4-20250514
  1. API key 存進 repo 的 Actions secrets (這邊建議可以再開一個新的 api key 喔~)

實戰:三種玩法

玩法一:issue 求解

在 issue 留言:

/opencode explain this issue

AI 讀完整討論串,回覆清楚的解釋——triage 神器。

玩法二:issue 直接變 PR

/opencode fix this

AI 建新 branch、實作修復、開好 PR 附說明。「看到 bug → 一行留言 → 收 PR」的循環就是這麼短。

玩法三:PR 逐行指導

在 PR 的 Files tab 直接在程式碼行上留言

/oc add error handling here

AI 自動收到檔案路徑、行號、diff 前後文——不用再說「那個那個哪個檔案的第幾行」,改完直接 commit 回同一個 PR。

不用留言也能觸發:事件驅動

workflow 的 on 不只吃 comment:

事件 觸發 備註
issue_comment / pull_request_review_comment 留言含 /oc 上面玩的
issues issue 建立或編輯 需帶 prompt,自動 triage
pull_request PR 開啟/更新 不帶 prompt 就預設做 review
schedule cron 排程 定時巡檢、開 issue
workflow_dispatch 手動觸發 Actions 頁面按下去

例如 PR 自動 review:

on:
  pull_request:
    types: [opened, synchronize, reopened]

prompt 換成你團隊的 review 準則,每一位隊友的 PR 都有 AI 先掃一輪。

設定選項

with: 常用參數:

  • model(必填):provider/model 格式
  • agent:指定 primary agent
  • share:公開 repo 預設 true,session 會自動分享
  • prompt:覆寫預設行為,塞你的 review 準則
  • mentions:自訂觸發詞(預設 /opencode,/oc
  • use_github_token: true:不裝 GitHub App、直接用 GITHUB_TOKEN 跑(要自開 permissions)

GitLab:兩條路

GitLab Duo Agent Platform

在 GitLab 環境設定好後,語法是 @opencode mention(GitLab 用 @,GitHub 用 /oc,別搞混):

@opencode explain this issue
@opencode review this merge request

一樣會建 branch、開 merge request。需要 GitLab Premium/Ultimate 訂閱與 Duo Agent Platform 設定,細節照官方 GitLab docs 走。

社群 CI component

沒有 Duo 也能玩——社群維護的 nagyv/gitlab-opencode 把 opencode 包成 GitLab CI component:

include:
  - component: $CI_SERVER_FQDN/nagyv/gitlab-opencode/opencode@2
    inputs:
      auth_json: $OPENCODE_AUTH_JSON
      message: 'Your prompt here'

同樣跑在你自己的 GitLab runner 上。

常見問題

Q:AI 亂改 code 怎麼辦?
A:它開的是新 branch + PR,merge 按鈕在你手上。把它當「超積極的自動化實習生」:產出快、量多,但 final review 永遠是人。

Q:成本怎麼算?
A:跑在你自己的 runner,運算免費;消耗的是 model API(key 放在 secrets)。建議先從便宜 model 開始,觀察幾週再升級。

Q:私有 repo 安全嗎?
A:code 全程在你的 runner 上,opencode 團隊拿不到。要注意的反而是 share——私有 repo 預設不分享,若手動開了,連結是公開可見的(Day 10 的提醒上線)。

小結

  • opencode github install 五分鐘裝完,GitHub 觸發詞 /opencode/oc
  • 三招:issue 解釋、fix this 開 PR、PR 逐行指令
  • 事件驅動(issues / pull_request / schedule)可全自動 review 與巡檢
  • GitLab 用 @opencode(Duo)或社群 CI component

明日預告

Day 20:多模型策略——provider 切換、任務 × 模型選型、成本控管,把每一塊錢花在刀口上。


有任何疑問但沒有 iT 邦幫忙帳號,或是想匿名提問?
歡迎到 https://dev.benben.me/q/P3C5U6 提問或加油打氣,沒意外的話會在完賽之後一起回答 :D


上一篇
18-opencode | Headless 與 server mode:CLI 自動化與 CI
下一篇
20-opencode | 多模型策略:provider 切換與成本平衡
系列文
[ opencode ] 開源 AI coding agent24
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言