default:
  interruptible: true
  image: debian:bookworm-slim
  retry:
    max: 1
    when:
      - job_execution_timeout
      - script_failure

variables:
  OSNAME: apertis
  OS_WEBSITE: www.apertis.org
  GITLAB_RULES_GIT: $CI_SERVER_URL/infrastructure/$OSNAME-infrastructure.git
  GITLAB_RULES_FILE: gitlab-rulez/rulez.yaml
  DEBUG:
    description: Set to any non-empty value to enable debug output
    value: ""
    options:
      - ""
      - "1"
  LOG_TO_FILE:
    description: Set to a string file name to enable logging to file
    value: ""
  PROJECTS_NAMESPACE: 'pkg'
  FILTER_PACKAGES:
    description: |
      Set to a glob of package/project names to narrow the amount of data
      retrieved to a particular subset of interest. Use `*` to retrieve
      everything.
      Note that in some particular cases package names and GitLab project names
      do not match and we do not do anything special to handle those cases.
    value: '*'
  TRIGGER_UPDATES:
    description: |
      Set to a glob of package names to automatically pull upstream updates on them.
      For instance use `*` to process all updates, `dash` to only process `pkg/dash`.
      Leave it empty to not trigger any update.
    value: ""
  TRIGGER_SECURITY_UPDATES:
    description: |
      Set to `1` to pull only security updates from upstream.
    value: ""
    options:
      - ""
      - "1"
  TRIGGER_GITLAB_RULEZ:
    description: |
      Set to `apply` to run gitlab-rulez on GitLab repositories.
      Leave it empty to not trigger gitlab-rulez.
    value: ""
    options:
      - ""
      - "apply"
  DISABLE_CACHE:
    description: |
      Parsing the big cache file can sometimes lead to an OOM issue with
      packaging-data-fetch-downstream. This option allows to disable the
      cache at this step to unblock dashboard updates.
      See https://phabricator.apertis.org/T9440
    value: "no"
    options:
      - "no"
      - "yes"
  FILTER_ON_CACHE:
    description: |
      Fetching upstream sources data for all Debian packages (~ 38100 pkgs)
      and not only those in Apertis (~ 5600 pkgs) generates big JSON files
      containing useless data. Processing these files lead to frequent
      out-of-memory issues. To reduce the memory consumption, we can filter out
      all packages not available in Apertis based on the cache file.
      See https://phabricator.apertis.org/T9585
    value: "yes"
  SKIP_STORAGE_USAGE:
    description: |
      The storage-usage job randomly fails. Disable it for now.
      See https://phabricator.apertis.org/T9403
    value: "yes"
    options:
      - "yes"
      - "no"
  SKIP_BUILD_DASHBOARD:
    description: |
      Set to yes to skip jobs building the dashboard.
    value: ""
    options:
      - ""
      - "yes"
  SKIP_TRIGGER_JOBS:
    description: |
      Set to yes to skip jobs triggering gitlab-rulez and trigger-updates.
    value: ""
    options:
      - ""
      - "yes"

include:
  - local: .gitlab-ci/dashboard.yml
    rules:
      - if: '$SKIP_BUILD_DASHBOARD != "yes"'
  - local: .gitlab-ci/trigger-jobs.yml
    rules:
      - if: '$SKIP_TRIGGER_JOBS != "yes"'

stages:
  - lint
  - fetch
  - check
  - plan
  - render
  - trigger

lint:
  stage: lint
  tags:
    - lightweight
  before_script:
    - apt update && apt install -y --no-install-recommends
        black
        flake8
        isort
  script:
    - isort bin/* localtest --check-only --diff
    - black bin/* localtest --check --diff
    - flake8 bin/* localtest
  rules:
    - if: $TRIGGER_FROM_JOB
      when: never
    - if: $CI_PIPELINE_SOURCE == "schedule"
      allow_failure: true
    - if: $CI_PIPELINE_SOURCE != "merge_request_event"

localtest:
  stage: lint
  tags:
    - lightweight
  before_script:
    - apt update && apt install -y --no-install-recommends
        python3-yaml
  script:
    - ": do a test run of ./localtest, the tool used to run the pipeline during development"
    - ./localtest
  rules:
    - if: $TRIGGER_FROM_JOB
      when: never
    - if: $CI_PIPELINE_SOURCE == "schedule"
      allow_failure: true
    - if: $CI_PIPELINE_SOURCE != "merge_request_event"