include:
  - remote: "https://gitlab.freedesktop.org/freedesktop/ci-templates/-/raw/80f87b3058efb75a1faae11826211375fba77e7f/templates/ci-fairy.yml"
  - remote: 'https://gitlab.freedesktop.org/freedesktop/ci-templates/-/raw/80f87b3058efb75a1faae11826211375fba77e7f/templates/alpine.yml'
  - remote: 'https://gitlab.freedesktop.org/freedesktop/ci-templates/-/raw/80f87b3058efb75a1faae11826211375fba77e7f/templates/fedora.yml'
  - remote: 'https://gitlab.freedesktop.org/freedesktop/ci-templates/-/raw/80f87b3058efb75a1faae11826211375fba77e7f/templates/ubuntu.yml'

variables:
  FDO_UPSTREAM_REPO: GNOME/tracker
  MESON_TEST_TIMEOUT_MULTIPLIER: 3
  # These can be used to see verbose log output from the functional-tests.
  # See HACKING.md for more information.
  TRACKER_DEBUG: ""

  # This can be used when debugging test failures that only occur within GitLab CI.
  MESON_TEST_EXTRA_ARGS: ""

stages:
  - review
  - prepare
  - code-review
  - build
  - test
  - analysis
  - website

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_PIPELINE_SOURCE == 'push'

.check-template: &check
  extends:
    - .fdo.ci-fairy
  artifacts:
    expire_in: 1 week
    paths:
      - check-junit-report.xml
    reports:
      junit: check-junit-report.xml

repo-sanity:
  stage: review
  script:
    - >
      if [[ -z "$CI_REGISTRY_IMAGE" ]] ;
      then
        .gitlab-ci/simple-junit-report.sh check-junit-report.xml \
          repo-sanity "The container registry should be enabled in the project general settings panel at $CI_PROJECT_URL/edit" ;
        exit 1 ;
      fi
  <<: *check

check-commit-log:
  variables:
    GIT_DEPTH: "100"
  stage: review
  script:
    - ci-fairy check-commits --junit-xml=check-junit-report.xml && exit 0;
      if [[ -z "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]] ;
      then
        echo "'ci-fairy check-commits' found commit messages with unexpected format, see https://wiki.gnome.org/Git/CommitMessages for the recommended guidelines. Please fix those before filing a merge request, this will be handled as a pipeline failure there." ;
        exit 255 ;
      fi ;
      exit 1
  allow_failure:
    exit_codes:
      - 255
  <<: *check

check-merge-request:
  variables:
    GIT_STRATEGY: none
  stage: review
  script:
    - if [[ x"$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" != "x" ]] ;
      then
        ci-fairy check-merge-request --require-allow-collaboration --junit-xml=check-junit-report.xml ;
      else
        echo "Not a merge request" ;
      fi
  <<: *check

.tracker.fedora@common:
  variables:
    BASE_TAG: '2022-02-10.0'
    FDO_DISTRIBUTION_PACKAGES: 'clang clang-analyzer gcovr git libasan libubsan python3-gobject python3-pip umockdev-devel xmlto uncrustify patch diffutils cmake python-devel'
    FDO_DISTRIBUTION_EXEC: |
      dnf install -y 'dnf-command(builddep)' 'dnf-command(download)' &&
      dnf builddep -y tracker tracker-miners --setopt=install_weak_deps=False &&
      dnf clean all &&
      pip3 install beautifulsoup4 mkdocs mkdocs-cinder tap.py meson hotdoc &&
      # Installing gtk-doc docs with dnf does not end up with docs at
      # /usr/share/gtk-doc, https://gitlab.gnome.org/GNOME/tracker/-/issues/324
      dnf download glib2-doc &&
      rpm -i ./glib2-doc*.rpm

.tracker.ubuntu@common:
  variables:
    BASE_TAG: '2022-03-07.0'
    FDO_DISTRIBUTION_PACKAGES: 'python3-tap umockdev libumockdev-dev upower asciidoc-base git gcovr'
    FDO_DISTRIBUTION_EXEC: |
      export DEBIAN_FRONTEND=noninteractive &&
      sed -Ei 's/^# deb-src /deb-src /' /etc/apt/sources.list &&
      apt-get -yq update && apt-get -yq upgrade &&
      apt-get -yq build-dep tracker tracker-miners

.tracker.alpine@common:
  variables:
    BASE_TAG: '2022-03-07.0'
    FDO_DISTRIBUTION_PACKAGES: 'alpine-sdk asciidoc bash-completion dbus dbus-dev git glib-dev gobject-introspection-dev gtk-doc icu-dev json-glib-dev libsoup-dev libxml2-dev meson py3-gobject3 py3-setuptools py3-tappy sqlite-dev vala gcovr'

.tracker.fedora:35@x86_64:
  extends: .tracker.fedora@common
  variables:
    FDO_DISTRIBUTION_VERSION: 35
    FDO_DISTRIBUTION_TAG: "x86_64-${BASE_TAG}"

.tracker.fedora:rawhide@x86_64:
  extends: .tracker.fedora@common
  variables:
    FDO_DISTRIBUTION_VERSION: rawhide
    FDO_DISTRIBUTION_TAG: "x86_64-${BASE_TAG}"

.tracker.fedora:35@aarch64:
  extends: .tracker.fedora@common
  variables:
    FDO_DISTRIBUTION_VERSION: 35
    FDO_DISTRIBUTION_TAG: "aarch64-${BASE_TAG}"
  tags:
    - aarch64

.tracker.ubuntu:rolling@x86_64:
  extends: .tracker.ubuntu@common
  variables:
    FDO_DISTRIBUTION_VERSION: rolling
    FDO_DISTRIBUTION_TAG: "x86_64-${BASE_TAG}"

.tracker.alpine:latest@x86_64:
  extends: .tracker.alpine@common
  variables:
    FDO_DISTRIBUTION_VERSION: latest
    FDO_DISTRIBUTION_TAG: "x86_64-${BASE_TAG}"

build-fedora-container@x86_64:
  extends:
    - .fdo.container-build@fedora
    - .tracker.fedora:35@x86_64
  stage: prepare
  variables:
    GIT_STRATEGY: none
  needs:
    - repo-sanity
    - check-commit-log
    - check-merge-request

build-fedora-rawhide-container@x86_64:
  extends:
    - .fdo.container-build@fedora
    - .tracker.fedora:rawhide@x86_64
  stage: prepare
  variables:
    GIT_STRATEGY: none
  allow_failure: true
  needs:
    - repo-sanity
    - check-commit-log
    - check-merge-request

build-fedora-container@aarch64:
  extends:
    - .fdo.container-build@fedora
    - .tracker.fedora:35@aarch64
  stage: prepare
  variables:
    GIT_STRATEGY: none
  needs:
    - repo-sanity
    - check-commit-log
    - check-merge-request

build-ubuntu-container@x86_64:
  extends:
    - .fdo.container-build@ubuntu
    - .tracker.ubuntu:rolling@x86_64
  stage: prepare
  variables:
    GIT_STRATEGY: none
  needs:
    - repo-sanity
    - check-commit-log
    - check-merge-request

build-alpine-container@x86_64:
  extends:
    - .fdo.container-build@alpine
    - .tracker.alpine:latest@x86_64
  stage: prepare
  variables:
    GIT_STRATEGY: none
  needs:
    - repo-sanity
    - check-commit-log
    - check-merge-request

check-code-style:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@x86_64
  needs:
    - build-fedora-container@x86_64
  stage: code-review
  script:
    - git fetch origin master;
      export common_parent_sha=$(diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-${CI_DEFAULT_BRANCH}}") <(git rev-list --first-parent HEAD) | head -1) ;
      python3 -u ./check-style.py --dry-run --sha $common_parent_sha ;
  allow_failure: true

.build-template: &build
  stage: build
  script:
    - meson . build -Ddocs=$([ -z "$NO_DOCS" ] && echo "true" || echo "false") -Db_coverage=true -Db_lto=true -Dsystemd_user_services=false -Dtests_tap_protocol=true --prefix /usr
    - ninja -C build
    - |
      if [ -z "$NO_DOCS" ]; then ninja -C build docs/reference/libtracker-sparql/Tracker-doc; fi
  artifacts:
    expire_in: 1 day
    paths:
      - build

build-fedora@x86_64:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@x86_64
  needs:
    - build-fedora-container@x86_64
  <<: *build

build-fedora-rawhide@x86_64:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:rawhide@x86_64
  needs:
    - build-fedora-rawhide-container@x86_64
  allow_failure: true
  <<: *build

build-fedora@aarch64:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@aarch64
  needs:
    - build-fedora-container@aarch64
  allow_failure: true
  variables:
    NO_DOCS: "true"
  <<: *build

build-ubuntu-rolling@x86_64:
  extends:
    - .fdo.distribution-image@ubuntu
    - .tracker.ubuntu:rolling@x86_64
  needs:
    - build-ubuntu-container@x86_64
  variables:
    NO_DOCS: "true"
  <<: *build

build-alpine-latest@x86_64:
  extends:
    - .fdo.distribution-image@alpine
    - .tracker.alpine:latest@x86_64
  needs:
    - build-alpine-container@x86_64
  variables:
    NO_DOCS: "true"
  <<: *build

.test-template: &test
  stage: test
  variables:
    G_SLICE: "always-malloc"
    MALLOC_CHECK_: "3"
  script:
    - cd build
    - |
      # Remove the many "CI_" variables from the environment. Meson dumps the
      # whole environment for every failed test, and that gives a whole
      # screenful of junk each time unless we strip these.
      unset $(env|grep -o '^CI_[^=]*')
      env LANG=C.UTF-8 LC_ALL=C.UTF-8 dbus-run-session meson test -t $MESON_TEST_TIMEOUT_MULTIPLIER --print-errorlogs ${MESON_TEST_EXTRA_ARGS}
  after_script:
    - pushd build
    - gcovr --root=.. --filter='\.\./src/'
        --exclude=../utils --exclude=../examples --exclude=../docs/reference
        --exclude='\.\./build/.*\.[ch]$' --exclude='.*/tests/.*\.[ch]$'
        --exclude='\.\./src/.*\.vala$'
        --json --print-summary --output=../coverage-${CI_JOB_NAME}.json 2>/dev/null
    - popd
    - |
      echo "Distribution: "
      echo
      egrep '^NAME=|^VERSION=' /etc/os-release
      echo
      echo "Test suite settings:"
      echo
      echo "G_MESSAGES_DEBUG: ${G_MESSAGES_DEBUG}"
      echo "TRACKER_DEBUG: ${TRACKER_DEBUG}"
      echo "MESON_TEST_EXTRA_ARGS: ${MESON_TEST_EXTRA_ARGS}"
      echo
      echo "These values can be set at https://gitlab.gnome.org/GNOME/tracker/pipelines/new"
  artifacts:
    expire_in: 1 day
    when: always
    paths:
    - build
    - coverage-*.json
    reports:
      junit: "build/meson-logs/testlog.junit.xml"

test-fedora@x86_64:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@x86_64
  needs:
    - build-fedora@x86_64
  <<: *test

test-fedora-rawhide@x86_64:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:rawhide@x86_64
  needs:
    - build-fedora-rawhide@x86_64
  allow_failure: true
  tags:
    # There is a mismatch between syscalls and seccomp rules in
    # some runners, using `crun` is advised in the mean time, see
    # https://gitlab.gnome.org/Infrastructure/GitLab/-/issues/545
    - crun
  <<: *test

test-fedora@aarch64:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@aarch64
  needs:
    - build-fedora@aarch64
  allow_failure: true
  <<: *test

test-ubuntu@x86_64:
  extends:
    - .fdo.distribution-image@ubuntu
    - .tracker.ubuntu:rolling@x86_64
  needs:
    - build-ubuntu-rolling@x86_64
  <<: *test

test-alpine@x86_64:
  extends:
    - .fdo.distribution-image@alpine
    - .tracker.alpine:latest@x86_64
  needs:
    - build-alpine-latest@x86_64
  <<: *test

test-website:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@x86_64
  stage: test
  script:
    - export install_prefix="$(pwd)/tracker-install"
    # Build tracker and install.
    - mkdir website-build; pushd website-build; meson .. --prefix="$install_prefix"; ninja; ninja install; popd
    # Build tracker-miners and install any documentation from there as well.
    - .gitlab-ci/checkout-tracker-miners.sh
    - pushd extra/tracker-miners; mkdir build; pushd build; env PKG_CONFIG_PATH="$install_prefix/lib64/pkgconfig" meson .. --prefix="$install_prefix"; ninja install; popd; popd
    # Generate the website using mkdocs.
    - |
      export tracker_commit=$CI_COMMIT_SHA
      export tracker_miners_commit=$(git -C ./extra/tracker-miners rev-parse HEAD)
      ./docs/website/build.py --output=website --api-docs="$install_prefix/share/doc/Tracker/html" --tracker-commit=${tracker_commit} --man-pages ./docs/manpages/*.txt ./extra/tracker-miners/docs/manpages/*.txt
  artifacts:
    paths:
      - website
  needs:
    - build-fedora-container@x86_64

coverage:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@x86_64
  stage: analysis
  allow_failure: true
  script:
    - mkdir coveragereport
    - gcovr --add-tracefile 'coverage-*.json'
        --html-details --print-summary --output coveragereport/index.html
  coverage: '/^lines: (\d+\.\d+\%)/'
  artifacts:
    when: always
    paths:
    - coveragereport
  needs:
    - test-fedora@x86_64
    - test-fedora-rawhide@x86_64
    - test-fedora@aarch64
    - test-alpine@x86_64

coverity:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@x86_64
  stage: analysis
  allow_failure: true
  script:
    - curl https://scan.coverity.com/download/linux64 --data "token=$COVERITY_TOKEN&project=Tracker" --output /tmp/coverity_tool.tgz
    - tar zxf /tmp/coverity_tool.tgz
    - mkdir coverity-build
    - cd coverity-build
    - env CC=clang meson ..
    - ../cov-analysis-linux64-*/bin/cov-build --dir cov-int ninja
    - tar czf cov-int.tar.gz cov-int
    - curl https://scan.coverity.com/builds?project=Tracker
      --form token=$COVERITY_TOKEN --form email=carlosg@gnome.org
      --form file=@cov-int.tar.gz --form version="`git describe --tags`"
      --form description="gitlab CI build"
  needs:
    - build-fedora-container@x86_64
  only:
    - master
  except:
    changes:
      - po/*.po

static-scan:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@x86_64
  stage: analysis
  needs:
    - build-fedora-container@x86_64
  script:
    - meson --buildtype=debug _scan_build
    - ninja -C _scan_build scan-build
  artifacts:
    paths:
      - _scan_build/meson-logs
  allow_failure: true

pages:
  extends:
    - .fdo.distribution-image@fedora
    - .tracker.fedora:35@x86_64
  stage: website
  script:
    - mv website public
  artifacts:
    paths:
      - public
  needs:
    - test-website
  only:
    - master
