name: CI

permissions: {}

on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
      - master

env:
  SOLC_MINIMUM: "0.8.13"
  SOLC_LATEST: "0.8.35"
  SOLC_PRERELEASE: ""

jobs:
  build-matrix:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    permissions:
      contents: read
    outputs:
      matrix: ${{ steps.generate.outputs.matrix }}
    steps:
      - id: generate
        run: |
          MINIMUM="${{ env.SOLC_MINIMUM }}"
          LATEST="${{ env.SOLC_LATEST }}"
          PRERELEASE="${{ env.SOLC_PRERELEASE }}"
          matrix='{"include":['
          for toolchain in stable nightly; do
            for flags in \
              "" \
              "--via-ir" \
              "--use solc:${LATEST}" \
              "--use solc:${LATEST} --via-ir" \
              "--use solc:${MINIMUM}" \
              "--use solc:${MINIMUM} --via-ir"
            do
              matrix+='{"toolchain":"'"$toolchain"'","flags":"'"$flags"'","prerelease":false},'
            done
          done
          # prerelease (nightly only, svm-rs is not up to date on stable)
          if [ -n "$PRERELEASE" ]; then
            for flags in \
              "--use solc:${PRERELEASE}" \
              "--use solc:${PRERELEASE} --via-ir"
            do
              matrix+='{"toolchain":"nightly","flags":"'"$flags"'","prerelease":true},'
            done
          fi
          matrix="${matrix%,}]}"
          echo "matrix=$matrix" >> "$GITHUB_OUTPUT"

  build:
    needs: build-matrix
    name: build +${{ matrix.toolchain }} ${{ matrix.flags }}
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix: ${{ fromJSON(needs.build-matrix.outputs.matrix) }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - uses: foundry-rs/foundry-toolchain@908c540300062bd5a7e473851cdb4282204cee09 # v1.9.1
        with:
          version: ${{ matrix.toolchain }}
      - run: forge --version
      # 3805: "This is a pre-release compiler version, please do not use it in production."
      - run: forge build -vvvvv --skip test --deny warnings ${{ matrix.prerelease && '--ignored-error-codes 3805' || '' }} ${{ matrix.flags }} --contracts 'test/compilation/*'

  test:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        toolchain: [stable, nightly]
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - uses: foundry-rs/foundry-toolchain@908c540300062bd5a7e473851cdb4282204cee09 # v1.9.1
        with:
          version: ${{ matrix.toolchain }}
      - run: forge --version
      - run: forge test -vvv

  fmt:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - uses: foundry-rs/foundry-toolchain@908c540300062bd5a7e473851cdb4282204cee09 # v1.9.1
      - run: forge --version
      - run: forge fmt --check

  typos:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - uses: crate-ci/typos@5374cbf686e897b15713110e233094e2874de7ef # v1.46.1

  codeql:
    name: Analyze (${{ matrix.language }})
    runs-on: ubuntu-latest
    permissions:
      security-events: write
      actions: read
      contents: read
    strategy:
      fail-fast: false
      matrix:
        include:
          - language: actions
            build-mode: none
    steps:
      - name: Checkout repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Initialize CodeQL
        uses: github/codeql-action/init@5595ccaf912efad79be6eef63a5619ff05969be3 # v4.37.6
        with:
          languages: ${{ matrix.language }}
          build-mode: ${{ matrix.build-mode }}
      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@5595ccaf912efad79be6eef63a5619ff05969be3 # v4.37.6
        with:
          category: "/language:${{matrix.language}}"

  ci-success:
    runs-on: ubuntu-latest
    if: always()
    needs:
      - build-matrix
      - build
      - test
      - fmt
      - typos
      - codeql
    timeout-minutes: 10
    steps:
      - name: Decide whether the needed jobs succeeded or failed
        uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe # release/v1
        with:
          jobs: ${{ toJSON(needs) }}
