---
name: Update for API change
on:
  workflow_dispatch:
    inputs:
      buftag:
        description: Tag or commit from https://buf.build/authzed/api/tags/main
        required: true
        type: string
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
      - name: "Update Buf Script"
        id: buf-update
        uses: authzed/actions/buf-api-update@main
        with:
          api-commit: ${{ inputs.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: "buf generate"
      - 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 ${{ inputs.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/${{ inputs.buftag }}
          base: "main"
          token: ${{ secrets.GITHUB_TOKEN }}
