name: CI

on:
  pull_request:
  push:
    branches:
      - master
      - staging
  schedule:
    - cron: '0 1 * * *'

env:
  CARGO_INCREMENTAL: 0
  RUSTFLAGS: -D warnings
  RUST_BACKTRACE: 1

defaults:
  run:
    shell: bash

jobs:
  test:
    strategy:
      matrix:
        rust:
          - stable
          - beta
          - nightly
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: taiki-e/github-actions/install-rust@main
        with:
          toolchain: ${{ matrix.rust }}
      - run: cargo install cargo-hack
      - run: cargo test --all --all-features
      - run: cargo hack check --all --ignore-private --feature-powerset --no-dev-deps
      - if: startsWith(matrix.rust, 'nightly')
        run: bash scripts/check-minimal-versions.sh

  build:
    strategy:
      matrix:
        range:
          # This is the minimum supported Rust version of this crate.
          # When updating this, the reminder to update the minimum supported
          # Rust version in README.md and clippy.toml.
          - 1.31..1.40
          - 1.41..
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: cargo install cargo-hack
      - run: cargo hack build --all --ignore-private --no-dev-deps --version-range ${{ matrix.range }}

  clippy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: taiki-e/github-actions/install-rust@main
        with:
          component: clippy
      - run: cargo clippy --all --all-features --all-targets

  rustfmt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: taiki-e/github-actions/install-rust@main
        with:
          component: rustfmt
      - run: cargo fmt --all -- --check

  rustdoc:
    env:
      RUSTDOCFLAGS: -D warnings
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: taiki-e/github-actions/install-rust@main
      - run: cargo doc --no-deps --all --all-features

  shellcheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: shellcheck **/*.sh

  # These jobs don't actually test anything, but they're used to tell bors the
  # build completed, as there is no practical way to detect when a workflow is
  # successful listening to webhooks only.
  #
  # ALL THE PREVIOUS JOBS NEEDS TO BE ADDED TO THE `needs` SECTION OF THIS JOB!

  ci-success:
    name: ci
    if: github.event_name == 'push' && success()
    needs:
      - test
      - build
      - clippy
      - rustfmt
      - rustdoc
      - shellcheck
    runs-on: ubuntu-latest
    steps:
      - name: Mark the job as a success
        run: exit 0
  ci-failure:
    name: ci
    if: github.event_name == 'push' && !success()
    needs:
      - test
      - build
      - clippy
      - rustfmt
      - rustdoc
      - shellcheck
    runs-on: ubuntu-latest
    steps:
      - name: Mark the job as a failure
        run: exit 1
