include:
  - "task-templates.yml"
  - project: 'redhat/centos-stream/ci-cd/osci-pipelines'
    file: 'rog/gitlabci-prototype.yaml'
    ref: 'main'
    rules:
      - if: '$CI_MERGE_REQUEST_PROJECT_PATH =~ /^redhat.centos-stream.rpms/ || $CI_PROJECT_PATH =~ /^redhat.centos-stream.rpms/ ||
            $CI_MERGE_REQUEST_PROJECT_PATH =~ /^redhat.rhel.rpms/ || $CI_PROJECT_PATH =~ /^redhat.rhel.rpms/'
  - project: 'redhat/centos-stream/ci-cd/osci-pipelines'
    file: 'rog/gitlabci-tests-prototype.yaml'
    ref: 'main'
    rules:
      - if: '$CI_MERGE_REQUEST_PROJECT_PATH =~ /^redhat.centos-stream.tests/ || $CI_PROJECT_PATH =~ /^redhat.centos-stream.tests/ ||
            $CI_MERGE_REQUEST_PROJECT_PATH =~ /^redhat.rhel.tests/ || $CI_PROJECT_PATH =~ /^redhat.rhel.tests/'

workflow:
  rules:
    # First, check if people have permissions and tell them if they don't.
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_PROJECT_ID != $CI_MERGE_REQUEST_PROJECT_ID'
      variables:
        PIPELINE_TYPE: 'external'
    # RoG rules
    - if: '($CI_MERGE_REQUEST_PROJECT_PATH =~ /^redhat.centos-stream.rpms/ || $CI_PROJECT_PATH =~ /^redhat.centos-stream.rpms/) &&
          $CI_PIPELINE_SOURCE == "merge_request_event" &&
          (
            $CI_MERGE_REQUEST_LABELS =~ /(^|,)feature::rhel-on-gitlab::enabled(,|$)/ ||
            (
              $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^c\d+s(-aie-nv|-backport-test(-.*)?)?$/ &&
              $CI_PROJECT_NAME != "kernel" && $CI_PROJECT_NAME != "kernel-rt"
            )
          )'
      variables:
        RHEL_OR_STREAM: 'centos-stream'
        NAMESPACE: 'rpms'
    - if: '($CI_MERGE_REQUEST_PROJECT_PATH =~ /^redhat.rhel.rpms/ || $CI_PROJECT_PATH =~ /^redhat.rhel.rpms/) &&
          $CI_PIPELINE_SOURCE == "merge_request_event" &&
          (
            $CI_MERGE_REQUEST_LABELS =~ /(^|,)feature::rhel-on-gitlab::enabled(,|$)/ ||
            (
              $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^(ext-)?rhel-\d+(\.\d+){0,2}(-main)?$/ &&
              $CI_PROJECT_NAME != "kernel" && $CI_PROJECT_NAME != "kernel-rt"
            )
          )'
      variables:
        RHEL_OR_STREAM: 'rhel'
        NAMESPACE: 'rpms'
    - if: '($CI_MERGE_REQUEST_PROJECT_PATH =~ /^redhat.centos-stream.tests/ || $CI_PROJECT_PATH =~ /^redhat.centos-stream.tests/) &&
          $CI_PIPELINE_SOURCE == "merge_request_event" &&
          (
            $CI_MERGE_REQUEST_LABELS =~ /(^|,)feature::rhel-on-gitlab::enabled(,|$)/ ||
            (
              $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^(main|master)$/ &&
              $CI_PROJECT_NAME == "osci-internal-test-package"
            )
          )'
      variables:
        RHEL_OR_STREAM: 'centos-stream'
        NAMESPACE: 'tests'
    - if: '($CI_MERGE_REQUEST_PROJECT_PATH =~ /^redhat.rhel.tests/ || $CI_PROJECT_PATH =~ /^redhat.rhel.tests/) &&
           $CI_PIPELINE_SOURCE == "merge_request_event" &&
           $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^(main|master)$/'
      variables:
        RHEL_OR_STREAM: 'rhel'
        NAMESPACE: 'tests'
    # Legacy non-RoG rule
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_PROJECT_ID == $CI_MERGE_REQUEST_PROJECT_ID'
      variables:
        PIPELINE_TYPE: 'internal'
    # Only run push pipeline in the target repo for certain branches, or the push action in the MR triggers a push pipeline
    - if: '$CI_PIPELINE_SOURCE == "push" &&
           $CI_PROJECT_PATH =~ /^redhat.centos-stream.rpms/ &&
           $CI_COMMIT_BRANCH =~ /^c\d+s(-aie-nv)?$/'
      variables:
        RHEL_OR_STREAM: 'centos-stream'
        NAMESPACE: 'rpms'
    - if: '$CI_PIPELINE_SOURCE == "push" &&
           $CI_PROJECT_PATH =~ /^redhat.centos-stream.tests/ &&
           $CI_COMMIT_BRANCH =~ /^(main|master)$/'
      variables:
        RHEL_OR_STREAM: 'centos-stream'
        NAMESPACE: 'tests'
    - if: '$CI_PIPELINE_SOURCE == "push" &&
           $CI_PROJECT_PATH =~ /^redhat.rhel.rpms/ &&
           $CI_COMMIT_BRANCH =~ /^(ext-)?rhel-\d+(\.\d+){0,2}(-main)?$/ &&
           $CI_COMMIT_BEFORE_SHA != "0000000000000000000000000000000000000000"'
      variables:
        RHEL_OR_STREAM: 'rhel'
        NAMESPACE: 'rpms'
    - if: '$CI_PIPELINE_SOURCE == "push" &&
           $CI_PROJECT_PATH =~ /^redhat.rhel.tests/ &&
           $CI_COMMIT_BRANCH =~ /^(main|master)$/ &&
           $CI_COMMIT_BEFORE_SHA != "0000000000000000000000000000000000000000"'
      variables:
        RHEL_OR_STREAM: 'rhel'
        NAMESPACE: 'tests'

.exclude_rog_components: &exclude_rog_components
  - if: '$RHEL_OR_STREAM'
    when: never


check-access:
  image: alpine:latest
  script:
    - |
      >&2 cat << EOF
      The user that initiated this pipeline does not have permissions to
      execute code within the Red Hat intranet. Please ask the Red Hat
      package maintainer to trigger the pipeline for you.

      If you are a Red Hat employee getting this message, make sure you're
      a member of the "devel" and "bugzilla-redhat" groups, and are logged into
      SSO. Use the https://red.ht/GitLabSSO link to refresh your access. You
      should now be able to start a new pipeline.


      To start a new pipeline, visit the MR's "Pipelines" page at

          $CI_MERGE_REQUEST_PROJECT_URL/-/merge_requests/$CI_MERGE_REQUEST_IID/pipelines

      and click the "Run pipeline" button on the right side.
      EOF
      exit 1
  rules:
    - *exclude_rog_components
    - if: '$PIPELINE_TYPE == "external" && $CI_PROJECT_NAME =~ /^(kernel|kernel-rt|systemd)$/'
      variables:
        KUBERNETES_HELPER_MEMORY_LIMIT: "768Mi"
    - if: '$PIPELINE_TYPE == "external"'

check-tickets:
  extends: .check-tickets
  rules:
    - *exclude_rog_components
    - if: '$PIPELINE_TYPE == "internal" &&
          $CI_PROJECT_NAME =~ /^(kernel|kernel-rt|systemd)$/ &&
          ($CI_MERGE_REQUEST_PROJECT_PATH =~ /(modules|rpms)/ || $CI_PROJECT_PATH =~ /(modules|rpms)/)'
      variables:
        KUBERNETES_HELPER_MEMORY_LIMIT: "768Mi"
    - if: '$PIPELINE_TYPE == "internal" &&
          ($CI_MERGE_REQUEST_PROJECT_PATH =~ /(modules|rpms)/ || $CI_PROJECT_PATH =~ /(modules|rpms)/)'
  tags:
    - redhat
    - distrobaker
