image: gajim-master:latest

workflow:
  rules:
    - if: $NIGHTLY_BUILD
    - if: $CI_PIPELINE_SOURCE == "push"

stages:
  - test
  - build
  - deploy
  - publish

test-other:
  stage: test
  script:
    - codespell gajim
    - pip3 install build
    - python3 -m build
    - python3 ./pep517build/build_metadata.py
    - appstream-util validate dist/metadata/org.gajim.Gajim.appdata.xml
  interruptible: true

test-pyright:
  stage: test
  rules:
    - changes:
      - "**/*.py"
  script:
    - pip3 install git+https://dev.gajim.org/gajim/python-nbxmpp.git
    - pip3 install --config-settings=config=Gtk3,Gdk3,GtkSource4 git+https://github.com/pygobject/pygobject-stubs.git
    - npm install pyright@1.1.292
    - node_modules/.bin/pyright --version
    - node_modules/.bin/pyright
  interruptible: true

test-ruff:
  stage: test
  rules:
    - changes:
      - "**/*.py"
  script:
    - pip3 install ruff==0.0.237
    - ruff .
  interruptible: true

test-isort:
  stage: test
  rules:
    - changes:
      - "**/*.py"
  script:
    - pip3 install isort==5.12.0
    - isort --check .
  interruptible: true

test-unittest:
  stage: test
  script:
    - pip3 install -I git+https://dev.gajim.org/gajim/python-nbxmpp.git
    - coverage run --source=gajim -m unittest discover -s test -v
    - coverage report -mi
    - coverage xml -i
  coverage: "/TOTAL.+ ([0-9]{1,3}%)/"
  artifacts:
    reports:
      coverage_report: 
        coverage_format: cobertura
        path: coverage.xml
  interruptible: true

build-unix:
  image: gajim-build:latest
  stage: build
  dependencies: []
  rules:
    - if: '$DEBIAN_BUILD'
    - if: '$NIGHTLY_BUILD'
    - if: '$CI_COMMIT_TAG'
  script:
    - pip3 install build
    - python3 -m build

  artifacts:
    name: "gajim-$CI_COMMIT_REF_NAME-$CI_COMMIT_SHA"
    expire_in: 1 week
    paths:
      - dist/gajim-*.tar.gz
      - dist/gajim-*.whl

build-debian-nightly:
  image: gajim-build:latest
  stage: build
  variables:
    REV: "1"
  needs:
    - job: build-unix
      artifacts: true
  rules:
    - if: '$DEBIAN_BUILD'
    - if: '$NIGHTLY_BUILD'
  script:
    - apt-get update
    - apt-get install -y python3-nbxmpp-nightly
    - release-helper build-debian-pkg "$(find dist/gajim-*.tar.gz)" $REV --pkgsuffix=nightly

  artifacts:
    name: "gajim-$CI_COMMIT_REF_NAME-$CI_COMMIT_SHA"
    expire_in: 1 week
    paths:
      - debian_build/*

build-windows:
  image: gajim-build:latest
  stage: build
  dependencies: []
  rules:
    - if: '$CI_COMMIT_TAG'
  script:
    - >
      release-helper build-windows-pkg \
        --account=lovetox \
        --slug=gajim \
        --branch=master \
        --tag=$CI_COMMIT_TAG \
        --commit-sha=$CI_COMMIT_SHA \
        --api-key=$APPVEYOR_API_KEY \
        .ci/appveyor.yml
  artifacts:
    expire_in: 1 day
    paths:
      - windows_build/*.json

build-windows-nightly:
  image: gajim-build:latest
  stage: build
  dependencies: []
  rules:
    - if: '$NIGHTLY_BUILD'
  script:
    - >
      release-helper build-windows-pkg \
        --account=lovetox \
        --slug=gajim \
        --branch=master \
        --tag=Nightly \
        --commit-sha=$CI_COMMIT_SHA \
        --api-key=$APPVEYOR_API_KEY \
        .ci/appveyor.yml
  artifacts:
    expire_in: 1 day
    paths:
      - windows_build/*.json

deploy-unix:
  image: gajim-deploy:latest
  stage: deploy
  dependencies:
    - build-unix
  rules:
    - if: '$CI_COMMIT_TAG'
  script:
    - >
      release-helper deploy-to-ftp \
        --host=$FTP_HOST \
        --user=$FTP_USER \
        --password=$FTP_PASS \
        --directory=downloads/"$(echo $CI_COMMIT_TAG | cut -c 1-3)" \
        --rename=gajim-$CI_COMMIT_TAG.tar.gz \
        "$(find dist/gajim-*.tar.gz)"

deploy-unix-nightly:
  image: gajim-deploy:latest
  stage: deploy
  dependencies:
    - build-unix
  rules:
    - if: '$NIGHTLY_BUILD'
  allow_failure:
    exit_codes:
      - 100
  script:
    - release-helper nightly-check
    - >
      release-helper deploy-to-ftp \
        --host=$FTP_HOST \
        --user=$FTP_USER \
        --password=$FTP_PASS \
        --directory=downloads/snap \
        --rename=gajim-"$(date +'%Y-%m-%d')".tar.gz \
        "$(find dist/gajim-*.tar.gz)"

deploy-debian-nightly:
  image: gajim-deploy:latest
  stage: deploy
  dependencies:
    - build-debian-nightly
  rules:
    - if: '$DEBIAN_BUILD'
    - if: '$NIGHTLY_BUILD'
  allow_failure:
    exit_codes:
      - 100
  script:
    - release-helper nightly-check
    - >
      release-helper deploy-to-ftp \
        --host=$FTP_HOST \
        --user=$FTP_USER \
        --password=$FTP_PASS \
        --directory=debian/gajim/"$(date +'%Y%m%d')" \
        debian_build

deploy-windows:
  image: gajim-deploy:latest
  stage: deploy
  dependencies:
    - build-windows
  rules:
    - if: '$CI_COMMIT_TAG'
  script:
    - release-helper download-appveyor-artifacts "$(find windows_build/*.json)" windows_build
    - rm windows_build/*.json
    - >
      release-helper deploy-to-ftp \
        --host=$FTP_HOST \
        --user=$FTP_USER \
        --password=$FTP_PASS \
        --directory=downloads/"$(echo $CI_COMMIT_TAG | cut -c 1-3)" \
        windows_build

deploy-windows-nightly:
  image: gajim-deploy:latest
  stage: deploy
  dependencies:
    - build-windows-nightly
  rules:
    - if: '$NIGHTLY_BUILD'
  allow_failure:
    exit_codes:
      - 100
  script:
    - release-helper nightly-check
    - release-helper download-appveyor-artifacts "$(find windows_build/*.json)" windows_build
    - rm windows_build/*.json
    - >
      release-helper deploy-to-ftp \
        --host=$FTP_HOST \
        --user=$FTP_USER \
        --password=$FTP_PASS \
        --directory=downloads/snap/win \
        windows_build

deploy-flatpak:
  image: gajim-deploy:latest
  stage: deploy
  dependencies: []
  rules:
    - if: '$CI_COMMIT_TAG'
  before_script:
    - eval $(ssh-agent -s)
    - .ci/prepare_git.sh github.com "${FLATHUB_DEPLOY_KEY}"
  script:
    # extract the current commit hash of the shared-modules submodule
    - submodule_commit=$(git submodule status flatpak/shared-modules | cut -c 2-41)
    - git clone git@github.com:flathub/org.gajim.Gajim.git
    - cd org.gajim.Gajim
    - mv ../flatpak/org.gajim.Gajim.yaml org.gajim.Gajim.yaml
    - mv ../flatpak/app-overrides.json app-overrides.json
    # point the shared-modules submodule to the new commit, without fetching the repository
    - git update-index --cacheinfo 160000,${submodule_commit},shared-modules
    - git diff
    - git add --all
    - git commit -m "$CI_COMMIT_TAG"
    - git push

publish-website:
  image: gajim-publish:latest
  stage: publish
  dependencies: []
  rules:
    - if: '$CI_COMMIT_TAG'
  before_script:
    - eval $(ssh-agent -s)
    - .ci/prepare_git.sh dev.gajim.org "${WEBSITE_DEPLOY_KEY}"
  script:
    - git clone git@dev.gajim.org:gajim/website.git
    - cd website
    - git checkout master
    - python3 ./scripts/bump_version.py $CI_COMMIT_TAG
    - git diff
    - git add -u
    - git commit -m "Release Gajim $CI_COMMIT_TAG"
    - git push

publish-release:
  image: gajim-publish:latest
  stage: publish
  dependencies: []
  rules:
    - if: '$CI_COMMIT_TAG'
  script:
    - release-helper make-gitlab-release $CI_COMMIT_TAG "${PROJECT_TOKEN}"
