Update .gitea/workflows/opencode.yml
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 3s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 3s
This commit is contained in:
@@ -25,21 +25,24 @@ jobs:
|
|||||||
GITEA_TOKEN: ${{ secrets.BOT_GITEA_TOKEN }}
|
GITEA_TOKEN: ${{ secrets.BOT_GITEA_TOKEN }}
|
||||||
REPO_FULL_NAME: ${{ github.repository }}
|
REPO_FULL_NAME: ${{ github.repository }}
|
||||||
COMMENT_BODY: ${{ github.event.comment.body || github.event.inputs.prompt }}
|
COMMENT_BODY: ${{ github.event.comment.body || github.event.inputs.prompt }}
|
||||||
COMMENT_AUTHOR: ${{ github.event.comment.user.login }}
|
COMMENT_AUTHOR: ${{ github.event.comment.user.login || github.actor }}
|
||||||
IS_PR_EVENT: ${{ github.event_name == 'pull_request_review_comment' }}
|
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_NUMBER_FROM_EVENT: ${{ github.event.pull_request.number }}
|
||||||
PR_HEAD_REF_FROM_EVENT: ${{ github.event.pull_request.head.ref }}
|
PR_HEAD_REF_FROM_EVENT: ${{ github.event.pull_request.head.ref }}
|
||||||
ISSUE_NUMBER: ${{ github.event.issue.number }}
|
ISSUE_NUMBER: ${{ github.event.issue.number }}
|
||||||
ISSUE_TITLE: ${{ github.event.issue.title }}
|
ISSUE_TITLE: ${{ github.event.issue.title }}
|
||||||
ISSUE_BODY: ${{ github.event.issue.body }}
|
ISSUE_BODY: ${{ github.event.issue.body }}
|
||||||
|
RUN_NUMBER: ${{ github.run_number }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Verifica autorizzazione utente
|
- name: Verifica autorizzazione utente
|
||||||
run: |
|
run: |
|
||||||
# Whitelist esplicita: nessuna esecuzione automatica per utenti non fidati.
|
# Whitelist esplicita: nessuna esecuzione automatica per utenti non fidati.
|
||||||
#ALLOWED_USERS="nicola-brecciaroli tuo-secondo-maintainer"
|
|
||||||
ALLOWED_USERS="maria nicola"
|
ALLOWED_USERS="maria nicola"
|
||||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
# L'esecuzione manuale (workflow_dispatch) è già ristretta a chi ha
|
||||||
|
# permessi di scrittura sul repo, quindi la lasciamo passare.
|
||||||
|
if [ "$IS_MANUAL" = "true" ]; then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
if ! echo " $ALLOWED_USERS " | grep -q " $COMMENT_AUTHOR "; then
|
if ! echo " $ALLOWED_USERS " | grep -q " $COMMENT_AUTHOR "; then
|
||||||
@@ -57,7 +60,7 @@ jobs:
|
|||||||
curl -fsSL https://opencode.ai/install | bash
|
curl -fsSL https://opencode.ai/install | bash
|
||||||
echo "$HOME/.opencode/bin" >> "$GITHUB_PATH"
|
echo "$HOME/.opencode/bin" >> "$GITHUB_PATH"
|
||||||
|
|
||||||
- name: Determina contesto (issue vs PR esistente) e branch di lavoro
|
- name: Determina contesto (issue / PR / manuale) e branch di lavoro
|
||||||
id: ctx
|
id: ctx
|
||||||
run: |
|
run: |
|
||||||
set -e
|
set -e
|
||||||
@@ -66,15 +69,22 @@ jobs:
|
|||||||
|
|
||||||
API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME"
|
API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME"
|
||||||
|
|
||||||
if [ "$IS_PR_EVENT" = "true" ]; then
|
if [ "$IS_MANUAL" = "true" ]; then
|
||||||
# Commento inline su una review: la PR e il suo branch sono già noti dal payload.
|
# Esecuzione manuale: nessuna issue/PR di contesto, branch dedicato.
|
||||||
|
IS_PR=false
|
||||||
|
PR_NUMBER=""
|
||||||
|
BRANCH_NAME="opencode-manual-$RUN_NUMBER"
|
||||||
|
elif [ "$IS_PR_EVENT" = "true" ]; then
|
||||||
|
# Commento inline su una review: PR e branch noti dal payload.
|
||||||
|
IS_PR=true
|
||||||
PR_NUMBER="$PR_NUMBER_FROM_EVENT"
|
PR_NUMBER="$PR_NUMBER_FROM_EVENT"
|
||||||
BRANCH_NAME="$PR_HEAD_REF_FROM_EVENT"
|
BRANCH_NAME="$PR_HEAD_REF_FROM_EVENT"
|
||||||
IS_PR=true
|
|
||||||
else
|
else
|
||||||
# issue_comment: può essere una issue "pura" o un commento generale su una PR.
|
# issue_comment: può essere una issue "pura" o un commento su una PR.
|
||||||
ISSUE_JSON=$(curl -s -H "Authorization: token $GITEA_TOKEN" "$API/issues/$ISSUE_NUMBER")
|
ISSUE_JSON=$(curl -s -H "Authorization: token $GITEA_TOKEN" "$API/issues/$ISSUE_NUMBER")
|
||||||
if echo "$ISSUE_JSON" | grep -q '"pull_request"'; then
|
# NB: l'API Gitea restituisce SEMPRE la chiave "pull_request",
|
||||||
|
# ma vale null per le issue normali: va controllato il VALORE, non la chiave.
|
||||||
|
if [ "$(echo "$ISSUE_JSON" | jq -r '.pull_request // empty')" != "" ]; then
|
||||||
IS_PR=true
|
IS_PR=true
|
||||||
PR_NUMBER="$ISSUE_NUMBER"
|
PR_NUMBER="$ISSUE_NUMBER"
|
||||||
PR_JSON=$(curl -s -H "Authorization: token $GITEA_TOKEN" "$API/pulls/$PR_NUMBER")
|
PR_JSON=$(curl -s -H "Authorization: token $GITEA_TOKEN" "$API/pulls/$PR_NUMBER")
|
||||||
@@ -86,16 +96,22 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Rete di sicurezza contro branch vuoti / "null".
|
||||||
|
if [ -z "$BRANCH_NAME" ] || [ "$BRANCH_NAME" = "null" ]; then
|
||||||
|
echo "BRANCH_NAME non valido ('$BRANCH_NAME'): impossibile determinare il contesto. Interrompo."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
echo "is_pr=$IS_PR" >> "$GITHUB_OUTPUT"
|
echo "is_pr=$IS_PR" >> "$GITHUB_OUTPUT"
|
||||||
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
|
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
|
||||||
echo "branch_name=$BRANCH_NAME" >> "$GITHUB_OUTPUT"
|
echo "branch_name=$BRANCH_NAME" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
if [ "$IS_PR" = "true" ]; then
|
if [ "$IS_PR" = "true" ]; then
|
||||||
# Continua a lavorare sullo STESSO branch della PR esistente (loop di review).
|
# Continua sullo STESSO branch della PR esistente (loop di review).
|
||||||
git fetch origin "$BRANCH_NAME"
|
git fetch origin "$BRANCH_NAME"
|
||||||
git checkout -B "$BRANCH_NAME" "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
|
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.
|
# Un run precedente ha già creato questo branch: riprendilo.
|
||||||
git fetch origin "$BRANCH_NAME"
|
git fetch origin "$BRANCH_NAME"
|
||||||
git checkout -B "$BRANCH_NAME" "origin/$BRANCH_NAME"
|
git checkout -B "$BRANCH_NAME" "origin/$BRANCH_NAME"
|
||||||
else
|
else
|
||||||
@@ -103,7 +119,6 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Raccogli contesto aggiuntivo (diff + commenti di review, se PR)
|
- name: Raccogli contesto aggiuntivo (diff + commenti di review, se PR)
|
||||||
id: extra
|
|
||||||
run: |
|
run: |
|
||||||
API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME"
|
API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME"
|
||||||
: > /tmp/context.txt
|
: > /tmp/context.txt
|
||||||
@@ -111,29 +126,24 @@ jobs:
|
|||||||
PR_NUMBER="${{ steps.ctx.outputs.pr_number }}"
|
PR_NUMBER="${{ steps.ctx.outputs.pr_number }}"
|
||||||
echo "== Diff della PR ==" >> /tmp/context.txt
|
echo "== Diff della PR ==" >> /tmp/context.txt
|
||||||
curl -s -H "Authorization: token $GITEA_TOKEN" "$API/pulls/$PR_NUMBER.diff" >> /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
|
echo "== Commenti recenti ==" >> /tmp/context.txt
|
||||||
curl -s -H "Authorization: token $GITEA_TOKEN" "$API/issues/$PR_NUMBER/comments" \
|
curl -s -H "Authorization: token $GITEA_TOKEN" "$API/issues/$PR_NUMBER/comments" \
|
||||||
| jq -r '.[] | "- (\(.user.login)): \(.body)"' >> /tmp/context.txt
|
| jq -r '.[] | "- (\(.user.login)): \(.body)"' >> /tmp/context.txt || true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Pulisci il comando dal trigger
|
- name: Pulisci il comando dal trigger
|
||||||
id: prompt
|
|
||||||
run: |
|
run: |
|
||||||
CLEAN_PROMPT=$(echo "$COMMENT_BODY" | sed -e 's|/oc||g' -e 's|/opencode||g')
|
CLEAN_PROMPT=$(printf '%s' "$COMMENT_BODY" | sed -e 's|/opencode||g' -e 's|/oc||g')
|
||||||
{
|
printf '%s' "$CLEAN_PROMPT" > /tmp/clean_prompt.txt
|
||||||
echo "clean<<'EOF'"
|
|
||||||
echo "$CLEAN_PROMPT"
|
|
||||||
echo "EOF"
|
|
||||||
} >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
- name: Esegui OpenCode
|
- name: Esegui OpenCode
|
||||||
env:
|
env:
|
||||||
OPENCODE_ZEN_API_KEY: ${{ secrets.OPENCODE_ZEN_API_KEY }}
|
OPENCODE_ZEN_API_KEY: ${{ secrets.OPENCODE_ZEN_API_KEY }}
|
||||||
OPENCODE_API_BASE: https://opencode.ai
|
OPENCODE_API_BASE: https://opencode.ai
|
||||||
OPENCODE_MODEL: opencode/big-pickle
|
OPENCODE_MODEL: opencode/big-pickle
|
||||||
CLEAN_PROMPT: ${{ steps.prompt.outputs.clean }}
|
|
||||||
run: |
|
run: |
|
||||||
CONTEXT=$(cat /tmp/context.txt)
|
CLEAN_PROMPT=$(cat /tmp/clean_prompt.txt)
|
||||||
|
CONTEXT=$(cat /tmp/context.txt 2>/dev/null || true)
|
||||||
opencode run \
|
opencode run \
|
||||||
--model "$OPENCODE_MODEL" \
|
--model "$OPENCODE_MODEL" \
|
||||||
"Contesto della Issue/PR: $ISSUE_TITLE - $ISSUE_BODY
|
"Contesto della Issue/PR: $ISSUE_TITLE - $ISSUE_BODY
|
||||||
@@ -155,22 +165,31 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
git add .
|
git add .
|
||||||
git commit -m "OpenCode (big-pickle): risposta a feedback su #${ISSUE_NUMBER:-${{ steps.ctx.outputs.pr_number }}}"
|
git commit -m "OpenCode (big-pickle): $BRANCH_NAME"
|
||||||
git push origin "$BRANCH_NAME"
|
git push origin "$BRANCH_NAME"
|
||||||
|
|
||||||
|
# Apri una PR solo se stiamo partendo da zero (issue o run manuale),
|
||||||
|
# non se stiamo già iterando su una PR esistente.
|
||||||
if [ "${{ steps.ctx.outputs.is_pr }}" != "true" ]; then
|
if [ "${{ steps.ctx.outputs.is_pr }}" != "true" ]; then
|
||||||
|
TITLE="PR Automatica OpenCode: $BRANCH_NAME"
|
||||||
curl -s -X POST "$API/pulls" \
|
curl -s -X POST "$API/pulls" \
|
||||||
-H "Authorization: token $GITEA_TOKEN" \
|
-H "Authorization: token $GITEA_TOKEN" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-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.\"}"
|
-d "{\"base\":\"main\",\"head\":\"$BRANCH_NAME\",\"title\":\"$TITLE\",\"body\":\"Generata da big-pickle su richiesta di @$COMMENT_AUTHOR.\"}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Commenta l'esito
|
- name: Commenta l'esito
|
||||||
if: always()
|
if: always()
|
||||||
|
continue-on-error: true
|
||||||
run: |
|
run: |
|
||||||
API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME"
|
API="$GITEA_API_URL/api/v1/repos/$REPO_FULL_NAME"
|
||||||
TARGET="${{ steps.ctx.outputs.pr_number || env.ISSUE_NUMBER }}"
|
TARGET="${{ steps.ctx.outputs.pr_number || env.ISSUE_NUMBER }}"
|
||||||
curl -s -X POST "$API/issues/$TARGET/comments" \
|
# Su esecuzione manuale non c'è una issue/PR di partenza su cui commentare.
|
||||||
|
if [ -z "$TARGET" ]; then
|
||||||
|
echo "Nessun target su cui commentare (probabile run manuale). Salto."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
curl -sS -X POST "$API/issues/$TARGET/comments" \
|
||||||
-H "Authorization: token $GITEA_TOKEN" \
|
-H "Authorization: token $GITEA_TOKEN" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "{\"body\":\"🤖 OpenCode ha completato l'elaborazione della richiesta di @$COMMENT_AUTHOR.\"}"
|
-d "{\"body\":\"🤖 OpenCode ha completato l'elaborazione della richiesta di @$COMMENT_AUTHOR.\"}"
|
||||||
Reference in New Issue
Block a user