name: Protobuf
# Protobuf runs buf (https://buf.build/) lint and check-breakage
# This workflow always runs for PRs and merge groups; jobs only execute when relevant proto files changed.
on:
  pull_request:
  merge_group:

permissions:
  contents: read

jobs:
  detect-proto-changes:
    runs-on: depot-ubuntu-22.04-4
    outputs:
      sdk_proto_diff: ${{ steps.sdk_proto_diff.outputs.diff }}
      poa_proto_diff: ${{ steps.poa_proto_diff.outputs.diff }}
      base_branch: ${{ steps.base_branch.outputs.branch }}
    steps:
      - uses: actions/checkout@v6
      - uses: technote-space/get-diff-action@v6.1.2
        id: sdk_proto_diff
        with:
          PATTERNS: |
            proto/**
      - uses: technote-space/get-diff-action@v6.1.2
        id: poa_proto_diff
        with:
          PATTERNS: |
            enterprise/poa/proto/**
      - id: base_branch
        run: |
          if [ "${{ github.event_name }}" = "merge_group" ]; then
            branch="${{ github.event.merge_group.base_ref }}"
            echo "branch=${branch#refs/heads/}" >> "$GITHUB_OUTPUT"
          else
            echo "branch=${{ github.event.pull_request.base.ref }}" >> "$GITHUB_OUTPUT"
          fi

  lint:
    needs: detect-proto-changes
    if: needs.detect-proto-changes.outputs.sdk_proto_diff == 'true'
    runs-on: depot-ubuntu-22.04-4
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v6
      - uses: bufbuild/buf-setup-action@v1.50.0
      - uses: bufbuild/buf-lint-action@v1
        with:
          input: "proto"

  break-check:
    needs: detect-proto-changes
    if: needs.detect-proto-changes.outputs.sdk_proto_diff == 'true'
    runs-on: depot-ubuntu-22.04-4
    steps:
      - uses: actions/checkout@v6
      - uses: bufbuild/buf-setup-action@v1.50.0
      - uses: bufbuild/buf-breaking-action@v1
        with:
          input: "proto"
          against: "https://github.com/${{ github.repository }}.git#branch=${{ needs.detect-proto-changes.outputs.base_branch }},ref=HEAD~1,subdir=proto"

  lint-poa:
    needs: detect-proto-changes
    if: needs.detect-proto-changes.outputs.poa_proto_diff == 'true'
    runs-on: depot-ubuntu-22.04-4
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v6
      - uses: bufbuild/buf-setup-action@v1.50.0
      - uses: bufbuild/buf-lint-action@v1
        with:
          input: "enterprise/poa/proto"

  break-check-poa:
    needs: detect-proto-changes
    if: needs.detect-proto-changes.outputs.poa_proto_diff == 'true'
    runs-on: depot-ubuntu-22.04-4
    steps:
      - uses: actions/checkout@v6
      - uses: bufbuild/buf-setup-action@v1.50.0
      - uses: bufbuild/buf-breaking-action@v1
        with:
          input: "enterprise/poa/proto"
          against: "https://github.com/${{ github.repository }}.git#branch=${{ needs.detect-proto-changes.outputs.base_branch }},ref=HEAD~1,subdir=enterprise/poa/proto"
