<commit_before>version: 2.1

orbs:
  slack: circleci/slack@3.4.2

references:
  defaults: &defaults
    working_directory: ~/parliamentary-questions
    resource_class: small

# Sets up the docker images and environment variables that we use
  test_container_config: &test_container_config
    docker:
      - image: circleci/ruby:2.7.2-node-browsers-legacy
        environment:
          GITHUB_TEAM_NAME_SLUG: pq-team
          APPLICATION_DEPLOY_NAME: pq
          RAILS_ENV: test
          RACK_ENV: test
          PQ_REST_API_HOST: http://localhost:8888
          POSTGRES_PASSWORD: "<PASSWORD>"
          PGUSER: postgres
          POSTGRES_DB: parliamentary_questions_test

      - image: postgres:10
        environment:
          POSTGRES_PASSWORD: "<PASSWORD>"
          POSTGRES_DB: parliamentary_questions_test
          POSTGRES_USER: postgres

  deploy_container_config: &deploy_container_config
    docker:
      - image: ministryofjustice/cloud-platform-tools:1.29

# These are defining the steps which are used below in the jobs
  install_expect: &install_expect
    run:
      name: Install Expect
      command: |
        apk add \
          --no-cache \
          --no-progress \
          expect

  create_gpg_key_file: &create_gpg_key_file
    run:
      name: Create GPG Key file from Environment variable
      command: |
        echo -e ${GPG_PRIVATE_KEY} > PRIVATE_GPG_KEY.asc

  configure_gpg: &configure_gpg
    run:
      name: Configure GPG
      command: |
        gpg --import --batch --allow-secret-key-import PRIVATE_GPG_KEY.asc

  delete_gpg_key_file: &delete_gpg_key_file
    run:
        name: Delete GPG Key file after import
        command: ¬
          rm PRIVATE_GPG_KEY.asc

  decrypt_secrets: &decrypt_secrets
    run:
      name: Decrypt secrets file
      command: |
        gpgconf --kill gpg-agent
        gpg-agent --daemon --allow-preset-passphrase
        /usr/libexec/gpg-preset-passphrase --preset --passphrase $GPG_PASSPHRASE $GPG_KEY_KEYGRIP_ID
        expect -c "spawn gpg --edit-key $GPG_KEY_ID trust quit; send \"5\ry\r\"; expect eof"
        git-crypt unlock

  install_bundler: &install_bundler
    run:
      name: Install bundler
      command: |
        gem install bundler -v 2.2.15

  check_bundler_version: &check_bundler_version
    run:
      name: Check the installed version of bundler
      command: |
        bundle -v

  restore_cache: &restore_cache
    restore_cache:
      keys:
        - parliamentary-questions--{{ checksum "Gemfile.lock" }}
        # fallback to using the latest cache if no exact match is found
        - parliamentary-questions-

  install_dependencies: &install_dependencies
    run:
      name: Install dependencies
      command: |
        if [ "${CIRCLE_NODE_INDEX}" == "0" ]; then
          bundle check --path vendor/bundle || bundle install --path vendor/bundle && bundle clean
        fi

  save_cache: &save_cache
    save_cache:
      key: parliamentary-questions--{{ checksum "Gemfile.lock" }}
      paths:
        - vendor/bundle

  wait_for_db: &wait_for_db
    run:
      name: Wait for DB
      command: dockerize -wait tcp://localhost:5432 -timeout 1m

  set_up_the_database: &set_up_the_database
    run:
      name: Set up the database
      command: |
        bundle exec rake db:setup

  rubocop: &rubocop
    run:
      name: Run rubocop
      command: bundle exec rubocop

  setup_test_reporter: &setup_test_reporter
    run:
      name: Setup Code Climate test-reporter
      command: |
        curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
        chmod +x ./cc-test-reporter
        ./cc-test-reporter before-build

  run_unit_and_feature_tests: &run_unit_and_feature_tests
    run:
      name: Run unit and feature tests
      command: |
        export COVERAGE=1
        bundle exec rspec

  upload_test_coverage: &upload_test_coverage
    run:
      name: Upload the test coverage into codeclimate
      command: |
        ./cc-test-reporter format-coverage -t simplecov -o "coverage/codeclimate.$CIRCLE_NODE_INDEX.json"
        ./cc-test-reporter sum-coverage --output - coverage/codeclimate.*.json | ./cc-test-reporter upload-coverage --debug --input -

  configure_aws_profile: &configure_aws_profile
    run:
      name: Configure AWS CLI
      command: |
        aws configure set aws_access_key_id ${AWS_ACCESS_KEY_ID} --profile ecr-live-1-parliamentary-questions
        aws configure set aws_secret_access_key ${AWS_SECRET_ACCESS_KEY} --profile ecr-live-1-parliamentary-questions
        aws configure set default.region ${AWS_DEFAULT_REGION} --profile ecr-live-1-parliamentary-questions

  build_and_push_docker_image: &build_and_push_docker_image
    run:
      name: Build docker image
      command: |
        . build.sh circleci
        mkdir -p workspace
        echo $BUILD_TAG > workspace/build_tag
        set +e
        python3 config/pipeline/aws_ecr_scan_results.py --search parliamentary-questions --tag $BUILD_TAG --slack_webhook $SLACK_WEBHOOK || true

  deploy_to_dev: &deploy_to_dev
    run:
      name: Deploy to Development environment
      command: |
        setup-kube-auth
        kubectl config use-context development
        build_tag=`cat /tmp/workspace/build_tag`
        ./deploy.sh $build_tag development circleci
        echo "export BUILD_TAG=${build_tag}" >> $BASH_ENV
        source $BASH_ENV

  deploy_to_staging: &deploy_to_staging
    run:
      name: Deploy to Staging environment
      command: |
        setup-kube-auth
        kubectl config use-context staging
        build_tag=`cat /tmp/workspace/build_tag`
        ./deploy.sh $build_tag staging circleci
        echo "export BUILD_TAG=${build_tag}" >> $BASH_ENV
        source $BASH_ENV
<commit_msg>Make scan non-blocking 6<commit_after>version: 2.1

orbs:
  slack: circleci/slack@3.4.2

references:
  defaults: &defaults
    working_directory: ~/parliamentary-questions
    resource_class: small

# Sets up the docker images and environment variables that we use
  test_container_config: &test_container_config
    docker:
      - image: circleci/ruby:2.7.2-node-browsers-legacy
        environment:
          GITHUB_TEAM_NAME_SLUG: pq-team
          APPLICATION_DEPLOY_NAME: pq
          RAILS_ENV: test
          RACK_ENV: test
          PQ_REST_API_HOST: http://localhost:8888
          POSTGRES_PASSWORD: "<PASSWORD>"
          PGUSER: postgres
          POSTGRES_DB: parliamentary_questions_test

      - image: postgres:10
        environment:
          POSTGRES_PASSWORD: "<PASSWORD>"
          POSTGRES_DB: parliamentary_questions_test
          POSTGRES_USER: postgres

  deploy_container_config: &deploy_container_config
    docker:
      - image: ministryofjustice/cloud-platform-tools:1.29

# These are defining the steps which are used below in the jobs
  install_expect: &install_expect
    run:
      name: Install Expect
      command: |
        apk add \
          --no-cache \
          --no-progress \
          expect

  create_gpg_key_file: &create_gpg_key_file
    run:
      name: Create GPG Key file from Environment variable
      command: |
        echo -e ${GPG_PRIVATE_KEY} > PRIVATE_GPG_KEY.asc

  configure_gpg: &configure_gpg
    run:
      name: Configure GPG
      command: |
        gpg --import --batch --allow-secret-key-import PRIVATE_GPG_KEY.asc

  delete_gpg_key_file: &delete_gpg_key_file
    run:
        name: Delete GPG Key file after import
        command: ¬
          rm PRIVATE_GPG_KEY.asc

  decrypt_secrets: &decrypt_secrets
    run:
      name: Decrypt secrets file
      command: |
        gpgconf --kill gpg-agent
        gpg-agent --daemon --allow-preset-passphrase
        /usr/libexec/gpg-preset-passphrase --preset --passphrase $GPG_PASSPHRASE $GPG_KEY_KEYGRIP_ID
        expect -c "spawn gpg --edit-key $GPG_KEY_ID trust quit; send \"5\ry\r\"; expect eof"
        git-crypt unlock

  install_bundler: &install_bundler
    run:
      name: Install bundler
      command: |
        gem install bundler -v 2.2.15

  check_bundler_version: &check_bundler_version
    run:
      name: Check the installed version of bundler
      command: |
        bundle -v

  restore_cache: &restore_cache
    restore_cache:
      keys:
        - parliamentary-questions--{{ checksum "Gemfile.lock" }}
        # fallback to using the latest cache if no exact match is found
        - parliamentary-questions-

  install_dependencies: &install_dependencies
    run:
      name: Install dependencies
      command: |
        if [ "${CIRCLE_NODE_INDEX}" == "0" ]; then
          bundle check --path vendor/bundle || bundle install --path vendor/bundle && bundle clean
        fi

  save_cache: &save_cache
    save_cache:
      key: parliamentary-questions--{{ checksum "Gemfile.lock" }}
      paths:
        - vendor/bundle

  wait_for_db: &wait_for_db
    run:
      name: Wait for DB
      command: dockerize -wait tcp://localhost:5432 -timeout 1m

  set_up_the_database: &set_up_the_database
    run:
      name: Set up the database
      command: |
        bundle exec rake db:setup

  rubocop: &rubocop
    run:
      name: Run rubocop
      command: bundle exec rubocop

  setup_test_reporter: &setup_test_reporter
    run:
      name: Setup Code Climate test-reporter
      command: |
        curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
        chmod +x ./cc-test-reporter
        ./cc-test-reporter before-build

  run_unit_and_feature_tests: &run_unit_and_feature_tests
    run:
      name: Run unit and feature tests
      command: |
        export COVERAGE=1
        bundle exec rspec

  upload_test_coverage: &upload_test_coverage
    run:
      name: Upload the test coverage into codeclimate
      command: |
        ./cc-test-reporter format-coverage -t simplecov -o "coverage/codeclimate.$CIRCLE_NODE_INDEX.json"
        ./cc-test-reporter sum-coverage --output - coverage/codeclimate.*.json | ./cc-test-reporter upload-coverage --debug --input -

  configure_aws_profile: &configure_aws_profile
    run:
      name: Configure AWS CLI
      command: |
        aws configure set aws_access_key_id ${AWS_ACCESS_KEY_ID} --profile ecr-live-1-parliamentary-questions
        aws configure set aws_secret_access_key ${AWS_SECRET_ACCESS_KEY} --profile ecr-live-1-parliamentary-questions
        aws configure set default.region ${AWS_DEFAULT_REGION} --profile ecr-live-1-parliamentary-questions

  build_and_push_docker_image: &build_and_push_docker_image
    run:
      name: Build docker image
      command: |
        . build.sh circleci
        mkdir -p workspace
        echo $BUILD_TAG > workspace/build_tag
        set +e
        python3 config/pipeline/aws_ecr_scan_results.py --search parliamentary-questions --tag $BUILD_TAG --slack_webhook $SLACK_WEBHOOK

  deploy_to_dev: &deploy_to_dev
    run:
      name: Deploy to Development environment
      command: |
        setup-kube-auth
        kubectl config use-context development
        build_tag=`cat /tmp/workspace/build_tag`
        ./deploy.sh $build_tag development circleci
        echo "export BUILD_TAG=${build_tag}" >> $BASH_ENV
        source $BASH_ENV

  deploy_to_staging: &deploy_to_staging
    run:
      name: Deploy to Staging environment
      command: |
        setup-kube-auth
        kubectl config use-context staging
        build_tag=`cat /tmp/workspace/build_tag`
        ./deploy.sh $build_tag staging circleci
        echo "export BUILD_TAG=${build_tag}" >> $BASH_ENV
        source $BASH_ENV

{'fraction_non_alphanumeric': 0.08121170277034608, 'fraction_numerical': 0.0061275567446276, 'mean_word_length': 2.527549467275495, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2088208', 'n_tokens_mistral': 3780, 'n_tokens_neox': 3613, 'n_words': 874}