# Continuous Integration configuration for at-spi2-core
#
# For documentation on how this works, see devel-docs/gitlab-ci.md
#
# Full documentation for Gitlab CI: https://docs.gitlab.com/ee/ci/
#
# Introduction to Gitlab CI: https://docs.gitlab.com/ee/ci/quick_start/index.html

# Include the parameters we need from Freedesktop CI Templates
include:
  - local: 'ci/container_builds.yml'

# Stages in the CI pipeline in which jobs will be run
stages:
  - container-build
  - style-check
  - build
  - analysis
  - docs
  - deploy

# Enable merge request pipelines and avoid duplicate pipelines
# https://docs.gitlab.com/ee/ci/yaml/index.html#switch-between-branch-pipelines-and-merge-request-pipelines
workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS && $CI_PIPELINE_SOURCE == "push"
      when: never
    - if: '$CI_COMMIT_TAG'
    - if: '$CI_COMMIT_BRANCH'

style-check-diff:
  extends:
    - '.container.opensuse@x86_64'
    - '.fdo.distribution-image@opensuse'
  needs: ['opensuse-container@x86_64']
  stage: style-check
  script:
    - sh ./ci/run-style-check.sh

# Default build recipe.
#
# Depends on these variables:
# @MESON_EXTRA_FLAGS: extra arguments for the meson setup invocation
opensuse-x86_64:
  stage: build
  extends:
    - '.container.opensuse@x86_64'
    - '.fdo.distribution-image@opensuse'
  needs: ['opensuse-container@x86_64']
  variables:
    MESON_EXTRA_FLAGS: "--buildtype=debug" # -Dwerror=true
  script:
    - meson setup ${MESON_EXTRA_FLAGS} --prefix /usr _build .
    - meson compile -C _build
    - meson install -C _build
    - mkdir /tmp/test+dir+with+funny+chars
    - export XDG_RUNTIME_DIR=/tmp/test+dir+with+funny+chars # See https://gitlab.gnome.org/GNOME/at-spi2-core/-/issues/48
    - xvfb-run --auto-servernum dbus-run-session -- ci/run-registryd-tests.sh
    - xvfb-run --auto-servernum dbus-run-session -- ci/run-tests.sh
  artifacts:
    reports:
      junit:
        - "_build/meson-logs/testlog.junit.xml"
        - "_build/tests/registryd/registryd-pytest.junit.xml"
    when: always
    name: "at-spi2-core-${CI_COMMIT_REF_NAME}"
    paths:
      - "_build/meson-logs"
      - "_build/tests/registryd"

fedora-x86_64:
  stage: build
  extends:
    - '.container.fedora@x86_64'
    - '.fdo.distribution-image@fedora'
  needs: ['fedora-container@x86_64']
  variables:
    MESON_EXTRA_FLAGS: "--buildtype=debug -Ddefault_bus=dbus-broker -Ddbus_broker=/usr/bin/dbus-broker-launch" # -Dwerror=true
  script:
    - meson setup ${MESON_EXTRA_FLAGS} --prefix /usr _build .
    - meson compile -C _build
    - meson install -C _build
  artifacts:
    reports:
      junit: "_build/meson-logs/testlog.junit.xml"
    when: always
    name: "at-spi2-core-${CI_COMMIT_REF_NAME}"
    paths:
      - "_build/meson-logs"

# Run static analysis on the code.
#
# The logs are part of the compilation stderr.
static-scan:
  stage: analysis
  extends:
    - '.container.opensuse@x86_64'
    - '.fdo.distribution-image@opensuse'
  needs: ['opensuse-container@x86_64']
  variables:
    MESON_EXTRA_FLAGS: "--buildtype=debug -Dintrospection=disabled -Ddocs=false"
  script:
    - meson setup ${MESON_EXTRA_FLAGS} --prefix /usr _scan_build .
    - ninja -C _scan_build scan-build
  artifacts:
    name: "at-spi2-core-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    paths:
      - "_scan_build/meson-logs/scanbuild"

# Build and run with address sanitizer (asan).
asan-build:
  stage: analysis
  extends:
    - '.container.opensuse@x86_64'
    - '.fdo.distribution-image@opensuse'
  needs: ['opensuse-container@x86_64']
  variables:
    MESON_EXTRA_FLAGS: "--buildtype=debug -Db_sanitize=address -Db_lundef=false -Dintrospection=disabled -Ddocs=false"
    # Add a suppressions file for address-sanitizer.  Looks like libdbus has a minor leak that is hurting
    # the tests while run with asan.  Hopefully this will go away once we convert everything to gdbus.
    #
    # https://gitlab.freedesktop.org/dbus/dbus/-/issues/326
    LSAN_OPTIONS: "suppressions=${CI_PROJECT_DIR}/ci/address-sanitizer.supp"
  script:
    - CC=clang meson setup ${MESON_EXTRA_FLAGS} --prefix /usr _build .
    - meson compile -C _build
    - meson install -C _build
    - xvfb-run --auto-servernum dbus-run-session -- ci/run-tests.sh
  artifacts:
    name: "at-spi2-core-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    paths:
      - "${CI_PROJECT_DIR}/_build/meson-logs"
  allow_failure: true

# Run the test suite and extract code coverage information.
#
# See the _coverage/ artifact for the HTML report.
coverage:
  stage: analysis
  extends:
    - '.container.opensuse@x86_64'
    - '.fdo.distribution-image@opensuse'
  needs: ['opensuse-container@x86_64']
  variables:
    MESON_EXTRA_FLAGS: "--buildtype=debug -Ddocs=false -Dintrospection=disabled"
    CFLAGS: "-coverage -ftest-coverage -fprofile-arcs"
  script:
    - source ./ci/env.sh
    - meson setup ${MESON_EXTRA_FLAGS} --prefix /usr _build .
    - meson compile -C _build
    - meson install -C _build
    - xvfb-run --auto-servernum dbus-run-session -- ci/run-registryd-tests.sh
    - xvfb-run --auto-servernum dbus-run-session -- ci/run-tests.sh
    - mkdir -p public
    - grcov _build --source-dir ./ --prefix-dir ../ --output-type cobertura --branch --ignore-not-existing -o coverage.xml
    - grcov _build --source-dir ./ --prefix-dir ../ --output-type html --branch --ignore-not-existing -o public/coverage
    # In the following line, the first grep finds what it is supposed to find, but exits with a nonzero code.
    # I have no idea why.  So, force the whole pipeline to return true.
    - (grep -Eo 'line-rate="[^"]+"' coverage.xml | head -n 1 | grep -Eo '[0-9.]+' | awk '{ print "Coverage:", $1 * 100 }') || true
  coverage: '/Coverage: \d+\.\d+/'
  artifacts:
    name: "at-spi2-core-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    expire_in: 2 days
    when: always
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
    paths:
      - "_build/meson-logs"
      - public
      - coverage.xml

reference:
  stage: docs
  extends:
    - '.container.opensuse@x86_64'
    - '.fdo.distribution-image@opensuse'
  needs: ['opensuse-container@x86_64']
  variables:
    MESON_EXTRA_FLAGS: "--buildtype=release -Ddocs=true"
  script:
    - meson setup ${MESON_EXTRA_FLAGS} --prefix /usr _build .
    - ninja -C _build doc/atk doc/libatspi devel-docs/html
    - mkdir _reference
    - mv _build/doc/libatspi _reference/libatspi
    - mv _build/doc/atk _reference/atk
    - mv _build/devel-docs/html  _reference/devel-docs
  artifacts:
    paths:
      - _reference
      - "_build/meson-logs"

# Publish the test coverage report
pages:
  stage: deploy
  needs: [ coverage, reference ]
  script:
    - mv _reference/* public/
  artifacts:
    paths:
      - public
  rules:
    - if: ($CI_DEFAULT_BRANCH == $CI_COMMIT_BRANCH)
    # Restrict it to the gnome namespace to avoid every fork pushing a set of pages by default
    # - if: ($CI_DEFAULT_BRANCH == $CI_COMMIT_BRANCH && $CI_PROJECT_NAMESPACE == "gnome")
