name: 'Start Flex build'
on:
  push:
    branches:
      - edge
      - '*internal-release*'
      - 'release*'
      - 'chore_release*'
    tags:
      - ot3@*
      - v*
  pull_request:
    types:
      - opened
      - synchronize
      - labeled

jobs:
  handle-push:
    runs-on: 'ubuntu-latest'
    if: github.event_name == 'push'
    name: "Start a Flex build for a branch/tag push"
    steps:
      - name: 'start build'
        uses: octokit/request-action@v2.x
        env:
          GITHUB_TOKEN: ${{ secrets.OT3_BUILD_CROSSREPO_ACCESS }}
        with:
          route: POST /repos/{owner}/{repo}/actions/workflows/{workflow-id}/dispatches
          owner: opentrons
          repo: oe-core
          workflow-id: build-ot3-actions.yml
          ref: main
          inputs: |
            {
              "oe-core-ref": "-",
              "monorepo-ref": "${{ github.ref }}",
              "infra-stage": "stage-prod"
            }


  handle-pr:
    runs-on: 'ubuntu-latest'
    if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == 'Opentrons/opentrons' && contains(github.event.pull_request.labels.*.name, 'ot3-build')
    name: "Start a Flex build for a requested PR"
    steps:
      - name: 'start build'
        uses: octokit/request-action@v2.x
        env:
          GITHUB_TOKEN: ${{ secrets.OT3_BUILD_CROSSREPO_ACCESS }}
        with:
          route: POST /repos/{owner}/{repo}/actions/workflows/{workflow-id}/dispatches
          owner: opentrons
          repo: oe-core
          workflow-id: build-ot3-actions.yml
          ref: main
          inputs: |
            {
              "oe-core-ref": "-",
              "monorepo-ref": "refs/heads/${{ github.head_ref }}",
              "infra-stage": "stage-prod"
            }