stages:
  - check
  - generate
  - triggers
  - deploy

default:
  image: registry.gitlab.com/gitlab-org/gitlab-build-images:gitlab-qa-ruby-2.7
  tags:
    - gitlab-org
  cache:
    key:
      files:
        - Gemfile.lock
        - gitlab-qa.gemspec
    paths:
      - vendor/ruby
  before_script:
    - bundle version
    - bundle config path vendor
    - bundle install --jobs=$(nproc) --retry=3 --quiet && bundle check
    - export LANG=C.UTF-8

workflow:
  rules:
    # For merge requests, create a pipeline.
    - if: '$CI_MERGE_REQUEST_IID'
    # For the default branch, create a pipeline (this includes on schedules, pushes, merges, etc.).
    - if: '$CI_COMMIT_BRANCH == "master"'
    # For tags, create a pipeline.
    - if: '$CI_COMMIT_TAG'
    # For triggers from GitLab MR pipelines (and pipelines from other projects), create a pipeline
    - if: '$CI_PIPELINE_SOURCE == "pipeline"'
    # When using Run pipeline button in the GitLab UI, from the project’s CI/CD > Pipelines section, create a pipeline.
    - if: '$CI_PIPELINE_SOURCE == "web"'

.check-base:
  stage: check
  script:
    - bundle exec $CI_JOB_NAME --color

rubocop:
  extends: .check-base

rspec:
  extends: .check-base

prepare:
  stage: generate
  extends: .rules:never-on-version-change-or-tag
  script:
    - 'export QA_IMAGE_TAG=${TOP_UPSTREAM_SOURCE_REF:=master} && echo "QA_IMAGE_TAG=$QA_IMAGE_TAG" >> prep_vars.env'
    - 'echo $QA_IMAGE_TAG'
  artifacts:
    reports:
      dotenv: prep_vars.env
    paths:
      - prep_vars.env

generate-jobs:
  stage: generate
  extends: .rules:never-on-version-change-or-tag
  script:
    # TOP_UPSTREAM_DEFAULT_BRANCH is the default branch name of the original project that triggered a pipeline in this project.
    - 'export TOP_UPSTREAM_DEFAULT_BRANCH=${TOP_UPSTREAM_DEFAULT_BRANCH:=master} && echo "TOP_UPSTREAM_DEFAULT_BRANCH=$TOP_UPSTREAM_DEFAULT_BRANCH" >> variables.env'
    - 'echo "RELEASE=$RELEASE" >> variables.env'
    - 'echo "GITLAB_QA_OPTIONS=$GITLAB_QA_OPTIONS" >> variables.env'
    - 'echo "QA_TESTS=$QA_TESTS" >> variables.env'
    - 'echo "KNAPSACK_GENERATE_REPORT=$KNAPSACK_GENERATE_REPORT" >> variables.env'
    - 'echo "TOP_UPSTREAM_SOURCE_JOB=$TOP_UPSTREAM_SOURCE_JOB" >> variables.env'
    - 'echo "TOP_UPSTREAM_SOURCE_REF=$TOP_UPSTREAM_SOURCE_REF" >> variables.env'
    - 'echo "DISABLE_RELATING_FAILURE_ISSUES=$DISABLE_RELATING_FAILURE_ISSUES" >> variables.env'
    - 'echo "TOP_UPSTREAM_SOURCE_PROJECT=$TOP_UPSTREAM_SOURCE_PROJECT" >> variables.env'
    - 'echo "TOP_UPSTREAM_SOURCE_SHA=$TOP_UPSTREAM_SOURCE_SHA" >> variables.env'
    - 'echo "NOTIFY_CHANNEL=$NOTIFY_CHANNEL" >> variables.env'
    - 'echo "QA_IMAGE=$QA_IMAGE" >> variables.env'
    - 'echo "EE_LICENSE=$EE_LICENSE" >> variables.env'
    - 'echo "TOP_UPSTREAM_MERGE_REQUEST_IID=$TOP_UPSTREAM_MERGE_REQUEST_IID" >> variables.env'
    - 'echo "ALLURE_JOB_NAME=$ALLURE_JOB_NAME" >> variables.env'

    - ./scripts/generate-qa-jobs.rb
    - cat generated-qa-jobs.yml
  artifacts:
    reports:
      dotenv: variables.env
    paths:
      - generated-qa-jobs.yml
      - variables.env
    expire_in: 2 days
  needs:
    - job: generate-count-example-files
      artifacts: true

generate-count-example-files:
  stage: generate
  extends: .rules:never-on-version-change-or-tag
  image:
    name: $CI_REGISTRY/gitlab-org/gitlab/gitlab-ee-qa:$QA_IMAGE_TAG
    entrypoint: [ "" ]
  variables:
    QA_EXPORT_TEST_METRICS: "false"
  needs:
    - prepare
  before_script:
    - cd /home/gitlab/qa
  script:
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Integration::Mattermost http://dummy.test http://mattermost.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Integration::ServicePingDisabled http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa QA::EE::Scenario::Test::Geo --count-examples-only --primary-address http://dummy1.test --primary-name gitlab-primary --secondary-address http://dummy2.test --secondary-name gitlab-secondary --without-setup
    - bundle exec bin/qa Test::Integration::LDAPNoTLS http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Integration::LDAPTLS http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Integration::LDAPNoServer http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Integration::InstanceSAML http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa QA::EE::Scenario::Test::Integration::GroupSAML http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Integration::RegistryTLS http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Integration::Registry http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa QA::EE::Scenario::Test::Integration::Elasticsearch http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Integration::SMTP http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Integration::RegistryWithCDN http://dummy.test --count-examples-only -- $QA_TESTS
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS --tag repository_storage
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS --tag packages
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS --tag integrations
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS --tag actioncable
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS --tag mtls
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS --tag jira
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS --tag can_use_large_setup
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS --tag cloud_activation
    - bundle exec bin/qa Test::Instance::All http://dummy.test --count-examples-only -- $QA_TESTS --tag object_storage

    - cp -r no_of_examples $CI_PROJECT_DIR
  artifacts:
    paths:
      - no_of_examples
  allow_failure: true

trigger-generated-jobs:
  stage: triggers
  extends: .rules:never-on-version-change-or-tag
  needs: [ generate-jobs ]
  variables:
    # Variables used in rules also need to be passed down using the `variables` section in addition to dotenv file
    # due to this bug: https://gitlab.com/gitlab-org/gitlab/-/issues/352828. For other variables, duplicate declaration here is needed
    # as well due to not using `needs` and `artifacts: true` in child pipelines.
    RELEASE: $RELEASE
    GITLAB_QA_OPTIONS: $GITLAB_QA_OPTIONS
    QA_TESTS: $QA_TESTS
    KNAPSACK_GENERATE_REPORT: $KNAPSACK_GENERATE_REPORT
    TOP_UPSTREAM_SOURCE_JOB: $TOP_UPSTREAM_SOURCE_JOB
    TOP_UPSTREAM_SOURCE_REF: $TOP_UPSTREAM_SOURCE_REF
    TOP_UPSTREAM_DEFAULT_BRANCH: $TOP_UPSTREAM_DEFAULT_BRANCH
    DISABLE_RELATING_FAILURE_ISSUES: $DISABLE_RELATING_FAILURE_ISSUES
    TOP_UPSTREAM_SOURCE_PROJECT: $TOP_UPSTREAM_SOURCE_PROJECT
    TOP_UPSTREAM_SOURCE_SHA: $TOP_UPSTREAM_SOURCE_SHA
    NOTIFY_CHANNEL: $NOTIFY_CHANNEL
    QA_IMAGE: $QA_IMAGE
    EE_LICENSE: $EE_LICENSE
    TOP_UPSTREAM_MERGE_REQUEST_IID: $TOP_UPSTREAM_MERGE_REQUEST_IID
    ALLURE_JOB_NAME: $ALLURE_JOB_NAME
  trigger:
    strategy: depend
    include:
      - artifact: generated-qa-jobs.yml
        job: generate-jobs

include:
  - local: .gitlab/ci/rules.gitlab-ci.yml
  - project: 'gitlab-org/quality/pipeline-common'
    file:
      - '/ci/gem-release.yml'
      - '/ci/danger-review.yml'

danger-review:
  stage: check