Documentation
name: CI

on:
  push:
    branches:
      - master
  pull_request:

env:
  ALL_FEATURES: "ota_mqtt_data,ota_http_data"

jobs:
  cancel_previous_runs:
    name: Cancel previous runs
    runs-on: ubuntu-latest
    steps:
      - uses: styfle/cancel-workflow-action@0.4.1
        with:
          access_token: ${{ secrets.GITHUB_TOKEN }}
          
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          target: thumbv7m-none-eabi
          override: true

      - name: Build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --all --target thumbv7m-none-eabi --features ${{ env.ALL_FEATURES }}

      - name: Test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --lib --features "ota_mqtt_data,log"
  grcov:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly
          target: x86_64-unknown-linux-gnu
          override: true

      - name: Install grcov
        uses: actions-rs/cargo@v1
        # uses: actions-rs/install@v0.1
        with:
          # crate: grcov
          # version: latest
          # use-tool-cache: true
          command: install
          args: --locked grcov --git https://github.com/mozilla/grcov

      - name: Test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --lib --no-fail-fast --features "ota_mqtt_data,ota_http_data,log"
        env:
          CARGO_INCREMENTAL: "0"
          RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=unwind -Zpanic_abort_tests"
          RUSTDOCFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=unwind -Zpanic_abort_tests"

      - name: Generate coverage data
        id: grcov
        # uses: actions-rs/grcov@v0.1
        run: |
          grcov target/debug/ \
              --branch \
              --llvm \
              --source-dir . \
              --output-file lcov.info \
              --ignore='/**' \
              --ignore='C:/**' \
              --ignore='../**' \
              --ignore-not-existing \
              --excl-line "#\\[derive\\(" \
              --excl-br-line "(#\\[derive\\()|(debug_assert)" \
              --excl-start "#\\[cfg\\(test\\)\\]" \
              --excl-br-start "#\\[cfg\\(test\\)\\]" \
              --commit-sha ${{ github.sha }} \
              --service-job-id ${{ github.job }} \
              --service-name "GitHub Actions" \
              --service-number ${{ github.run_id }}
      - name: Upload coverage as artifact
        uses: actions/upload-artifact@v2
        with:
          name: lcov.info
          # path: ${{ steps.grcov.outputs.report }}
          path: lcov.info

      - name: Upload coverage to codecov.io
        uses: codecov/codecov-action@v1
        with:
          # file: ${{ steps.grcov.outputs.report }}
          file: lcov.info
          fail_ci_if_error: true
  rustfmt:
    name: rustfmt
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly
          override: true
          components: rustfmt

      - name: Run rustfmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check --verbose

  # tomlfmt:
  #   name: tomlfmt
  #   runs-on: ubuntu-latest
  #   steps:
  #     - name: Checkout source code
  #       uses: actions/checkout@v2

  #     - name: Install Rust
  #       uses: actions-rs/toolchain@v1
  #       with:
  #         profile: minimal
  #         toolchain: nightly
  #         override: true

  #     - name: Install tomlfmt
  #       uses: actions-rs/install@v0.1
  #       with:
  #         crate: cargo-tomlfmt
  #         version: latest
  #         use-tool-cache: true

  #     - name: Run Tomlfmt
  #       uses: actions-rs/cargo@v1
  #       with:
  #         command: tomlfmt
  #         args: --dryrun

  clippy:
    name: clippy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: clippy

      - name: Run clippy
        uses: actions-rs/clippy-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          args: -- ${{ env.CLIPPY_PARAMS }}
  docs:
    name: Documentation
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2
        with:
          persist-credentials: false

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly
          override: true

      - name: Build documentation
        uses: actions-rs/cargo@v1
        with:
          command: doc
          args: --verbose --no-deps --features ${{ env.ALL_FEATURES }}

  integration-test:
    name: Integration Tests
    runs-on: ubuntu-latest
    needs: ['test', 'rustfmt', 'clippy']
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          target: x86_64-unknown-linux-gnu
          override: true
      - name: Create OTA Job
        run: |
          ./scripts/create_ota.sh
        env:
          AWS_DEFAULT_REGION: ${{ secrets.MGMT_AWS_DEFAULT_REGION }}
          AWS_ACCESS_KEY_ID: ${{ secrets.MGMT_AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.MGMT_AWS_SECRET_ACCESS_KEY }}
      - name: Integration Tests
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --test '*' --features "ota_mqtt_data,log" --target x86_64-unknown-linux-gnu
        env:
          IDENTITY_PASSWORD: ${{ secrets.DEVICE_ADVISOR_PASSWORD }}
          AWS_HOSTNAME: a1vq3mi5y3c6j5-ats.iot.eu-west-1.amazonaws.com

      - name: Cleanup OTA Jobs
        if: ${{ always() }}
        run: |
          ./scripts/cleanup_ota.sh
        env:
          AWS_DEFAULT_REGION: ${{ secrets.MGMT_AWS_DEFAULT_REGION }}
          AWS_ACCESS_KEY_ID: ${{ secrets.MGMT_AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.MGMT_AWS_SECRET_ACCESS_KEY }}