name: CI

on:
  push:
    branches: [main, staging, trying]
  pull_request:
    branches: [main, 'testnet_*']
    types:
    - opened
    - reopened
    - synchronize
    - ready_for_review

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: full
  RUSTFLAGS: "--deny warnings"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  # Quick tests on each commit/PR
  sanity:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: 1.72.1
          components: rustfmt
          override: true
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: "sanity"
          save-if: ${{ github.ref_name == 'testnet_26' }}
      - uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check

  check:
    if: github.ref != 'refs/heads/staging'
    needs: sanity
    runs-on: ubuntu-latest
    continue-on-error: true
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: 1.72.1
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: "check"
          save-if: ${{ github.ref_name == 'testnet_26' }}
      - uses: arduino/setup-protoc@v1
        with:
          version: '3.x'
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: actions-rs/cargo@v1
        with:
          command: check

  clippy:
    if: github.ref != 'refs/heads/staging'
    needs: sanity
    runs-on: ubuntu-latest
    continue-on-error: true
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: 1.72.1
          components: clippy
          override: true
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: "clippy"
          save-if: ${{ github.ref_name == 'testnet_26' }}
      - uses: arduino/setup-protoc@v1
        with:
          version: '3.x'
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: actions-rs/clippy-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          args: -- -A clippy::uninlined-format-args

  security:
    if: github.ref != 'refs/heads/staging'
    needs: sanity
    runs-on: ubuntu-latest
    continue-on-error: true
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - uses: actions-rust-lang/audit@v1
        name: Audit Rust Dependencies

  # Full cross-platform tests required by bors to merge on main branch
  full:
    name: full
    needs: sanity
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macOS-latest
          # - windows-latest
    steps:
      - name: Free Disk Space (Ubuntu)
        if: runner.os == 'Linux'
        uses: jlumbroso/free-disk-space@main
        with:
          # this might remove tools that are actually needed,
          # if set to "true" but frees about 6 GB
          tool-cache: false
          # all of these default to true, but feel free to set to
          # "false" if necessary for your workflow
          android: true
          dotnet: true
          haskell: true
          large-packages: false
          docker-images: true
          swap-storage: true
      - name: Purge more (Ubuntu)
        if: runner.os == 'Linux'
        run: |
          sudo docker rmi $(docker image ls -aq) || true
          sudo swapoff /swapfile || true
          sudo rm -rf /swapfile /usr/share/dotnet /usr/local/lib/android /opt/ghc  || true
          sudo apt purge aria2 ansible azure-cli shellcheck rpm xorriso zsync \
            clang-6.0 lldb-6.0 lld-6.0 clang-format-6.0 clang-8 lldb-8 lld-8 clang-format-8 \
            dotnet-sdk-3.0 dotnet-sdk-3.1=3.1.101-1 \
            esl-erlang firefox g++-8 g++-9 gfortran-8 gfortran-9 google-chrome-stable \
            google-cloud-sdk ghc-8.0.2 ghc-8.2.2 ghc-8.4.4 ghc-8.6.2 ghc-8.6.3 ghc-8.6.4 \
            ghc-8.6.5 ghc-8.8.1 ghc-8.8.2 ghc-8.8.3 ghc-8.10.1 cabal-install-2.0 cabal-install-2.2 \
            cabal-install-2.4 cabal-install-3.0 cabal-install-3.2 heroku imagemagick \
            libmagickcore-dev libmagickwand-dev libmagic-dev ant ant-optional kubectl \
            mercurial apt-transport-https mono-complete mysql-client libmysqlclient-dev \
            mysql-server mssql-tools unixodbc-dev yarn bazel chrpath libxft-dev \
            libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev php7.1 php7.1-bcmath \
            php7.1-bz2 php7.1-cgi php7.1-cli php7.1-common php7.1-curl php7.1-dba php7.1-dev \
            php7.1-enchant php7.1-fpm php7.1-gd php7.1-gmp php7.1-imap php7.1-interbase php7.1-intl \
            php7.1-json php7.1-ldap php7.1-mbstring php7.1-mcrypt php7.1-mysql php7.1-odbc \
            php7.1-opcache php7.1-pgsql php7.1-phpdbg php7.1-pspell php7.1-readline php7.1-recode \
            php7.1-snmp php7.1-soap php7.1-sqlite3 php7.1-sybase php7.1-tidy php7.1-xml \
            php7.1-xmlrpc php7.1-xsl php7.1-zip php7.2 php7.2-bcmath php7.2-bz2 php7.2-cgi \
            php7.2-cli php7.2-common php7.2-curl php7.2-dba php7.2-dev php7.2-enchant php7.2-fpm \
            php7.2-gd php7.2-gmp php7.2-imap php7.2-interbase php7.2-intl php7.2-json php7.2-ldap \
            php7.2-mbstring php7.2-mysql php7.2-odbc php7.2-opcache php7.2-pgsql php7.2-phpdbg \
            php7.2-pspell php7.2-readline php7.2-recode php7.2-snmp php7.2-soap php7.2-sqlite3 \
            php7.2-sybase php7.2-tidy php7.2-xml php7.2-xmlrpc php7.2-xsl php7.2-zip php7.3 \
            php7.3-bcmath php7.3-bz2 php7.3-cgi php7.3-cli php7.3-common php7.3-curl php7.3-dba \
            php7.3-dev php7.3-enchant php7.3-fpm php7.3-gd php7.3-gmp php7.3-imap php7.3-interbase \
            php7.3-intl php7.3-json php7.3-ldap php7.3-mbstring php7.3-mysql php7.3-odbc \
            php7.3-opcache php7.3-pgsql php7.3-phpdbg php7.3-pspell php7.3-readline php7.3-recode \
            php7.3-snmp php7.3-soap php7.3-sqlite3 php7.3-sybase php7.3-tidy php7.3-xml \
            php7.3-xmlrpc php7.3-xsl php7.3-zip php7.4 php7.4-bcmath php7.4-bz2 php7.4-cgi \
            php7.4-cli php7.4-common php7.4-curl php7.4-dba php7.4-dev php7.4-enchant php7.4-fpm \
            php7.4-gd php7.4-gmp php7.4-imap php7.4-interbase php7.4-intl php7.4-json php7.4-ldap \
            php7.4-mbstring php7.4-mysql php7.4-odbc php7.4-opcache php7.4-pgsql php7.4-phpdbg \
            php7.4-pspell php7.4-readline php7.4-snmp php7.4-soap php7.4-sqlite3 php7.4-sybase \
            php7.4-tidy php7.4-xml php7.4-xmlrpc php7.4-xsl php7.4-zip php-amqp php-apcu \
            php-igbinary php-memcache php-memcached php-mongodb php-redis php-xdebug \
            php-zmq snmp pollinate libpq-dev postgresql-client powershell ruby-full \
            sphinxsearch subversion mongodb-org -yq >/dev/null 2>&1 || true
          sudo apt-get autoremove -y >/dev/null 2>&1 || true
          sudo apt-get autoclean -y >/dev/null 2>&1 || true
      - name: Check disk space (Ubuntu)
        if: runner.os == 'Linux'
        run: df -h
      - uses: ilammy/setup-nasm@v1
      - uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: 1.72.1
          override: true
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: "massa"
          save-if: ${{ github.ref_name == 'testnet_26' }}
      - uses: arduino/setup-protoc@v1
        with:
          version: '3.x'
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: actions-rs/cargo@v1
        with:
          command: install
          args: cargo-nextest
      - uses: actions-rs/cargo@v1
        with:
          command: nextest
          args: run --features testing --retries 10
      - uses: actions-rs/cargo@v1
        with:
          command: clean
      - uses: actions-rs/cargo@v1
        with:
          command: test
          args: --features testing --doc

  build: # quick hack because bors wrongly detect matrix jobs status
    needs: full
    runs-on: ubuntu-latest
    steps:
      - run: exit 0

  # Maybe launch it only in testnet branches
  gas-costs-check:
    if: github.ref != 'refs/heads/staging' && github.ref != 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: 1.72.1
          components: rustfmt
          override: true
      - uses: actions/checkout@v3
        with:
          repository: massalabs/gas-calibration
          path: gas-calibration
          ref: main
      - uses: actions/checkout@v3
        with:
          repository: massalabs/massa-as-sdk
          path: massa-as-sdk
          ref: main
      # Replace the branch in the dependencies in gas-calibration project by the current branch
      # Special case for massa-sc-runtime: we use the branch referenced in the Cargo.toml of massa-execution-worker.
      - name: "Use the current branch as dependency for gas-calibration"
        run: >
          cd gas-calibration &&
          sed -i 's!main!${{ github.head_ref || github.ref_name }}!g' Cargo.toml &&
          sed -i 's!massa-sc-runtime = { git = "https://github.com/massalabs/massa-sc-runtime", features = \["gas_calibration"\] }!'"$(cat ../massa-execution-worker/Cargo.toml | grep 'massalabs/massa-sc-runtime' | sed 's!/!\\/!g' | sed 's!}!, features = ["gas_calibration"]}!g' )"'!g' Cargo.toml
      #- name: "Launch gas-calibration with one SC per ABI in massa-as-sdk to see if there is an ABI missing"
      #  run: >
      #    cd gas-calibration &&
      #    cargo run -r -- --nb-scs-by-abi=1 --as-sdk-env-path=../massa-as-sdk/assembly/env/env.ts --only-generate


  doc:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: 1.72.1
          components: rustfmt
          override: true
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: "doc"
          save-if: ${{ github.ref_name == 'testnet_26' }}
      - uses: arduino/setup-protoc@v1
        with:
          version: '3.x'
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: actions-rs/cargo@v1
        with:
          command: doc
          args: --no-deps --document-private-items
      - uses: JamesIves/github-pages-deploy-action@4.1.7
        with:
          branch: gh-pages
          folder: target/doc

  unit_tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - name: setup_tests
        shell: bash
        run: |
          cargo install cargo-script && cargo script tools/setup_test.rs && git diff --no-ext-diff --quiet