executor-machine: &executor-machine 'ubuntu-2004:2022.04.1'

jobs:
    test_front_lint_connectivity:
        machine:
            image: *executor-machine
            docker_layer_caching: true
        steps:
            -   attach_workspace:
                    at: ~/
            -   run:
                    name: Create yarn cache folder
                    command: mkdir -p  ~/.cache/yarn
            -   run:
                    name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                    command: sudo chown -R 1000:1000 ../project ~/.cache/yarn
            -   run:
                    name: Pull docker image for node
                    command: docker-compose pull node
            -   run:
                    name: Build other front libraries
                    command: docker-compose run -u node --rm node yarn catalogs:build
            -   run:
                    name: Lint front
                    command: make connectivity-connection-lint-front

    test_front_build_connectivity:
        machine:
            image: *executor-machine
            docker_layer_caching: true
        steps:
            -   attach_workspace:
                    at: ~/
            -   run:
                    name: Create yarn cache folder
                    command: mkdir -p  ~/.cache/yarn
            -   run:
                    name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                    command: sudo chown -R 1000:1000 ../project ~/.cache/yarn
            -   run:
                    name: Build front in strict mode
                    command: make javascript-dev-strict

    test_front_unit_connectivity:
        machine:
            image: *executor-machine
            docker_layer_caching: true
        steps:
            -   attach_workspace:
                    at: ~/
            -   run:
                    name: Create yarn cache folder
                    command: mkdir -p  ~/.cache/yarn
            -   run:
                    name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                    command: sudo chown -R 1000:1000 ../project ~/.cache/yarn
            -   run:
                    name: Pull docker image for node
                    command: docker-compose pull node
            -   run:
                    name: Build other front libraries
                    command: docker-compose run -u node --rm node yarn catalogs:build
            -   run:
                    name: Unit front
                    command: make connectivity-connection-unit-front

    test_back_unit_connectivity:
        machine:
            image: *executor-machine
            docker_layer_caching: true
        steps:
            -   attach_workspace:
                    at: ~/
            -   run:
                    name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                    command: sudo chown -R 1000:1000 ../project
            -   run:
                    name: Pull docker image for php
                    command: docker-compose pull php
            -   run:
                    name: Coupling back
                    command: make connectivity-connection-coupling-back
            -   run:
                    name: Lint back
                    command: make connectivity-connection-lint-back
            -   run:
                    name: Unit back
                    command: make connectivity-connection-unit-back

    test_back_integration_connectivity:
        machine:
            image: *executor-machine
            docker_layer_caching: true
        steps:
            -   attach_workspace:
                    at: ~/
            -   run:
                    name: Copy docker-compose.override.yml.dist
                    command: cp .circleci/docker-compose.override.yml.dist docker-compose.override.yml
            -   run:
                    name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                    command: sudo chown -R 1000:1000 ../project
            -   run:
                    name: Pull docker images
                    command: docker-compose pull php fpm mysql elasticsearch object-storage pubsub-emulator
            -   run:
                    name: Start containers
                    command: |
                        APP_ENV=test C='fpm mysql elasticsearch object-storage pubsub-emulator' make up
                        docker/wait_docker_up.sh
            -   run:
                    name: Install database
                    command: APP_ENV=test O="--withoutFixtures" make database
            -   run:
                    name: Integration back
                    command: CI=false EXPERIMENTAL_TEST_DATABASE=1 make connectivity-connection-integration-back
            -   store_test_results:
                    path: var/tests/phpunit

    test_back_e2e_connectivity:
        machine:
            image: *executor-machine
            docker_layer_caching: true
        steps:
            -   attach_workspace:
                    at: ~/
            -   run:
                    name: Copy docker-compose.override.yml.dist
                    command: cp .circleci/docker-compose.override.yml.dist docker-compose.override.yml
            -   run:
                    name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                    command: sudo chown -R 1000:1000 ../project
            -   run:
                    name: Pull docker images
                    command: docker-compose pull php fpm mysql elasticsearch object-storage pubsub-emulator
            -   run:
                    name: Start containers
                    command: |
                        APP_ENV=test C='fpm mysql elasticsearch object-storage pubsub-emulator' make up
                        docker/wait_docker_up.sh
            -   run:
                    name: Install database
                    command: APP_ENV=test O="--withoutFixtures" make database
            -   run:
                    name: End to end back
                    command: CI=false make connectivity-connection-e2e-back
            -   store_test_results:
                    path: var/tests/phpunit

    test_back_behat_connectivity:
        machine:
            image: *executor-machine
        steps:
            -   attach_workspace:
                    at: ~/
            -   run:
                    name: Copy docker-compose.override.yml.dist
                    command: cp .circleci/docker-compose.override.yml.dist docker-compose.override.yml
            -   run:
                    name: Change owner on project dir in order to archive the project into the workspace
                    command: sudo chown -R 1000:1000 ../project
            -   run:
                    name: Start containers
                    command: |
                        docker load -i php-pim-image.tar
                        APP_ENV=behat docker-compose -f docker-compose.yml -f src/Akeneo/Connectivity/Connection/tests/docker-compose.yml up -d --remove-orphans fpm mysql elasticsearch httpd object-storage selenium pubsub-emulator
                        docker/wait_docker_up.sh
            -   run:
                    name: Install database
                    command: APP_ENV=behat make database
            -   run:
                    name: End to end Behat tests
                    command: PIM_CONTEXT=test make connectivity-connection-critical-e2e
            -   store_test_results:
                    path: var/tests/behat
            -   store_artifacts:
                    path: var/tests/behat
            -   store_artifacts:
                    path: var/logs
            -   store_artifacts:
                    path: var/tests/screenshots

    test_octopus_code_quality:
        machine:
            image: *executor-machine
        steps:
            - attach_workspace:
                  at: ~/
            - run:
                  name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                  command: sudo chown -R 1000:1000 ../project
            - run:
                  name: PHP Insights results
                  command: make connectivity-connection-insight | tee insights.txt
            - run:
                  name: PSALM results
                  command: make connectivity-connection-psalm || true
            -   run:
                    name: Coupling detector unused rules check
                    command: make connectivity-connection-unused-coupling-rules | tee unused-rules.txt || true
            - run:
                  name: Start containers
                  command: |
                      docker load -i php-pim-image.tar
                      APP_ENV=test C='fpm mysql elasticsearch object-storage pubsub-emulator' make up
                      docker/wait_docker_up.sh
            - run:
                  name: Install database
                  command: APP_ENV=test make database
            - run:
                  name: Compute code coverage of all tests
                  command: PIM_CONTEXT=test make connectivity-connection-coverage
            - run:
                  name: Store results in environment variables
                  command: |
                      echo "export PHPINSIGHTS_SCORE_QUALITY=$(sed -n 's/^\[CODE\] \([0-9.]*\).*/\1/p' insights.txt)" >> $BASH_ENV
                      echo "export PHPINSIGHTS_SCORE_COMPLEXITY=$(sed -n 's/^\[COMPLEXITY\] \([0-9.]*\).*/\1/p' insights.txt)" >> $BASH_ENV
                      echo "export PHPINSIGHTS_SCORE_ARCHITECTURE=$(sed -n 's/^\[ARCHITECTURE\] \([0-9.]*\).*/\1/p' insights.txt)" >> $BASH_ENV
                      echo "export PHPINSIGHTS_SCORE_STYLE=$(sed -n 's/^\[MISC\] \([0-9.]*\).*/\1/p' insights.txt)" >> $BASH_ENV
                      echo "export PHPCD_UNUSED_RULES=$(cat unused-rules.txt | grep -c '<warning>' | awk '{if ($1==0) print ":white_check_mark:"; else print ":warning:"}')" >> $BASH_ENV
            -   slack-app/notify:
                    custom: |
                        {
                            "blocks": [
                                {
                                    "type": "section",
                                    "text": {
                                        "type": "mrkdwn",
                                        "text": "*Weekly update:*"
                                    }
                                },
                                {
                                    "type": "divider"
                                },
                                {
                                    "type": "section",
                                    "text": {
                                        "type": "mrkdwn",
                                        "text": "PHP Insights Code Quality: *$PHPINSIGHTS_SCORE_QUALITY%*\nPHP Insights Code Complexity: *$PHPINSIGHTS_SCORE_COMPLEXITY%*\nPHP Insights Code Architecture: *$PHPINSIGHTS_SCORE_ARCHITECTURE%*\nPHP Insights Code Style: *$PHPINSIGHTS_SCORE_STYLE%*"
                                    }
                                },
                                {
                                    "type": "section",
                                    "text": {
                                        "type": "mrkdwn",
                                        "text": "PHP Coupling detector rules: $PHPCD_UNUSED_RULES"
                                    }
                                }
                            ]
                        }
                    event: pass
            - store_artifacts:
                  path: coverage/Connectivity/Back/Global/
