From bfeabbc640538421cd543a04e26090106b0ef581 Mon Sep 17 00:00:00 2001 From: josedario87 Date: Sun, 12 Oct 2025 17:18:59 -0600 Subject: [PATCH] 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 --- .claude/hooks/README.md | 32 +++++++++--- .claude/hooks/monitor-gitea-action.sh | 70 ++++++++++++++++++++++++--- .claude/settings.json | 16 ++++++ README.md | 23 +++++++++ 4 files changed, 128 insertions(+), 13 deletions(-) create mode 100644 .claude/settings.json diff --git a/.claude/hooks/README.md b/.claude/hooks/README.md index e9e9d12..d5a5b9e 100644 --- a/.claude/hooks/README.md +++ b/.claude/hooks/README.md @@ -51,21 +51,39 @@ Deberías ver tu token. Una vez configurado, el hook se activa automáticamente cuando Claude Code ejecuta `git push`: 1. Claude ejecuta `git push` -2. El hook se activa automáticamente -3. **Claude se congela** mientras espera a que termine la Gitea Action (máximo 10 minutos) -4. Puedes presionar **Ctrl+C** para interrumpir la espera si es necesario -5. Cuando termine, Claude te muestra el resultado: +2. El hook verifica que Actions estén habilitadas +3. El hook se activa automáticamente y comienza a monitorear +4. **Claude se congela** mientras espera a que termine la Gitea Action (máximo 10 minutos) +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 📋 Detalles: - • ID: 123 + • Workflow: build-and-deploy (build-and-deploy.yml) + • Run #3 + • Evento: push + • Branch: master • Commit: a1b2c3d4 + • Título: Update README: Add Claude Code hooks feature + • Duración: 5m 23s • 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 diff --git a/.claude/hooks/monitor-gitea-action.sh b/.claude/hooks/monitor-gitea-action.sh index 866b6fb..7781def 100755 --- a/.claude/hooks/monitor-gitea-action.sh +++ b/.claude/hooks/monitor-gitea-action.sh @@ -41,11 +41,26 @@ EOF exit 0 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 get_latest_action_status() { - curl -s -H "Authorization: token $GITEA_TOKEN" \ - "$GITEA_URL/api/v1/repos/$OWNER/$REPO/actions/tasks?limit=1" | \ - jq -r '.workflow_runs[0] // .data[0] // empty' + local response=$(curl -s -H "Authorization: token $GITEA_TOKEN" \ + "$GITEA_URL/api/v1/repos/$OWNER/$REPO/actions/tasks?limit=1") + + # 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 @@ -54,10 +69,34 @@ format_result() { local task_data="$2" 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 started=$(echo "$task_data" | jq -r '.started_at // "N/A"') - local stopped=$(echo "$task_data" | jq -r '.stopped_at // "N/A"') + local started=$(echo "$task_data" | jq -r '.run_started_at // .started_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 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 success) @@ -78,14 +117,33 @@ format_result() { ;; 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 <&2 +ACTIONS_ENABLED=$(check_actions_enabled) + +if [[ "$ACTIONS_ENABLED" != "true" ]]; then + cat <&2 diff --git a/.claude/settings.json b/.claude/settings.json new file mode 100644 index 0000000..bd27691 --- /dev/null +++ b/.claude/settings.json @@ -0,0 +1,16 @@ +{ + "hooks": { + "PostToolUse": [ + { + "matcher": "Bash", + "hooks": [ + { + "type": "command", + "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/monitor-gitea-action.sh", + "timeout": 600 + } + ] + } + ] + } +} diff --git a/README.md b/README.md index ec75a8d..b21a201 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,29 @@ npm install 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 El proyecto incluye Gitea Actions que automáticamente: