Improve Gitea Actions monitoring hook
- Add detailed action information (workflow, duration, branch, etc.) - Detect and notify when Actions are disabled - Check for API permission errors - Add example settings.json for easy setup - Update README with hook configuration guide
This commit is contained in:
@@ -51,21 +51,39 @@ Deberías ver tu token.
|
|||||||
Una vez configurado, el hook se activa automáticamente cuando Claude Code ejecuta `git push`:
|
Una vez configurado, el hook se activa automáticamente cuando Claude Code ejecuta `git push`:
|
||||||
|
|
||||||
1. Claude ejecuta `git push`
|
1. Claude ejecuta `git push`
|
||||||
2. El hook se activa automáticamente
|
2. El hook verifica que Actions estén habilitadas
|
||||||
3. **Claude se congela** mientras espera a que termine la Gitea Action (máximo 10 minutos)
|
3. El hook se activa automáticamente y comienza a monitorear
|
||||||
4. Puedes presionar **Ctrl+C** para interrumpir la espera si es necesario
|
4. **Claude se congela** mientras espera a que termine la Gitea Action (máximo 10 minutos)
|
||||||
5. Cuando termine, Claude te muestra el resultado:
|
5. Puedes presionar **Ctrl+C** para interrumpir la espera si es necesario
|
||||||
|
6. Cuando termine, Claude te muestra información detallada:
|
||||||
|
|
||||||
|
**Ejemplo de action exitosa:**
|
||||||
```
|
```
|
||||||
✅ Gitea Action completada: EXITOSO
|
✅ Gitea Action completada: EXITOSO
|
||||||
|
|
||||||
📋 Detalles:
|
📋 Detalles:
|
||||||
• ID: 123
|
• Workflow: build-and-deploy (build-and-deploy.yml)
|
||||||
|
• Run #3
|
||||||
|
• Evento: push
|
||||||
|
• Branch: master
|
||||||
• Commit: a1b2c3d4
|
• Commit: a1b2c3d4
|
||||||
|
• Título: Update README: Add Claude Code hooks feature
|
||||||
|
• Duración: 5m 23s
|
||||||
• Iniciado: 2025-10-12T14:30:05Z
|
• Iniciado: 2025-10-12T14:30:05Z
|
||||||
• Finalizado: 2025-10-12T14:35:00Z
|
• Finalizado: 2025-10-12T14:35:28Z
|
||||||
|
|
||||||
🔗 Ver en Gitea: https://gitea.nucleoriofrio.com/nucleo000/plantillaNuxtAuthentikProxy/actions
|
🔗 Ver logs completos:
|
||||||
|
https://gitea.nucleoriofrio.com/nucleo000/plantillaNuxtAuthentikProxy/actions/runs/123
|
||||||
|
```
|
||||||
|
|
||||||
|
**Si las Actions no están habilitadas:**
|
||||||
|
```
|
||||||
|
⚠️ Git push exitoso, pero las Gitea Actions NO están habilitadas en este repositorio.
|
||||||
|
|
||||||
|
📝 Para habilitar Actions:
|
||||||
|
1. Ve a: https://gitea.nucleoriofrio.com/nucleo000/plantillaNuxtAuthentikProxy/settings
|
||||||
|
2. Busca la sección 'Actions' o 'Workflows'
|
||||||
|
3. Activa las Actions
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configuración Avanzada
|
## Configuración Avanzada
|
||||||
|
|||||||
@@ -41,11 +41,26 @@ EOF
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Función para verificar si las actions están habilitadas
|
||||||
|
check_actions_enabled() {
|
||||||
|
local repo_info=$(curl -s -H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
"$GITEA_URL/api/v1/repos/$OWNER/$REPO")
|
||||||
|
echo "$repo_info" | jq -r '.has_actions // false'
|
||||||
|
}
|
||||||
|
|
||||||
# Función para consultar el estado de la última action
|
# Función para consultar el estado de la última action
|
||||||
get_latest_action_status() {
|
get_latest_action_status() {
|
||||||
curl -s -H "Authorization: token $GITEA_TOKEN" \
|
local response=$(curl -s -H "Authorization: token $GITEA_TOKEN" \
|
||||||
"$GITEA_URL/api/v1/repos/$OWNER/$REPO/actions/tasks?limit=1" | \
|
"$GITEA_URL/api/v1/repos/$OWNER/$REPO/actions/tasks?limit=1")
|
||||||
jq -r '.workflow_runs[0] // .data[0] // empty'
|
|
||||||
|
# Verificar si hay un error de permisos
|
||||||
|
if echo "$response" | jq -e '.message' > /dev/null 2>&1; then
|
||||||
|
echo "ERROR: $(echo "$response" | jq -r '.message')" >&2
|
||||||
|
echo ""
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$response" | jq -r '.workflow_runs[0] // .data[0] // empty'
|
||||||
}
|
}
|
||||||
|
|
||||||
# Función para formatear el resultado
|
# Función para formatear el resultado
|
||||||
@@ -54,10 +69,34 @@ format_result() {
|
|||||||
local task_data="$2"
|
local task_data="$2"
|
||||||
|
|
||||||
local id=$(echo "$task_data" | jq -r '.id // "N/A"')
|
local id=$(echo "$task_data" | jq -r '.id // "N/A"')
|
||||||
|
local workflow_name=$(echo "$task_data" | jq -r '.name // "N/A"')
|
||||||
|
local workflow_file=$(echo "$task_data" | jq -r '.workflow_id // "N/A"')
|
||||||
|
local run_number=$(echo "$task_data" | jq -r '.run_number // "N/A"')
|
||||||
|
local event=$(echo "$task_data" | jq -r '.event // "N/A"')
|
||||||
|
local branch=$(echo "$task_data" | jq -r '.head_branch // "N/A"')
|
||||||
|
local title=$(echo "$task_data" | jq -r '.display_title // "N/A"')
|
||||||
local created=$(echo "$task_data" | jq -r '.created_at // "N/A"')
|
local created=$(echo "$task_data" | jq -r '.created_at // "N/A"')
|
||||||
local started=$(echo "$task_data" | jq -r '.started_at // "N/A"')
|
local started=$(echo "$task_data" | jq -r '.run_started_at // .started_at // "N/A"')
|
||||||
local stopped=$(echo "$task_data" | jq -r '.stopped_at // "N/A"')
|
local updated=$(echo "$task_data" | jq -r '.updated_at // .stopped_at // "N/A"')
|
||||||
local commit=$(echo "$task_data" | jq -r '.head_sha[0:8] // "N/A"')
|
local commit=$(echo "$task_data" | jq -r '.head_sha[0:8] // "N/A"')
|
||||||
|
local run_url=$(echo "$task_data" | jq -r '.url // ""')
|
||||||
|
|
||||||
|
# Calcular duración si es posible
|
||||||
|
local duration="N/A"
|
||||||
|
if [[ "$started" != "N/A" && "$updated" != "N/A" ]]; then
|
||||||
|
local start_ts=$(date -d "$started" +%s 2>/dev/null || echo "0")
|
||||||
|
local end_ts=$(date -d "$updated" +%s 2>/dev/null || echo "0")
|
||||||
|
if [[ $start_ts -gt 0 && $end_ts -gt 0 ]]; then
|
||||||
|
local diff=$((end_ts - start_ts))
|
||||||
|
if [[ $diff -lt 60 ]]; then
|
||||||
|
duration="${diff}s"
|
||||||
|
else
|
||||||
|
local mins=$((diff / 60))
|
||||||
|
local secs=$((diff % 60))
|
||||||
|
duration="${mins}m ${secs}s"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
case "$status" in
|
case "$status" in
|
||||||
success)
|
success)
|
||||||
@@ -78,14 +117,33 @@ format_result() {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
# Construir URL de logs si no está disponible
|
||||||
|
if [[ -z "$run_url" || "$run_url" == "null" ]]; then
|
||||||
|
run_url="$GITEA_URL/$OWNER/$REPO/actions/runs/$id"
|
||||||
|
fi
|
||||||
|
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
{
|
{
|
||||||
"decision": "block",
|
"decision": "block",
|
||||||
"reason": "$emoji Gitea Action completada: $msg\n\n📋 Detalles:\n • ID: $id\n • Commit: $commit\n • Iniciado: $started\n • Finalizado: $stopped\n\n🔗 Ver en Gitea: $GITEA_URL/$OWNER/$REPO/actions"
|
"reason": "$emoji Gitea Action completada: $msg\n\n📋 Detalles:\n • Workflow: $workflow_name ($workflow_file)\n • Run #$run_number\n • Evento: $event\n • Branch: $branch\n • Commit: $commit\n • Título: $title\n • Duración: $duration\n • Iniciado: $started\n • Finalizado: $updated\n\n🔗 Ver logs completos:\n $run_url"
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Verificar si las actions están habilitadas
|
||||||
|
echo "🔍 Verificando si Actions están habilitadas..." >&2
|
||||||
|
ACTIONS_ENABLED=$(check_actions_enabled)
|
||||||
|
|
||||||
|
if [[ "$ACTIONS_ENABLED" != "true" ]]; then
|
||||||
|
cat <<EOF
|
||||||
|
{
|
||||||
|
"decision": "block",
|
||||||
|
"reason": "⚠️ Git push exitoso, pero las Gitea Actions NO están habilitadas en este repositorio.\n\n📝 Para habilitar Actions:\n1. Ve a: $GITEA_URL/$OWNER/$REPO/settings\n2. Busca la sección 'Actions' o 'Workflows'\n3. Activa las Actions\n\nLuego podrás ver tus workflows en:\n$GITEA_URL/$OWNER/$REPO/actions"
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
# Notificar que empezamos a monitorear
|
# Notificar que empezamos a monitorear
|
||||||
echo "🔄 Monitoreando Gitea Action (máximo ${MAX_WAIT_SECONDS}s)..." >&2
|
echo "🔄 Monitoreando Gitea Action (máximo ${MAX_WAIT_SECONDS}s)..." >&2
|
||||||
|
|
||||||
|
|||||||
16
.claude/settings.json
Normal file
16
.claude/settings.json
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"hooks": {
|
||||||
|
"PostToolUse": [
|
||||||
|
{
|
||||||
|
"matcher": "Bash",
|
||||||
|
"hooks": [
|
||||||
|
{
|
||||||
|
"type": "command",
|
||||||
|
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/monitor-gitea-action.sh",
|
||||||
|
"timeout": 600
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
23
README.md
23
README.md
@@ -28,6 +28,29 @@ npm install
|
|||||||
npm run dev
|
npm run dev
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Claude Code Hooks
|
||||||
|
|
||||||
|
Este proyecto incluye hooks de Claude Code para monitorear automáticamente las Gitea Actions.
|
||||||
|
|
||||||
|
### Configuración Rápida:
|
||||||
|
|
||||||
|
1. **Crear token de Gitea:**
|
||||||
|
- Ve a tu instancia de Gitea → Settings → Applications
|
||||||
|
- Genera un token con permisos `repo`
|
||||||
|
|
||||||
|
2. **Configurar token:**
|
||||||
|
```bash
|
||||||
|
echo "export GITEA_TOKEN='tu_token_aqui'" >> ~/.bashrc
|
||||||
|
source ~/.bashrc
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Listo!** Cuando Claude Code haga `git push`, automáticamente:
|
||||||
|
- Monitoreará la Gitea Action
|
||||||
|
- Te notificará cuando termine con información detallada
|
||||||
|
- Te mostrará un link directo a los logs
|
||||||
|
|
||||||
|
📖 Ver documentación completa en [`.claude/hooks/README.md`](.claude/hooks/README.md)
|
||||||
|
|
||||||
## Despliegue
|
## Despliegue
|
||||||
|
|
||||||
El proyecto incluye Gitea Actions que automáticamente:
|
El proyecto incluye Gitea Actions que automáticamente:
|
||||||
|
|||||||
Reference in New Issue
Block a user