.template: &job-deploy-template
  image: gitlab.wpdesk.dev:5050/wpdesk/docker-tests/amazon-svn-deploy:24.09
  stage: deploy
  tags:
    - deploy
  dependencies:
    - build to deploy
  retry: 2
  when: manual
  only:
    - tags

build to deploy:
  image: wpdesknet/phpunit-woocommerce:0-0
  stage: pre-deploy
  tags:
    - deploy
  needs:
    - prepare prefixed vendor
    - prepare translations
    - job: prepare npm assets
      optional: true
  artifacts:
    expire_in: 6 mos
    name: "production release"
    paths:
      - release
      - release.zip
  before_script:
    - !reference [.composer-auth, script]
  script:
    - echo ${WPDESK_CI_VERSION}
    - php --version
    - ls -l
    - rm -rf ${CI_PROJECT_DIR}/release ${CI_PROJECT_DIR}/release.zip /tmp/release
    - mkdir /tmp/release
    - mkdir -p ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}
    - ls -al ${CI_PROJECT_DIR}
    - cp -rf ${CI_PROJECT_DIR}/* /tmp/release
    - if [ -d "${CI_PROJECT_DIR}/.wordpress-org" ]; then mkdir /tmp/release/.wordpress-org && cp -rf ${CI_PROJECT_DIR}/.wordpress-org/* /tmp/release/.wordpress-org; fi
    - cp -rf /tmp/release/* ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}
    - if [ -d "/tmp/release/.wordpress-org" ]; then mkdir ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}/.wordpress-org && cp -rf /tmp/release/.wordpress-org/* ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}/.wordpress-org; fi
    - cd ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}
    - php -d memory_limit=-1 /usr/local/bin/composer install --no-dev --no-progress --optimize-autoloader --prefer-dist
    - rm -f auth.json
    - rm -rf build-coverage release tests docs .git .editorconfig .gitignore .gitlab-ci.yml apigen.neon phpunit.xml acceptance test_soap.php .gitlab
    - rm -rf composer.lock auth.json phpcs.xml.dist phpunit-integration.xml phpunit-unit.xml composer.phar wp-cli.phar functional tmp_artifacts .tmp tools codeception.dist.yml .env.testing wp-install.sh
    - rm -rf phpcs.xml.dist phpunit-integration.xml phpunit-unit.xml wp-cli.phar functional tmp_artifacts scoper.inc.php phpstan.dist phpstan.neon.dist
    - rm -rf assets-src package.json package-lock.json .babelrc webpack.mix.js webpack.config.js README.md
    - find ./vendor -type d -name "tests" -exec rm -rf {} \; 2>/dev/null || (exit 0)
    - find ./vendor -name "LICENSE" -delete
    - find ./vendor -name "README.md" -delete
    - find ./vendor -name ".gitlab-ci.yml" -delete
    - cd ../
    - zip -r -q ../release.zip ./
  except:
    variables:
      - $IS_LIBRARY
      - $IS_NPM_LIBRARY

plugin zip:
  image: wpdesknet/phpunit-woocommerce:0-0
  stage: pre-deploy
  tags:
    - deploy
  needs:
    - build to deploy
  artifacts:
    expire_in: 24 mos
    name: "$CI_PROJECT_NAME-${CI_COMMIT_REF_SLUG}"
    paths:
      - "*"
  script:
    - ls -al
    - rm -rf $(ls -A | grep -v release)
    - ls -al
    - rm -rf ${CI_PROJECT_NAME}
    - if [ -d "${CI_PROJECT_DIR}/.wordpress-org" ]; then rm -rf .wordpress-org; fi;
    - mv release/${CI_PROJECT_NAME} ./
    - rm -rf ./release
    - rm -rf ./*.zip
    - ls -al
  except:
    variables:
      - $IS_LIBRARY
      - $IS_NPM_LIBRARY

deploy to shop:
  <<: *job-deploy-template
  script:
    - WP_TESTED_UP=`grep -h 'Tested up to' *.php | grep -oE '([0-9]+.?)+'`
    - WP_REQUIRES_AT_LEAST=`grep -h 'Requires at least' *.php | grep -oE '([0-9]+.?)+'`
    - WC_TESTED_UP=`grep -h 'WC tested up to' *.php | grep -oE '([0-9]+.?)+' || echo "not set!"`
    - WC_REQUIRES_AT_LEAST=`grep -h 'WC requires at least' *.php | grep -oE '([0-9]+.?)+' || echo "not set!"`
    - PHP_REQUIRES=`grep -h 'Requires PHP' *.php | grep -oE '([0-9]+.?)+'`
    - echo ${WPDESK_CI_VERSION}
    - /tmp/deploy_shop.sh ${CI_PROJECT_NAME} release.zip ${CI_PROJECT_NAME}.zip ${WP_TESTED_UP} ${WC_TESTED_UP} ${WP_REQUIRES_AT_LEAST} ${WC_REQUIRES_AT_LEAST} ${PHP_REQUIRES}
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <${CI_PROJECT_URL}|${CI_PROJECT_NAME}> zdeployowany do sklepu w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/${SLACK_AUTH}'
  environment:
    name: wpdesk shop
    url: https://wpdeskplugin.s3.amazonaws.com/${CI_PROJECT_NAME}.zip
  except:
    variables:
      - $IS_LIBRARY
      - $IS_NPM_LIBRARY

deploy to license:
  <<: *job-deploy-template
  script:
    - apt-get install sshpass
    - ssh-keyscan ${WPDESK_LICENSE_HOST} >> ~/.ssh/known_hosts
    - sshpass -p ${WPDESK_LICENSE_PASSWORD} ssh plugin_deploy@${WPDESK_LICENSE_HOST} "rm -f /var/www/license/var/files/${CI_PROJECT_NAME}.zip"
    - sshpass -p ${WPDESK_LICENSE_PASSWORD} scp release.zip plugin_deploy@${WPDESK_LICENSE_HOST}:/var/www/license/var/files/${CI_PROJECT_NAME}.zip
    - sshpass -p ${WPDESK_LICENSE_PASSWORD} scp release.zip plugin_deploy@${WPDESK_LICENSE_HOST}:/var/www/license/var/files/${CI_PROJECT_NAME}-${CI_COMMIT_REF_NAME}.zip
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <${CI_PROJECT_URL}|${CI_PROJECT_NAME}> zdeployowany do systemu licencji w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/${SLACK_AUTH}'
  environment:
    name: wpdesk license server
    url: https://license.wpdesk.pl/
  except:
    variables:
      - $IS_LIBRARY
      - $IS_NPM_LIBRARY

deploy to repository:
  <<: *job-deploy-template
  cache:
    key: ${CI_PROJECT_NAME}-svn
    paths:
      - svn-repository
  script:
    - echo ${WPDESK_CI_VERSION}
    - mkdir -p ${CI_PROJECT_DIR}/svn-repository
    - rm -f ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}/lang/*.po ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}/lang/*.mo ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}/lang/*.json
    - /tmp/deploy_repository.sh ${CI_PROJECT_NAME} ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME} ${CI_PROJECT_DIR}/svn-repository/${CI_PROJECT_NAME}
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <${CI_PROJECT_URL}|${CI_PROJECT_NAME}> zdeployowany do repozytorium WP w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/${SLACK_AUTH}'
  environment:
    name: wordpress repository
    url: https://downloads.wordpress.org/plugin/${CI_PROJECT_NAME}.${CI_COMMIT_REF_NAME}.zip
  except:
    variables:
      - $IS_LIBRARY
      - $IS_NPM_LIBRARY

update wpdesk plugins database:
  image: mysql:5.7
  stage: post-deploy
  tags:
    - deploy
  only:
    - tags
  except:
    variables:
      - $IS_LIBRARY
      - $IS_NPM_LIBRARY
  script:
    - WP_TESTED_UP=`grep -h 'Tested up to' *.php | grep -oE '([0-9]+.?)+'`
    - WC_TESTED_UP=`grep -h 'WC tested up to' *.php | grep -oE '([0-9]+.?)+' || echo "not set!"`
    - WP_REQUIRES_AT_LEAST=`grep -h 'Requires at least' *.php | grep -oE '([0-9]+.?)+'`
    - WC_REQUIRES_AT_LEAST=`grep -h 'WC requires at least' *.php | grep -oE '([0-9]+.?)+' || echo "not set!"`
    - PHP_REQUIRES=`grep -h 'Requires PHP' *.php | grep -oE '([0-9]+.?)+'`
    - mysql ${WPDESK_PLUGINS_DATABASE} -e "insert into wpdesk_plugin_deploys (project_name, plugin_version, wp_tested_up, wc_tested_up, wp_requires_at_least, wc_requires_at_least, php_requires) values ( '${CI_PROJECT_NAME}', '${CI_COMMIT_REF_NAME}', '${WP_TESTED_UP}', '${WC_TESTED_UP}', '${WP_REQUIRES_AT_LEAST}', '${WC_REQUIRES_AT_LEAST}', '${PHP_REQUIRES}' )"

prepare release data:
  image: gitlab.wpdesk.dev:5050/wpdesk/library/change:0.1.1
  stage: post-deploy
  artifacts:
    expire_in: 1 week
    paths:
      - description.txt
  rules:
    - if: $IS_LIBRARY || $IS_NPM_LIBRARY
      when: never
    - if: $CI_COMMIT_TAG
  script:
    - WP_TESTED_UP=`grep -h 'Tested up to' *.php | grep -oE '[0-9\.]+'`
    - WC_TESTED_UP=`grep -h 'WC tested up to' *.php | grep -oE '[0-9\.]+' || echo "not set!"`
    - WP_REQUIRES_AT_LEAST=`grep -h 'Requires at least' *.php | grep -oE '[0-9\.]+'`
    - WC_REQUIRES_AT_LEAST=`grep -h 'WC requires at least' *.php | grep -oE '[0-9\.]+' || echo "not set!"`
    - PHP_REQUIRES=`grep -h 'Requires PHP' *.php | grep -oE '[0-9\.]+'`
    - |
      {
        echo -e "# Plugin requirements\n"
        echo "- Tested up to: $WP_TESTED_UP"
        echo "- Requires at least: $WP_REQUIRES_AT_LEAST"
        echo "- WC tested up to: $WC_TESTED_UP"
        echo "- WC requires at least: $WC_REQUIRES_AT_LEAST"
        echo "- Requires PHP: $PHP_REQUIRES"
        echo -e "# Changelog\n"
      } > description.txt
    - |
      if [ -z "$CI_COMMIT_TAG_MESSAGE" ]; then
        change get "$CI_COMMIT_TAG" >> description.txt
      else
        echo "$CI_COMMIT_TAG_MESSAGE" >> description.txt
      fi

release:
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  stage: post-deploy
  needs:
    - plugin zip
    - prepare release data
  script:
    - echo "Preparing release..."
  rules:
    - if: $IS_LIBRARY || $IS_NPM_LIBRARY
      when: never
    - if: $CI_COMMIT_TAG
  release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties
    tag_name: '$CI_COMMIT_TAG'
    description: './description.txt'
    assets:
      links:
        - name: '${CI_PROJECT_NAME}-${CI_COMMIT_TAG}.zip'
          url: '${CI_PROJECT_URL}/-/jobs/artifacts/${CI_COMMIT_TAG}/download?job=plugin+zip'

library:
  image: wpdesknet/phpunit-woocommerce:0-0
  stage: deploy
  tags:
    - deploy
  artifacts:
    expire_in: 1 year
    name: "${CI_PROJECT_NAME}-${CI_COMMIT_REF_NAME}"
    paths:
      - wp-desk
  before_script:
    - !reference [.composer-auth, script]
  script:
    - rm -rf ${CI_PROJECT_DIR}/wp-desk
    - mkdir -p /tmp/wp-desk
    - mkdir -p ${CI_PROJECT_DIR}/wp-desk/${CI_PROJECT_NAME}
    - cp -rf ${CI_PROJECT_DIR}/* /tmp/wp-desk
    - cp -rf /tmp/wp-desk/* ${CI_PROJECT_DIR}/wp-desk/${CI_PROJECT_NAME}
    - cd ${CI_PROJECT_DIR}/wp-desk/${CI_PROJECT_NAME}
    - php -d memory_limit=-1 /usr/local/bin/composer install --no-dev --no-progress --optimize-autoloader --prefer-dist
    - rm -f auth.json
    - rm -rf build-coverage wp-desk release tests docs .git .editorconfig .gitignore .gitlab-ci.yml apigen.neon phpunit.xml acceptance test_soap.php .gitlab
    - rm -rf composer.json composer.lock auth.json phpcs.xml.dist phpunit-integration.xml phpunit-unit.xml composer.phar wp-cli.phar functional tmp_artifacts .tmp tools
    - rm -rf assets-src package.json package-lock.json .babelrc weback.mix.js
  only:
    variables:
      - $IS_LIBRARY

deploy to npm:
  <<: *job-deploy-template
  image: node:20.18-slim
  stage: deploy
  tags:
    - deploy
  when: on_success
  script:
    - echo "//registry.npmjs.org/:_authToken=${NPM_PUBLISH_ACCESS_TOKEN}">.npmrc
    - npm install --unsafe-perm
    - npm publish --unsafe-perm
  only:
    refs:
      - tags
    variables:
      - $IS_NPM_LIBRARY

deploy to devel:
  <<: *job-deploy-template
  stage: deploy
  tags:
    - deploy
  when: on_success
  environment:
    name: wpdesk devel
    url: https://devel.wpdesk.dev
  before_script:
    - echo "$DEVEL_SSH_KEY" | tr -d '\r' > ~/.ssh/id_rsa_devel
    - chmod 0600 ~/.ssh/id_rsa_devel
    - echo "$SSH_KNOWN_HOSTS" | tr -d '\r' > ~/.ssh/known_hosts
  script:
    - rsync -v --delete -rlDe 'ssh -p 22 -v -i ~/.ssh/id_rsa_devel' ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME} devel@tests.wpdesk.org:/home/devel/www/wp-content/plugins
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <${CI_PROJECT_URL}|${CI_PROJECT_NAME}> zdeployowany na https://devel.wpdesk.dev\", }" https://hooks.slack.com/services/${SLACK_AUTH}'
  only:
    - devel
  except:
    variables:
      - $IS_LIBRARY
      - $IS_NPM_LIBRARY

composer package:
  stage: deploy
  script:
    - 'curl --header "Job-Token: $CI_JOB_TOKEN" --data tag=${CI_COMMIT_REF_NAME} "${CI_API_V4_URL}/projects/$CI_PROJECT_ID/packages/composer"'
  environment: production
  only:
    refs:
      - tags
    variables:
      - $IS_LIBRARY
