---
name: new_client
on:
  push:
    # Per GitHub Action docs, specifying both branch and paths joins them with AND.
    branches:
      - main
    paths:
      - '**/version.go'

permissions:
  contents: read

env:
  GOTOOLCHAIN: local

jobs:
  new_versions:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 2
    - uses: actions/setup-go@v5
      with:
        go-version: 1.25.x
    - name: Find new version files
      id: versions
      # Ignore changes to the internal and root directories.
      # Focus on newly added version.go files generated by GAPIC.
      # Multiple new version files in a single module file will be deduped.
      run: |
        dirs=$(go run ./internal/actions/cmd/changefinder -q --base=HEAD~1 --diff-filter=A --path-filter='*version.go' --content-regex='internal\.Version')
        if [ -z "$dirs" ]
        then
          echo "skip=1" >> $GITHUB_OUTPUT
          echo "No new version files!"
        else
          for d in $dirs; do list=${list},\"${d}\"; done
          echo "new={\"new\":[${list#,}]}" >> $GITHUB_OUTPUT
          echo "skip=" >> $GITHUB_OUTPUT
        fi
    outputs:
      versions: ${{ steps.versions.outputs.new }}
      skip: ${{ steps.versions.outputs.skip }}
  bump_module:
    needs: new_versions
    runs-on: ubuntu-latest
    if: "!needs.new_versions.outputs.skip"
    continue-on-error: true
    strategy:
      matrix: ${{ fromJson(needs.new_versions.outputs.versions) }}
    steps:
    - uses: actions/checkout@v4
    - run: echo >> ${{ matrix.new }}/CHANGES.md
    - uses: googleapis/code-suggester@v4
      id: code_suggester
      env:
        ACCESS_TOKEN: ${{ secrets.YOSHI_CODE_BOT_TOKEN }}  
      with:
        command: pr
        upstream_owner: googleapis
        upstream_repo: google-cloud-go
        description: 'New client(s) generated in ${{ github.event.commits[0].url }}, triggering release.'
        title: 'feat(${{ matrix.new }}): new client(s)'
        message: 'feat(${{ matrix.new }}): new clients'
        primary: 'main'
        branch: release-${{ matrix.new }}-client
        git_dir: '.'
        force: true
