---
name: "Called update for API change"
on:
  repository_dispatch:
    types: ["api_update"]
jobs:
  test:
    name: "Create PR for API update"
    timeout-minutes: 10
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v4
      - uses: bahmutov/npm-install@v1
        with:
          useLockFile: false
      - name: "Update Buf Script"
        id: buf-update
        uses: authzed/actions/buf-api-update@main
        with:
          api-commit: "${{ github.event.client_payload.BUFTAG }}"
          spec-path: buf.gen.yaml
          file-format: "buf-gen-yaml"
      - name: "Output update status"
        env:
          UPDATED_STATUS: ${{ steps.buf-update.outputs.updated }}
        run: |
          echo "Update status: $UPDATED_STATUS"
      - name: "Install buf"
        uses: "bufbuild/buf-setup-action@v1.50.0"
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
        if: steps.buf-update.outputs.updated == 'true'
      - name: "Run buf generate"
        if: steps.buf-update.outputs.updated == 'true'
        run: "yarn run buf"
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v7.0.8
        if: steps.buf-update.outputs.updated == 'true'
        with:
          delete-branch: "true"
          title: "Update API to ${{ github.event.client_payload.BUFTAG }}"
          # https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#triggering-further-workflow-runs
          draft: "always-true"
          branch: "api-change/${{ github.event.client_payload.BUFTAG }}"
          base: "main"
          token: ${{ secrets.GITHUB_TOKEN }}
