# RSpec preditive pipeline loaded dynamically by script: scripts/generate_rspec_pipeline.rb

include:
  - local: .gitlab/ci/rails/shared.gitlab-ci.yml

default:
  image: $DEFAULT_CI_IMAGE
  <%- if job_tags.any? -%>
  tags:
    <%- job_tags.each do |job_tag| -%>
    - <%= job_tag %>
    <%- end -%>
  <%- end -%>
  # Default job timeout set to 90m https://gitlab.com/gitlab-com/gl-infra/infrastructure/-/issues/10520
  timeout: 90m
  interruptible: true

stages:
  - test
  - post-test

workflow:
  name: '$PIPELINE_NAME'
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      auto_cancel:
        on_new_commit: none
    - when: always

dont-interrupt-me:
  stage: .pre
  image: alpine:latest
  interruptible: false
  variables:
    GIT_STRATEGY: none
  script:
    - echo "This jobs makes sure this pipeline won't be interrupted! See https://docs.gitlab.com/ci/yaml/#interruptible."
  rules:
    - if: $CI_MERGE_REQUEST_EVENT_TYPE == "merged_result" || $CI_MERGE_REQUEST_EVENT_TYPE == "detached"
      when: manual
      allow_failure: true

.base-predictive:
  needs:
  <% if repo_from_artifacts %>
    - pipeline: $PARENT_PIPELINE_ID
      job: clone-gitlab-repo
  <% end %>
    - pipeline: $PARENT_PIPELINE_ID
      job: setup-test-env
    - pipeline: $PARENT_PIPELINE_ID
      job: retrieve-tests-metadata
    - pipeline: $PARENT_PIPELINE_ID
      job: compile-test-assets
    - pipeline: $PARENT_PIPELINE_ID
      job: rspec-predictive:pipeline-generate
  rules:
    - when: always
  variables:
    GLCI_PREDICTIVE_RSPEC_TESTS_MAPPING_ENABLED: "true"

.predictive-system-tests-default:
  # See .rails:rules:system-default-rules for full system tests rules
  rules:
    - if: '$CI_MERGE_REQUEST_LABELS =~ /pipeline:spec-only/'
    - if: '$CI_MERGE_REQUEST_LABELS =~ /pipeline::tier-3/'
      when: never
    - when: always

<% if test_suite_prefix.nil? %>
.base-rspec-predictive:
  extends:
    - .rspec-base-pg17
    - .base-predictive
  variables:
    # We're using the FOSS one here because we want to exclude EE-only ones
    # For EE-only ones, we have EE-only jobs.
    RSPEC_TESTS_FILTER_FILE: "${GLCI_PREDICTIVE_RSPEC_MATCHING_TESTS_FOSS_PATH}"

<% if rspec_files_per_test_level.dig(:migration, :files).size > 0 %>
rspec migration predictive:
  extends:
    - .base-rspec-predictive
    - .rspec-base-migration
<% if rspec_files_per_test_level.dig(:migration, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:migration, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.dig(:background_migration, :files).size > 0 %>
rspec background_migration predictive:
  extends:
    - .base-rspec-predictive
    - .rspec-base-migration
<% if rspec_files_per_test_level.dig(:background_migration, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:background_migration, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.dig(:unit, :files).size > 0 %>
rspec unit predictive:
  extends:
    - .base-rspec-predictive
<% if rspec_files_per_test_level.dig(:unit, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:unit, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.dig(:integration, :files).size > 0 %>
rspec integration predictive:
  extends:
    - .base-rspec-predictive
<% if rspec_files_per_test_level.dig(:integration, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:integration, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.dig(:system, :files).size > 0 %>
rspec system predictive:
  tags:
    - <%= medium_runner_tag %>
  extends:
    - .base-rspec-predictive
  rules:
    - !reference [.predictive-system-tests-default, rules]
<% if rspec_files_per_test_level.dig(:system, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:system, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.values.any? { |config| config[:files].size > 0 } %>
rspec-predictive-artifact-collector:
  stage: post-test
  needs:
<% rspec_files_per_test_level.each do |test_level, config| -%>
<% if config[:files].size > 0 -%>
    - job: rspec <%= test_level %> predictive
      optional: true
      artifacts: true
<% end -%>
<% end -%>
  rules:
    - when: always
  script:
    - echo "Collecting artifacts from predictive pipeline jobs"
  artifacts:
    name: predictive-coverage
    expire_in: 31d
    paths:
      - coverage/
      - rspec/
      - crystalball/
    when: always
<% end %>

<% end %>

<% if test_suite_prefix == 'ee/' %>
.base-rspec-ee-predictive:
  extends:
    - .rspec-ee-base-pg17
    - .base-predictive
  variables:
    RSPEC_TESTS_FILTER_FILE: "${GLCI_PREDICTIVE_RSPEC_MATCHING_TESTS_EE_PATH}"

<% if rspec_files_per_test_level.dig(:migration, :files).size > 0 %>
rspec-ee migration predictive:
  extends:
    - .base-rspec-ee-predictive
    - .rspec-base-migration
<% if rspec_files_per_test_level.dig(:migration, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:migration, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.dig(:background_migration, :files).size > 0 %>
rspec-ee background_migration predictive:
  extends:
    - .base-rspec-ee-predictive
    - .rspec-base-migration
<% if rspec_files_per_test_level.dig(:background_migration, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:background_migration, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.dig(:unit, :files).size > 0 %>
rspec-ee unit predictive:
  extends:
    - .base-rspec-ee-predictive
<% if rspec_files_per_test_level.dig(:unit, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:unit, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.dig(:integration, :files).size > 0 %>
rspec-ee integration predictive:
  extends:
    - .base-rspec-ee-predictive
<% if rspec_files_per_test_level.dig(:integration, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:integration, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.dig(:system, :files).size > 0 %>
rspec-ee system predictive:
  tags:
    - <%= medium_runner_tag %>
  extends:
    - .base-rspec-ee-predictive
  rules:
    - !reference [.predictive-system-tests-default, rules]
<% if rspec_files_per_test_level.dig(:system, :parallelization) > 1 %>
  parallel: <%= rspec_files_per_test_level.dig(:system, :parallelization) %>
<% end %>
<% end %>

<% if rspec_files_per_test_level.values.any? { |config| config[:files].size > 0 } %>
rspec-ee-predictive-artifact-collector:
  stage: post-test
  needs:
<% rspec_files_per_test_level.each do |test_level, config| -%>
<% if config[:files].size > 0 -%>
    - job: rspec-ee <%= test_level %> predictive
      optional: true
      artifacts: true
<% end -%>
<% end -%>
  rules:
    - when: always
  script:
    - echo "Collecting artifacts from EE predictive pipeline jobs"
  artifacts:
    name: ee-predictive-coverage
    expire_in: 31d
    paths:
      - coverage/
      - rspec/
      - crystalball/
    when: always
<% end %>

<% end %>
