on:
  - pull_request
permissions:
  contents: read
name: CI
jobs:
  build:
    strategy:
      matrix:
        go-version: [1.22.x, 1.23.x]
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5
        with:
          go-version: ${{ matrix.go-version }}
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - run: go build ./...
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5
        with:
          check-latest: true
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - run: go test ./...
  node_test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4
        with:
          node-version: 10
      - uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5
        with:
          check-latest: true
      - run: >
          cd examples/internal/browser &&
          npm install gulp-cli &&
          npm install &&
          ./node_modules/.bin/gulp
  generate:
    container:
      image: ghcr.io/grpc-ecosystem/grpc-gateway/build-env:1.23
      options: "--user root"
      credentials:
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        # Required with newer versions of Git
        # https://github.com/actions/checkout/issues/766
      - run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - run: make install
      - run: make clean
      - run: make generate
      - run: go mod tidy
      - run: git diff --exit-code
  bazel:
    container:
      image: ghcr.io/grpc-ecosystem/grpc-gateway/build-env:1.23
      options: "--user root"
      credentials:
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4
        with:
          path: /home/vscode/.cache/_grpc_gateway_bazel
          key: v1-bazel-cache-${{ hashFiles('repositories.bzl') }}
          restore-keys: v1-bazel-cache-
        # Required with newer versions of Git
        # https://github.com/actions/checkout/issues/766
      - run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: Configure bazel
        run:
          | # put .bazelrc in $HOME so that it's read before project's .bazelrc
          cat > /home/vscode/.bazelrc << EOF
          startup --output_base=/home/vscode/.cache/_grpc_gateway_bazel
          build --@io_bazel_rules_go//go/config:race
          # Workaround https://github.com/bazelbuild/bazel/issues/3645
          # See https://docs.bazel.build/versions/0.23.0/command-line-reference.html
          build --local_ram_resources=7168 # Github runners have 7G of memory
          build --local_cpu_resources=2    # Github runners have 2 vCPU
          EOF
      - name: Check that Bazel BUILD files are up-to-date
        run: bazel run //:gazelle && git diff --exit-code
      - name: Check that repositories.bzl is up-to-date
        run: |
          bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=repositories.bzl%go_repositories &&
          git diff --exit-code
      - name: Check formatting of Bazel BUILD files
        run: bazel run //:buildifier && git diff --exit-code
      - name: Run tests with Bazel
        run: bazel test //...
  gorelease:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5
        with:
          go-version: 1.23
          check-latest: true
      - run: go run golang.org/x/exp/cmd/gorelease@latest -base=v2.25.0
  proto_lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5
        with:
          check-latest: true
      - run: make install
      - run: PATH=$PATH:~/go/bin buf build
      - run: PATH=$PATH:~/go/bin buf lint
      - run: PATH=$PATH:~/go/bin buf format -w && git diff --exit-code
      - run: PATH=$PATH:~/go/bin buf breaking --path protoc-gen-openapiv2/ --against 'https://github.com/grpc-ecosystem/grpc-gateway.git#branch=main'
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5
        with:
          go-version: 1.23
          check-latest: true
      - uses: dominikh/staticcheck-action@fe1dd0c3658873b46f8c9bb3291096a617310ca6 # v1.3.1
        with:
          install-go: false
  fuzz:
    runs-on: ubuntu-latest
    steps:
      - name: Build Fuzzers
        id: build
        uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master
        with:
          oss-fuzz-project-name: "grpc-gateway"
          dry-run: false
          language: go
      - name: Run Fuzzers
        uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master
        with:
          oss-fuzz-project-name: "grpc-gateway"
          fuzz-seconds: 600
          dry-run: false
          language: go
      - name: Upload Crash
        uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b # v4
        if: failure() && steps.build.outcome == 'success'
        with:
          name: artifacts
          path: ./out/artifacts
