name: Protobuf
on:
  push:
    tags:
      - v*
    branches:
      - master
    paths:
      - "api/**"
      - "Makefile"
      - "js/Makefile"
      - "go/Makefile"
      - "config/**"
      - "docs/Makefile"
      - ".github/workflows/protobuf.yml"
      - "js/packages/i18n/locale/en/messages.json"
      - "**/gen.sum"
      - "**.pb.go"
      - "**.gen.go"
      - "**.gen.graphql"
      - "**.gen.js"
      - "**.gen.ts"
      - "**.gen.tsx"
      - "**.gen.yml"
      - "**.pb.d.ts"
      - "**.pb.go"
      - "**.pb.js"
      - "**/pb_test.go"
      - "**/docs/*/api.md"
      - "**/gen.sum"
      - "**/go.mod"
      - "**/go.sum"
      - "**/yarn.lock"
      - "**/package.json"
      - "**/Podfile.lock"
  pull_request:
    paths:
      - "api/**"
      - "Makefile"
      - "js/Makefile"
      - "go/Makefile"
      - "config/**"
      - "docs/Makefile"
      - ".github/workflows/protobuf.yml"
      - "js/packages/i18n/locale/en/messages.json"
      - "**/gen.sum"
      - "**.pb.go"
      - "**.gen.go"
      - "**.gen.graphql"
      - "**.gen.js"
      - "**.gen.ts"
      - "**.gen.tsx"
      - "**.gen.yml"
      - "**.pb.d.ts"
      - "**.pb.go"
      - "**.pb.js"
      - "**/pb_test.go"
      - "**/docs/*/api.md"
      - "**/gen.sum"
      - "**/go.mod"
      - "**/go.sum"
      - "**/yarn.lock"
      - "**/package.json"
      - "**/Podfile.lock"
jobs:
  gen-go-and-docs:
    name: Generate go protobuf and docs
    runs-on: ubuntu-latest
    container: bertytech/protoc:32
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Mark repository as safe
        run: git config --global --add safe.directory $GITHUB_WORKSPACE

      - name: Unshallow
        run: git fetch --prune --unshallow

      - name: Remove lock files
        run: find . -name gen.sum ! -path './js/*' -delete

      - name: Load variables from file
        uses: antifree/json-to-variables@v1.0.1
        with:
          filename: .github/workflows/utils/variables.json

      - name: Setup asdf
        uses: asdf-vm/actions/setup@9cd779f40fe38688dd19505ccbc4eaaf018b44e7
        with:
          asdf_version: 0.16.7

      - name: Setup go
        run: |
          asdf plugin add golang
          asdf install golang
          echo "go_version=$(asdf current golang | xargs | cut -d ' ' -f 6)" >> $GITHUB_ENV

      - name: Setup jq
        run: |
          asdf plugin add jq
          asdf install jq

      - name: Cache go modules
        uses: actions/cache@v4
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-

      - name: Fetch go modules
        run: go mod download

      - name: Generate docs
        working-directory: docs
        run: make generate_local

      - name: Generate go protobuf
        working-directory: go
        run: |
          make generate_local
          git checkout ../go.mod ../go.sum

      - name: Check diff
        run: |
          git status | cat
          git diff -w | cat
          git diff-index -w --quiet HEAD --

      - name: Prepare openapi documentation
        working-directory: docs
        run: make openapi.prepare

      - name: Setup apiary
        run: |
          apt-get update
          apt-get install -y --no-install-recommends ruby-dev g++ && gem install apiaryio

      - name: Upload API docs to apiary.io
        env:
          APIARY_API_KEY: "${{ secrets.APIARY_API_KEY }}"
        if: ${{ env.APIARY_API_KEY != 0 }}
        run: |
          apiary publish --api-name=bertymessenger --path="docs/.tmp/openapi/bertymessenger.swagger.json" || true

  gen-js:
    name: Generate js protobuf
    runs-on: ubuntu-latest
    container: bertytech/protoc:32
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Mark repository as safe
        run: git config --global --add safe.directory $GITHUB_WORKSPACE

      - name: Load variables from file
        uses: antifree/json-to-variables@v1.0.1
        with:
          filename: .github/workflows/utils/variables.json

      - name: Setup asdf
        uses: asdf-vm/actions/setup@9cd779f40fe38688dd19505ccbc4eaaf018b44e7
        with:
          asdf_version: 0.16.7

      - name: Setup yq
        run: |
          asdf plugin add yq
          asdf install yq

      - name: Setup node
        working-directory: js
        run: |
          asdf plugin add nodejs
          asdf install nodejs
          echo "node_version=$(asdf current nodejs | xargs | cut -d ' ' -f 6)" >> $GITHUB_ENV

      - name: Setup yarn
        working-directory: js
        run: |
          asdf plugin add yarn
          asdf install yarn

      - name: Setup go
        run: |
          asdf plugin add golang
          asdf install golang
          echo "go_version=$(asdf current golang | xargs | cut -d ' ' -f 6)" >> $GITHUB_ENV

      - name: Cleanup js gen
        working-directory: js
        run: make gen.clean

      - name: Cache node modules
        uses: actions/cache@v4
        with:
          path: js/node_modules
          key: ${{ runner.OS }}-node-${{ env.node_version }}-${{ env.json_cache-versions_node }}-${{ hashFiles('js/yarn.lock') }}
          restore-keys: ${{ runner.OS }}-node-${{ env.node_version }}-${{ env.json_cache-versions_node }}-

      - name: Cache go modules
        uses: actions/cache@v4
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-

      - name: Fetch node modules
        working-directory: js
        run: make node_modules

      - name: Fetch go modules
        run: go mod download

      - name: Generate js protobuf
        working-directory: js
        run: |
          make _gen.pbjs
          make _write_gen_sum
          rm -f gen.sum && make generate

      - name: Check diff
        run: |
          go mod tidy
          git status | cat
          git diff -w | cat
          git diff-index -w --quiet HEAD --
