213 lines
8.5 KiB
YAML
213 lines
8.5 KiB
YAML
name: OpenCode Gitea Integration
|
|
|
|
on:
|
|
issues:
|
|
types: [opened]
|
|
issue_comment:
|
|
types: [created]
|
|
pull_request_review_comment:
|
|
types: [created]
|
|
workflow_dispatch:
|
|
inputs:
|
|
prompt:
|
|
description: "Istruzione manuale per OpenCode"
|
|
required: true
|
|
|
|
jobs:
|
|
opencode-gitea:
|
|
if: |
|
|
github.event_name == 'workflow_dispatch' ||
|
|
(github.event_name == 'issues' &&
|
|
(contains(github.event.issue.body, '/oc') || contains(github.event.issue.body, '/opencode'))) ||
|
|
(github.event_name != 'issues' &&
|
|
(contains(github.event.comment.body, '/oc') || contains(github.event.comment.body, '/opencode')))
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 20
|
|
|
|
env:
|
|
GITEA_API_URL: "https://gitea.maribit.it"
|
|
GITEA_TOKEN: ${{ secrets.BOT_GITEA_TOKEN }}
|
|
REPO_FULL_NAME: ${{ github.repository }}
|
|
COMMENT_BODY: ${{ github.event.comment.body || github.event.issue.body || github.event.inputs.prompt }}
|
|
COMMENT_AUTHOR: ${{ github.event.comment.user.login || github.event.issue.user.login || github.actor }}
|
|
COMMENT_ID: ${{ github.event.comment.id }}
|
|
IS_PR_EVENT: ${{ github.event_name == 'pull_request_review_comment' }}
|
|
IS_MANUAL: ${{ github.event_name == 'workflow_dispatch' }}
|
|
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 }}
|
|
RUN_NUMBER: ${{ github.run_number }}
|
|
|
|
steps:
|
|
- name: Verifica autorizzazione utente
|
|
run: |
|
|
ALLOWED_USERS="maria nicola"
|
|
if [ "$IS_MANUAL" = "true" ]; 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
|
|
|
|
# Feedback immediato: 👀 sul commento (o sulla issue) che ha invocato l'agente.
|
|
# Le reazioni NON generano eventi issue_comment -> nessun doppio run.
|
|
- name: Reagisci al comando (👀)
|
|
id: react
|
|
run: |
|
|
API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME"
|
|
if [ -n "$COMMENT_ID" ]; then
|
|
RPATH="issues/comments/$COMMENT_ID/reactions"
|
|
elif [ -n "$ISSUE_NUMBER" ]; then
|
|
RPATH="issues/$ISSUE_NUMBER/reactions"
|
|
else
|
|
RPATH=""
|
|
fi
|
|
echo "path=$RPATH" >> "$GITHUB_OUTPUT"
|
|
if [ -n "$RPATH" ]; then
|
|
curl -s -X POST "$API/$RPATH" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"content":"eyes"}' >/dev/null || true
|
|
fi
|
|
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Install OpenCode CLI
|
|
run: |
|
|
curl -fsSL https://opencode.ai/install | bash
|
|
echo "$HOME/.opencode/bin" >> "$GITHUB_PATH"
|
|
|
|
- name: Determina contesto (issue / PR / manuale) 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_MANUAL" = "true" ]; then
|
|
IS_PR=false
|
|
PR_NUMBER=""
|
|
BRANCH_NAME="opencode-manual-$RUN_NUMBER"
|
|
elif [ "$IS_PR_EVENT" = "true" ]; then
|
|
IS_PR=true
|
|
PR_NUMBER="$PR_NUMBER_FROM_EVENT"
|
|
BRANCH_NAME="$PR_HEAD_REF_FROM_EVENT"
|
|
else
|
|
# issue / issue_comment: la chiave "pull_request" c'è sempre ma vale null per le issue.
|
|
ISSUE_JSON=$(curl -s -H "Authorization: token $GITEA_TOKEN" "$API/issues/$ISSUE_NUMBER")
|
|
if [ "$(echo "$ISSUE_JSON" | jq -r '.pull_request // empty')" != "" ]; 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
|
|
|
|
if [ -z "$BRANCH_NAME" ] || [ "$BRANCH_NAME" = "null" ]; then
|
|
echo "BRANCH_NAME non valido ('$BRANCH_NAME'). Interrompo."
|
|
exit 1
|
|
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
|
|
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
|
|
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, se PR)
|
|
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 recenti ==" >> /tmp/context.txt
|
|
curl -s -H "Authorization: token $GITEA_TOKEN" "$API/issues/$PR_NUMBER/comments" \
|
|
| jq -r '.[] | "- (\(.user.login)): \(.body)"' >> /tmp/context.txt || true
|
|
fi
|
|
|
|
- name: Pulisci il comando dal trigger
|
|
run: |
|
|
CLEAN_PROMPT=$(printf '%s' "$COMMENT_BODY" | sed -e 's|/opencode||g' -e 's|/oc||g')
|
|
printf '%s' "$CLEAN_PROMPT" > /tmp/clean_prompt.txt
|
|
|
|
- name: Esegui OpenCode
|
|
env:
|
|
OPENCODE_ZEN_API_KEY: ${{ secrets.OPENCODE_ZEN_API_KEY }}
|
|
OPENCODE_API_BASE: https://opencode.ai
|
|
OPENCODE_MODEL: opencode/big-pickle
|
|
run: |
|
|
CLEAN_PROMPT=$(cat /tmp/clean_prompt.txt)
|
|
CONTEXT=$(cat /tmp/context.txt 2>/dev/null || true)
|
|
opencode run \
|
|
--model "$OPENCODE_MODEL" \
|
|
"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): $BRANCH_NAME"
|
|
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: $BRANCH_NAME\",\"body\":\"Generata da big-pickle su richiesta di @$COMMENT_AUTHOR.\"}"
|
|
fi
|
|
|
|
# Stato finale via reazione (niente commenti -> niente self-trigger).
|
|
- name: Reazione finale 🚀
|
|
if: success() && steps.react.outputs.path != ''
|
|
continue-on-error: true
|
|
run: |
|
|
API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME"
|
|
curl -s -X POST "$API/${{ steps.react.outputs.path }}" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"content":"rocket"}' >/dev/null || true
|
|
|
|
- name: Reazione finale 😕 (fallimento)
|
|
if: failure() && steps.react.outputs.path != ''
|
|
continue-on-error: true
|
|
run: |
|
|
API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME"
|
|
curl -s -X POST "$API/${{ steps.react.outputs.path }}" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"content":"confused"}' >/dev/null || true |