on:
  schedule:
    # daily at 12:30 am
    - cron: '30 0 * * *'
  workflow_dispatch:

name: codegen
jobs:
  discovery:
    uses: googleapis/discovery-artifact-manager/.github/workflows/list-services.yml@master
  batch:
    runs-on: ubuntu-24.04
    needs: discovery
    outputs:
      services: ${{ steps.chunk.outputs.result }}
    steps:
      - uses: actions/github-script@v8
        id: chunk
        with:
          script: |
            console.log('splitting service names list into batches')
            const services = ${{ needs.discovery.outputs.services }}
            const result = {
              "one": [],
              "two": [],
            };
            for (let i = 0; i < services.length; i++) {
              if (!["discovery", "websecurityscanner"].includes(services[i])) {
                j = i < (services.length/2) ? "one" : "two";
                result[j].push(services[i])
              }
            }
            return result
  generate_batch1:
    runs-on: ubuntu-24.04
    needs: batch
    strategy:
      fail-fast: false
      max-parallel: 4
      matrix:
        service: ${{fromJson(needs.batch.outputs.services).one}}
    steps:
      - run: echo generating ${{ matrix.service }}
      - uses: actions/checkout@v5
        with:
          fetch-depth: 1
          path: google-api-php-client-services
      - uses: actions/checkout@v5
        with:
          repository: googleapis/discovery-artifact-manager
          fetch-depth: 1
          path: discovery-artifact-manager
      - uses: actions/setup-python@v6
        with:
          python-version: "3.14"
      - id: find-preferred-versions
        run: |
          versions=$( cat discovery-artifact-manager/discoveries/index.json | jq -r '[.items[] | select(.name == "${{ matrix.service }}") | select(.name == "admin" or .preferred).version] | join(",")' )
          echo "::set-output name=versions::$versions"
      - run: ./google-api-php-client-services/.github/workflows/generate.sh ${{ matrix.service }} ${{ steps.find-preferred-versions.outputs.versions }}
      - uses: googleapis/code-suggester@v5
        env:
          ACCESS_TOKEN: ${{ secrets.YOSHI_CODE_BOT_TOKEN }}
        with:
          command: pr
          upstream_owner: ${{ github.repository_owner }}
          upstream_repo: google-api-php-client-services
          description: 'Generated in GitHub action: https://github.com/${{ github.repository_owner }}/${{ github.repository }}/actions/workflows/codegen.yaml'
          title: 'Regenerate ${{ matrix.service }} client'
          message: 'Regenerate ${{ matrix.service }} client'
          branch: regenerate-${{ matrix.service }}
          git_dir: 'google-api-php-client-services/src'
          primary: main
          force: true
          fork: true

  generate_batch2:
    runs-on: ubuntu-24.04
    needs: batch
    strategy:
      fail-fast: false
      max-parallel: 4
      matrix:
        service: ${{fromJson(needs.batch.outputs.services).two}}
    steps:
      - run: echo generating ${{ matrix.service }}
      - uses: actions/checkout@v5
        with:
          fetch-depth: 1
          path: google-api-php-client-services
      - uses: actions/checkout@v5
        with:
          repository: googleapis/discovery-artifact-manager
          fetch-depth: 1
          path: discovery-artifact-manager
      - uses: actions/setup-python@v6
        with:
          python-version: "3.14"
      - id: find-preferred-versions
        run: |
          versions=$( cat discovery-artifact-manager/discoveries/index.json | jq -r '[.items[] | select(.name == "${{ matrix.service }}") | select(.name == "admin" or .preferred).version] | join(",")' )
          echo "::set-output name=versions::$versions"
      - run: ./google-api-php-client-services/.github/workflows/generate.sh ${{ matrix.service }} ${{ steps.find-preferred-versions.outputs.versions }}
      - uses: googleapis/code-suggester@v5
        env:
          ACCESS_TOKEN: ${{ secrets.YOSHI_CODE_BOT_TOKEN }}
        with:
          command: pr
          upstream_owner: ${{ github.repository_owner }}
          upstream_repo: google-api-php-client-services
          description: 'Generated in GitHub action: https://github.com/${{ github.repository_owner }}/${{ github.repository }}/actions/workflows/codegen.yaml'
          title: 'Regenerate ${{ matrix.service }} client'
          message: 'Regenerate ${{ matrix.service }} client'
          branch: regenerate-${{ matrix.service }}
          git_dir: 'google-api-php-client-services/src'
          primary: main
          force: true
          fork: true