# THESE JOBS ARE COPIED IN release.yml and release-dev.yml
# PLEASE ALSO UPDATE THOSE FILES WHEN CHANGING LINES HERE
name: CI

on:
  push:
    branches: [dev, beta, release]

  pull_request:

jobs:
  ci-with-container:
    name: ${{ matrix.name }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - id: clang-format
            name: Run script/clang-format
          - id: clang-tidy
            name: Run script/clang-tidy 1/4
            split: 1
          - id: clang-tidy
            name: Run script/clang-tidy 2/4
            split: 2
          - id: clang-tidy
            name: Run script/clang-tidy 3/4
            split: 3
          - id: clang-tidy
            name: Run script/clang-tidy 4/4
            split: 4

    # cpp lint job runs with esphome-lint docker image so that clang-format-*
    # doesn't have to be installed
    container: ghcr.io/esphome/esphome-lint:1.1
    steps:
      - uses: actions/checkout@v2

      - name: Register problem matchers
        run: |
          echo "::add-matcher::.github/workflows/matchers/clang-tidy.json"
          echo "::add-matcher::.github/workflows/matchers/gcc.json"

      # Also run git-diff-index so that the step is marked as failed on formatting errors,
      # since clang-format doesn't do anything but change files if -i is passed.
      - name: Run clang-format
        run: |
          script/clang-format -i
          git diff-index --quiet HEAD --
        if: ${{ matrix.id == 'clang-format' }}

      - name: Run clang-tidy
        run: script/clang-tidy --all-headers --fix --split-num 4 --split-at ${{ matrix.split }}
        if: ${{ matrix.id == 'clang-tidy' }}

      - name: Suggested changes
        run: script/ci-suggest-changes
        if: always()

  ci:
    # Don't use the esphome-lint docker image because it may contain outdated requirements.
    # This way, all dependencies are cached via the cache action.
    name: ${{ matrix.name }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - id: ci-custom
            name: Run script/ci-custom
          - id: lint-python
            name: Run script/lint-python
          - id: test
            file: tests/test1.yaml
            name: Test tests/test1.yaml
          - id: test
            file: tests/test2.yaml
            name: Test tests/test2.yaml
          - id: test
            file: tests/test3.yaml
            name: Test tests/test3.yaml
          - id: test
            file: tests/test4.yaml
            name: Test tests/test4.yaml
          - id: test
            file: tests/test5.yaml
            name: Test tests/test5.yaml
          - id: pytest
            name: Run pytest

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.7'

      - name: Cache pip modules
        uses: actions/cache@v1
        with:
          path: ~/.cache/pip
          key: esphome-pip-3.7-${{ hashFiles('setup.py') }}
          restore-keys: |
            esphome-pip-3.7-

      # Use per test platformio cache because tests have different platform versions
      - name: Cache ~/.platformio
        uses: actions/cache@v1
        with:
          path: ~/.platformio
          key: test-home-platformio-${{ matrix.file }}-${{ hashFiles('esphome/core/config.py') }}
          restore-keys: |
            test-home-platformio-${{ matrix.file }}-
        if: ${{ matrix.id == 'test' }}

      - name: Set up python environment
        run: script/setup

      - name: Register problem matchers
        run: |
          echo "::add-matcher::.github/workflows/matchers/ci-custom.json"
          echo "::add-matcher::.github/workflows/matchers/lint-python.json"
          echo "::add-matcher::.github/workflows/matchers/python.json"
          echo "::add-matcher::.github/workflows/matchers/pytest.json"
          echo "::add-matcher::.github/workflows/matchers/gcc.json"

      - name: Lint Custom
        run: |
          script/ci-custom.py
          script/build_codeowners.py --check
        if: ${{ matrix.id == 'ci-custom' }}
      - name: Lint Python
        run: script/lint-python
        if: ${{ matrix.id == 'lint-python' }}

      - run: esphome compile ${{ matrix.file }}
        if: ${{ matrix.id == 'test' }}

      - name: Run pytest
        run: |
          pytest -vv --tb=native tests
        if: ${{ matrix.id == 'pytest' }}