# mongotools continuous integration
name: main

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the prod branch
  push:
    branches: [ npmjs ]
  pull_request:

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    strategy:
      matrix:
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
        node-version: [ 20.x ]

    services:
      mongodb:
        image: mongo:5
        ports:
          - 27017:27017

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          # version from package.json
          run_install: false

      - name: Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v5
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'pnpm'

      - name: Install dependencies
        run: |
          echo ::group::Install dependencies
          echo "install"
          pnpm install
          echo "show outdated (if any)"
          pnpm outdated --depth=3 || echo "::warning::🕸️ you must think about update your dependencies :)"
          echo ::endgroup::
          pnpm outdated --depth=3 || echo "::notice title=OneLineReport::ℹ️ you must think about update your dependencies :)"

      - name: Setup mongo tools # add mongosh and mongodump/mongorestore
        uses: boly38/action-mongo-tools@stable

      - name: Setup mongo user
        # doc: https://docs.mongodb.com/manual/reference/built-in-roles/ "Backup and Restoration Roles"
        # doc: https://docs.mongodb.com/manual/reference/method/db.createUser/
        run: mongosh admin --eval 'db.createUser({user:"root",pwd:"mypass",roles:[{"role":"readWrite","db":"myDbForTest"}, "restore"]});'

      - name: Run TypeScript consumer acceptance test
        run: pnpm test:typescript

      - name: Run tests
        env:
          MT_MONGO_USER: root
          MT_MONGO_PWD: mypass
        run: |
          echo ::group::Run ci-tests
          pnpm ci-test
          echo ::endgroup::

      - name: COVERAGE - Report coverage on pull request
        if: github.event_name == 'pull_request'
        continue-on-error: true
        # uses: romeovs/lcov-reporter-action@v0.3.1
        uses: andybelltree/lcov-reporter-action@v1.7.0
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          lcov-file: ./coverage/lcov.info
          filter-changed-files: true

      - name: pnpm audit & comment on PR
        if: ${{ github.event_name == 'pull_request' }}
        uses: JamesRobertWiseman/pnpm-audit@v1.0.6
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          level: moderate
          fails: true

      # Publishing via 2FA must be done manually from the command line for now, until a better solution is available.
      # - name: Publish NpmJS package
      #   if: github.ref == 'refs/heads/npmjs'
      #   run: |
      #     echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_DEPLOY_TOKEN }}" > .npmrc
      #     npm whoami # rely on .npmrc
      #     npm publish
