name: Update estimated test times

on:
  schedule:
    - cron: '0 2 * * 1'  # Every Monday at 02:00 UTC
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

env:
  UPSTREAM_REPO: vllm-project/vllm-ascend
  FORK_OWNER: vllm-ascend-ci
  BRANCH_NAME: auto/update-estimated-times-${{ github.run_id }}

concurrency:
  group: update-estimated-times-${{ github.ref }}
  cancel-in-progress: true

jobs:
  e2e-test:
    name: e2e-test
    strategy:
      matrix:
        vllm_version: [v0.20.2]
        type: [full, light]
    uses: ./.github/workflows/_e2e_test.yaml
    with:
      vllm: ${{ matrix.vllm_version }}
      image: swr.cn-southwest-2.myhuaweicloud.com/base_image/ascend-ci/vllm-ascend:main
      contains_310: false
      type: ${{ matrix.type }}
      continue_on_error: true  # Continue even if some tests fail, we want to collect as much timing data as possible

  update-estimated-times:
    name: Update estimated_time in config.yaml
    needs: [e2e-test]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout fork repo
        uses: actions/checkout@v6
        with:
          repository: ${{ env.FORK_OWNER }}/vllm-ascend
          token: ${{ secrets.PAT_TOKEN }}

      - name: Download all timing artifacts
        uses: actions/download-artifact@v8
        with:
          pattern: timing-data-*
          path: timing-artifacts/
          merge-multiple: false

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pyyaml

      - name: Config git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git remote add upstream https://github.com/${{ env.UPSTREAM_REPO }}.git
          git fetch upstream main && git checkout -b ${{ env.BRANCH_NAME }} upstream/main

      - name: Update config.yaml from timing data
        run: |
          python3 .github/workflows/scripts/update_estimated_time.py \
            --timing-dir timing-artifacts/ \
            --config .github/workflows/scripts/config.yaml

      - name: Check for changes
        id: check_changes
        run: |
          if git diff --quiet .github/workflows/scripts/config.yaml; then
            echo "changed=false" >> "$GITHUB_OUTPUT"
            echo "No changes to config.yaml."
          else
            echo "changed=true" >> "$GITHUB_OUTPUT"
            echo "config.yaml has been updated:"
            git diff .github/workflows/scripts/config.yaml
          fi

      - name: Create pull request
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
        run: |
          git add .github/workflows/scripts/config.yaml
          git commit -sm "[CI] Auto-update estimated test times in config.yaml Computed from timing-data artifacts on workflow run-${{ github.run_id }}"
          git remote -v
          git push -f origin ${{ env.BRANCH_NAME }}:${{ env.BRANCH_NAME }}
          gh pr create \
            --repo ${{ env.UPSTREAM_REPO }} \
            --base main \
            --head ${{ env.FORK_OWNER }}:${{ env.BRANCH_NAME }} \
            --title "[CI]: Auto-update estimated test times in config.yaml" \
            --body "## Summary

          This PR was auto-generated by the **Update estimated test times** [workflow](https://github.com/${{ env.UPSTREAM_REPO }}/actions/runs/${{ github.run_id }}).

          It updates the \`estimated_time\` values in \`.github/workflows/scripts/config.yaml\` based on actual elapsed times collected from CI workflow runs.

          ### Methodology

          - Each e2e test job uploads its elapsed time as a \`timing-data-*\` artifact upon completion.
          - The workflow aggregates all collected timing artifacts across jobs.
          - For each test, the **median** elapsed time is computed to reduce outlier impact.
          - A **10% safety buffer** is applied and the result is rounded to the nearest 10 seconds.

          ### Review Checklist

          - [ ] Verify that updated \`estimated_time\` values are within a reasonable range.
          - [ ] Confirm no test entries are missing or unexpectedly removed.

          > If the new values look reasonable, feel free to merge. Otherwise, leave a comment describing the anomaly."