stages:
  - build
  - analysis
  - pages
  - tarball
  - release

variables:
  TARBALL_PATH_PREFIX: ${CI_PROJECT_NAME}-${CI_COMMIT_TAG}
  TARBALL_NAME: ${TARBALL_PATH_PREFIX}.tar.xz

include:
  - component: gitlab.gnome.org/GNOME/citemplates/release-service@master
    inputs:
      job-stage: release
      dist-job-name: "tarball"
      tarball-artifact-path: "${TARBALL_NAME}"

.build:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v2
  stage: build
  script:
    - export SOURCE=$PWD
    - export PREFIX=$HOME/_prefix
    - mkdir $PREFIX
    - pushd po/ >/dev/null && export TXT=`intltool-update -m 2>&1` && popd >/dev/null
    - if [ ! -z "$TXT" ]; then echo -e "\\033[1;31m" 1>&2; echo "$TXT" 1>&2; echo -e "\\033[0m" 1>&2; exit 1; fi
    - mkdir -p _build && pushd _build
    - cmake -GNinja
            -DCMAKE_BUILD_TYPE=Release
            -DCMAKE_INSTALL_PREFIX=$PREFIX
            -DENABLE_GOA=ON
            -DENABLE_GTK=ON
            -DENABLE_GTK4=ON
            -DENABLE_EXAMPLES=ON
            -DENABLE_TESTS=ON
            -DWITH_LIBDB=OFF
            -DWITH_PHONENUMBER=ON
            -DENABLE_INTROSPECTION=${PERFORM_FULL_BUILD}
            -DENABLE_VALA_BINDINGS=${PERFORM_FULL_BUILD}
            -DENABLE_INSTALLED_TESTS=${PERFORM_FULL_BUILD}
            -DENABLE_GTK_DOC=${PERFORM_FULL_BUILD}
            -DENABLE_GI_DOCGEN=${PERFORM_FULL_BUILD}
            -DWITH_PRIVATE_DOCS=${PERFORM_FULL_BUILD}
            ..
    - ninja
    - ninja install
    - export CAMEL_TEST_REQUIRE_DOVECOT=1
    - ctest --force-new-ctest-process --output-on-failure
    - sudo /usr/libexec/cyrus-imapd/master -d
    - sleep 1
    - export TERM=dumb
    - echo 'createmailbox user/testuser' | cyradm --user cyrus --password cyruspass 127.0.0.1 10143
    - GSETTINGS_SCHEMA_DIR=$PREFIX/share/glib-2.0/schemas src/camel/tests/test-camel-provider-imapx --use-server $SOURCE/.gitlab-ci/cyrus-test-server.conf --data-dir $SOURCE/src/camel/tests/data
    - popd

fedora:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v2
  stage: build
  variables:
    PERFORM_FULL_BUILD: "ON"
  script:
    - !reference [".build", "script"]
    - mkdir public
    - cp -R $PREFIX/share/doc/camel public
    - cp -R $PREFIX/share/doc/libedataserver public
    - cp -R $PREFIX/share/doc/libebackend public
    - cp -R $PREFIX/share/doc/libebook-contacts public
    - cp -R $PREFIX/share/doc/libedata-book public
    - cp -R $PREFIX/share/doc/libebook public
    - cp -R $PREFIX/share/doc/libecal public
    - cp -R $PREFIX/share/doc/libedata-cal public
    - cp -R $PREFIX/share/doc/libedataserverui public
    - cp -R $PREFIX/share/doc/libedataserverui4 public
    - cp $SOURCE/docs/index.html public/
  artifacts:
    name: "${CI_PROJECT_NAME}-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    paths:
      - "_build/evolution-data-server-config.h"
      - "public"

flatpak:
  image: "quay.io/gnome_infrastructure/gnome-runtime-images:gnome-master"
  stage: build
  tags:
    - flatpak
  variables:
    BUNDLE: "evolution-data-server-nightly.flatpak"
    MANIFEST_PATH: "data/org.gnome.EvolutionDataServer.Devel.json"
    FLATPAK_MODULE: "evolution-data-server"
    APP_ID: "org.gnome.EvolutionDataServer.Devel"
    RUNTIME_REPO: "https://nightly.gnome.org/gnome-nightly.flatpakrepo"
  when: manual
  before_script:
    - export PATH="/usr/lib/ccache:$PATH"
    - export CCACHE_BASEDIR="$PWD"
    - export CCACHE_DIR="$PWD/ccache"
  script:
    - jq -M '(.modules[] | select(.name=="evolution-data-server")).sources[0].url = env.CI_REPOSITORY_URL' ${MANIFEST_PATH} > tmp.json && mv tmp.json ${MANIFEST_PATH}
    - jq -M 'del((.modules[] | select(.name=="evolution-data-server")).sources[0].branch)' ${MANIFEST_PATH} > tmp.json && mv tmp.json ${MANIFEST_PATH}
    - jq -M '(.modules[] | select(.name=="evolution-data-server")).sources[0].commit = env.CI_COMMIT_SHA' ${MANIFEST_PATH} > tmp.json && mv tmp.json ${MANIFEST_PATH}

    - flatpak-builder --verbose --keep-build-dirs --user --disable-rofiles-fuse flatpak_app --repo=repo --ccache ${BRANCH:+--default-branch=$BRANCH} ${MANIFEST_PATH}
    - flatpak build-bundle repo ${BUNDLE} --runtime-repo=${RUNTIME_REPO} ${APP_ID} ${BRANCH}
  cache:
    paths:
      - ccache
      - .flatpak-builder
  artifacts:
    name: "Flatpak artifacts"
    expose_as: "Get Flatpak bundle here"
    when: "always"
    paths:
      - "${BUNDLE}"
    expire_in: 14 days

static analysis:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v2
  stage: analysis
  script:
    - mkdir -p _build && cd _build
    - scan-build --html-title="scan-build (${CI_PROJECT_NAME})" -o scan-build sh ../.gitlab-ci/build-and-test.sh
    - cd ..
    - mkdir -p public
    # scan-build puts everything in a subdirectory (e.g. scan-build/ugly-name-here), so move that to a well-known name
    - mv _build/scan-build/* public/scan-build
  artifacts:
    name: "${CI_PROJECT_NAME}-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    paths:
      - "public/scan-build"

address and undefined behavior sanitizers:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v2
  stage: analysis
  variables:
    PERFORM_FULL_BUILD: "OFF"
    CFLAGS: "-fsanitize=address,undefined"
    CXXFLAGS: "-fsanitize=address,undefined"
    LSAN_OPTIONS: "suppressions=$CI_PROJECT_DIR/.gitlab-ci/lsan.supp"
  script:
    - mkdir -p public/scan-build
    - !reference [".build", "script"]

coverage:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v2
  stage: analysis
  needs:
    - job: fedora
      artifacts: false
  variables:
    PERFORM_FULL_BUILD: "OFF"
    CFLAGS: "-coverage -ftest-coverage -fprofile-arcs"
    CXXFLAGS: "-coverage -ftest-coverage -fprofile-arcs"
  script:
    - !reference [".build", "script"]
    - bash -x ./.gitlab-ci/gen-coverage.sh
  coverage: '/Coverage: \d+\.\d+/'
  artifacts:
    name: ${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}
    expire_in: 2 days
    when: always
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
    paths:
      - public

pages:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v2
  stage: pages
  needs:
    - job: fedora
      artifacts: true
    - job: coverage
      artifacts: true
    - job: "static analysis"
      artifacts: true
  interruptible: false
  variables:
    FLATPAK_MODULE: "evolution-data-server"
  script:
    - ls public/
  artifacts:
    paths:
    - public
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: never
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

tarball:
  stage: tarball
  image: "quay.io/gnome_infrastructure/gnome-runtime-images:gnome-master"
  dependencies:
    - fedora
  allow_failure: false
  script:
    - git config --global --add safe.directory `pwd`
    - git archive --prefix=${TARBALL_PATH_PREFIX}/ HEAD | xz -z > ${TARBALL_NAME}
  artifacts:
    name: "${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    expire_in: 14 days
    paths:
      - "${TARBALL_NAME}"
  rules:
    - if: $CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED
