name: Update protobufs and regenerate classes
on: workflow_dispatch

permissions: read-all

jobs:
  update-protobufs:
    runs-on: ubuntu-latest
    permissions: # Needed for peter-evans/create-pull-request.
      contents: write
      pull-requests: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          submodules: true

      - name: Update submodule
        if: ${{ github.ref_name == 'master' || github.ref_name == 'develop' }}
        working-directory: protobufs
        env:
          # Use the branch that triggered the workflow as the protobuf branch.
          GIT_BRANCH: ${{ github.ref_name }}
        run: |
          git fetch --prune origin $GIT_BRANCH
          git checkout origin/$GIT_BRANCH

      - name: Download nanopb
        run: |
          wget https://github.com/nanopb/nanopb/releases/download/nanopb-0.4.9.1/nanopb-0.4.9.1-linux-x86.tar.gz
          tar xvzf nanopb-0.4.9.1-linux-x86.tar.gz
          mv nanopb-0.4.9.1-linux-x86 nanopb-0.4.9

      - name: Re-generate protocol buffers
        run: |
          ./bin/regen-protos.sh

      - name: Create pull request
        uses: peter-evans/create-pull-request@v8
        with:
          branch: create-pull-request/update-protobufs-${{ github.ref_name }}
          labels: submodules
          title: Update protobufs and classes
          commit-message: Update protobufs
          add-paths: |
            protobufs
            src/mesh
