version: 2.1
orbs:
  tool-kit: financial-times/dotcom-tool-kit@5
  slack: circleci/slack@4.6.0
executors:
  node:
    docker:
      - image: cimg/node:18.20
  node20_15:
    docker:
      - image: cimg/node:20.15
  node22_4:
    docker:
      - image: cimg/node:22.4
commands:
  notify_slack_error:
    steps:
      - slack/notify:
          custom: |
            {
              "blocks": [
                {
                  "type": "section",
                  "fields": [
                    {
                      "type": "mrkdwn",
                      "text": ":space_invader: The nightly *$CIRCLE_PROJECT_REPONAME* tests *failed* with Node version *$NODE_VERSION*. Hardcoded concept IDs might have changed and need updating"
                    }
                  ]
                },
                {
                  "type": "actions",
                  "elements": [
                    {
                      "type": "button",
                      "text": {
                        "type": "plain_text",
                        "text": "View Job"
                      },
                      "url": "${CIRCLE_BUILD_URL}"
                    }
                  ]
                }
              ]
            }
          event: fail
          channel: content-discovery-dev
jobs:
  checkout:
    docker:
      - image: cimg/base:stable
    steps:
      - checkout
      - tool-kit/persist-workspace:
          path: .
workflows:
  tool-kit:
    when:
      not:
        equal:
          - scheduled_pipeline
          - << pipeline.trigger_source >>
    jobs:
      - checkout:
          filters:
            tags:
              only: /^v\d+\.\d+\.\d+(-.+)?/
      - waiting-for-approval:
          type: approval
          filters:
            branches:
              only: /(^renovate-.*|^nori/.*)/
      - tool-kit/setup:
          name: tool-kit/setup-<< matrix.executor >>
          requires:
            - checkout
            - waiting-for-approval
          matrix:
            parameters:
              executor:
                - node
                - node20_15
                - node22_4
          filters:
            tags:
              only: /^v\d+\.\d+\.\d+(-.+)?/
      - tool-kit/build:
          name: tool-kit/build-<< matrix.executor >>
          requires:
            - tool-kit/setup-<< matrix.executor >>
          matrix:
            parameters:
              executor:
                - node
                - node20_15
                - node22_4
          filters:
            tags:
              only: /^v\d+\.\d+\.\d+(-.+)?/
      - tool-kit/test:
          name: tool-kit/test-<< matrix.executor >>
          requires:
            - tool-kit/build-<< matrix.executor >>
          matrix:
            parameters:
              executor:
                - node
                - node20_15
                - node22_4
          filters:
            tags:
              only: /^v\d+\.\d+\.\d+(-.+)?/
      - tool-kit/publish-tag:
          requires:
            - tool-kit/test-node
          name: tool-kit/publish-tag-node
          executor: node
          filters:
            tags:
              only: /^v\d+\.\d+\.\d+(-.+)?/
            branches:
              ignore: /.*/
          context: npm-publish-token
  nightly:
    when:
      and:
        - equal:
            - scheduled_pipeline
            - << pipeline.trigger_source >>
        - equal:
            - nightly
            - << pipeline.schedule.name >>
    jobs:
      - checkout
      - tool-kit/setup:
          name: tool-kit/setup-<< matrix.executor >>
          requires:
            - checkout
          matrix:
            parameters:
              executor:
                - node
                - node20_15
                - node22_4
      - tool-kit/build:
          name: tool-kit/build-<< matrix.executor >>
          requires:
            - tool-kit/setup-<< matrix.executor >>
          matrix:
            parameters:
              executor:
                - node
                - node20_15
                - node22_4
      - tool-kit/test:
          post-steps:
            - notify_slack_error
          name: tool-kit/test-<< matrix.executor >>
          requires:
            - tool-kit/build-<< matrix.executor >>
          matrix:
            parameters:
              executor:
                - node
                - node20_15
                - node22_4
