name: CI

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]
  push:
    branches:
      - main
  workflow_dispatch:

env:
  RUST_TOOLCHAIN: stable

jobs:
  typos:
    name: Spell Check with Typos
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
      - uses: crate-ci/typos@master

  check:
    name: Check
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v4
      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.24.3"
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.RUST_TOOLCHAIN }}
      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
      - name: Run cargo check
        run: cargo check --workspace --all-targets
      - uses: arduino/setup-protoc@v3
        with:
          version: '21.12'
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - name: Generate protobuf
        run: |
          make all
      - name: Check protobuf generation
        run: |
          git diff --quiet || (echo "Fail: generated go protobuf in github action is different from pushed ones, please run 'make all'"; exit 1)

  proto-fmt:
    name: Protobuf Format
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v4
      - name: Setup Buf
        uses: bufbuild/buf-action@v1
        with:
          setup_only: true
      - name: Run Buf Format
        run: buf format --diff --exit-code

  fmt:
    name: Rustfmt
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.RUST_TOOLCHAIN }}
          components: rustfmt
      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
      - name: Run cargo fmt
        run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.RUST_TOOLCHAIN }}
          components: clippy
      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
      - name: Run cargo clippy
        run: cargo clippy --workspace --all-targets -- -D warnings -D clippy::print_stdout -D clippy::print_stderr
