include: 'https://gitlab.gnome.org/GNOME/citemplates/raw/master/flatpak/flatpak_ci_initiative.yml'

cache:
  paths:
    - _ccache/

variables:
  BUNDLE: 'epiphany-git.flatpak'
  MANIFEST_PATH: 'org.gnome.Epiphany.json'
  RUNTIME_REPO: 'https://nightly.gnome.org/gnome-nightly.flatpakrepo'
  FLATPAK_MODULE: 'epiphany'
  APP_ID: 'org.gnome.Epiphany.Devel'
  ESLINT_LOG: "eslint-report.txt"

stages:
  - test

check-code-style:
  image: registry.gitlab.gnome.org/gnome/epiphany/master:v4
  interruptible: true
  script:
    - bash data/check-code-style
    - eslint -o eslint-report.txt --no-color embed/web-process-extension/resources/js/ || { cat $ESLINT_LOG; false; }
  artifacts:
    paths:
      - ${ESLINT_LOG}
    when: on_failure
  except:
    - gnome-*

flatpak:
  extends: '.flatpak'
  variables:
    CONFIG_OPTS: '-Dprofile=Devel -Dunit_tests=enabled -Dgsb_api_key=${GSB_API_KEY} -Denable_gsb=true --werror'
  except:
    - master
    - gnome-*

flatpak stable:
  extends: '.flatpak'
  variables:
    CONFIG_OPTS: '-Dprofile=Devel -Dunit_tests=enabled -Dgsb_api_key=${GSB_API_KEY} -Denable_gsb=true -Dsoup2=enabled'
  only:
    - gnome-*

flatpak master:
  extends: '.flatpak'
  variables:
    CONFIG_OPTS: '-Dtech_preview=true -Dprofile=Devel -Dunit_tests=enabled -Dgsb_api_key=${GSB_API_KEY} -Denable_gsb=true'
  only:
    - master

scanbuild:
  extends: '.flatpak'
  image: "registry.gitlab.gnome.org/gnome/gnome-runtime-images/llvm_bundle:master"
  variables:
    CONFIG_OPTS: '-Dprofile=Devel -Dunit_tests=enabled'
  script:
    - flatpak-builder --user --disable-rofiles-fuse --stop-at=${FLATPAK_MODULE} flatpak_app ${MANIFEST_PATH}
    - flatpak build flatpak_app bash -c "source /usr/lib/sdk/llvm12/enable.sh; meson --prefix=/app ${CONFIG_OPTS} _build; ninja -C _build scan-build"
    - if [[ -n "$(ls -A _build/meson-logs/scanbuild/)" ]]; then echo "Scan build log found, assuming defects exist"; exit 1; fi
  artifacts:
    when: on_failure
    paths:
      - _build/meson-logs/scanbuild

nightly:
  extends: '.publish_nightly'
  stage: .post
  dependencies: ['flatpak master']

canary:
  image: 'registry.gitlab.gnome.org/gnome/gnome-runtime-images/gnome:master'
  stage: 'test'
  interruptible: true
  tags:
    - flatpak
  variables:
    SDK_REPO: 'https://software.igalia.com/flatpak-refs/webkit-sdk.flatpakrepo'
    BUNDLE: 'epiphany-canary.flatpak'
  script:
    - pip3 install --user requests
    # TODO: Switch to debug? 5GB downloads though.
    - python generate-canary-manifest.py --release
    - flatpak remote-add --user --if-not-exists webkit-sdk ${SDK_REPO}
    - flatpak-builder --user --install-deps-from=webkit-sdk --disable-rofiles-fuse --repo=repo canary_flatpak_app org.gnome.Epiphany.Canary.json
    - flatpak build-bundle repo ${BUNDLE} --runtime-repo=${SDK_REPO} org.gnome.Epiphany.Canary
    - tar cf canary-repo.tar repo/
    - rm -rf canary-repo canary_flatpak_app org.gnome.Epiphany.Canary.json webkitgtk.zip

  artifacts:
    name: 'Canary Flatpak artifacts'
    expose_as: 'Get Canary Flatpak bundle here'
    when: 'always'
    paths:
      - "${BUNDLE}"
      - "canary-repo.tar"
    expire_in: 14 days
  cache:
    - key: "$CI_JOB_NAME"
      paths:
        - '.flatpak-builder/downloads'
        - '.flatpak-builder/git'
    - key: "$CI_JOB_NAME"
      paths:
        - '.flatpak-builder/cache'
        - '.flatpak-builder/ccache'
  except:
    - gnome-*

canary nightly:
  extends: '.publish_nightly'
  stage: .post
  dependencies: ['canary']
  before_script:
    - mv canary-repo.tar repo.tar
