name: ci
on: ["push", "pull_request"]

jobs:

  lint:
    name: lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v1
        with:
          python-version: '3.7'
      - name: extract pip cache
        uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
          restore-keys: ${{ runner.os }}-pip-
      - run: pip install --user --upgrade pip wheel
      - run: pip install -e .[torrent,lint]
      - run: make lint

  tests-unit:
    name: "tests / unit"
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v1
        with:
          python-version: '3.7'
      - name: set pip cache dir
        id: pip-cache
        run: echo "::set-output name=dir::$(pip cache dir)"
      - name: extract pip cache
        uses: actions/cache@v2
        with:
          path: ${{ steps.pip-cache.outputs.dir }}
          key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
          restore-keys: ${{ runner.os }}-pip-
      - id: os-name
        uses: ASzc/change-string-case-action@v1
        with:
          string: ${{ runner.os }}
      - run: pip install --user --upgrade pip wheel
      - if: startsWith(runner.os, 'linux')
        run: pip install -e .[torrent,test]
      - if: startsWith(runner.os, 'linux')
        env:
          HOME: /tmp
        run: make test-unit-coverage
      - if: startsWith(runner.os, 'linux') != true
        run: pip install -e .[test]
      - if: startsWith(runner.os, 'linux') != true
        env:
          HOME: /tmp
        run: coverage run --source=lbry -m unittest tests/unit/test_conf.py
      - name: submit coverage report
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COVERALLS_FLAG_NAME: tests-unit-${{ steps.os-name.outputs.lowercase }}
          COVERALLS_PARALLEL: true
        run: |
          pip install coveralls
          coveralls --service=github

  tests-integration:
    name: "tests / integration"
    runs-on: ubuntu-latest
    strategy:
      matrix:
        test:
          - datanetwork
          - blockchain
          - blockchain_legacy_search
          - other
    steps:
      - name: Configure sysctl limits
        run: |
          sudo swapoff -a
          sudo sysctl -w vm.swappiness=1
          sudo sysctl -w fs.file-max=262144
          sudo sysctl -w vm.max_map_count=262144
      - name: Runs Elasticsearch
        uses: elastic/elastic-github-actions/elasticsearch@master
        with:
          stack-version: 7.6.0
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v1
        with:
          python-version: '3.7'
      - if: matrix.test == 'other'
        run: |
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends ffmpeg
      - name: extract pip cache
        uses: actions/cache@v2
        with:
          path: ./.tox
          key: tox-integration-${{ matrix.test }}-${{ hashFiles('setup.py') }}
          restore-keys: txo-integration-${{ matrix.test }}-
      - run: pip install tox coverage coveralls
      - run: tox -e ${{ matrix.test }}
      - name: submit coverage report
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COVERALLS_FLAG_NAME: tests-integration-${{ matrix.test }}
          COVERALLS_PARALLEL: true
        run: |
          coverage combine tests
          coveralls --service=github


  coverage:
    needs: ["tests-unit", "tests-integration"]
    runs-on: ubuntu-latest
    steps:
      - name: finalize coverage report submission
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          pip install coveralls
          coveralls --service=github --finish

  build:
    needs: ["lint", "tests-unit", "tests-integration"]
    name: "build / binary"
    strategy:
      matrix:
        os:
          - ubuntu-18.04
          - macos-latest
          - windows-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v1
        with:
          python-version: '3.7'
      - id: os-name
        uses: ASzc/change-string-case-action@v1
        with:
          string: ${{ runner.os }}
      - name: set pip cache dir
        id: pip-cache
        run: echo "::set-output name=dir::$(pip cache dir)"
      - name: extract pip cache
        uses: actions/cache@v2
        with:
          path: ${{ steps.pip-cache.outputs.dir }}
          key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
          restore-keys: ${{ runner.os }}-pip-
      - run: pip install pyinstaller==4.4
      - run: pip install -e .
      - if: startsWith(github.ref, 'refs/tags/v')
        run: python docker/set_build.py
      - if: startsWith(runner.os, 'linux') || startsWith(runner.os, 'mac')
        name: Build & Run (Unix)
        run: |
          pyinstaller --onefile --name lbrynet lbry/extras/cli.py
          dist/lbrynet --version
      - if: startsWith(runner.os, 'windows')
        name: Build & Run (Windows)
        run: |
          pip install pywin32==301
          pyinstaller --additional-hooks-dir=scripts/. --icon=icons/lbry256.ico --onefile --name lbrynet lbry/extras/cli.py
          dist/lbrynet.exe --version
      - uses: actions/upload-artifact@v2
        with:
          name: lbrynet-${{ steps.os-name.outputs.lowercase }}
          path: dist/

  release:
    name: "release"
    if: startsWith(github.ref, 'refs/tags/v')
    needs: ["build"]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/download-artifact@v2
      - name: upload binaries
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_API_TOKEN }}
        run: |
          pip install githubrelease
          chmod +x lbrynet-macos/lbrynet
          chmod +x lbrynet-linux/lbrynet
          zip --junk-paths lbrynet-mac.zip lbrynet-macos/lbrynet
          zip --junk-paths lbrynet-linux.zip lbrynet-linux/lbrynet
          zip --junk-paths lbrynet-windows.zip lbrynet-windows/lbrynet.exe
          ls -lh
          githubrelease release lbryio/lbry-sdk info ${GITHUB_REF#refs/tags/}
          githubrelease asset lbryio/lbry-sdk upload ${GITHUB_REF#refs/tags/} \
            lbrynet-mac.zip lbrynet-linux.zip lbrynet-windows.zip
          githubrelease release lbryio/lbry-sdk publish ${GITHUB_REF#refs/tags/}