include:
  - remote: 'https://gitlab.freedesktop.org/freedesktop/ci-templates/-/raw/28b430bd847da130d7c678772b8be260abaab5b3/templates/fedora.yml'
  - remote: 'https://gitlab.freedesktop.org/freedesktop/ci-templates/-/raw/34f4ade99434043f88e164933f570301fd18b125/templates/ci-fairy.yml'

stages:
 - review
 - prepare
 - code-review
 - build
 - test
 - analyze
 - docs
 - deploy

variables:
  FDO_UPSTREAM_REPO: GNOME/mutter

.skip-git-clone:
  variables:
    GIT_STRATEGY: none

.mutter.git-clone:
  extends:
    - .skip-git-clone
  variables:
    MUTTER_CLONE_PATH: $CI_BUILDS_DIR/$CI_PROJECT_PATH
    MUTTER_CLONE_DEPTH: 1
  before_script: |
    if [ -n "$MUTTER_CLONE_PATH" ]; then
      set -x
      uri="$CI_REPOSITORY_URL"
      if [ -n "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" ]; then
        uri="$CI_MERGE_REQUEST_SOURCE_PROJECT_URL.git"
        branch="$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"
      elif [ -n "$CI_COMMIT_TAG" ]; then
        branch="$CI_COMMIT_TAG"
      elif [ -n "$CI_COMMIT_BRANCH" ]; then
        branch="$CI_COMMIT_BRANCH"
      else
        branch="$CI_DEFAULT_BRANCH"
      fi
      if [ ! -d "$MUTTER_CLONE_PATH" ] || [ -z "$(ls -A "$MUTTER_CLONE_PATH")" ]; then
        git clone --depth $MUTTER_CLONE_DEPTH "$uri" "$MUTTER_CLONE_PATH" -b "$branch"
      elif [ ! -d "$MUTTER_CLONE_PATH/.git" ]; then
        git clone --bare --depth $MUTTER_CLONE_DEPTH "$uri" "$MUTTER_CLONE_PATH/.git" -b "$branch"
        if git -C "$MUTTER_CLONE_PATH" config --unset core.bare; then
          git -C "$MUTTER_CLONE_PATH" checkout
        else
          # For some weird reasons sometimes the fast-path could fail with
          # "fatal: not in a git directory" error, so handle it manually
          tmpdir=$(mktemp --directory --tmpdir mutter-XXXXXX)
          mkdir "$tmpdir/repo"
          mv "$MUTTER_CLONE_PATH/.git" "$tmpdir/repo/"
          git clone "$tmpdir/repo" "$tmpdir/src"
          mv "$tmpdir"/src/* "$tmpdir"/src/.* "$MUTTER_CLONE_PATH"
          rm -r "$tmpdir/repo"
          rm -rv "$tmpdir"
        fi
      fi
      set +x
    fi

.mutter.skip-git-clone:
  extends:
    - .skip-git-clone
  variables:
    MUTTER_CLONE_PATH: ''

.mutter.run-as-user:
  image:
    name: ${FDO_DISTRIBUTION_IMAGE}
    entrypoint:
      - 'runuser'
      - '-u'
      - !reference [.mutter.fedora@common, variables, MUTTER_USER]
      - '--'

.mutter.distribution-image:
  extends:
    - .fdo.distribution-image@fedora
    - .mutter.run-as-user

.mutter.fedora@common:
  extends:
    - .skip-git-clone
  variables:
    FDO_DISTRIBUTION_VERSION: 39
    BASE_TAG: '2023-09-12.0'
    MUTTER_USER: 'meta-user'
    FDO_DISTRIBUTION_PACKAGES:
      asciidoc
      clang
      gcovr
      gdm
      gnome-shell
      sassc
      uncrustify
      xorg-x11-server-Xvfb
      mesa-dri-drivers
      xorg-x11-proto-devel
      qemu-system-x86-core
      busybox
      zenity
      gi-docgen
      python3-dbusmock
      pkgconfig(libgcrypt)
      pkgconfig(libdisplay-info)

    FDO_DISTRIBUTION_EXEC: |
      set -e

      # Create $MUTTER_USER
      useradd -u 9999 -b /opt/mutter -ms /bin/bash $MUTTER_USER

      # Enable sudo for $MUTTER_USER
      echo "%$MUTTER_USER ALL = (ALL) NOPASSWD: ALL" > /etc/sudoers.d/99_mutter-user

      dnf install -y 'dnf-command(builddep)'

      dnf builddep -y mutter --setopt=install_weak_deps=False
      dnf builddep -y gnome-shell --setopt=install_weak_deps=False
      dnf builddep -y kernel --setopt=install_weak_deps=False
      dnf builddep -y libei --setopt=install_weak_deps=False

      ./.gitlab-ci/install-meson-project.sh \
        https://gitlab.gnome.org/jadahl/catch.git \
        main . 29ad36e2b1d28ac9d8b2bc44af46296cb1db5d66

      ./.gitlab-ci/install-meson-project.sh \
        https://gitlab.gnome.org/GNOME/glib.git \
        main . a7c34b431bfaf3d56de64cb1cfea851ac11099d3

      ./.gitlab-ci/install-meson-project.sh \
        https://gitlab.freedesktop.org/libinput/libei.git \
        1.0.901 .

      rpm -e --nodeps gnome-bluetooth-libs-devel \
                      mutter mutter-devel \
                      gnome-shell

      # Ensure that we mark the project clone dir as safe directory
      git config --system --add safe.directory "$CI_PROJECT_DIR"

      if [[ x"$(uname -m )" = "xx86_64" ]] ; then
        if [ -n "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" ]; then
          git clone --depth $MUTTER_CLONE_DEPTH \
            $CI_MERGE_REQUEST_SOURCE_PROJECT_URL.git mutter-src \
            -b "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"
        elif [ -n "$CI_COMMIT_BRANCH" ]; then
          git clone --depth $MUTTER_CLONE_DEPTH \
            $CI_REPOSITORY_URL mutter-src -b "$CI_COMMIT_BRANCH"
        else
          git clone --depth $MUTTER_CLONE_DEPTH $CI_REPOSITORY_URL mutter-src
        fi
        meson setup build mutter-src -Dkvm_tests=true
        ninja -C build src/tests/kvm/bzImage
        mkdir -p /opt/mutter
        cp build/src/tests/kvm/bzImage /opt/mutter/bzImage

        dnf install -y python3-argcomplete

        git clone https://github.com/arighi/virtme-ng.git
        cd virtme-ng
        git fetch --tags
        git checkout v1.8
        ./setup.py install --prefix=/usr
        cd ..
        rm -rf virtme-ng
        rm -rf build mutter-src
      fi
  retry:
    max: 2
    when:
      - 'always'

default:
  # Cancel jobs if newer commits are pushed to the branch
  interruptible: true
  # Auto-retry jobs in case of infra failures
  retry:
    max: 1
    when:
      - 'runner_system_failure'
      - 'stuck_or_timeout_failure'
      - 'scheduler_failure'
      - 'api_failure'

.mutter.fedora@x86_64:
  extends:
    - .mutter.fedora@common
    - .mutter.git-clone
  variables:
    FDO_DISTRIBUTION_TAG: "x86_64-${BASE_TAG}"

.mutter.fedora@aarch64:
  extends:
    - .mutter.fedora@common
    - .mutter.git-clone
  variables:
    FDO_DISTRIBUTION_TAG: "aarch64-${BASE_TAG}"
  tags:
    - aarch64

workflow:
  rules:
    # Allow to switch from branch pipelines to MR pipelines seamlessly
    # https://docs.gitlab.com/ee/ci/jobs/job_control.html#avoid-duplicate-pipelines
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS && $CI_PIPELINE_SOURCE == "push"
      when: never
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    # Don't trigger a branch pipeline if there is an open MR
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: '$CI_COMMIT_BRANCH'
    - if: '$CI_COMMIT_TAG'

.pipeline-guard:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_TAG'
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
    - if: '$CI_COMMIT_BRANCH =~ /^gnome-[0-9-]+$/'
    # Avoid catchall `when: manual` rule which might
    # cause duplicate pipelines to be triggered.
    # https://docs.gitlab.com/ee/ci/jobs/job_control.html#avoid-duplicate-pipelines
    #
    # Also make it so pipelines without MR need to be started
    # manually, since their state will most likely be WIP
    - if: '$CI_COMMIT_BRANCH'
      when: 'manual'

.only-merge-requests:
  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^$/'
      when: never
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
      when: on_success

check-commit-log:
  extends:
    - .fdo.ci-fairy
  stage: review
  variables:
    GIT_DEPTH: "100"
  script:
    ci-fairy check-commits --junit-xml=commit-message-junit-report.xml
  artifacts:
    expire_in: 1 week
    paths:
      - commit-message-junit-report.xml
    reports:
      junit: commit-message-junit-report.xml
  rules:
    - !reference [.only-merge-requests, rules]

check-merge-request:
  extends:
    - .fdo.ci-fairy
    - .skip-git-clone
  stage: review
  script:
    ci-fairy check-merge-request --require-allow-collaboration --junit-xml=check-merge-request-report.xml
  artifacts:
    expire_in: 1 week
    paths:
      - check-merge-request-report.xml
    reports:
      junit: check-merge-request-report.xml
  rules:
    - !reference [.only-merge-requests, rules]

build-fedora-container@x86_64:
  extends:
    - .fdo.container-build@fedora@x86_64
    - .mutter.fedora@x86_64
    - .mutter.skip-git-clone
  stage: prepare
  rules:
    - !reference [.pipeline-guard, rules]

build-fedora-container@aarch64:
  extends:
    - .fdo.container-build@fedora@aarch64
    - .mutter.fedora@aarch64
    - .mutter.skip-git-clone
  stage: prepare
  rules:
    - !reference [.pipeline-guard, rules]
  when: manual

check-code-style:
  extends:
    - .mutter.distribution-image
    - .mutter.fedora@x86_64
  variables:
    MUTTER_CLONE_DEPTH: 200
  stage: code-review
  needs:
    - job: build-fedora-container@x86_64
      artifacts: false
  script:
      git remote add target $CI_MERGE_REQUEST_PROJECT_URL.git ;
      git fetch target $CI_MERGE_REQUEST_TARGET_BRANCH_NAME ;
      export common_parent_sha=$(diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "target/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME") <(git rev-list --first-parent HEAD) | head -1) ;
      python3 -u ./check-style.py --dry-run --sha $common_parent_sha ;
  allow_failure: true
  rules:
    - !reference [.only-merge-requests, rules]

.build-mutter-base:
  variables:
    BASE_MESON_OPTIONS:
      -Degl_device=true
      -Dwayland_eglstream=true
      -Dcatch=true
      -Dlibdisplay_info=true

.build-mutter:
  extends:
    - .mutter.distribution-image
    - .build-mutter-base
  stage: build
  script:
    - meson setup . build
        --prefix /usr
        --werror
        --fatal-meson-warnings
        --warnlevel 2
        -Dbuildtype=debugoptimized
        -Db_coverage=true
        $BASE_MESON_OPTIONS
        $EXTRA_MESON_OPTIONS
    - meson compile -C build
    - sudo meson install --dry-run -C build
  artifacts:
    expire_in: 1 day
    paths:
      - build

build-mutter@x86_64:
  variables:
    EXTRA_MESON_OPTIONS:
      -Dkvm_tests=true
      -Dkvm_kernel_image=/opt/mutter/bzImage
  extends:
    - .build-mutter
    - .mutter.fedora@x86_64
  needs:
    - job: build-fedora-container@x86_64
      artifacts: false

build-mutter@aarch64:
  extends:
    - .build-mutter
    - .mutter.fedora@aarch64
  needs:
    - job: build-fedora-container@aarch64
      artifacts: false
  when: manual

build-without-opengl-and-glx@x86_64:
  extends:
    - .mutter.distribution-image
    - .mutter.fedora@x86_64
  stage: build
  needs:
    - job: build-fedora-container@x86_64
      artifacts: false
  script:
    - meson setup . build --werror --prefix /usr
        -Dbuildtype=debugoptimized
        -Dopengl=false
        -Dglx=false
        -Degl_device=true
        -Dwayland_eglstream=true
        -Dintrospection=false
    - meson compile -C build
    - sudo meson install --no-rebuild -C build
  artifacts:
    paths:
      - build/meson-logs

build-without-native-backend-and-wayland@x86_64:
  extends:
    - .mutter.distribution-image
    - .mutter.fedora@x86_64
  stage: build
  needs:
    - job: build-fedora-container@x86_64
      artifacts: false
  script:
    - meson setup . build --werror --prefix /usr
        -Dbuildtype=debugoptimized
        -Dnative_backend=false
        -Dudev=false
        -Dwayland=false
        -Dxwayland=false
        -Dcore_tests=false
        -Dnative_tests=false
        -Dintrospection=false
    - meson compile -C build
    - sudo meson install --no-rebuild -C build
  artifacts:
    paths:
      - build/meson-logs

build-wayland-only@x86_64:
  extends:
    - .mutter.distribution-image
    - .mutter.fedora@x86_64
  stage: build
  needs:
    - job: build-fedora-container@x86_64
      artifacts: false
  script:
    - meson setup . build --werror --prefix /usr
        -Dbuildtype=debugoptimized
        -Dwayland=true
        -Dxwayland=false
        -Dcore_tests=false
        -Dnative_tests=false
        -Dintrospection=false
    - meson compile -C build
    - sudo meson install --no-rebuild -C build
  artifacts:
    paths:
      - build/meson-logs

.test-setup:
  variables:
    XDG_RUNTIME_DIR: "$CI_PROJECT_DIR/runtime-dir"
    GSETTINGS_SCHEMA_DIR: "$CI_PROJECT_DIR/build/data"
    MUTTER_DEBUG_DUMMY_MODE_SPECS: "800x600@10.0"
    PIPEWIRE_DEBUG: 2
    PIPEWIRE_LOG: "$CI_PROJECT_DIR/build/meson-logs/pipewire.log"
    XVFB_SERVER_ARGS: "+iglx -noreset"
    G_SLICE: "always-malloc"
    MALLOC_CHECK_: "3"
    NO_AT_BRIDGE: "1"
    GTK_A11Y: "none"
  before_script:
    - !reference [.mutter.git-clone, before_script]
    # Disable e.g. audio support to not dead lock screen cast tests
    - mkdir -m 700 $XDG_RUNTIME_DIR
    - pipewire & sleep 2

.test-mutter-base:
  extends:
    - .mutter.distribution-image
    - .test-setup
  stage: test
  after_script:
    - pushd build
    - gcovr --root=..
        --filter='\.\./src/'
        --filter='\.\./clutter/'
        --filter='\.\./cogl/'
        --filter='\.\./mtk/'
        --exclude='\.\./build/.*\.[ch]$' --exclude='.*/tests/.*\.[ch]$'
        --json --output=../coverage-${CI_JOB_NAME}.json
    - popd
  artifacts:
    expire_in: 1 day
    name: "mutter-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    paths:
      - build
      - coverage-*.json

.test-mutter:
  extends:
    - .mutter.distribution-image
    - .test-mutter-base
  script:
    - glib-compile-schemas $GSETTINGS_SCHEMA_DIR
    - xvfb-run -a -s "$XVFB_SERVER_ARGS"
        ./src/tests/meta-dbus-runner.py
          --launch=wireplumber
          meson test
            -C build
            --setup plain
            --no-suite 'mutter/kvm'
            --no-rebuild
            --timeout-multiplier 10
            --print-errorlogs
  artifacts:
    reports:
      junit: "build/meson-logs/testlog-plain.junit.xml"

test-mutter@x86_64:
  extends:
    - .mutter.fedora@x86_64
    - .test-mutter
  tags:
    - asan
  needs:
    - build-mutter@x86_64

test-mutter-kvm@x86_64:
  extends:
    - .mutter.fedora@x86_64
    - .test-mutter-base
  tags:
    - kvm
  script:
    - meson test -C build
        --no-rebuild
        --timeout-multiplier 10
        --setup plain
        --suite 'mutter/kvm'
        --print-errorlogs
  needs:
    - build-mutter@x86_64
  artifacts:
    reports:
      junit: "build/meson-logs/testlog-plain.junit.xml"

test-mutter@aarch64:
  extends:
    - .mutter.fedora@aarch64
    - .test-mutter
  tags:
    - asan-aarch64
  needs:
    - build-mutter@aarch64
  when: manual

coverage:
  extends:
    - .mutter.distribution-image
    - .mutter.fedora@x86_64
  stage: analyze
  script:
    - mkdir coveragereport
    - gcovr --add-tracefile 'coverage-*.json'
        --html-details --print-summary --output coveragereport/index.html
    - gcovr --add-tracefile 'coverage-*.json'
        --xml --output coveragereport/coverage.xml
  artifacts:
    expose_as: 'Coverage Report'
    paths:
      - coveragereport
      - coveragereport/index.html
    reports:
      coverage_report:
        coverage_format: cobertura
        # TODO: we may need to split this file once it will reach the
        # gitlab limit size of 10M, or it will stop working:
        #  https://gitlab.com/gitlab-org/gitlab/-/issues/328772
        path: coveragereport/coverage.xml
  coverage: '/^lines: (\d+\.\d+\%)/'
  needs:
    - test-mutter@x86_64
#    - test-mutter@aarch64
    - test-mutter-kvm@x86_64

can-build-gnome-shell@x86_64:
  extends:
    - .mutter.distribution-image
    - .mutter.fedora@x86_64
  stage: test
  needs:
    - build-mutter@x86_64
  before_script:
    - !reference [.mutter.fedora@x86_64, before_script]
    - sudo meson install --no-rebuild -C build
  script:
    - .gitlab-ci/checkout-gnome-shell.sh
    - meson setup gnome-shell gnome-shell/build --prefix /usr -Dbuildtype=debugoptimized -Dman=false --werror --fatal-meson-warnings
    - sudo meson install -C gnome-shell/build

test-mutter-coverity:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $MUTTER_SCHEDULED_JOB == "coverity"'
      when: on_success
    - if: '$CI_COMMIT_BRANCH'
      when: 'manual'
  extends:
    - .mutter.distribution-image
    - .mutter.fedora@x86_64
  needs:
    - job: build-fedora-container@x86_64
      artifacts: false
  stage: analyze
  allow_failure: true
  script:
    - .gitlab-ci/download-coverity-tarball.sh
    - CC=clang meson setup coverity-build -Dprofiler=false
    - ./coverity/cov-analysis-linux64-*/bin/cov-build --dir cov-int meson compile -C coverity-build
    - tar czf cov-int.tar.gz cov-int
    - curl https://scan.coverity.com/builds?project=mutter
      --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"
  cache:
    key: coverity-tarball
    paths:
      - coverity

dist-mutter:
  extends:
    - .fdo.distribution-image@fedora
    - .mutter.fedora@x86_64
    - .build-mutter-base
    - .test-setup
  stage: deploy
  needs:
    - job: build-fedora-container@x86_64
      artifacts: false
  script:
    - meson setup . build --werror --prefix /usr
        -Dbuildtype=debugoptimized
    - glib-compile-schemas $GSETTINGS_SCHEMA_DIR
    - xvfb-run -a -s "$XVFB_SERVER_ARGS"
        ./src/tests/meta-dbus-runner.py
          --launch=wireplumber
          meson dist -C build
  rules:
    - if: '$CI_PIPELINE_SOURCE != "merge_request_event"'
      when: manual
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes:
        - "**/meson.build"
        - meson/*
      when: on_success
    - if: '$GITLAB_USER_LOGIN == "marge-bot"'
      when: on_success
    - if: '$CI_MERGE_REQUEST_ASSIGNEES == "marge-bot"'
      when: on_success

dist-mutter-tarball:
  extends: dist-mutter
  artifacts:
    expose_as: 'Get tarball here'
    paths:
      - build/meson-dist/$CI_PROJECT_NAME-$CI_COMMIT_TAG.tar.xz
  rules:
    - if: '$CI_COMMIT_TAG'

reference:
  extends:
    - .mutter.distribution-image
    - .mutter.fedora@x86_64
  stage: docs
  needs:
    - job: build-fedora-container@x86_64
      artifacts: false
  script:
    - meson setup . build --werror -Ddocs=true -Dtests=false
    - ninja -C build
    - mkdir references
    - mv build/doc/reference/{cally/cally,clutter/clutter,cogl/cogl,cogl-pango/cogl-pango,meta/meta,mtk/mtk} references/
  artifacts:
    expire_in: 1 week
    expose_as: 'Documentation'
    paths:
      - references/

pages:
  stage: deploy
  needs: ['reference']
  extends:
    - .skip-git-clone
  script:
    - mv references public/
  artifacts:
    paths:
      - public
  rules:
    - if: ($CI_DEFAULT_BRANCH == $CI_COMMIT_BRANCH && $CI_PROJECT_NAMESPACE == "GNOME")