####################
# Changes patterns #
####################

.code-backstage-patterns: &code-backstage-patterns
  - ".gitlab-ci.yml"
  - ".golangci.yml"
  - ".gitlab/ci/**/*"
  - ".gitattributes"
  - "Makefile*"
  - "**/*.go"
  - "{ci,dockerfiles,packaging,scripts,tests}/**/*"
  - "**/testdata/**/*"
  - "go.*"
  - "tmp/gitlab-test/**"
  - "VERSION"

.docs-patterns: &docs-patterns
  - ".vale.ini"
  - ".markdownlint.yml"
  - "docs/**/*"
  - "scripts/lint-docs"


##############
# Conditions #
##############

.if-not-canonical-namespace: &if-not-canonical-namespace
  if: '$CI_PROJECT_NAMESPACE !~ /^gitlab-org($|\/)/'

.if-default-branch: &if-default-branch
  if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'

.if-release-candidate-tag: &if-release-candidate-tag
  if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+/'

.if-stable-release-tag: &if-stable-release-tag
  if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'

.if-merge-request-pipeline: &if-merge-request-pipeline
  if: $CI_PIPELINE_SOURCE == "merge_request_event"

.if-runner-merge-request-pipeline: &if-runner-merge-request-pipeline
  if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_PROJECT_PATH == "gitlab-org/gitlab-runner"

.if-runner-security-merge-request-pipeline: &if-runner-security-merge-request-pipeline
  if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_PROJECT_PATH == "gitlab-org/security/gitlab-runner"

.if-runner-default-branch: &if-runner-default-branch
  if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PROJECT_PATH == "gitlab-org/gitlab-runner"

.if-runner-or-security-runner-default-branch: &if-runner-or-security-runner-default-branch
  if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && ($CI_PROJECT_PATH == "gitlab-org/gitlab-runner" || $CI_PROJECT_PATH == "gitlab-org/security/gitlab-runner")

.if-runner-or-security-runner-stable-ref: &if-runner-or-security-runner-stable-ref
  if: $CI_COMMIT_REF_NAME =~ /\A[0-9]+-[0-9]+-stable\z/ && ($CI_PROJECT_PATH == "gitlab-org/gitlab-runner" || $CI_PROJECT_PATH == "gitlab-org/security/gitlab-runner")

.if-runner-release-ref: &if-runner-release-ref
  if: $CI_COMMIT_REF_NAME =~ /\Av[0-9]+\.[0-9]+\.[0-9]+(-rc[0-9]+)?\z/ && $CI_PROJECT_PATH == "gitlab-org/gitlab-runner"

.if-runner-stable-release-ref: &if-runner-stable-release-ref
  if: $CI_COMMIT_REF_NAME =~ /\Av[0-9]+\.[0-9]+\.[0-9]+?\z/ && $CI_PROJECT_PATH == "gitlab-org/gitlab-runner"

.if-security-runner-release-ref: &if-security-runner-release-ref
  if: $CI_COMMIT_REF_NAME =~ /\Av[0-9]+\.[0-9]+\.[0-9]+?\z/ && $CI_PROJECT_PATH == "gitlab-org/security/gitlab-runner"

.if-runner-bleeding-edge-release-ref: &if-runner-bleeding-edge-release-ref
  if: $CI_COMMIT_REF_NAME =~ /\Av[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+\z/ && $CI_PROJECT_PATH == "gitlab-org/gitlab-runner"

.if-runner-or-security-bleeding-edge-release-ref: &if-runner-or-security-bleeding-edge-release-ref
  if: $CI_COMMIT_REF_NAME =~ /\Av[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+\z/ && ($CI_PROJECT_PATH == "gitlab-org/gitlab-runner" || $CI_PROJECT_PATH == "gitlab-org/security/gitlab-runner")

########################
# Default branch rules #
########################

.rules:default-branch-only:no_docs:
  rules:
  - <<: *if-runner-or-security-runner-default-branch
    changes: *code-backstage-patterns

.rules:default-branch-only:no_docs:always:
  rules:
  - <<: *if-runner-or-security-runner-default-branch
    changes: *code-backstage-patterns
    when: always

#######################
# Merge Request rules #
#######################

.rules:merge_request_pipelines:
  rules:
  - <<: *if-merge-request-pipeline
  - <<: *if-runner-or-security-runner-default-branch
  - <<: *if-runner-or-security-runner-stable-ref
  - <<: *if-runner-release-ref
  - <<: *if-security-runner-release-ref

.rules:merge_request_pipelines:no_docs:
  rules:
  - <<: *if-merge-request-pipeline
    changes: *code-backstage-patterns
  - <<: *if-runner-or-security-runner-default-branch
    changes: *code-backstage-patterns
  - <<: *if-runner-or-security-runner-stable-ref
    changes: *code-backstage-patterns
  - <<: *if-runner-release-ref
    changes: *code-backstage-patterns
  - <<: *if-security-runner-release-ref
    changes: *code-backstage-patterns

.rules:merge_request_pipelines:no_docs:no-community-mr:
  rules:
  - <<: *if-not-canonical-namespace
    when: never
  - <<: *if-merge-request-pipeline
    changes: *code-backstage-patterns
  - <<: *if-runner-or-security-runner-default-branch
    changes: *code-backstage-patterns
  - <<: *if-runner-or-security-runner-stable-ref
    changes: *code-backstage-patterns
  - <<: *if-runner-release-ref
    changes: *code-backstage-patterns
  - <<: *if-security-runner-release-ref
    changes: *code-backstage-patterns

# Rules cannot be merged, instead of opt for creating a new rule like this one
.rules:merge_request_pipelines:no_docs:only_canonical:
  rules:
    - <<: *if-not-canonical-namespace
      when: never
    - <<: *if-runner-merge-request-pipeline
      changes: *code-backstage-patterns
    - <<: *if-runner-default-branch
      changes: *code-backstage-patterns
    - <<: *if-runner-stable-release-ref
      changes: *code-backstage-patterns
    - <<: *if-runner-release-ref
      changes: *code-backstage-patterns

.rules:merge_request_pipelines:no_docs:always:
  rules:
  - <<: *if-merge-request-pipeline
    changes: *code-backstage-patterns
    when: always
  - <<: *if-runner-or-security-runner-default-branch
    changes: *code-backstage-patterns
    when: always
  - <<: *if-runner-or-security-runner-stable-ref
    changes: *code-backstage-patterns
    when: always
  - <<: *if-runner-release-ref
    changes: *code-backstage-patterns
    when: always
  - <<: *if-security-runner-release-ref
    changes: *code-backstage-patterns
    when: always

#################
# Release rules #
#################

.rules:release:all:
  rules:
  - <<: *if-not-canonical-namespace
    when: never
  - <<: *if-default-branch
  - <<: *if-release-candidate-tag
  - <<: *if-stable-release-tag

.rules:release:bleeding-edge:
  rules:
  - <<: *if-runner-default-branch
    changes: *code-backstage-patterns
  - <<: *if-runner-bleeding-edge-release-ref
    changes: *code-backstage-patterns

.rules:release-or-security:bleeding-edge:
  rules:
    - <<: *if-runner-or-security-runner-default-branch
      changes: *code-backstage-patterns
    - <<: *if-runner-or-security-bleeding-edge-release-ref
      changes: *code-backstage-patterns

.rules:release:stable-or-rc:
  rules:
  - if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(-rc[0-9]+)?$/ && $CI_PROJECT_PATH == "gitlab-org/gitlab-runner"
    changes: *code-backstage-patterns
    when: on_success
  - if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(-rc[0-9]+)?$/ && $CI_PROJECT_PATH == "gitlab-org/security/gitlab-runner"
    changes: *code-backstage-patterns
    when: manual

.rules:release:stable:branch:
  rules:
  - <<: *if-runner-stable-release-ref
    changes: *code-backstage-patterns
  - <<: *if-security-runner-release-ref
    changes: *code-backstage-patterns

.rules:release:stable:branch:ignore-changes:
  rules:
  - <<: *if-runner-stable-release-ref
  - <<: *if-security-runner-release-ref

.rules:release:development:merge-requests:
  rules:
  - <<: *if-runner-merge-request-pipeline
    changes: *code-backstage-patterns
  - <<: *if-runner-security-merge-request-pipeline
    changes: *code-backstage-patterns

.rules:release:development:merge-requests:no-community-mr:
  rules:
  - <<: *if-not-canonical-namespace
    when: never
  - <<: *if-runner-merge-request-pipeline
    changes: *code-backstage-patterns
  - <<: *if-runner-security-merge-request-pipeline
    changes: *code-backstage-patterns

.rules:runner-only:release:development:merge-requests:
  rules:
  - <<: *if-runner-merge-request-pipeline
    changes: *code-backstage-patterns

.rules:prepare:ci:image:merge-requests:
  rules:
  - <<: *if-runner-merge-request-pipeline
    changes:
    - dockerfiles/ci/*
    - .gitlab/ci/_common.gitlab-ci.yml
    - .gitlab/ci/prepare.gitlab-ci.yml

.rules:prepare:alpine-no-root:image:merge-requests:
  rules:
  - <<: *if-runner-merge-request-pipeline
    changes:
    - tests/dockerfiles/alpine-no-root/*
    - .gitlab/ci/prepare.gitlab-ci.yml

.rules:prepare:alpine-entrypoint:image:merge-requests:
  rules:
  - <<: *if-runner-merge-request-pipeline
    changes:
    - tests/dockerfiles/alpine-entrypoint/*
    - .gitlab/ci/prepare.gitlab-ci.yml

.rules:prepare:alpine-id-overflow:image:merge-requests:
  rules:
  - <<: *if-runner-merge-request-pipeline
    changes:
    - tests/dockerfiles/alpine-id-overflow/*
    - .gitlab/ci/prepare.gitlab-ci.yml

.rules:prepare:go-fips:image:merge-requests:
  rules:
    - <<: *if-runner-merge-request-pipeline
      changes:
        - dockerfiles/ci/go.fips.Dockerfile
        - dockerfiles/ci/go.fips.Dockerfile.rebuild
        - .gitlab/ci/prepare.gitlab-ci.yml
        - .gitlab/ci/_common.gitlab-ci.yml

.rules:prepare:gitlab-runner-helper-entrypoint:image:merge-requests:
  rules:
  - <<: *if-runner-merge-request-pipeline
    changes:
    - tests/dockerfiles/gitlab-runner-helper-entrypoint/*
    - .gitlab/ci/prepare.gitlab-ci.yml

##############
# Docs rules #
##############

.rules:docs:skip:
  rules:
  - changes: *docs-patterns
    when: never
  - when: on_success

.rules:docs:review:
  rules:
  - <<: *if-not-canonical-namespace
    when: never
  - <<: *if-merge-request-pipeline
    when: manual
