2
0

github.mdx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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@v6
  43. with:
  44. fetch-depth: 1
  45. persist-credentials: false
  46. - name: Run OpenCode
  47. uses: anomalyco/opencode/github@latest
  48. env:
  49. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  50. with:
  51. model: anthropic/claude-sonnet-4-20250514
  52. # share: true
  53. # github_token: xxxx
  54. ```
  55. 3. **Store the API keys in secrets**
  56. In your organization or project **settings**, expand **Secrets and variables** on the left and select **Actions**. And add the required API keys.
  57. ---
  58. ## Configuration
  59. - `model`: The model to use with OpenCode. Takes the format of `provider/model`. This is **required**.
  60. - `agent`: The agent to use. Must be a primary agent. Falls back to `default_agent` from config or `"build"` if not found.
  61. - `share`: Whether to share the OpenCode session. Defaults to **true** for public repositories.
  62. - `prompt`: Optional custom prompt to override the default behavior. Use this to customize how OpenCode processes requests.
  63. - `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.
  64. 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:
  65. ```yaml
  66. permissions:
  67. id-token: write
  68. contents: write
  69. pull-requests: write
  70. issues: write
  71. ```
  72. 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.
  73. ---
  74. ## Supported Events
  75. OpenCode can be triggered by the following GitHub events:
  76. | Event Type | Triggered By | Details |
  77. | ----------------------------- | -------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
  78. | `issue_comment` | Comment on an issue or PR | Mention `/opencode` or `/oc` in your comment. OpenCode reads context and can create branches, open PRs, or reply. |
  79. | `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. |
  80. | `issues` | Issue opened or edited | Automatically trigger OpenCode when issues are created or modified. Requires `prompt` input. |
  81. | `pull_request` | PR opened or updated | Automatically trigger OpenCode when PRs are opened, synchronized, or reopened. Useful for automated reviews. |
  82. | `schedule` | Cron-based schedule | Run OpenCode on a schedule. Requires `prompt` input. Output goes to logs and PRs (no issue to comment on). |
  83. | `workflow_dispatch` | Manual trigger from GitHub UI | Trigger OpenCode on demand via Actions tab. Requires `prompt` input. Output goes to logs and PRs. |
  84. ### Schedule Example
  85. Run OpenCode on a schedule to perform automated tasks:
  86. ```yaml title=".github/workflows/opencode-scheduled.yml"
  87. name: Scheduled OpenCode Task
  88. on:
  89. schedule:
  90. - cron: "0 9 * * 1" # Every Monday at 9am UTC
  91. jobs:
  92. opencode:
  93. runs-on: ubuntu-latest
  94. permissions:
  95. id-token: write
  96. contents: write
  97. pull-requests: write
  98. issues: write
  99. steps:
  100. - name: Checkout repository
  101. uses: actions/checkout@v6
  102. with:
  103. persist-credentials: false
  104. - name: Run OpenCode
  105. uses: anomalyco/opencode/github@latest
  106. env:
  107. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  108. with:
  109. model: anthropic/claude-sonnet-4-20250514
  110. prompt: |
  111. Review the codebase for any TODO comments and create a summary.
  112. If you find issues worth addressing, open an issue to track them.
  113. ```
  114. For scheduled events, the `prompt` input is **required** since there's no comment to extract instructions from. 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.
  115. ---
  116. ### Pull Request Example
  117. Automatically review PRs when they are opened or updated:
  118. ```yaml title=".github/workflows/opencode-review.yml"
  119. name: opencode-review
  120. on:
  121. pull_request:
  122. types: [opened, synchronize, reopened, ready_for_review]
  123. jobs:
  124. review:
  125. runs-on: ubuntu-latest
  126. permissions:
  127. id-token: write
  128. contents: read
  129. pull-requests: read
  130. issues: read
  131. steps:
  132. - uses: actions/checkout@v6
  133. with:
  134. persist-credentials: false
  135. - uses: anomalyco/opencode/github@latest
  136. env:
  137. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  138. with:
  139. model: anthropic/claude-sonnet-4-20250514
  140. prompt: |
  141. Review this pull request:
  142. - Check for code quality issues
  143. - Look for potential bugs
  144. - Suggest improvements
  145. ```
  146. For `pull_request` events, if no `prompt` is provided, OpenCode defaults to reviewing the pull request.
  147. ---
  148. ### Issues Triage Example
  149. Automatically triage new issues. This example filters to accounts older than 30 days to reduce spam:
  150. ```yaml title=".github/workflows/opencode-triage.yml"
  151. name: Issue Triage
  152. on:
  153. issues:
  154. types: [opened]
  155. jobs:
  156. triage:
  157. runs-on: ubuntu-latest
  158. permissions:
  159. id-token: write
  160. contents: write
  161. pull-requests: write
  162. issues: write
  163. steps:
  164. - name: Check account age
  165. id: check
  166. uses: actions/github-script@v7
  167. with:
  168. script: |
  169. const user = await github.rest.users.getByUsername({
  170. username: context.payload.issue.user.login
  171. });
  172. const created = new Date(user.data.created_at);
  173. const days = (Date.now() - created) / (1000 * 60 * 60 * 24);
  174. return days >= 30;
  175. result-encoding: string
  176. - uses: actions/checkout@v6
  177. if: steps.check.outputs.result == 'true'
  178. with:
  179. persist-credentials: false
  180. - uses: anomalyco/opencode/github@latest
  181. if: steps.check.outputs.result == 'true'
  182. env:
  183. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  184. with:
  185. model: anthropic/claude-sonnet-4-20250514
  186. prompt: |
  187. Review this issue. If there's a clear fix or relevant docs:
  188. - Provide documentation links
  189. - Add error handling guidance for code examples
  190. Otherwise, do not comment.
  191. ```
  192. For `issues` events, the `prompt` input is **required** since there's no comment to extract instructions from.
  193. ---
  194. ## Custom prompts
  195. Override the default prompt to customize OpenCode's behavior for your workflow.
  196. ```yaml title=".github/workflows/opencode.yml"
  197. - uses: anomalyco/opencode/github@latest
  198. with:
  199. model: anthropic/claude-sonnet-4-5
  200. prompt: |
  201. Review this pull request:
  202. - Check for code quality issues
  203. - Look for potential bugs
  204. - Suggest improvements
  205. ```
  206. This is useful for enforcing specific review criteria, coding standards, or focus areas relevant to your project.
  207. ---
  208. ## Examples
  209. Here are some examples of how you can use OpenCode in GitHub.
  210. - **Explain an issue**
  211. Add this comment in a GitHub issue.
  212. ```
  213. /opencode explain this issue
  214. ```
  215. OpenCode will read the entire thread, including all comments, and reply with a clear explanation.
  216. - **Fix an issue**
  217. In a GitHub issue, say:
  218. ```
  219. /opencode fix this
  220. ```
  221. And OpenCode will create a new branch, implement the changes, and open a PR with the changes.
  222. - **Review PRs and make changes**
  223. Leave the following comment on a GitHub PR.
  224. ```
  225. Delete the attachment from S3 when the note is removed /oc
  226. ```
  227. OpenCode will implement the requested change and commit it to the same PR.
  228. - **Review specific code lines**
  229. 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.
  230. ```
  231. [Comment on specific lines in Files tab]
  232. /oc add error handling here
  233. ```
  234. When commenting on specific lines, OpenCode receives:
  235. - The exact file being reviewed
  236. - The specific lines of code
  237. - The surrounding diff context
  238. - Line number information
  239. This allows for more targeted requests without needing to specify file paths or line numbers manually.