From c8de42392f0ec742998db7b0d9c0b48511640850 Mon Sep 17 00:00:00 2001 From: maria Date: Sun, 5 Jul 2026 16:13:10 +0000 Subject: [PATCH] Update .gitea/workflows/opencode.yml --- .gitea/workflows/opencode.yml | 193 +++++++++++++++++++++++++--------- 1 file changed, 146 insertions(+), 47 deletions(-) diff --git a/.gitea/workflows/opencode.yml b/.gitea/workflows/opencode.yml index bc5e25a..5fa580a 100644 --- a/.gitea/workflows/opencode.yml +++ b/.gitea/workflows/opencode.yml @@ -3,75 +3,174 @@ name: OpenCode Gitea Integration on: issue_comment: types: [created] - dispatch: - types: [workflow_dispatch] + pull_request_review_comment: + types: [created] + workflow_dispatch: + inputs: + prompt: + description: "Istruzione manuale per OpenCode" + required: true jobs: opencode-gitea: if: | - contains(github.event.comment.body, '/oc') || + github.event_name == 'workflow_dispatch' || + contains(github.event.comment.body, '/oc') || contains(github.event.comment.body, '/opencode') runs-on: ubuntu-latest timeout-minutes: 20 - + + env: + GITEA_API_URL: "https://maribit.it" + GITEA_TOKEN: ${{ secrets.BOT_GITEA_TOKEN }} + REPO_FULL_NAME: ${{ github.repository }} + COMMENT_BODY: ${{ github.event.comment.body || github.event.inputs.prompt }} + COMMENT_AUTHOR: ${{ github.event.comment.user.login }} + IS_PR_EVENT: ${{ github.event_name == 'pull_request_review_comment' }} + PR_NUMBER_FROM_EVENT: ${{ github.event.pull_request.number }} + PR_HEAD_REF_FROM_EVENT: ${{ github.event.pull_request.head.ref }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + ISSUE_TITLE: ${{ github.event.issue.title }} + ISSUE_BODY: ${{ github.event.issue.body }} + steps: + - name: Verifica autorizzazione utente + run: | + # Whitelist esplicita: nessuna esecuzione automatica per utenti non fidati. + #ALLOWED_USERS="nicola-brecciaroli tuo-secondo-maintainer" + ALLOWED_USERS="maria nicola" + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then + exit 0 + fi + if ! echo " $ALLOWED_USERS " | grep -q " $COMMENT_AUTHOR "; then + echo "Utente '$COMMENT_AUTHOR' non autorizzato a invocare OpenCode. Interrompo." + exit 1 + fi + - name: Checkout repository uses: actions/checkout@v4 with: - fetch-depth: 0 # Cambiato a 0 per permettere all'AI di vedere la cronologia dei branch se serve + fetch-depth: 0 - name: Install OpenCode CLI run: | - curl -fsSL https://opencode.ai | bash - echo "$HOME/.opencode/bin" >> $GITHUB_PATH + curl -fsSL https://opencode.ai/install | bash + echo "$HOME/.opencode/bin" >> "$GITHUB_PATH" - - name: Execute OpenCode (Big Pickle) + - name: Determina contesto (issue vs PR esistente) e branch di lavoro + id: ctx + run: | + set -e + git config --global user.name "OpenCode Bot" + git config --global user.email "opencode-bot@maribit.it" + + API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME" + + if [ "$IS_PR_EVENT" = "true" ]; then + # Commento inline su una review: la PR e il suo branch sono già noti dal payload. + PR_NUMBER="$PR_NUMBER_FROM_EVENT" + BRANCH_NAME="$PR_HEAD_REF_FROM_EVENT" + IS_PR=true + else + # issue_comment: può essere una issue "pura" o un commento generale su una PR. + ISSUE_JSON=$(curl -s -H "Authorization: token $GITEA_TOKEN" "$API/issues/$ISSUE_NUMBER") + if echo "$ISSUE_JSON" | grep -q '"pull_request"'; then + IS_PR=true + PR_NUMBER="$ISSUE_NUMBER" + PR_JSON=$(curl -s -H "Authorization: token $GITEA_TOKEN" "$API/pulls/$PR_NUMBER") + BRANCH_NAME=$(echo "$PR_JSON" | jq -r '.head.ref') + else + IS_PR=false + PR_NUMBER="" + BRANCH_NAME="opencode-issue-$ISSUE_NUMBER" + fi + fi + + echo "is_pr=$IS_PR" >> "$GITHUB_OUTPUT" + echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT" + echo "branch_name=$BRANCH_NAME" >> "$GITHUB_OUTPUT" + + if [ "$IS_PR" = "true" ]; then + # Continua a lavorare sullo STESSO branch della PR esistente (loop di review). + git fetch origin "$BRANCH_NAME" + git checkout -B "$BRANCH_NAME" "origin/$BRANCH_NAME" + elif git ls-remote --exit-code --heads origin "$BRANCH_NAME" >/dev/null 2>&1; then + # Un run precedente ha già creato questo branch per la stessa issue: riprendilo. + git fetch origin "$BRANCH_NAME" + git checkout -B "$BRANCH_NAME" "origin/$BRANCH_NAME" + else + git checkout -b "$BRANCH_NAME" + fi + + - name: Raccogli contesto aggiuntivo (diff + commenti di review, se PR) + id: extra + run: | + API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME" + : > /tmp/context.txt + if [ "${{ steps.ctx.outputs.is_pr }}" = "true" ]; then + PR_NUMBER="${{ steps.ctx.outputs.pr_number }}" + echo "== Diff della PR ==" >> /tmp/context.txt + curl -s -H "Authorization: token $GITEA_TOKEN" "$API/pulls/$PR_NUMBER.diff" >> /tmp/context.txt + echo "== Commenti di review recenti ==" >> /tmp/context.txt + curl -s -H "Authorization: token $GITEA_TOKEN" "$API/issues/$PR_NUMBER/comments" \ + | jq -r '.[] | "- (\(.user.login)): \(.body)"' >> /tmp/context.txt + fi + + - name: Pulisci il comando dal trigger + id: prompt + run: | + CLEAN_PROMPT=$(echo "$COMMENT_BODY" | sed -e 's|/oc||g' -e 's|/opencode||g') + { + echo "clean<<'EOF'" + echo "$CLEAN_PROMPT" + echo "EOF" + } >> "$GITHUB_OUTPUT" + + - name: Esegui OpenCode env: - # OpenCode Zen Configuration OPENCODE_ZEN_API_KEY: ${{ secrets.OPENCODE_ZEN_API_KEY }} OPENCODE_API_BASE: https://opencode.ai OPENCODE_MODEL: opencode/big-pickle - - # Parametri Gitea passati in modo esplicito - GITEA_TOKEN: ${{ secrets.BOT_GITEA_TOKEN }} - GITEA_API_URL: "https://maribit.it" - - # Contesto dell'evento estratti da Gitea - ISSUE_TITLE: ${{ github.event.issue.title }} - ISSUE_BODY: ${{ github.event.issue.body }} - COMMENT_BODY: ${{ github.event.comment.body }} - ISSUE_NUMBER: ${{ github.event.issue.number }} - REPO_FULL_NAME: ${{ github.repository }} + CLEAN_PROMPT: ${{ steps.prompt.outputs.clean }} run: | - # 1. Configurazione Git locale per permettere l'operatività sui branch - git config --global user.name "OpenCode Bot" - git config --global user.email "opencode-bot@maribit.it" - - # 2. Creiamo un branch dedicato per la risoluzione della issue - BRANCH_NAME="opencode-fix-issue-$ISSUE_NUMBER" - git checkout -b "$BRANCH_NAME" - - # 3. Puliamo il comando dal token di attivazione (/oc) per dare all'AI solo le istruzioni pulite - CLEAN_PROMPT=$(echo "$COMMENT_BODY" | sed 's|/oc||g' | sed 's|/opencode||g') - - # 4. Eseguiamo l'agente OpenCode in modalità workspace locale sul codice scaricato - # L'AI analizzerà i file locali e applicherà i fix in base alla richiesta + CONTEXT=$(cat /tmp/context.txt) opencode run \ --model "$OPENCODE_MODEL" \ - --prompt "Risolvi il seguente problema nel codice. Contesto della Issue: $ISSUE_TITLE - $ISSUE_BODY. Richiesta specifica dell'utente: $CLEAN_PROMPT" - - # 5. Se l'agente ha modificato dei file, facciamo il push e apriamo la PR su Gitea via API - if [ -n "$(git status --porcelain)" ]; then - git add . - git commit -m "Fix automatico OpenCode (Big Pickle) per issue #$ISSUE_NUMBER" - git push origin "$BRANCH_NAME" - - # Chiamata API nativa a Gitea per creare la Pull Request - curl -X 'POST' \ - "$GITEA_API_URL/repos/$REPO_FULL_NAME/pulls" \ + "Contesto della Issue/PR: $ISSUE_TITLE - $ISSUE_BODY + + $CONTEXT + + Richiesta specifica dell'utente ($COMMENT_AUTHOR): $CLEAN_PROMPT + + Se stai lavorando su una PR già aperta, applica solo le modifiche richieste dal feedback di review, senza riscrivere lavoro già approvato." + + - name: Pusha modifiche e apri/aggiorna la PR + run: | + API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME" + BRANCH_NAME="${{ steps.ctx.outputs.branch_name }}" + + if [ -z "$(git status --porcelain)" ]; then + echo "Nessuna modifica rilevata da OpenCode." + exit 0 + fi + + git add . + git commit -m "OpenCode (big-pickle): risposta a feedback su #${ISSUE_NUMBER:-${{ steps.ctx.outputs.pr_number }}}" + git push origin "$BRANCH_NAME" + + if [ "${{ steps.ctx.outputs.is_pr }}" != "true" ]; then + curl -s -X POST "$API/pulls" \ -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ - -d "{\"base\":\"main\",\"head\":\"$BRANCH_NAME\",\"title\":\"PR Automatica OpenCode: Issue #$ISSUE_NUMBER\",\"body\":\"Risoluzione automatica generata dal modello big-pickle a fronte del commento: $CLEAN_PROMPT\"}" - else - echo "Nessuna modifica rilevata dall'agente OpenCode." + -d "{\"base\":\"main\",\"head\":\"$BRANCH_NAME\",\"title\":\"PR Automatica OpenCode: Issue #$ISSUE_NUMBER\",\"body\":\"Risoluzione automatica generata da big-pickle a fronte del commento di @$COMMENT_AUTHOR.\"}" fi + + - name: Commenta l'esito + if: always() + run: | + API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME" + TARGET="${{ steps.ctx.outputs.pr_number || env.ISSUE_NUMBER }}" + curl -s -X POST "$API/issues/$TARGET/comments" \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{\"body\":\"🤖 OpenCode ha completato l'elaborazione della richiesta di @$COMMENT_AUTHOR.\"}" \ No newline at end of file