github.mdx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. ---
  2. title: GitHub
  3. description: Usa OpenCode in issue e pull request su GitHub.
  4. ---
  5. OpenCode si integra nel tuo workflow GitHub. Menziona `/opencode` o `/oc` in un commento e OpenCode eseguira' i task dentro il runner di GitHub Actions.
  6. ---
  7. ## Funzionalità
  8. - **Triage delle issue**: chiedi a OpenCode di indagare su una issue e spiegartela.
  9. - **Fix e implementazioni**: chiedi a OpenCode di risolvere una issue o implementare una feature. Lavorera' su un nuovo branch e inviera' una PR con tutte le modifiche.
  10. - **Sicuro**: OpenCode gira all'interno dei runner GitHub.
  11. ---
  12. ## Installazione
  13. Esegui il comando seguente in un progetto che si trova in un repo GitHub:
  14. ```bash
  15. opencode github install
  16. ```
  17. Questo ti guidera' nell'installazione della GitHub app, nella creazione del workflow e nella configurazione dei secret.
  18. ---
  19. ### Configurazione manuale
  20. In alternativa, puoi configurarlo manualmente.
  21. 1. **Installa la GitHub app**
  22. Vai su [**github.com/apps/opencode-agent**](https://github.com/apps/opencode-agent). Assicurati che sia installata sul repository di destinazione.
  23. 2. **Aggiungi il workflow**
  24. Aggiungi il seguente file workflow in `.github/workflows/opencode.yml` nel tuo repo. Assicurati di impostare il `model` appropriato e le API key richieste 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. **Salva le API key nei secret**
  56. Nelle **impostazioni** della tua organizzazione o progetto, espandi **Secrets and variables** sulla sinistra e seleziona **Actions**. Poi aggiungi le API key richieste.
  57. ---
  58. ## Configurazione
  59. - `model`: il modello da usare con OpenCode. Usa il formato `provider/model`. E' **obbligatorio**.
  60. - `agent`: l'agente da usare. Deve essere un agente primario. Se non trovato, usa `default_agent` dalla config o `"build"`.
  61. - `share`: se condividere la sessione OpenCode. Di default e' **true** per repository pubblici.
  62. - `prompt`: prompt personalizzato opzionale per sovrascrivere il comportamento di default. Usalo per personalizzare come OpenCode processa le richieste.
  63. - `token`: token di accesso GitHub opzionale per eseguire operazioni come creare commenti, committare modifiche e aprire pull request. Di default, OpenCode usa l'installation access token della OpenCode GitHub App, quindi commit, commenti e pull request risultano provenire dalla app.
  64. In alternativa, puoi usare il [token integrato `GITHUB_TOKEN`](https://docs.github.com/en/actions/tutorials/authenticate-with-github_token) del runner GitHub Actions senza installare la OpenCode GitHub App. Assicurati solo di concedere i permessi necessari nel workflow:
  65. ```yaml
  66. permissions:
  67. id-token: write
  68. contents: write
  69. pull-requests: write
  70. issues: write
  71. ```
  72. Se preferisci, puoi anche usare un [personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)(PAT).
  73. ---
  74. ## Eventi supportati
  75. OpenCode puo' essere attivato dai seguenti eventi GitHub:
  76. | Tipo evento | Attivato da | Dettagli |
  77. | ----------------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
  78. | `issue_comment` | Commento su una issue o PR | Menziona `/opencode` o `/oc` nel commento. OpenCode legge il contesto e puo' creare branch, aprire PR o rispondere. |
  79. | `pull_request_review_comment` | Commento su specifiche righe in una PR | Menziona `/opencode` o `/oc` durante una review. OpenCode riceve path file, numeri di riga e contesto del diff. |
  80. | `issues` | Issue aperta o modificata | Attiva automaticamente OpenCode quando le issue vengono create o modificate. Richiede l'input `prompt`. |
  81. | `pull_request` | PR aperta o aggiornata | Attiva automaticamente OpenCode quando le PR vengono aperte, sincronizzate o riaperte. Utile per review automatiche. |
  82. | `schedule` | Pianificazione basata su cron | Esegue OpenCode a pianificazione. Richiede l'input `prompt`. Output nei log e nelle PR (nessuna issue su cui commentare). |
  83. | `workflow_dispatch` | Trigger manuale dalla UI GitHub | Attiva OpenCode on-demand dalla tab Actions. Richiede l'input `prompt`. Output nei log e nelle PR. |
  84. ### Esempio con schedule
  85. Esegui OpenCode a pianificazione per eseguire task automatizzati:
  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. Per gli eventi schedulati, l'input `prompt` e' **obbligatorio** dato che non c'e' un commento da cui estrarre le istruzioni. I workflow schedulati girano senza un contesto utente per i controlli di permesso, quindi il workflow deve concedere `contents: write` e `pull-requests: write` se ti aspetti che OpenCode crei branch o PR.
  115. ---
  116. ### Esempio di pull request
  117. Review automatica delle PR quando vengono aperte o aggiornate:
  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. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  139. with:
  140. model: anthropic/claude-sonnet-4-20250514
  141. use_github_token: true
  142. prompt: |
  143. Review this pull request:
  144. - Check for code quality issues
  145. - Look for potential bugs
  146. - Suggest improvements
  147. ```
  148. Per gli eventi `pull_request`, se non viene fornito alcun `prompt`, OpenCode fa di default la review della pull request.
  149. ---
  150. ### Esempio di triage delle issue
  151. Triage automatico delle nuove issue. Questo esempio filtra gli account piu' vecchi di 30 giorni per ridurre lo spam:
  152. ```yaml title=".github/workflows/opencode-triage.yml"
  153. name: Issue Triage
  154. on:
  155. issues:
  156. types: [opened]
  157. jobs:
  158. triage:
  159. runs-on: ubuntu-latest
  160. permissions:
  161. id-token: write
  162. contents: write
  163. pull-requests: write
  164. issues: write
  165. steps:
  166. - name: Check account age
  167. id: check
  168. uses: actions/github-script@v7
  169. with:
  170. script: |
  171. const user = await github.rest.users.getByUsername({
  172. username: context.payload.issue.user.login
  173. });
  174. const created = new Date(user.data.created_at);
  175. const days = (Date.now() - created) / (1000 * 60 * 60 * 24);
  176. return days >= 30;
  177. result-encoding: string
  178. - uses: actions/checkout@v6
  179. if: steps.check.outputs.result == 'true'
  180. with:
  181. persist-credentials: false
  182. - uses: anomalyco/opencode/github@latest
  183. if: steps.check.outputs.result == 'true'
  184. env:
  185. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  186. with:
  187. model: anthropic/claude-sonnet-4-20250514
  188. prompt: |
  189. Review this issue. If there's a clear fix or relevant docs:
  190. - Provide documentation links
  191. - Add error handling guidance for code examples
  192. Otherwise, do not comment.
  193. ```
  194. Per gli eventi `issues`, l'input `prompt` e' **obbligatorio** dato che non c'e' un commento da cui estrarre le istruzioni.
  195. ---
  196. ## Prompt personalizzati
  197. Sovrascrivi il prompt di default per personalizzare il comportamento di OpenCode nel tuo workflow.
  198. ```yaml title=".github/workflows/opencode.yml"
  199. - uses: anomalyco/opencode/github@latest
  200. with:
  201. model: anthropic/claude-sonnet-4-5
  202. prompt: |
  203. Review this pull request:
  204. - Check for code quality issues
  205. - Look for potential bugs
  206. - Suggest improvements
  207. ```
  208. E' utile per imporre criteri specifici di review, standard di codice o aree di focus rilevanti per il progetto.
  209. ---
  210. ## Esempi
  211. Ecco alcuni esempi di come puoi usare OpenCode su GitHub.
  212. - **Spiega una issue**
  213. Aggiungi questo commento in una issue GitHub.
  214. ```
  215. /opencode explain this issue
  216. ```
  217. OpenCode leggera' l'intero thread, inclusi tutti i commenti, e rispondera' con una spiegazione chiara.
  218. - **Risolvi una issue**
  219. In una issue GitHub, scrivi:
  220. ```
  221. /opencode fix this
  222. ```
  223. OpenCode creera' un nuovo branch, implementera' le modifiche e aprira' una PR con i cambiamenti.
  224. - **Rivedi PR e fai modifiche**
  225. Lascia il seguente commento su una PR GitHub.
  226. ```
  227. Delete the attachment from S3 when the note is removed /oc
  228. ```
  229. OpenCode implementera' la modifica richiesta e la committera' nella stessa PR.
  230. - **Rivedi righe specifiche di codice**
  231. Lascia un commento direttamente sulle righe di codice nella tab "Files" della PR. OpenCode rileva automaticamente file, numeri di riga e contesto del diff per fornire risposte precise.
  232. ```
  233. [Comment on specific lines in Files tab]
  234. /oc add error handling here
  235. ```
  236. Quando commenti su righe specifiche, OpenCode riceve:
  237. - Il file esatto in review
  238. - Le righe di codice specifiche
  239. - Il contesto del diff circostante
  240. - Informazioni sul numero di riga
  241. Questo permette richieste piu' mirate senza dover specificare manualmente path file o numeri di riga.