# Write CI cache once for faster runs
'write cache: ruby':
  stage: pre
  cache: !reference [.cache, read_write_ruby]
  before_script:
    - !reference [.scripts, source_rvm]
  script:
    - !reference [.scripts, bundle_install]
  after_script: []

'write cache: nodejs':
  stage: pre
  cache: !reference [.cache, read_write_nodejs]
  before_script: []
  script:
    - !reference [.scripts, pnpm_install_all]
  after_script: []

'push to github':
  stage: pre
  cache: []
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /^(private|cherry-pick-|renovate|dependabot)/
      when: never
    - when: always
  before_script:
    - test -d $HOME/.ssh || mkdir $HOME/.ssh
    - ssh-keyscan github.com >> $HOME/.ssh/known_hosts
    - eval $(ssh-agent -s)
    - echo "$PUSH_TO_GITHUB_SSH_PRIVATE_KEY" | ssh-add -
    - ssh-add -l
  script:
    - git fetch --unshallow
    - script/build/sync_repo.sh git@github.com:zammad/zammad.git
  after_script: []

'publish source archives':
  stage: pre
  tags:
    - push-to-ftp
  cache: []
  rules:
    - if: $CI_COMMIT_TAG
      when: always
    - if: $CI_COMMIT_BRANCH == "stable"
      when: always
    - when: never
  before_script:
    - test -d $HOME/.ssh || mkdir $HOME/.ssh
    - ssh-keyscan ftp.zammad.com >> $HOME/.ssh/known_hosts
    - eval $(ssh-agent -s)
    - echo "$FTP_ZAMMAD_COM_SSH_PRIVATE_KEY" | ssh-add -
    - ssh-add -l
  script:
    - script/build/deploy_archives.sh
  after_script: []

# This job will create commits in pipelines pushed from Weblate.
# It re-generates the catalog, and any additional files such as view templates and chat assets.
'push translations update commit':
  stage: pre
  needs: ['write cache: ruby', 'write cache: nodejs']
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /Translations update from translations[.]zammad[.]org/
      when: always
    - when: never
  cache:
    - !reference [.cache, read_only_ruby]
    - !reference [.cache, read_only_nodejs]
  services:
    - !reference [.services, postgresql]
  before_script:
    - !reference [.scripts, source_rvm]
    - !reference [.scripts, bundle_install]
    - !reference [.scripts, configure_environment]
    - !reference [.scripts, zammad_db_init]
    - !reference [.scripts, pnpm_install_chat]
  variables:
    ZAMMAD_SAFE_MODE: 1
  script:
    - bundle exec rails generate zammad:translation_catalog --full
    - (cd public/assets/chat && pnpm exec gulp build)
    - git status
    - git config user.email "noreply@weblate.org"
    - git config user.name "Weblate"
    # TODO drop usage of dedicated access token when CI_JOB_TOKEN support for commit pushing was added to weblate
    #   https://docs.gitlab.com/ee/ci/jobs/ci_job_token.html#git-push-to-your-project-repository
    - git remote add gitlab_origin https://oauth2:${WEBLATE_ACCESS_TOKEN}@${CI_SERVER_FQDN}/${CI_PROJECT_PATH}.git
    - git add .
    - git status --porcelain | grep "^[MA]" || exit 0 # check if files were added/changed
    - 'git commit -m "Maintenance: Update translated files and catalog"'
    - git show --stat
    - git push gitlab_origin HEAD:$CI_COMMIT_REF_NAME