name: CI

on:
  workflow_call:

env:
  CGO_LDFLAGS_ALLOW: "-s|-w"
  PKG_CONFIG_LIBDIR: /opt/imgproxy/lib/pkgconfig
  LD_LIBRARY_PATH: /opt/imgproxy/lib
  GOFLAGS: -buildvcs=false

jobs:
  test:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/imgproxy/imgproxy-base:v4.0.2
    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          submodules: true
      - name: Setup cache
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Download mods
        run: go mod download
      - name: Mark git workspace as safe
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: Test
        run: go test ./...

  lint:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/imgproxy/imgproxy-base:v4.0.2
    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          submodules: true
      - name: Setup cache
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Download mods
        run: go mod download
      - name: Lint
        uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0
        with:
          args: --timeout 10m0s
          install-mode: none
        env:
          PKG_CONFIG_LIBDIR: /opt/imgproxy/lib/pkgconfig
          GOFLAGS: -buildvcs=false

  c-lint:
    runs-on: ubuntu-24.04
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          submodules: true
      - uses: cpp-linter/cpp-linter-action@77c390c5ba9c947ebc185a3e49cc754f1558abb5 # v2.18.0
        id: linter
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          style: file
          version: 20
          tidy-checks: "-*" # disable clang-tidy

      - name: Fail fast
        continue-on-error: true # TODO: remove this line in the future
        if: steps.linter.outputs.checks-failed > 0
        run: exit 1
