name: get-changes-triggers

on:
  workflow_call:
    inputs:
      production_paths:
        required: true
        type: string
      unit_test_paths:
        required: false
        type: string
        default: ''
      e2e_test_paths:
        required: false
        type: string
        default: ''
      is_nightly:
        required: true
        type: string
      base_sha:
        required: false
        type: string
        default: ''
      labels:
        required: false
        type: string
        default: '[]'
    outputs:
      trigger_unit_testing:
        description: "if the unit testing job should be triggered"
        value: ${{ jobs.get-triggers.outputs.trigger_unit_testing }}
      trigger_packaging:
        description: "if the packaging job should be triggered"
        value: ${{ jobs.get-triggers.outputs.trigger_packaging }}
      trigger_e2e_testing:
        description: "if the E2E testing job should be triggered"
        value: ${{ jobs.get-triggers.outputs.trigger_e2e_testing }}
      trigger_delivery:
        description: "if the delivery job should be triggered"
        value: ${{ jobs.get-triggers.outputs.trigger_delivery }}

jobs:
  get-triggers:
    runs-on: ubuntu-24.04
    outputs:
      trigger_unit_testing: ${{ github.event_name == 'workflow_dispatch' && steps.get_dispatch_triggers.outputs.trigger_unit_testing || steps.get_triggers.outputs.trigger_unit_testing || 'true' }}
      trigger_packaging: ${{ github.event_name == 'workflow_dispatch' && steps.get_dispatch_triggers.outputs.trigger_packaging || steps.get_triggers.outputs.trigger_packaging || 'true' }}
      trigger_e2e_testing: ${{ github.event_name == 'workflow_dispatch' && steps.get_dispatch_triggers.outputs.trigger_e2e_testing || steps.get_triggers.outputs.trigger_e2e_testing || 'true' }}
      trigger_delivery: ${{ github.event_name == 'workflow_dispatch' && steps.get_dispatch_triggers.outputs.trigger_delivery || steps.get_triggers.outputs.trigger_delivery || 'true' }}

    steps:
      - name: Checkout sources
        if: inputs.base_sha != ''
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-tags: true

      - name: Convert inputs to yaml list
        if: github.event_name == 'pull_request' || inputs.base_sha != ''
        id: inline_inputs
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        env:
          PRODUCTION_PATHS: ${{ inputs.production_paths }}
          UNIT_TEST_PATHS: ${{ inputs.unit_test_paths }}
          E2E_TEST_PATHS: ${{ inputs.e2e_test_paths }}
        with:
          script: |
            const paths = {
              production_paths: process.env.PRODUCTION_PATHS,
              unit_test_paths: process.env.UNIT_TEST_PATHS,
              e2e_test_paths: process.env.E2E_TEST_PATHS
            };

            for (const [key, value] of Object.entries(paths)) {
              const formattedValue = value
                .split('\n')
                .map(line => line.trim())
                .map(line => line.replace(/^("|')|("|')$/g, ''))
                .filter(line => line.length > 0)
                .map(line => `  - "${line}"`)
                .join('\n');
              console.log(`${key}:\n${formattedValue}`);
              core.setOutput(key, formattedValue);
            }

      - name: Get changed files
        if: github.event_name == 'pull_request' || inputs.base_sha != ''
        id: get_changed_files
        uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46.0.5
        with:
          base_sha: ${{ github.event_name == 'pull_request' && ' ' || inputs.base_sha }}
          files_yaml: |
            production:
            ${{ steps.inline_inputs.outputs.production_paths }}
            unit_test:
            ${{ steps.inline_inputs.outputs.unit_test_paths }}
            e2e_test:
            ${{ steps.inline_inputs.outputs.e2e_test_paths }}

      - name: Determine triggers for event ${{ github.event_name }}
        if: github.event_name == 'pull_request' || inputs.base_sha != ''
        id: get_triggers
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        env:
          IS_NIGHTLY: ${{ inputs.is_nightly }}
          HAS_PRODUCTION_CHANGES: ${{ steps.get_changed_files.outputs.production_any_changed }}
          HAS_UNIT_TEST_CHANGES: ${{ steps.get_changed_files.outputs.unit_test_any_changed }}
          HAS_E2E_TEST_CHANGES: ${{ steps.get_changed_files.outputs.e2e_test_any_changed }}
          LABELS: ${{ inputs.labels }}
        with:
          script: |
            try {
              const labels = JSON.parse(process.env.LABELS);
              if (labels.includes('force-unit-tests')) {
                core.info('Label force-unit-tests found: forcing unit testing triggers to true');
                process.env.HAS_UNIT_TEST_CHANGES = 'true';
              }
              if (labels.includes('force-e2e-tests')) {
                core.info('Label force-e2e-tests found: forcing E2E testing trigger to true');
                process.env.HAS_E2E_TEST_CHANGES = 'true';
              }
              if (labels.includes('force-all-tests')) {
                core.info('Label force-all-tests found: forcing all testing triggers to true');
                process.env.HAS_TEST_CHANGES = 'true';
                process.env.HAS_E2E_TEST_CHANGES = 'true';
              }
            } catch (e) {
              core.warning(`Could not parse labels: ${e}`);
            }

            const outputs = {
              'trigger_unit_testing':
                process.env.IS_NIGHTLY === 'true'
                || process.env.HAS_UNIT_TEST_CHANGES === 'true'
                || process.env.HAS_PRODUCTION_CHANGES === 'true',
              'trigger_packaging':
                process.env.IS_NIGHTLY === 'true'
                || process.env.HAS_PRODUCTION_CHANGES === 'true'
                || process.env.HAS_E2E_TEST_CHANGES === 'true',
              'trigger_e2e_testing':
                process.env.IS_NIGHTLY === 'true'
                || process.env.HAS_PRODUCTION_CHANGES === 'true'
                || process.env.HAS_E2E_TEST_CHANGES === 'true',
              'trigger_delivery':
                process.env.IS_NIGHTLY === 'true'
                || process.env.HAS_PRODUCTION_CHANGES === 'true',
            };

            for (const [key, value] of Object.entries(outputs)) {
              console.log(`${key}=${value}`);
              core.setOutput(key, value ? 'true' : 'false');
            }
      - name: Determine triggers for event ${{ github.event_name }}
        if: github.event_name == 'workflow_dispatch'
        id: get_dispatch_triggers
        uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
        env:
          LABELS: ${{ inputs.labels }}
        with:
          script: |
            let triggerUnitTesting = true;
            let triggerE2eTesting = true;

            try {
              const labels = JSON.parse(process.env.LABELS);
              if (labels.includes('skip-unit-tests')) {
                core.info('Label skip-unit-tests found: forcing unit testing trigger to false');
                triggerUnitTesting = false;
              }
              if (labels.includes('skip-e2e-tests')) {
                core.info('Label skip-e2e-tests found: forcing E2E testing trigger to false');
                triggerE2eTesting = false;
              }
              if (labels.includes('skip-all-tests')) {
                core.info('Label skip-all-tests found: forcing all testing triggers to false');
                triggerUnitTesting = false;
                triggerE2eTesting = false;
              }
            } catch (e) {
              core.warning(`Could not parse labels: ${e}`);
            }

            core.setOutput('trigger_unit_testing', triggerUnitTesting ? 'true' : 'false');
            core.setOutput('trigger_packaging', 'true');
            core.setOutput('trigger_e2e_testing', triggerE2eTesting ? 'true' : 'false');
            core.setOutput('trigger_delivery', 'true');
