第 4 / 8 节15 分钟
🐙

GitHub 集成

中级

与 GitHub 深度集成,自动处理 Issue 和 PR

🎬

视频教程即将上线

GitHub 集成

学习如何将 OpenCode 与 GitHub 深度集成,实现更高效的协作开发。

# 配置 GitHub 认证#

## 方式一:使用 GitHub CLI#

如果已安装 gh CLI:

bash
1# 登录 GitHub
2gh auth login
3
4# OpenCode 会自动使用 gh 的认证

## 方式二:Personal Access Token#

创建 Token:

  1. 访问 GitHub Settings → Developer settings → Personal access tokens
  2. 生成新 token,选择所需权限
  3. 配置 OpenCode:
bash
export GITHUB_TOKEN="ghp_xxxxx"

或在配置文件中:

toml
[github]
token = "ghp_xxxxx"

## 方式三:GitHub App#

适用于组织级别的集成:

toml
1[github.app]
2id = "123456"
3private_key_path = "~/.opencode/github-app.pem"
4installation_id = "789"

# 仓库操作#

## 克隆仓库#

❯ 克隆 facebook/react 仓库
Cloning facebook/react...
✓ Cloned to ./react

❯ 克隆到指定目录
Cloning to ./my-react...

## 查看仓库信息#

❯ 显示当前仓库信息

Repository: user/my-project
  ⭐ Stars: 1,234
  🍴 Forks: 567
  📋 Open Issues: 23
  🔀 Open PRs: 5
  📅 Last updated: 2 hours ago

## 搜索仓库#

❯ 搜索 TypeScript 状态管理库

Found 10 repositories:
1. pmndrs/zustand (⭐ 35k) - 简单的状态管理
2. reduxjs/redux (⭐ 60k) - 可预测的状态容器
3. mobxjs/mobx (⭐ 27k) - 响应式状态管理
...

# Issue 管理#

## 查看 Issues#

❯ 列出当前仓库的 issues

Open Issues (23):
  #142 Bug: Login fails on Safari - bug, high-priority
  #138 Feature: Add dark mode - enhancement
  #135 Docs: Update API documentation - documentation
...

## 创建 Issue#

❯ 创建一个 bug 报告,标题是 "登录页面样式错乱"

Creating issue...
✓ Created #143: 登录页面样式错乱
  URL: https://github.com/user/repo/issues/143

## 评论和关闭#

❯ 在 #142 上添加评论:已在 PR #150 中修复

Adding comment to #142...
✓ Comment added

❯ 关闭 issue #142
Closing #142...
✓ Issue closed

# Pull Request 工作流#

## 创建 PR#

❯ 为当前分支创建 PR

Creating pull request...
  Title: feat: Add user authentication
  Base: main ← feature/auth

  Summary generated:
  - Added login/logout functionality
  - Implemented JWT authentication
  - Added auth middleware

✓ Created PR #150
  URL: https://github.com/user/repo/pull/150

## 查看 PR#

❯ 显示 PR #150 的详情

PR #150: feat: Add user authentication
  Status: Open
  Author: @username
  Reviews: 1 approved, 1 changes requested
  Checks: ✓ CI passed

  Files changed (5):
    src/auth/login.ts (+120, -0)
    src/auth/middleware.ts (+45, -0)
    ...

## 代码审查#

❯ 审查 PR #150 的代码

Reviewing PR #150...

Issues found:
1. src/auth/login.ts:45 - 缺少错误处理
2. src/auth/middleware.ts:20 - 应使用常量替代硬编码

Suggestions:
1. 考虑添加 rate limiting
2. JWT secret 应从环境变量读取

Overall: 代码质量良好,有几处小问题需要修复

## 合并 PR#

❯ 合并 PR #150

Merge options:
1. Create a merge commit
2. Squash and merge (recommended)
3. Rebase and merge

Select [2]: 2

Squashing and merging...
✓ PR #150 merged successfully
✓ Branch 'feature/auth' deleted

# 分支管理#

## 查看分支#

❯ 列出所有分支

Local branches:
  * main (up to date)
    feature/auth (2 commits ahead)
    bugfix/login (1 commit behind)

Remote branches:
    origin/main
    origin/develop
    origin/release/v2.0

## 创建分支#

❯ 创建功能分支 feature/dark-mode

Creating branch...
✓ Created feature/dark-mode from main
✓ Switched to feature/dark-mode

## 同步分支#

❯ 同步当前分支与 main

Syncing with main...
  Fetching origin/main...
  Rebasing onto main...

✓ Branch synced successfully
  Your branch is now up to date with 'origin/main'

# GitHub Actions#

## 查看工作流#

❯ 显示 CI 工作流状态

Recent workflow runs:
  ✓ CI - main (#234) - 2m ago
  ✗ CI - feature/auth (#233) - 15m ago
  ✓ Deploy - main (#232) - 1h ago

## 查看失败详情#

❯ 显示 #233 失败的原因

Workflow Run #233 - CI
Status: Failed
Duration: 3m 45s

Failed job: test
  Step: Run tests
  Error:
    FAIL src/auth/__tests__/login.test.ts
    ✕ should handle invalid credentials (15 ms)
    Expected: 401
    Received: 500

## 重新运行#

❯ 重新运行 #233

Re-running workflow #233...
✓ Workflow triggered
  Watch: https://github.com/user/repo/actions/runs/233

# 协作功能#

## 请求审查#

❯ 请求 @alice 和 @bob 审查 PR #150

Requesting reviews...
✓ Review requested from @alice
✓ Review requested from @bob

## 分配任务#

❯ 将 issue #142 分配给 @alice

Assigning issue...
✓ Assigned #142 to @alice

## 添加标签#

❯ 给 PR #150 添加标签 "ready-for-review"

Adding labels...
✓ Label 'ready-for-review' added to #150

# 高级配置#

## 自定义 PR 模板#

toml
1[github.pr_template]
2auto_generate = true
3include_summary = true
4include_test_plan = true
5include_screenshots = false

## 自动化规则#

toml
1[github.automation]
2# 自动分配审查者
3auto_assign_reviewers = ["@alice", "@bob"]
4
5# 自动添加标签
6auto_label_rules = [
7  { path = "docs/*", label = "documentation" },
8  { path = "src/api/*", label = "api" }
9]

# 下一步#

掌握 GitHub 集成后,学习如何使用记忆系统提高开发效率。