name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  schedule:
    - cron: "0 0 * * *"

jobs:
  code_format:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: -- --check -l

  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: |
          curl -sOL https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/native_client.amd64.cpu.linux.tar.xz
          unxz native_client.amd64.cpu.linux.tar.xz
          tar -xf native_client.amd64.cpu.linux.tar
          sudo cp libdeepspeech.so /usr/local/lib/
          sudo ldconfig
          sudo apt install postgresql postgresql-contrib
          sudo systemctl restart postgresql.service
          sudo -u postgres createuser btfm
          sudo -u postgres createdb btfm
          sudo -u postgres psql -c "ALTER USER btfm PASSWORD 'password';"
          sudo -u postgres psql -c "ALTER DATABASE btfm OWNER to btfm;"
          export DATABASE_URL=postgres://btfm:password@localhost/btfm
          echo "DATABASE_URL=$DATABASE_URL" >> $GITHUB_ENV
          cargo install sqlx-cli
          cargo sqlx database setup
      - uses: actions-rs/cargo@v1
        with:
          command: build

  audit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install latest nightly
        uses: actions-rs/toolchain@v1
        with:
            toolchain: stable
      - name: Install cargo-audit
        run: |
          cargo install cargo-audit
      - uses: actions-rs/cargo@v1
        with:
          command: audit

  clippy:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: |
          curl -sOL https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/native_client.amd64.cpu.linux.tar.xz
          unxz native_client.amd64.cpu.linux.tar.xz
          tar -xf native_client.amd64.cpu.linux.tar
          sudo cp libdeepspeech.so /usr/local/lib/
          sudo ldconfig
          sudo apt install postgresql postgresql-contrib
          sudo systemctl restart postgresql.service
          sudo -u postgres createuser btfm
          sudo -u postgres createdb btfm
          sudo -u postgres psql -c "ALTER USER btfm PASSWORD 'password';"
          sudo -u postgres psql -c "ALTER DATABASE btfm OWNER to btfm;"
          export DATABASE_URL=postgres://btfm:password@localhost/btfm
          echo "DATABASE_URL=$DATABASE_URL" >> $GITHUB_ENV
          cargo install sqlx-cli
          cargo sqlx database setup
      - uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: --all-targets --all-features -- -D warnings

  test:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: |
          curl -sOL https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/native_client.amd64.cpu.linux.tar.xz
          unxz native_client.amd64.cpu.linux.tar.xz
          tar -xf native_client.amd64.cpu.linux.tar
          sudo cp libdeepspeech.so /usr/local/lib/
          sudo ldconfig
          sudo apt install postgresql postgresql-contrib
          sudo systemctl restart postgresql.service
          sudo -u postgres createuser btfm
          sudo -u postgres createdb btfm
          sudo -u postgres psql -c "ALTER USER btfm PASSWORD 'password';"
          sudo -u postgres psql -c "ALTER DATABASE btfm OWNER to btfm;"
          export DATABASE_URL=postgres://btfm:password@localhost/btfm
          echo "DATABASE_URL=$DATABASE_URL" >> $GITHUB_ENV
          cargo install sqlx-cli
          cargo sqlx database setup
      - uses: actions-rs/cargo@v1
        with:
          command: test
