# SPDX-FileCopyrightText: 2024 Gerald Wiese
#
# SPDX-License-Identifier: GPL-3.0-or-later

# woodpecker syntax: https://woodpecker-ci.org/docs/usage/workflow-syntax

# This ci is used to auto update po files of his.git, they do following jobs:
# 1. clone his.git dev-5.0 branch to tmp/his dir.
# 2. clone his-utils.git dev-5.0 branch to tmp/his-utils dir.
# 3. Install gnuhealth to tmp/ghis dir with the help of devel-installer in his-utils.
# 4. merge origin/translate branch to dev-5.0 branch.
# 5. Use po-export tool to update po files.
# 6. push to origin/translate branch.
# 7. push to origin/dev-5.0 branch.

# we have added ci-test and ci-test-output branch, if we will test ci,
# we can force push to ci-test, then the changes will be force pushed
# to ci-test-output branch.

depends_on:
  - lint


steps:
  - name: clone-his-utils-git
    image: woodpeckerci/plugin-git
    settings:
      remote: https://codeberg.org/gnuhealth/his-utils.git
      branch: dev-5.0
      path: tmp/his-utils-git
      sha: ''
      ref: 'refs/heads/dev-5.0'
      partial: true
      lfs: false
    when:
      - branch: [dev-5.0, translate, ci-test]
        event: [push, manual]

  - name: clone-his-git
    image: woodpeckerci/plugin-git
    settings:
      remote: https://codeberg.org/gnuhealth/his.git
      branch: dev-5.0
      path: tmp/his-git
      sha: ''
      ref: 'refs/heads/dev-5.0'
      partial: false
      lfs: false
    when:
      - branch: [dev-5.0, translate]
        event: [push, manual]

  - name: clone-his-git-for-ci-test
    image: woodpeckerci/plugin-git
    settings:
      remote: https://codeberg.org/gnuhealth/his.git
      branch: ci-test
      path: tmp/his-git
      sha: ''
      ref: 'refs/heads/ci-test'
      partial: false
      lfs: false
    when:
      - branch: [ci-test]
        event: [push, manual]

  - name: run-po-export-script
    image: debian
    commands:
      - apt-get update -y
      # installing packages in woodpecker of codeberg.org is very
      # slow, so just install packages necessary.
      - apt-get install -y --no-install-recommends --no-upgrade sqlite3 patch python3-pip python3-venv python3-dev gcc libsasl2-dev libldap2-dev git gettext libmagic1
      - git config --global user.email "po-export@po-export.invalid"
      - git config --global user.name "po-export"
      # We git clone his.git and his-utils.git to 'tmp' directory.
      - cd tmp
      # Install gnuhealth with the help of devel-installer in his-utils repo.
      - his-utils-git/devel-installer/devel-installer.sh --git-repo his-git --install-dir ghis --sqlite-db po-export-db
      # NOTE: if we do not use --fast-export argument, po-export.sh in
      # woodpecker will run > 1 hour, then fail for context deadline
      # exceeded reason, so if we change some packages with big xml
      # data files, for example health_gene*, we should run
      # po-export.sh in our own machine without --fast-export
      # argument.
      - ghis/ghis_env his-utils-git/scripts/po-export/update-his-repo.sh his-git --use-sqlite --database po-export-db --fast-export
    when:
      - branch: [dev-5.0, translate, ci-test]
        event: [push, manual]

  - name: push-po-changes-to-his-ci-test-output-branch
    image: appleboy/drone-git-push
    settings:
      path: tmp/his-git
      remote: git@codeberg.org:gnuhealth/his.git
      ssh_key:
        from_secret: ssh_deploy_key
      branch: ci-test-output
      force: true
      commit: false
    when:
      - branch: [ci-test]
        event: [push, manual]

  - name: push-po-changes-to-his-translate-branch
    image: appleboy/drone-git-push
    settings:
      path: tmp/his-git
      remote: git@codeberg.org:gnuhealth/his.git
      ssh_key:
        from_secret: ssh_deploy_key
      branch: translate
      force: false
      commit: false
    when:
      - branch: [dev-5.0, translate]
        event: [push, manual]

  - name: push-po-changes-to-his-dev-branch
    image: appleboy/drone-git-push
    settings:
      path: tmp/his-git
      remote: git@codeberg.org:gnuhealth/his.git
      ssh_key:
        from_secret: ssh_deploy_key
      branch: dev-5.0
      force: false
      commit: false
    when:
      - branch: [dev-5.0, translate]
        event: [push, manual]
