github.mdx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. ---
  2. title: GitHub
  3. description: Use OpenCode in GitHub issues and pull-requests.
  4. ---
  5. OpenCode integrates with your GitHub workflow. Mention `/opencode` or `/oc` in your comment, and OpenCode will execute tasks within your GitHub Actions runner.
  6. ---
  7. ## Features
  8. - **Triage issues**: Ask OpenCode to look into an issue and explain it to you.
  9. - **Fix and implement**: Ask OpenCode to fix an issue or implement a feature. And it will work in a new branch and submits a PR with all the changes.
  10. - **Secure**: OpenCode runs inside your GitHub's runners.
  11. ---
  12. ## Installation
  13. Run the following command in a project that is in a GitHub repo:
  14. ```bash
  15. opencode github install
  16. ```
  17. This will walk you through installing the GitHub app, creating the workflow, and setting up secrets.
  18. ---
  19. ### Manual Setup
  20. Or you can set it up manually.
  21. 1. **Install the GitHub app**
  22. Head over to [**github.com/apps/opencode-agent**](https://github.com/apps/opencode-agent). Make sure it's installed on the target repository.
  23. 2. **Add the workflow**
  24. Add the following workflow file to `.github/workflows/opencode.yml` in your repo. Make sure to set the appropriate `model` and required API keys in `env`.
  25. ```yml title=".github/workflows/opencode.yml" {24,26}
  26. name: opencode
  27. on:
  28. issue_comment:
  29. types: [created]
  30. pull_request_review_comment:
  31. types: [created]
  32. jobs:
  33. opencode:
  34. if: |
  35. contains(github.event.comment.body, '/oc') ||
  36. contains(github.event.comment.body, '/opencode')
  37. runs-on: ubuntu-latest
  38. permissions:
  39. id-token: write
  40. steps:
  41. - name: Checkout repository
  42. uses: actions/checkout@v4
  43. with:
  44. fetch-depth: 1
  45. - name: Run OpenCode
  46. uses: sst/opencode/github@latest
  47. env:
  48. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  49. with:
  50. model: anthropic/claude-sonnet-4-20250514
  51. # share: true
  52. # github_token: xxxx
  53. ```
  54. 3. **Store the API keys in secrets**
  55. In your organization or project **settings**, expand **Secrets and variables** on the left and select **Actions**. And add the required API keys.
  56. ---
  57. ## Configuration
  58. - `model`: The model to use with OpenCode. Takes the format of `provider/model`. This is **required**.
  59. - `agent`: The agent to use. Must be a primary agent. Falls back to `default_agent` from config or `"build"` if not found.
  60. - `share`: Whether to share the OpenCode session. Defaults to **true** for public repositories.
  61. - `prompt`: Optional custom prompt to override the default behavior. Use this to customize how OpenCode processes requests.
  62. - `token`: Optional GitHub access token for performing operations such as creating comments, committing changes, and opening pull requests. By default, OpenCode uses the installation access token from the OpenCode GitHub App, so commits, comments, and pull requests appear as coming from the app.
  63. Alternatively, you can use the GitHub Action runner's [built-in `GITHUB_TOKEN`](https://docs.github.com/en/actions/tutorials/authenticate-with-github_token) without installing the OpenCode GitHub App. Just make sure to grant the required permissions in your workflow:
  64. ```yaml
  65. permissions:
  66. id-token: write
  67. contents: write
  68. pull-requests: write
  69. issues: write
  70. ```
  71. You can also use a [personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)(PAT) if preferred.
  72. ---
  73. ## Supported Events
  74. OpenCode can be triggered by the following GitHub events:
  75. | Event Type | Triggered By | Details |
  76. | ----------------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  77. | `issue_comment` | Comment on an issue or PR | Mention `/opencode` or `/oc` in your comment. OpenCode reads the issue/PR context and can create branches, open PRs, or reply with explanations. |
  78. | `pull_request_review_comment` | Comment on specific code lines in a PR | Mention `/opencode` or `/oc` while reviewing code. OpenCode receives file path, line numbers, and diff context for precise responses. |
  79. | `schedule` | Cron-based schedule | Run OpenCode on a schedule using the `prompt` input. Useful for automated code reviews, reports, or maintenance tasks. OpenCode can create issues or PRs as needed. |
  80. | `pull_request` | PR opened or updated | Automatically trigger OpenCode when PRs are opened, synchronized, or reopened. Useful for automated reviews without needing to leave a comment. |
  81. ### Schedule Example
  82. Run OpenCode on a schedule to perform automated tasks:
  83. ```yaml title=".github/workflows/opencode-scheduled.yml"
  84. name: Scheduled OpenCode Task
  85. on:
  86. schedule:
  87. - cron: "0 9 * * 1" # Every Monday at 9am UTC
  88. jobs:
  89. opencode:
  90. runs-on: ubuntu-latest
  91. permissions:
  92. id-token: write
  93. contents: write
  94. pull-requests: write
  95. issues: write
  96. steps:
  97. - name: Checkout repository
  98. uses: actions/checkout@v4
  99. - name: Run OpenCode
  100. uses: sst/opencode/github@latest
  101. env:
  102. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  103. with:
  104. model: anthropic/claude-sonnet-4-20250514
  105. prompt: |
  106. Review the codebase for any TODO comments and create a summary.
  107. If you find issues worth addressing, open an issue to track them.
  108. ```
  109. For scheduled events, the `prompt` input is **required** since there's no comment to extract instructions from.
  110. > **Note:** Scheduled workflows run without a user context to permission-check, so the workflow must grant `contents: write` and `pull-requests: write` if you expect OpenCode to create branches or PRs during a scheduled run.
  111. ---
  112. ### Pull Request Example
  113. Automatically review PRs when they are opened or updated:
  114. ```yaml title=".github/workflows/opencode-review.yml"
  115. name: opencode-review
  116. on:
  117. pull_request:
  118. types: [opened, synchronize, reopened, ready_for_review]
  119. jobs:
  120. review:
  121. runs-on: ubuntu-latest
  122. permissions:
  123. id-token: write
  124. contents: read
  125. pull-requests: read
  126. issues: read
  127. steps:
  128. - uses: actions/checkout@v4
  129. - uses: sst/opencode/github@latest
  130. env:
  131. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  132. with:
  133. model: anthropic/claude-sonnet-4-20250514
  134. prompt: |
  135. Review this pull request:
  136. - Check for code quality issues
  137. - Look for potential bugs
  138. - Suggest improvements
  139. ```
  140. For `pull_request` events, if no `prompt` is provided, OpenCode defaults to reviewing the pull request.
  141. ---
  142. ## Custom prompts
  143. Override the default prompt to customize OpenCode's behavior for your workflow.
  144. ```yaml title=".github/workflows/opencode.yml"
  145. - uses: sst/opencode/github@latest
  146. with:
  147. model: anthropic/claude-sonnet-4-5
  148. prompt: |
  149. Review this pull request:
  150. - Check for code quality issues
  151. - Look for potential bugs
  152. - Suggest improvements
  153. ```
  154. This is useful for enforcing specific review criteria, coding standards, or focus areas relevant to your project.
  155. ---
  156. ## Examples
  157. Here are some examples of how you can use OpenCode in GitHub.
  158. - **Explain an issue**
  159. Add this comment in a GitHub issue.
  160. ```
  161. /opencode explain this issue
  162. ```
  163. OpenCode will read the entire thread, including all comments, and reply with a clear explanation.
  164. - **Fix an issue**
  165. In a GitHub issue, say:
  166. ```
  167. /opencode fix this
  168. ```
  169. And OpenCode will create a new branch, implement the changes, and open a PR with the changes.
  170. - **Review PRs and make changes**
  171. Leave the following comment on a GitHub PR.
  172. ```
  173. Delete the attachment from S3 when the note is removed /oc
  174. ```
  175. OpenCode will implement the requested change and commit it to the same PR.
  176. - **Review specific code lines**
  177. Leave a comment directly on code lines in the PR's "Files" tab. OpenCode automatically detects the file, line numbers, and diff context to provide precise responses.
  178. ```
  179. [Comment on specific lines in Files tab]
  180. /oc add error handling here
  181. ```
  182. When commenting on specific lines, OpenCode receives:
  183. - The exact file being reviewed
  184. - The specific lines of code
  185. - The surrounding diff context
  186. - Line number information
  187. This allows for more targeted requests without needing to specify file paths or line numbers manually.