# generator: n-circle2-cli
# template: component

references:

  container_config_node: &container_config_node
    working_directory: ~/project/build
    docker:
      - image: cimg/node:16.17-browsers
  workspace_root: &workspace_root
    ~/project

  attach_workspace: &attach_workspace
    attach_workspace:
      at: *workspace_root

  npm_cache_keys: &npm_cache_keys
    keys:
        - v3-dependency-npm-{{ checksum "package.json" }}-
        - v3-dependency-npm-{{ checksum "package.json" }}
        - v3-dependency-npm-

  cache_npm_cache: &cache_npm_cache
    save_cache:
        key: v3-dependency-npm-{{ checksum "package.json" }}-{{ epoch }}
        paths:
        - ./node_modules/

  restore_npm_cache: &restore_npm_cache
    restore_cache:
        <<: *npm_cache_keys

  filters_only_main: &filters_only_main
    branches:
      only: main

  filters_only_renovate_nori: &filters_only_renovate_nori
    branches:
      only: /(^renovate-.*|^nori/.*)/

  filters_ignore_tags_renovate_nori: &filters_ignore_tags_renovate_nori
    tags:
      ignore: /.*/
    branches:
      ignore: /(^renovate-.*|^nori/.*)/

  filters_version_tag: &filters_version_tag
    tags:
      only:
        - /^v?\d+\.\d+\.\d+(?:-beta\.\d+)?$/
    branches:
      ignore: /.*/

version: 2.1

orbs:
  node: circleci/node@4.9.0

jobs:

  build:
    <<: *container_config_node
    steps:
      - checkout
      - run:
          name: Checkout next-ci-shared-helpers
          command: git clone --depth 1 git@github.com:Financial-Times/next-ci-shared-helpers.git .circleci/shared-helpers
      - *restore_npm_cache
      - node/install-npm:
          version: "^7"
      - run:
          name: Install project dependencies
          command: make install
      - run:
          name: Run the project build task
          command: make build
      - run:
          name: shared-helper / generate-build-state-artifacts
          command: .circleci/shared-helpers/helper-generate-build-state-artifacts
          when: always
      - *cache_npm_cache
      - store_artifacts:
          path: build-state
          destination: build-state
      - persist_to_workspace:
          root: *workspace_root
          paths:
            - build

  test:
    <<: *container_config_node
    steps:
      - *attach_workspace
      - run:
          name: Run tests
          command: npm run lint && make test
          environment:
            JEST_JUNIT_OUTPUT: test-results/jest/results.xml
            MOCHA_FILE: test-results/mocha/results.xml
      - store_test_results:
          path: test-results
      - store_artifacts:
          path: test-results
          destination: test-results

  publish:
    <<: *container_config_node
    steps:
      - *attach_workspace
      - run:
          name: shared-helper / npm-store-auth-token
          command: .circleci/shared-helpers/helper-npm-store-auth-token
      - run: npx snyk monitor --org=customer-products --project-name=Financial-Times/n-conversion-forms
      - run:
          name: shared-helper / npm-version-and-publish-public
          command: .circleci/shared-helpers/helper-npm-version-and-publish-public

workflows:

  version: 2

  build-test:
    jobs:
      - build:
          filters:
            <<: *filters_ignore_tags_renovate_nori
      - test:
          requires:
            - build

  build-test-publish:
    jobs:
      - build:
          filters:
            <<: *filters_version_tag
      - test:
          filters:
            <<: *filters_version_tag
          requires:
            - build
      - publish:
          context: npm-publish-token
          filters:
            <<: *filters_version_tag
          requires:
            - test

  renovate-nori-build-test-provision:
    jobs:
      - waiting-for-approval:
          type: approval
          filters:
              <<: *filters_only_renovate_nori
      - build:
          requires:
            - waiting-for-approval
      - test:
          requires:
            - build

  nightly:
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            <<: *filters_only_main
    jobs:
      - build:
          context: next-nightly-build
      - test:
          requires:
            - build
          context: next-nightly-build
