name: CI Failure Monitor

on:
  schedule:
    - cron: '0 */6 * * *'  # 每6小时检查一次
  workflow_dispatch:
    inputs:
      days:
        description: '统计最近N天的失败'
        required: false
        default: '7'
      fail_threshold:
        description: '失败次数阈值'
        required: false
        default: '3'
      fail_rate_threshold:
        description: '失败率阈值(%)'
        required: false
        default: '30'

permissions:
  contents: read

jobs:
  monitor:
    name: CI重复失败检测
    runs-on: ci-l2
    timeout-minutes: 10

    steps:
    - name: Checkout code
      shell: sh
      env:
        GITHUB_TOKEN: ${{ github.token }}
      run: |
        curl -sH "Authorization: token $GITHUB_TOKEN" \
          "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/raw/scripts/ci/step_checkout.sh?ref=${GITHUB_SHA}" \
          | bash

    - name: Record job start time
      shell: sh
      run: bash scripts/ci/step_timer_start.sh

    - name: Run failure detection
      shell: sh
      env:
        GITEA_API_TOKEN: ${{ secrets.REVIEW_GITEA_TOKEN }}
        GITEA_URL: https://git.xiaoxiajianji.com
        GITEA_REPO: xiaoxia/xiaoxia-saas
        CI_NOTIFY_WEBHOOK: ${{ secrets.CI_NOTIFY_WEBHOOK }}
        FAIL_CHECK_DAYS: ${{ inputs.days || 7 }}
        FAIL_THRESHOLD: ${{ inputs.fail_threshold || 3 }}
        FAIL_RATE_THRESHOLD: ${{ inputs.fail_rate_threshold || 30 }}
      run: |
        set +e
        python3 scripts/ci/ci_repeated_failure_detector.py
        EXIT_CODE=$?
        echo "检测完成，退出码: $EXIT_CODE"
        # 0=无异常, 1=有警告, 2=有严重问题
        # 监控脚本永远不fail，避免告警风暴
        exit 0

    - name: Job duration summary
      if: always()
      shell: sh
      run: bash scripts/ci/step_timer_end.sh

    - name: Report CI trace
      if: always()
      shell: sh
      env:
        AGENTLOOP_LICENSE_KEY: ${{ secrets.AGENTLOOP_LICENSE_KEY }}
      run: |
        STATUS="ok"
        [ ${{ job.status }} = "success" ] || STATUS="error"
        START_TIME=""
        [ -f /tmp/ci_job_start_time ] && START_TIME=$(cat /tmp/ci_job_start_time)
        python3 scripts/ci/ci_trace_report.py --service xiaoxia-saas-ci --status $STATUS --start-time "$START_TIME" || true