image: fedora:latest

.flatpak:
  image: 'quay.io/gnome_infrastructure/gnome-runtime-images:gnome-master'
  stage: 'test'
  interruptible: true
  variables:
    LANG: "C.UTF-8"
    # Force Software rendering for GL and Vulkan so the tests run locally
    LIBGL_ALWAYS_SOFTWARE: true
    # This the hardcoded value for llvmpipe
    MESA_VK_DEVICE_SELECT: "10005:0"
    # Set to anything else to skip tests
    RUN_TESTS: "1"
    # vala is still using autotools, otherwise there is no reason
    # to skip dist
    MESON_DIST: "1"
    # Pass "--runtime" in order to build a runtime or extension
    EXPORT_RUNTIME: ""
    NIGHTLY_REPO: "https://nightly.gnome.org/gnome-nightly.flatpakrepo"
    USE_CACHE: "1"
    _ORAS_CACHE_REGISTRY: "quay.io"
    _ORAS_CACHE_REPOSITORY: "gnome_infrastructure/gnome-nightly-cache"
    # Default for backwards compatibility
    ARCH: "x86_64"
  # Till now this was the template job used for everything,
  # so assume its default tags are x86 to avoid breaking backwards
  # compatibility.
  tags:
    - flatpak
  script:
    - cat /etc/os-release

    - whoami && id -u && id -g

    - flatpak --version
    # Report the installed versions of the runtime
    - flatpak info org.gnome.Platform
    - flatpak info org.gnome.Sdk

    # Print the date, since appstream depends on local timezone
    - date && date -u

    # Make sure we don't end up with any leftover volume cache
    # as we are going to extract our own anyway
    - rm -rf .flatpak-builder

    - app_id_lc=${APP_ID,,}
    # FIXME: Only hardcode cache for main atm
    # eventually we can also do stable branch caches but that needs more logic
    # to determine when and what to pull and push
    - branch=main
    - registry="${_ORAS_CACHE_REGISTRY}/${_ORAS_CACHE_REPOSITORY}:${ARCH}-${app_id_lc}-${branch}"
    - echo "Pulling cache from ${registry}"
    - oras pull $registry || true
    - tar --extract --xattrs --zstd --file "builder.tar.zstd" || true
    - du -hs "builder.tar.zstd" || true

    - |-
      if [[ -n "$BRANCH" ]]; then
          default_branch="$BRANCH"
      elif [[ -n "$CI_MERGE_REQUEST_IID" ]]; then
          default_branch="mr-$CI_MERGE_REQUEST_IID"
      elif [[ -n "$CI_DEFAULT_BRANCH" ]] && [[ "$CI_DEFAULT_BRANCH" == "$CI_COMMIT_BRANCH" ]]; then
          default_branch="master"
      else
          default_branch="test"
      fi
    - echo "${default_branch}"

    - export REWRITE_RUN_TESTS="--run-tests"
    - |-
      if [[ "${RUN_TESTS}" != "1" ]]; then
        export REWRITE_RUN_TESTS="--no-run-tests"
      fi
    - echo "${REWRITE_RUN_TESTS}"

    - rewrite-flatpak-manifest "${REWRITE_RUN_TESTS}" "${MANIFEST_PATH}" "${FLATPAK_MODULE}" -- ${CONFIG_OPTS}

    - >-
      xvfb-run -a -s "-screen 0 1024x768x24" --
      dbus-run-session
      flatpak-builder ${CI_FB_ARGS} --default-branch="${default_branch}" --ccache --keep-build-dirs --user --disable-rofiles-fuse --build-only flatpak_app --repo=repo "${MANIFEST_PATH}"

    # rewrite-flatpak-manifest will make our checkout dirty and meson will complain
    # FIXME: we need to copy the manifest rather than modify it in place to avoid making the checkout dirty
    # Meson dist is still fine for now however, as meson will use the last commit rather than the current state of the repository
    # FIXME: We end up creating 2 "build" sandboxes and its this one that ends up with the dist apparently
    # our release service will be relying on the hardcoded path of
    # build/${FLATPAK_MODULE}-2/_flatpak_build/meson-dist/${CI_PROJECT_NAME}-${CI_COMMIT_TAG}.tar.xz"
    # to contain the tarball. This needs to never change.
    # build/${FLATPAK_MODULE}/ is a symlink to -2 atm thankfully as well.
    - |-
      cat > dist.sh <<'EOF'
      #! /bin/bash
      set -ex
      if [[ "${MESON_DIST}" == "1" ]]; then
          flatpak-builder ${CI_FB_ARGS} --default-branch="${default_branch}" --ccache --keep-build-dirs --user --disable-rofiles-fuse --build-shell="${FLATPAK_MODULE}" --disable-download --disable-updates flatpak_app "${MANIFEST_PATH}" <<'END'
            LANG=C.UTF-8 meson dist --no-tests --include-subprojects --allow-dirty
      END

          # The path doesn't always exit, and won't work in some cases
          # This is a partial workaround for:
          # https://gitlab.gnome.org/GNOME/citemplates/-/issues/32
          dist_path=".flatpak-builder/build/${FLATPAK_MODULE}-2/_flatpak_build/meson-dist/"
          if [[ -d "$dist_path" ]]; then
              cp --recursive --preserve=all "$dist_path" "$CI_PROJECT_DIR/public-dist/"
          fi
      fi
      EOF
    - bash ./dist.sh

    - flatpak-builder ${CI_FB_ARGS} --default-branch="${default_branch}" --ccache --user --disable-rofiles-fuse --finish-only --disable-download --disable-updates flatpak_app --repo=repo "${MANIFEST_PATH}"

    # Generate a Flatpak bundle
    - flatpak build-bundle repo "$CI_PROJECT_DIR/${BUNDLE}" ${EXPORT_RUNTIME} --runtime-repo="${RUNTIME_REPO:-$NIGHTLY_REPO}" "${APP_ID}" "${default_branch}"
    - tar cf "$CI_PROJECT_DIR/repo.tar" repo/

    # Export the documentation if it exist
    - docs_path="flatpak_app/files/share/doc/"
    - >-
      if [[ -d "$docs_path" ]]; then
          tar --create --auto-compress --file "$CI_PROJECT_DIR/${CI_PROJECT_NAME}-docs.tar.gz" --directory $docs_path .
      fi

    # Fix dist-path for artifacts
    # Previously gitlab would create a copy of the files in the symlink `build/${FLATPAK_MODULE}-2/` ->  `build/${FLATPAK_MODULE}/`
    # and the artifacts would contain the tarball under `build/${FLATPAK_MODULE}`. This changed recently
    # and now symlinks are ignored, which broke the release-service component as we were recommending people use
    # TARBALL_ARTIFACT_PATH: ".flatpak-builder/build/${FLATPAK_MODULE}/_flatpak_build/meson-dist/${CI_PROJECT_NAME}-${CI_COMMIT_TAG}.tar.xz"
    # in the old handbook documentation.
    # Manually unlink and move the meson-dist so things keep working.
    - dist_path=".flatpak-builder/build/${FLATPAK_MODULE}/_flatpak_build/meson-dist/"
    - dist_path_real=".flatpak-builder/build/${FLATPAK_MODULE}-2/_flatpak_build/meson-dist/"
    - |-
      if [[ -d "$dist_path" ]]; then
          unlink .flatpak-builder/build/${FLATPAK_MODULE}
          mkdir -p .flatpak-builder/build/${FLATPAK_MODULE}/_flatpak_build/
          mv "$dist_path_real" ".flatpak-builder/build/${FLATPAK_MODULE}/_flatpak_build/meson-dist/"
      fi

    - >-
      if [[ -n "$NIGHTLY_CACHE_ORAS_TOKEN_FILE" ]] && [[ "$USE_CACHE" == "1" ]] && [[ $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH ]]; then
        echo "Uploading cache..."
        oras logout ${_ORAS_CACHE_REGISTRY} || true
        cat $NIGHTLY_CACHE_ORAS_TOKEN_FILE | oras login -u "${NIGHTLY_CACHE_ORAS_USER}" --password-stdin ${_ORAS_CACHE_REGISTRY} || true
        tar --create --xattrs --zstd --file "builder.tar.zstd" --exclude .flatpak-builder/build --exclude .flatpak-builder/rofiles .flatpak-builder/ || true
        du -hs "builder.tar.zstd" || true
        oras push $registry "builder.tar.zstd" || true
        oras logout ${_ORAS_CACHE_REGISTRY} || true
      fi
  artifacts:
    name: 'Flatpak artifacts'
    when: 'always'
    paths:
      - $BUNDLE
      - 'repo.tar'
      - "${CI_PROJECT_NAME}-docs.tar.gz"
      - '.flatpak-builder/build/**/_flatpak_build/meson-logs/'
      - '.flatpak-builder/build/**/_flatpak_build/meson-dist/'
      - 'public-dist'
    reports:
      junit: ".flatpak-builder/build/${FLATPAK_MODULE}*/_flatpak_build/meson-logs/testlog*.junit.xml"
    expire_in: 14 days

.flatpak@x86_64:
  extends: '.flatpak'
  variables:
    ARCH: "x86_64"
  tags:
    - flatpak

.flatpak@aarch64:
  extends: '.flatpak'
  variables:
    ARCH: "aarch64"
  tags:
    - flatpak-aarch64

stages:
- check
- build-flatpak
- deploy

variables:
  BUNDLE: saldo-dev.flatpak
  GIT_SUBMODULE_STRATEGY: recursive
flatpak:
  extends: ".flatpak"
  stage: build-flatpak
  tags:
  variables:
    APP_ID: 'org.tabos.saldo'
    FLATPAK_MODULE: 'saldo'
    MANIFEST_PATH: 'org.tabos.saldo.json'
    RUNTIME_REPO: https://nightly.gnome.org/gnome-nightly.flatpakrepo

codespell:
  stage: check
  script:
    - dnf install -y codespell
    - codespell saldo -L crate -S "*.png,*.po,.git,*.jpg,eff_large_wordlist.txt,.mypy_cache,_build" -f

mypy:
  stage: check
  script:
    - dnf install -y python3-mypy python3-cryptography python3-gobject pip
    - pip install schwifty fints mt940
    - mypy saldo --explicit-package-bases  --ignore-missing-imports --disallow-incomplete-defs

ruff:
  stage: check
  script:
    - dnf install -y ruff
    - ruff format --check saldo
    - ruff check saldo
