name: Verify

on:
  push:
    branches:
      - '**'
      - '!**--skip-ci'
      - '!**--visual-reports'
      - '!wip/**'
      - '!experiments/**'
  pull_request:
    branches:
      - 'portal'
      - 'release'
      - 'beta'
      - 'alpha'
      - 'next'
    types: [opened]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

env:
  GH_EMAIL: ${{ secrets.GH_EMAIL }}
  GH_NAME: ${{ secrets.GH_NAME }}
  GH_TOKEN: ${{ secrets.GH_TOKEN }}
  NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
  FIGMA_TOKEN: ${{ secrets.FIGMA_TOKEN }}
  FIGMA_ICONS_FILE: ${{ secrets.FIGMA_ICONS_FILE }}

jobs:
  verify:
    name: Run tests and checks

    runs-on: ubuntu-latest

    timeout-minutes: 20

    steps:
      - name: Git checkout
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: 'package.json'

      - name: Use node_modules cache
        uses: actions/cache@v4
        id: modules-cache
        with:
          path: '**/node_modules'
          key: ${{ secrets.CACHE_VERSION }}-${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
          restore-keys: ${{ secrets.CACHE_VERSION }}-${{ runner.os }}-modules-

      - name: Install dependencies
        if: steps.modules-cache.outputs.cache-hit != 'true'
        run: yarn install --immutable

      - name: Audit dependencies
        run: yarn workspace @dnb/eufemia audit:ci

      - name: Run lint
        run: yarn workspace @dnb/eufemia lint:ci && yarn workspace dnb-design-system-portal lint:ci

      - name: Run type checks
        run: yarn workspace @dnb/eufemia test:types && yarn workspace dnb-design-system-portal test:types

      - name: Run tests
        run: yarn workspace @dnb/eufemia test:ci && yarn workspace dnb-design-system-portal test:ci

      - name: Slack
        uses: 8398a7/action-slack@v3
        with:
          channel: '#eufemia-dev'
          status: ${{ job.status }}
          fields: author,ref,workflow,job,took
          job_name: Run tests and checks
          github_base_url: https://github.com/
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
        if: ${{ always() && !github.event.pull_request.head.repo.fork }}
        continue-on-error: true

  build-check:
    name: Run build tests

    runs-on: ubuntu-latest

    steps:
      - name: Git checkout (with fetch-depth)
        uses: actions/checkout@v4
        with:
          persist-credentials: false
          fetch-depth: 20 # The "postbuild:ci" method "getCommittedFiles" needs all history

      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: 'package.json'

      - name: Use node_modules cache
        uses: actions/cache@v4
        id: modules-cache
        with:
          path: '**/node_modules'
          key: ${{ secrets.CACHE_VERSION }}-${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
          restore-keys: ${{ secrets.CACHE_VERSION }}-${{ runner.os }}-modules-

      - name: Install dependencies
        if: steps.modules-cache.outputs.cache-hit != 'true'
        run: yarn install --immutable

      - name: Verify isCI
        run: yarn workspace repo-utils test-is-ci

      - name: Prebuild Library
        run: yarn workspace @dnb/eufemia prebuild:ci

      - name: Postbuild Library
        run: yarn workspace @dnb/eufemia postbuild:ci

      - name: Slack
        uses: 8398a7/action-slack@v3
        with:
          channel: '#eufemia-dev'
          status: ${{ job.status }}
          fields: author,ref,workflow,job,took
          job_name: Run build tests
          github_base_url: https://github.com/
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
        if: ${{ always() && !github.event.pull_request.head.repo.fork }}
        continue-on-error: true