name: check
on:
  pull_request:
  workflow_dispatch:
  workflow_call:
    inputs:
      branch:
        description: "ref branch for this workflow"
        default: "master"
        required: true
        type: string

permissions: {}

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

env:
  CARGO_TERM_COLOR: always

jobs:

  checks:
    runs-on: ubuntu-24.04
    steps:

    - name: Checkout repository
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
      with:
        persist-credentials: false
        ref: ${{ inputs.branch }}

    - name: Install Python 3.12
      uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 #v6.2.0
      with:
        python-version: '3.12'

    - name: Setup Python venv
      run: |
        bin/activate_python3_venv.sh
        echo "/tmp/hurl-python3-venv/bin" >> "${GITHUB_PATH}"

    - name: Python infos
      run: |
        which python3
        python3 --version
        python3 -m pip --version

    - name: Install prerequisites
      run: bin/check/install_prerequisites.sh

    - name: Semver
      if: success() || failure()
      env:
        GH_TOKEN: ${{ github.token }}
      run: bin/check/semver.sh --install-dependencies

    - name: Shellcheck
      if: success() || failure()
      run: bin/check/shellcheck.sh

    - name: Check ad hoc
      if: success() || failure()
      run: bin/check/ad_hoc.sh

    - name: Gitleaks
      if: success() || failure()
      run: |
        git config --global --add safe.directory /github/workspace
        docker run --rm \
          -v "$PWD":/github/workspace \
          -w /github/workspace ghcr.io/zricethezav/gitleaks:latest detect --verbose --config .github/workflows/config/gitleaks.toml

    - name: Hadolint
      if: success() || failure()
      run: bin/check/hadolint.sh ./contrib/docker/Dockerfile

    - name: Zizmor
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: success() || failure()
      run: bin/check/zizmor.sh --github-token "${GITHUB_TOKEN}" .github/workflows/*.yml

    - name: Check Rust version
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: success() || failure()
      run: bin/check/rust_version.py --token "${GITHUB_TOKEN}" 7

    - name: Generated
      if: success() || failure()
      run: bin/check/generated.sh

    - name: Rustfmt
      if: success() || failure()
      run: bin/check/rustfmt.sh

    - name: Clippy
      if: success() || failure()
      run: bin/check/clippy.sh

    - name: Ruff
      if: success() || failure()
      run: bin/check/ruff.sh

    - name: Check XML/HTML files
      if: success() || failure()
      run: bin/check/xmllint.sh

    - name: Hurlfmt
      if: success() || failure()
      run: |
        bin/release/release.sh
        export PATH=target/release:$PATH
        bin/check/hurlfmt.sh

    - name: Check crates licence
      if: success() || failure()
      run: python3 bin/check/license.py

    - name: Check Hurl crate API changes
      if: success() || failure()
      run: bin/check/compatibility.sh

    - name: Check doc generation
      if: success() || failure()
      run: bin/check/doc.sh

    - name: Check Valgrind
      if: success() || failure()
      run: bin/check/valgrind.sh

    - name: Check completion files
      if: success() || failure()
      run: bin/check/completion.sh
