# The variables are with the functions because they are only used by the
# functions and also because you can't use variables across includes.
variables:
  - &save-artifact-params-private
    aws_key: ${aws_key}
    aws_secret: ${aws_secret}
    bucket: mciuploads
    content_type: application/octet-stream
    permissions: private
    visibility: signed
  - &save-artifact-params-public
    aws_key: ${aws_key}
    aws_secret: ${aws_secret}
    bucket: mciuploads
    content_type: application/octet-stream
    permissions: public-read
  - &get-artifact-params
    aws_key: ${aws_key}
    aws_secret: ${aws_secret}
    bucket: mciuploads
    content_type: application/octet-stream
    # NOTE: anything added here should probably also be added to the list
  - &compass-env
    WORKDIR: ${workdir}
    CI: '1'
    EVERGREEN: '1'
    EVERGREEN_AUTHOR: ${author}
    EVERGREEN_BRANCH_NAME: ${branch_name}
    EVERGREEN_BUILD_ID: ${build_id}
    EVERGREEN_BUILD_VARIANT: ${build_variant}
    EVERGREEN_EXECUTION: ${execution}
    EVERGREEN_IS_PATCH: ${is_patch}
    EVERGREEN_PROJECT: ${project}
    EVERGREEN_REVISION: ${revision}
    EVERGREEN_TASK_ID: ${task_id}
    EVERGREEN_TASK_NAME: ${task_name}
    EVERGREEN_TASK_URL: https://evergreen.mongodb.com/task/${task_id}
    EVERGREEN_VERSION_ID: ${version_id}
    EVERGREEN_WORKDIR: ${workdir}
    # WARN: This version is behind our electron runtime, but updating it will
    # drop support for some older linux platforms, so we are keeping them out of
    # sync for now
    # TODO: https://jira.mongodb.org/browse/COMPASS-6915
    NODE_JS_VERSION: '16.20.2'
    NPM_VERSION: '8.19.4'
    # secrets
    HADRON_METRICS_INTERCOM_APP_ID: ${metrics_intercom_app_id}
    HADRON_METRICS_SEGMENT_API_KEY: ${metrics_segment_api_key}
    E2E_TESTS_METRICS_URI: ${e2e_tests_metrics_string}
    E2E_TESTS_ATLAS_HOST: ${e2e_tests_atlas_host}
    E2E_TESTS_DATA_LAKE_HOST: ${e2e_tests_data_lake_host}
    E2E_TESTS_SERVERLESS_HOST: ${e2e_tests_serverless_host}
    E2E_TESTS_ANALYTICS_NODE_HOST: ${e2e_tests_analytics_node_host}
    E2E_TESTS_FREE_TIER_HOST: ${e2e_tests_free_tier_host}
    E2E_TESTS_ATLAS_USERNAME: ${e2e_tests_atlas_username}
    E2E_TESTS_ATLAS_PASSWORD: ${e2e_tests_atlas_password}
    E2E_TESTS_ATLAS_X509_PEM: ${e2e_tests_atlas_x509_pem}
    E2E_TESTS_ATLAS_IAM_ACCESS_KEY_ID: ${e2e_tests_atlas_iam_aws_access_key_id}
    E2E_TESTS_ATLAS_IAM_SECRET_ACCESS_KEY: ${e2e_tests_atlas_iam_aws_secret_access_key}
    E2E_TESTS_ATLAS_IAM_TEMP_ROLE_ARN: ${e2e_tests_atlas_iam_temp_role_arn}
    E2E_TESTS_ATLAS_IAM_USER_ARN: ${e2e_tests_atlas_iam_user_arn}
    E2E_TESTS_ATLAS_READWRITEANY_STRING: ${e2e_tests_atlas_readwriteany_string}
    E2E_TESTS_ATLAS_READANYDATABASE_STRING: ${e2e_tests_atlas_readanydatabase_string}
    E2E_TESTS_ATLAS_CUSTOMROLE_STRING: ${e2e_tests_atlas_customrole_string}
    E2E_TESTS_ATLAS_SPECIFICPERMISSION_STRING: ${e2e_tests_atlas_specificpermission_string}
    NOTARY_URL: http://notary-service.build.10gen.cc:5000
    NOTARY_AUTH_TOKEN: ${signing_auth_token}
    NOTARY_SIGNING_KEY: ${signing_key_name}
    NOTARY_SIGNING_COMMENT: Evergreen project mongodb/compass ${revision} - ${build_variant} - ${branch_name}
    MACOS_NOTARY_KEY: ${macos_notary_key}
    MACOS_NOTARY_SECRET: ${macos_notary_secret}
    MACOS_NOTARY_CLIENT_URL: 'https://macos-notary-1628249594.s3.amazonaws.com/releases/client/latest/darwin_amd64.zip'
    MACOS_NOTARY_API_URL: 'https://dev.macos-notary.build.10gen.cc/api'
    GITHUB_TOKEN: ${devtoolsbot_github_token}
    DOWNLOAD_CENTER_AWS_ACCESS_KEY_ID: ${aws_key_evergreen_integrations}
    DOWNLOAD_CENTER_AWS_SECRET_ACCESS_KEY: ${aws_secret_evergreen_integrations}
    EVERGREEN_BUCKET_NAME: mciuploads
    EVERGREEN_BUCKET_KEY_PREFIX: ${project}/${revision}_${revision_order_id}
    MONGODB_RUNNER_LOG_DIR: ${workdir}/src/.testserver/
    E2E_TESTS_ATLAS_CS_WITHOUT_SEARCH: ${e2e_tests_atlas_cs_without_search}
    E2E_TESTS_ATLAS_CS_WITH_SEARCH: ${e2e_tests_atlas_cs_with_search}

# This is here with the variables because anchors aren't supported across includes
post:
  - command: s3.put
    params:
      <<: *save-artifact-params-private
      local_files_include_filter:
        - src/.deps/.npm/_logs/*.log
        - src/packages/compass-e2e-tests/.log/**/*
        - src/packages/compass-e2e-tests/.nyc_output/coverage.json
        - src/.testserver/logs/*
        - src/.evergreen/logs/*
      remote_file: ${project}/${revision}_${revision_order_id}/${build_variant}/${task_name}
      content_type: text/plain
  - command: s3.put
    params:
      <<: *save-artifact-params-private
      local_file: src/.sbom/dependencies.json
      remote_file: ${project}/${revision}_${revision_order_id}/dependencies.json
      content_type: application/json
      optional: true
  - command: s3.put
    params:
      <<: *save-artifact-params-private
      local_file: src/.sbom/snyk-test-result.json
      remote_file: ${project}/${revision}_${revision_order_id}/snyk-test-result.json
      content_type: application/json
      optional: true
  - command: s3.put
    params:
      <<: *save-artifact-params-private
      local_file: src/.sbom/snyk-test-result.html
      remote_file: ${project}/${revision}_${revision_order_id}/snyk-test-result.html
      content_type: text/html
      optional: true
  - command: s3.put
    params:
      <<: *save-artifact-params-private
      local_file: src/.sbom/vulnerability-report.md
      remote_file: ${project}/${revision}_${revision_order_id}/vulnerability-report.md
      content_type: text/markdown
      optional: true
  - command: attach.results
    params:
      file_location: src/packages/compass-e2e-tests/.log/report.json

functions:
  clone:
    - command: git.get_project
      params:
        directory: src

  prepare:
    - command: git.get_project
      params:
        directory: src

    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
        script: |
          set -e
          eval $(.evergreen/print-compass-env.sh)

          # Make all the dirs
          mkdir -p $ARTIFACTS_PATH
          mkdir -p $NPM_CACHE_DIR
          mkdir -p $NPM_TMP_DIR

    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
        script: |
          set -e
          eval $(.evergreen/print-compass-env.sh)

          .evergreen/preinstall.sh

          # Make sure install worked
          echo "Using node version:";
          node -v;
          echo "Using npm version:";
          npm -v;

          echo "Using gcc version:"
          gcc --version;
          echo "Using g++ version:"
          g++ --version;

  install:
    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
        script: |
          set -e
          eval $(.evergreen/print-compass-env.sh)

          echo "Installing Compass dependencies with the following npm configuration"
          npm config ls -l
          echo "(if npm fails, debug.log will be uploaded to S3)"

          # Install dependencies
          bash ".evergreen/retry-with-backoff.sh" npm ci --unsafe-perm

  bootstrap:
    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
        script: |
          set -e
          eval $(.evergreen/print-compass-env.sh)

          # Pre-built binaries for keytar do not support older RHEL versions.
          # Since we have started importing keytar from top-level scripts in packages,
          # re-built keytar before the bootstrap process so that it is always usable.
          if [[ "$IS_RHEL" == "true" ]]; then
            echo "Rebuilding packages for rhel"
            npm run node-rebuild
          fi

          # Prepare workspaces in provided scope
          npx lerna run bootstrap \
            --stream \
            --scope "${scope}" \
            --include-dependencies

  check:
    command: shell.exec
    params:
      working_dir: src
      shell: bash
      env:
        <<: *compass-env
      script: |
        set -e
        eval $(.evergreen/print-compass-env.sh)

        echo "Run static analysis..."
        npm run check-ci

  test:
    - command: shell.exec
      # Fail the task if it's idle for 10 mins
      timeout_secs: 600
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          DEBUG: ${debug|}
          MONGODB_VERSION: ${mongodb_version|}
          MONGODB_RUNNER_VERSION: ${mongodb_version|}
        script: |
          set -e

          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)

          echo "Running tests in Node.js runtime"

          # compass-e2e-tests are ignored as we are running this test suite
          # separately in the test-packaged-app task
          export IGNORE_TESTS="compass-e2e-tests"

          npm run --unsafe-perm test-ci -- --stream --ignore="$IGNORE_TESTS"

  test-electron:
    - command: shell.exec
      # Fail the task if it's idle for 10 mins
      timeout_secs: 600
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          DEBUG: ${debug|}
          MONGODB_VERSION: ${mongodb_version|}
          MONGODB_RUNNER_VERSION: ${mongodb_version|}
        script: |
          set -e

          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)

          echo "Running tests in Electron runtime"

          # Preparing native addons for electron runtime
          npm run --unsafe-perm electron-rebuild

          npm run --unsafe-perm test-ci-electron -- --stream

  apply-compass-target-expansion:
    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)

          echo "Synchronizing evergreen environment from Compass build tools..."

          # Generates and expansion file with build target metadata in packages/compass/expansions.yml
          npm run --workspace mongodb-compass build-info -- ${target_platform} ${target_arch} --format=yaml --flatten ${compass_distribution} --out expansions.yml
    - command: expansions.update
      params:
        # packaging and publishing is using all the *_filename variables
        #
        # e2e tests are using appPath / appName to extract packaged application
        # and be able to find the binary that is used for the tests
        file: src/packages/compass/expansions.yml

  package:
    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          DEBUG: ${debug}
          npm_config_loglevel: ${npm_loglevel}
          COMPASS_DISTRIBUTION: ${compass_distribution}
        script: |
          set -e

          eval $(.evergreen/print-compass-env.sh)
          .evergreen/compass_package.sh

  publish:
    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)
          if [[ "${project}" == "10gen-compass-main" ]]; then
            export npm_config_dry_run=true
          fi
          echo "Uploading release assets to S3 and GitHub if needed..."
          npm run --workspace mongodb-compass upload

  get-packaged-app:
    - command: s3.get
      params:
        <<: *get-artifact-params
        local_file: src/packages/compass/dist/${app_archive_name}
        remote_file: ${project}/${revision}_${revision_order_id}/${app_archive_name}
    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          COMPASS_APP_PATH_ORIGINAL: ${appPath}
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)
          set -x

          # Check the app bundle size.
          npm run --workspace mongodb-compass check-bundled-app-size

          if [[ "$IS_WINDOWS" == "true" ]]; then
            COMPASS_OUTDIR="$COMPASS_APP_PATH_ORIGINAL"
          else
            # Unpack outside the Compass working tree to avoid interactions with
            # npm packages from inside it.
            # (This doesn't seem to work on Windows without issues around being
            # able to spawn the extracted executable, unfortunately.)
            COMPASS_OUTDIR="$PWD/../tmp/from-packaged"
          fi

          mkdir -p "$COMPASS_OUTDIR"
          echo "Unpacking ${app_archive_name} to $COMPASS_OUTDIR ..."

          # Unpack the app archive
          if [[ "${app_archive_name}" == *.zip ]]; then
            unzip "packages/compass/dist/${app_archive_name}" -d "$COMPASS_OUTDIR"
          else
            tar -xzf "packages/compass/dist/${app_archive_name}" -C "$COMPASS_OUTDIR"
          fi

          # Print folder content to confirm that it worked
          ls -la "$COMPASS_OUTDIR"

          if [[ "$IS_WINDOWS" == "true" ]]; then
            chmod +x "$COMPASS_OUTDIR"/*.exe
          fi

  e2e-coverage:
    - command: shell.exec
      # Fail the task if it's idle for 10 mins
      timeout_secs: 600
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          DEBUG: ${debug|}
          MONGODB_VERSION: ${mongodb_version|}
          MONGODB_RUNNER_VERSION: ${mongodb_version|}
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)
          source .evergreen/start-docker-envs.sh

          echo "Running E2E tests while collecting coverage..."

          npm run --unsafe-perm --workspace compass-e2e-tests test-ci

  test-packaged-app:
    - command: shell.exec
      # Fail the task if it's idle for 10 mins
      timeout_secs: 600
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          COMPASS_APP_PATH_ORIGINAL: ${appPath}
          COMPASS_APP_NAME: ${packagerOptions.name}
          DEBUG: ${debug|}
          MONGODB_VERSION: ${mongodb_version|}
          MONGODB_RUNNER_VERSION: ${mongodb_version|}
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)
          source .evergreen/start-docker-envs.sh

          if [[ "$IS_WINDOWS" == "true" ]]; then
            export COMPASS_APP_PATH="$COMPASS_APP_PATH_ORIGINAL"
          else
            export COMPASS_APP_PATH="$PWD/../tmp/from-packaged/$(basename "$COMPASS_APP_PATH_ORIGINAL")"
          fi
          echo "Running tests against packaged app at $COMPASS_APP_PATH for MongoDB $MONGODB_VERSION..."

          if [[ "$IS_OSX" == "true" ]]; then
            echo "Disabling keychain usage in Compass (TODO: https://jira.mongodb.org/browse/COMPASS-7102)"
            export COMPASS_E2E_DISABLE_KEYCHAIN_USAGE="true"
            echo "Disabling clipboard usage in e2e tests (TODO: https://jira.mongodb.org/browse/BUILD-14780)"
            export COMPASS_E2E_DISABLE_CLIPBOARD_USAGE="true"
          fi

          npm run --unsafe-perm --workspace compass-e2e-tests test-packaged-ci

  test-connectivity:
    - command: shell.exec
      # Fail the task if it's idle for 10 mins
      timeout_secs: 600
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          COMPASS_SKIP_KERBEROS_TESTS: 'true'
          COMPASS_RUN_DOCKER_TESTS: 'true'
          DEBUG: ${debug}
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)
          source .evergreen/start-docker-envs.sh
          bash .evergreen/connectivity-tests/run.sh

  test-csfle:
    - command: shell.exec
      # Fail the task if it's idle for 10 mins
      timeout_secs: 600
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          DEBUG: ${debug}
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)
          rm -rf mongodb-crypt && mkdir -p mongodb-crypt
          (cd mongodb-crypt && \
            curl -sSfL $(npx -y mongodb-download-url --enterprise --crypt_shared --version '>= 7.0.0-rc0') | \
            tar -xvz)
          export COMPASS_CRYPT_LIBRARY_PATH=$(echo $PWD/mongodb-crypt/lib/mongo_*_v1.*)
          npm run test-csfle --workspace mongodb-data-service

  save-all-artifacts:
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${windows_setup_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${windows_setup_filename}
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${windows_msi_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${windows_msi_filename}
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${windows_zip_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${windows_zip_filename}
        content_type: application/zip
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${windows_nupkg_full_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${windows_nupkg_full_filename}
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${windows_releases_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${windows_releases_filename}
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${osx_dmg_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${osx_dmg_filename}
        content_type: application/x-apple-diskimage
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${osx_zip_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${osx_zip_filename}
        content_type: application/zip
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${linux_rpm_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${linux_rpm_filename}
        content_type: application/x-redhat-package-manager
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${rhel_tar_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${rhel_tar_filename}
        content_type: application/x-gzip
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${linux_deb_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${linux_deb_filename}
        content_type: application/vnd.debian.binary-package
        optional: true
    - command: s3.put
      params:
        <<: *save-artifact-params-public
        local_file: src/packages/compass/dist/${linux_tar_filename}
        remote_file: ${project}/${revision}_${revision_order_id}/${linux_tar_filename}
        content_type: application/x-gzip
        optional: true

  get-all-artifacts:
    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          DEBUG: ${debug}
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)
          echo "Downloading release assets from evergreen bucket..."
          npm run --workspace mongodb-compass download

  generate-tasks:
    - command: generate.tasks
      params:
        files:
          - tasks.json

  publish-packages-next:
    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          NPM_TOKEN: ${devtoolsbot_npm_token}
        script: |
          # Only package publish for commits on the main evergreen project.
          if [[ "${requester}" == "commit" ]] && [[ "${project}" == "10gen-compass-main" ]]; then
            set -e
            # Load environment variables
            eval $(.evergreen/print-compass-env.sh)
            echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc
            npm config list
            echo "Publishing packages as $(npm whoami)"
            npm run version-packages-next
            # unstage after lerna staged version
            git reset
            # mark files as unchanged so that lerna can publish
            git update-index --assume-unchanged $(git diff --name-only HEAD)
            bash ".evergreen/retry-with-backoff.sh" npm run publish-packages-next
          fi

  generate-vulnerability-report:
    - command: shell.exec
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          DEBUG: ${debug}
          SNYK_TOKEN: ${snyk_token}
          JIRA_API_TOKEN: ${jira_api_token}
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)

          set +e
          npm run generate-vulnerability-report
          return_code=$?
          set -e

          # Runs for all the commits on main, including nightly builds:
          if [[ "$EVERGREEN_IS_PATCH" != "true" ]] && [[ "${project}" == "10gen-compass-main" ]]; then
            export JIRA_BASE_URL="https://jira.mongodb.org"
            export JIRA_PROJECT="COMPASS"
            export JIRA_VULNERABILITY_BUILD_INFO="- [Evergreen task|$EVERGREEN_TASK_URL]"

            npm run create-vulnerability-tickets
          else
            cat .sbom/vulnerability-report.md
          fi

          # Fails if the report failed and is not a patch, including release branches:
          if [[ "$EVERGREEN_IS_PATCH" != "true" ]]; then
            exit $return_code
          fi

  generative-ai-accuracy-tests:
    - command: shell.exec
      # Fail the task if it's idle for 10 mins
      timeout_secs: 600
      params:
        working_dir: src
        shell: bash
        env:
          <<: *compass-env
          ATLAS_PUBLIC_KEY: ${atlas_public_key}
          ATLAS_PRIVATE_KEY: ${atlas_private_key}
          AI_ACCURACY_RESULTS_MONGODB_CONNECTION_STRING: ${accuracy_results_mdb_connection_string}
        script: |
          set -e
          # Load environment variables
          eval $(.evergreen/print-compass-env.sh)

          set +e
          npm run --workspace @mongodb-js/compass-generative-ai ai-accuracy-tests
          return_code=$?
          set -e

          # Fail when the accuracy tests fail:
          exit $return_code
