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

on:
  push:
    # On dev branch release-dev already performs CI checks
    # On other branches the `pull_request` trigger will be used
    branches: [beta, master]

  pull_request:

jobs:
  # A fast overview job that checks only changed files
  overview:
    runs-on: ubuntu-latest
    container: esphome/esphome-lint:latest
    steps:
      # Also fetch history and dev branch so that we can check which files changed
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Fetch dev branch
        run: git fetch origin dev

      # Cache the .pio directory with (primarily) library dependencies
      - name: Cache .pio lib_deps
        uses: actions/cache@v1
        with:
          path: .pio
          key: lint-cpp-pio-${{ hashFiles('platformio.ini') }}
          restore-keys: |
            lint-cpp-pio-
      - name: Set up python environment
        run: script/setup
      # Set up the pio project so that the cpp checks know how files are compiled
      # (build flags, libraries etc)
      - name: Set up platformio environment
        run: pio init --ide atom

      - name: Register problem matchers
        run: |
          echo "::add-matcher::.github/workflows/matchers/ci-custom.json"
          echo "::add-matcher::.github/workflows/matchers/clang-tidy.json"
          echo "::add-matcher::.github/workflows/matchers/gcc.json"
          echo "::add-matcher::.github/workflows/matchers/lint-python.json"
          echo "::add-matcher::.github/workflows/matchers/python.json"
      - name: Run a quick lint over all changed files
        run: script/quicklint
      - name: Suggest changes
        run: script/ci-suggest-changes

  lint-clang-format:
    runs-on: ubuntu-latest
    # cpp lint job runs with esphome-lint docker image so that clang-format-*
    # doesn't have to be installed
    container: esphome/esphome-lint:latest
    steps:
      - uses: actions/checkout@v2
      # Cache platformio intermediary files (like libraries etc)
      # Note: platformio platform versions should be cached via the esphome-lint image
      - name: Cache Platformio
        uses: actions/cache@v1
        with:
          path: .pio
          key: lint-cpp-pio-${{ hashFiles('platformio.ini') }}
          restore-keys: |
            lint-cpp-pio-
      # Set up the pio project so that the cpp checks know how files are compiled
      # (build flags, libraries etc)
      - name: Set up platformio environment
        run: pio init --ide atom

      - name: Run clang-format
        run: script/clang-format -i
      - name: Suggest changes
        run: script/ci-suggest-changes

  lint-clang-tidy:
    runs-on: ubuntu-latest
    # cpp lint job runs with esphome-lint docker image so that clang-format-*
    # doesn't have to be installed
    container: esphome/esphome-lint:latest
    # Split clang-tidy check into 4 jobs. Each one will check 1/4th of the .cpp files
    strategy:
      matrix:
        split: [1, 2, 3, 4]
    steps:
      - uses: actions/checkout@v2
      # Cache platformio intermediary files (like libraries etc)
      # Note: platformio platform versions should be cached via the esphome-lint image
      - name: Cache Platformio
        uses: actions/cache@v1
        with:
          path: .pio
          key: lint-cpp-pio-${{ hashFiles('platformio.ini') }}
          restore-keys: |
            lint-cpp-pio-
      # Set up the pio project so that the cpp checks know how files are compiled
      # (build flags, libraries etc)
      - name: Set up platformio environment
        run: pio init --ide atom


      - name: Register problem matchers
        run: |
          echo "::add-matcher::.github/workflows/matchers/clang-tidy.json"
          echo "::add-matcher::.github/workflows/matchers/gcc.json"
      - name: Run clang-tidy
        run: script/clang-tidy --all-headers --fix --split-num 4 --split-at ${{ matrix.split }}
      - name: Suggest changes
        run: script/ci-suggest-changes

  lint-python:
    # Don't use the esphome-lint docker image because it may contain outdated requirements.
    # This way, all dependencies are cached via the cache action.
    runs-on: ubuntu-latest
    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-
      - 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"
      - name: Lint Custom
        run: script/ci-custom.py
      - name: Lint Python
        run: script/lint-python
      - name: Lint CODEOWNERS
        run: script/build_codeowners.py --check

  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
          test:
          - test1
          - test2
          - test3
          - test4
    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.test }}-${{ hashFiles('esphome/core_config.py') }}
          restore-keys: |
            test-home-platformio-${{ matrix.test }}-
      - name: Set up environment
        run: script/setup


      - name: Register problem matchers
        run: |
          echo "::add-matcher::.github/workflows/matchers/gcc.json"
          echo "::add-matcher::.github/workflows/matchers/python.json"
      - run: esphome tests/${{ matrix.test }}.yaml compile

  pytest:
    runs-on: ubuntu-latest
    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-
      - name: Set up environment
        run: script/setup
      - name: Install Github Actions annotator
        run: pip install pytest-github-actions-annotate-failures

      - name: Register problem matchers
        run: |
          echo "::add-matcher::.github/workflows/matchers/python.json"
      - name: Run pytest
        run: |
          pytest \
            -qq \
            --durations=10 \
            -o console_output_style=count \
            tests