stages:
  - static_code_analysis
  - build
  - test
  - deploy

include:
  - project: 'hive/common-ci-configuration'
    ref: 5806284d3c6feb2ce52bdb6077c20a9a578bb643
    file:
      - '/templates/npm_projects.gitlab-ci.yml'
      - '/templates/wasm_build.gitlab-ci.yml'
      - '/templates/python_projects.gitlab-ci.yml'

variables:
  GIT_DEPTH: 0
  GIT_STRATEGY: clone
  # only build steps needs to have access to the hive submodule
  GIT_SUBMODULE_STRATEGY: none
  # uses registry.gitlab.syncad.com/hive/wax/ci-base-image:ubuntu22.04-3
  CI_BASE_IMAGE_TAG: "@sha256:f263bfa8a0ea40a29d99d147601171fb6320c447abc8f3ab1e9082cd51a2254a"
  CI_BASE_IMAGE: "registry.gitlab.syncad.com/hive/wax/ci-base-image${CI_BASE_IMAGE_TAG}"

  # uses registry.gitlab.syncad.com/hive/wax/ci-base-image:ubuntu22.04-3
  PYTHON_IMAGE_TAG: "${CI_BASE_IMAGE_TAG}"
  PYTHON_IMAGE: "${CI_BASE_IMAGE}"

  # colors:
  TXT_BLUE: "\e[1;34m"
  TXT_CLEAR: "\e[0m"
  NPM_TOKEN: $CI_JOB_TOKEN

image: "${CI_BASE_IMAGE}"

default:
  tags:
    - public-runner-docker

build_wheel:
  extends: .build_wheel_template
  variables:
    PYPROJECT_DIR: "${CI_PROJECT_DIR}/python"
    GIT_SUBMODULE_STRATEGY: recursive
  stage: build
  script:
    - ./python/scripts/build_wax.sh 1
  artifacts:
    when: always
    paths:
      - "${PYPROJECT_DIR}/dist/"
      - "${PYPROJECT_DIR}/.build/logs/"
      - "${PYPROJECT_DIR}/.build/*.so"
      - "build_wheel.env"
      - "${PYPROJECT_DIR}/wax/proto"
    reports:
      dotenv: "build_wheel.env"

verify_poetry_lock_sanity:
  extends: .verify_poetry_lock_sanity_template
  stage: static_code_analysis
  variables:
    PYPROJECT_DIR: "$CI_PROJECT_DIR/python"
  tags:
    - public-runner-docker

wax_wasm_proto_tsc_generation:
  extends: .npm_build_template
  stage: build
  variables:
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    DIST_DIR: "$CI_PROJECT_DIR/ts/wasm/dist"
    NPM_PACKAGE_SCOPE: "@hiveio"
    NPM_PACKAGE_NAME: "wax"
    GIT_SUBMODULE_STRATEGY: recursive
    REPLACE_DOC_URL_ENV: "GEN_DOC_URL"
    REPLACE_FILE_PATH: "${SOURCE_DIR}/README.md"

  artifacts:
    when: always
    paths:
      - "${DIST_DIR}/*.tgz"  # Built package
      - "${REPLACE_FILE_PATH}"  # Modified README
      - "${CI_PROJECT_DIR}/ts/wasm/lib/proto" # For documentation generator
      - "${CI_PROJECT_DIR}/ts/wasm/lib/build_wasm" # For documentation generator
      - "${DIST_DIR}/lib" # For Node.js tests
      - "${CI_PROJECT_DIR}/ts/wasm/build_wasm/*.log"
    reports:
      dotenv:
        - "${SOURCE_DIR}/gen_doc.env"  # contains URL to generated documentation pointed by `GEN_DOC_URL` variable
        - "${SOURCE_DIR}/built_package_info.env"  # contains path to produced tgz
        - "${SOURCE_DIR}/built_package_version_info.env"  # contains information related to generated package version and git revision

  tags:
    - public-runner-docker

wax_wasm_build_tests:
  extends: .npm_process_built_package_tarball
  stage: build
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"

  needs:
    - job: wax_wasm_proto_tsc_generation
      artifacts: true
  script:
    - cd "${SOURCE_DIR}"
    - npm run build:test
  artifacts:
    paths:
      - "$CI_PROJECT_DIR/ts/wasm/dist/assets"
      - "$CI_PROJECT_DIR/ts/wasm/dist/detailed"
      - "$CI_PROJECT_DIR/ts/wasm/dist/index.spec.*"

    when: always
    expire_in: 1 week

  tags:
    - public-runner-docker

build_wax_extensions:
  stage: build
  extends: .npm_process_built_package_tarball
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    DIST_DIR: "$CI_PROJECT_DIR/ts/wasm/dist"
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"

  script:
    - cd "${SOURCE_DIR}/packages"
    - pnpm install --frozen-lockfile
    - pnpm run build

  needs:
    - job: wax_wasm_proto_tsc_generation
      artifacts: true

  artifacts:
    paths:
      - "${SOURCE_DIR}/packages/*/dist"

    when: always
    expire_in: 1 week

generate_docs:
  stage: build
  extends: .npm_based_job_base
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    DIST_DIR: "$CI_PROJECT_DIR/ts/wasm/dist"

  script:
    - scripts/generate_api_docs.sh "${CI_PROJECT_URL}" "${CI_COMMIT_SHA}"

  needs:
    - job: wax_wasm_proto_tsc_generation
      artifacts: true

  artifacts:
    paths:
      - "${DIST_DIR}/docs"

    when: always
    expire_in: 1 week

push_to_wiki:
  extends: .npm_push_doc_template
  stage: deploy
  variables:
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    WIKI_PUSH_TOKEN: "$WIKI_PUSH_TOKEN"
    DIST_DIR: "${CI_PROJECT_DIR}/ts/wasm/dist"

  needs:
    - job: generate_docs
      artifacts: true
    - job: wax_wasm_proto_tsc_generation
      artifacts: true

.runtime_like_python_env:
  stage: test
  extends: .configuration_template
  variables:
    PYPROJECT_DIR: "${CI_PROJECT_DIR}/python"
  needs:
    - job: build_wheel
      artifacts: true
  before_script:
    - !reference [.configuration_template, before_script]
    - pip install ${PYPROJECT_DIR}/dist/*.whl
    - pip list
    - ls -al $(poetry -C "${PYPROJECT_DIR}" env info --path)/lib/python3.10/site-packages/

.test_python_env:
  extends: .runtime_like_python_env
  before_script:
    - !reference [.runtime_like_python_env, before_script]
    - poetry -C "${PYPROJECT_DIR}" install --no-root  # install dependencies for tests
    - pip list

test_importing_wax:
  extends: .runtime_like_python_env
  script:
    - echo -e "${TXT_BLUE}Testing...${TXT_CLEAR}"
    - cd ..  # needed to import installed package instead of local one
    - python3 -c "import wax; print(wax.__version__); exit(wax.__version__ == '0.0.0')"

test_wax_protobuf_python:
  extends: .test_python_env
  script:
    - echo -e "${TXT_BLUE}Testing wax with proto ...${TXT_CLEAR}"
    - ${PYPROJECT_DIR}/tests/run_tests.sh
  artifacts:
    reports:
      junit: python/tests/report.xml

test_wax_protobuf_python_pattern:
  extends: .test_python_env
  needs:
    - job: build_wheel
      artifacts: true
  script:
    - echo -e "${TXT_BLUE}Testing generated proto files with patterns ...${TXT_CLEAR}"
    - diff --brief --recursive --color=never --no-ignore-file-name-case --no-dereference  $CI_PROJECT_DIR/python/protobuf_patterns/ $CI_PROJECT_DIR/python/wax/proto/
  artifacts:
    paths:
      - $CI_PROJECT_DIR/python/wax/proto/

    when: always
    expire_in: 1 week

test_wax_protobuf_python_examples:
  extends: .runtime_like_python_env
  script:
    - echo -e "${TXT_BLUE}Testing visitor of wax protobuf...${TXT_CLEAR}"
    - cd $CI_PROJECT_DIR
    - ./examples/python/run_example.sh

test_wax_wasm:
  extends: .npm_test_template
  variables:
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"

  needs:
    - job: wax_wasm_proto_tsc_generation
      artifacts: true
    - job: wax_wasm_build_tests
      artifacts: true
  tags:
    - public-runner-docker
  parallel:
    matrix:
      - TEST_GROUPS: [
        --project=wax_testsuite,
        --project=healthchecker_tests,
        --project=wax_utils --project=wax_non_encrypted_operations --project=wax_mock_tests --project=wax_encrypted_operations --project=wax_custom_chain_online_tx --project=wax_testsuite_custom_chain_options,
        --project=wax_regression_tests --project=wax_operation_factories --project=wax_testsuite_protocol_benchmarks,
      ]
  script:
    - cd ts
    - npm run test -- $TEST_GROUPS

test_wax_wasm_proto_pattern:
  extends: .npm_process_built_package_tarball
  needs:
    - job: wax_wasm_proto_tsc_generation
      artifacts: true

  variables:
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"

  script:
    - diff --brief --recursive --color=never --no-ignore-file-name-case --no-dereference "$CI_PROJECT_DIR/ts/protobuf_patterns/proto" "$CI_PROJECT_DIR/ts/wasm/dist/lib/proto"
    - tar -tf "${PACKAGE_TGZ_PATH}" > "$CI_PROJECT_DIR/ts/wasm/dist/npm-pack.gen"
    - diff --brief --color=never --no-dereference "$CI_PROJECT_DIR/ts/wasm/dist/npm-pack.gen" "$CI_PROJECT_DIR/ts/protobuf_patterns/npm-pack.out"
  artifacts:
    paths:
      - "$CI_PROJECT_DIR/ts/wasm/dist/npm-pack.gen"
      - "$CI_PROJECT_DIR/ts/wasm/dist/lib/proto/"

    when: always
    expire_in: 1 week
  tags:
    - public-runner-docker

test_wax_wasm_examples:
  extends: .npm_process_built_package_tarball
  needs:
    - job: wax_wasm_proto_tsc_generation
      artifacts: true

  variables:
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"

  script:
    - cd "${SOURCE_DIR}"
    - npm run examples
  tags:
    - public-runner-docker

.deploy_wheel_needs: &deploy_wheel_needs
  needs:
    - job: test_importing_wax
    - job: test_wax_protobuf_python
    - job: build_wheel
      artifacts: true

deploy_wheel_to_gitlab:
  stage: deploy
  extends: .deploy_wheel_to_gitlab_template
  variables:
    PYPROJECT_DIR: "${CI_PROJECT_DIR}/python"
  <<: *deploy_wheel_needs
  when: on_success

deploy_wheel_to_pypi:
  extends: .deploy_wheel_to_pypi_template
  stage: deploy
  variables:
    PYPROJECT_DIR: "${CI_PROJECT_DIR}/python"
  <<: *deploy_wheel_needs

deploy_wax_wasm_dev_package:
  extends: .npm_deploy_package_template
  stage: deploy
  variables:
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"
    NPM_PACKAGE_SCOPE: "@hiveio"
    GIT_SUBMODULE_STRATEGY: "normal"

  needs:
    - job: test_wax_wasm_examples
    - job: test_wax_wasm
    - job: test_wax_wasm_proto_pattern
    - job: push_to_wiki
    - job: wax_wasm_proto_tsc_generation
      artifacts: true
  tags:
    - public-runner-docker

deploy_wax_wasm_dev_extension_packages:
  extends: .npm_process_built_package_tarball
  stage: deploy
  variables:
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"
    DIST_DIR: "${SOURCE_DIR}/dist"
    GIT_SUBMODULE_STRATEGY: "normal"
    NPM_PACKAGE_SCOPE: "@hiveio"
    NPM_REGISTRY_URL: "gitlab.syncad.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/"
    NPM_PUBLISH_TOKEN: "${CI_JOB_TOKEN}"

  script:
    - cd "${SOURCE_DIR}/packages"
    - echo "//${NPM_REGISTRY_URL}:_authToken=\"${NPM_PUBLISH_TOKEN}\"" > "${SOURCE_DIR}/packages/.npmrc"
    - echo "${NPM_PACKAGE_SCOPE}:registry=https://${NPM_REGISTRY_URL}" >> "${SOURCE_DIR}/packages/.npmrc"
    - pnpm publish --no-git-checks --access public -r --tag $(jq -r '.publishConfig.tag' "${SOURCE_DIR}/package.json")

  needs:
    - job: deploy_wax_wasm_dev_package
    - job: wax_wasm_proto_tsc_generation
      artifacts: true
    - job: build_wax_extensions
      artifacts: true
  tags:
    - public-runner-docker

deploy_wax_wasm_production_public_npm:
  extends: .registry_npmjs_org_deploy_package_template
  stage: deploy
  variables:
    NPM_PUBLISH_TOKEN: "$INTERNAL_HIDDEN_PUBLISH_TOKEN"
    NPM_PACKAGE_NAME: "wax"
    SOURCE_DIR: "${CI_PROJECT_DIR}/ts"
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"
    GIT_SUBMODULE_STRATEGY: "normal"

  needs:
    - job: test_wax_wasm_examples
    - job: test_wax_wasm
    - job: test_wax_wasm_proto_pattern
    - job: push_to_wiki
    - job: wax_wasm_proto_tsc_generation
      artifacts: true

  tags:
    - public-runner-docker
