name: Node CI for app development

on:
  push:
    branches-ignore:
      - release/**
      - rc/**
      - chore/**
      - support/prepare-v**
    paths:
      - .github/workflows/ci-app.yml
      - .eslint*
      - tsconfig.base.json
      - yarn.lock
      - packages/app/**
      - '!packages/app/docker/**'
      - packages/core/**
      - packages/slack/**
      - packages/ui/**
      - packages/plugin-*/**

jobs:
  lint:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'yarn'
          cache-dependency-path: '**/yarn.lock'

      - name: Cache/Restore node_modules
        id: cache-dependencies
        uses: actions/cache@v3
        with:
          path: |
            **/node_modules
          key: node_modules-${{ runner.OS }}-node${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('packages/app/package.json') }}
          restore-keys: |
            node_modules-${{ runner.OS }}-node${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}-
            node_modules-${{ runner.OS }}-node${{ matrix.node-version }}-

      - name: lerna bootstrap
        run: |
          npx lerna bootstrap -- --frozen-lockfile

      - name: lerna run lint for plugins
        run: |
          yarn lerna run lint --scope @growi/plugin-*
      - name: lerna run lint for app
        run: |
          yarn lerna run lint --scope @growi/app --scope @growi/codemirror-textlint --scope @growi/core --scope @growi/ui

      - name: Slack Notification
        uses: weseek/ghaction-slack-notification@master
        if: failure()
        with:
          type: ${{ job.status }}
          job_name: '*Node CI for growi - lint (${{ matrix.node-version }})*'
          channel: '#ci'
          isCompactMode: true
          url: ${{ secrets.SLACK_WEBHOOK_URL }}

  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]

    services:
      mongodb:
        image: mongo:4.4
        ports:
          - 27017/tcp

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'yarn'
          cache-dependency-path: '**/yarn.lock'

      - name: Cache/Restore node_modules
        id: cache-dependencies
        uses: actions/cache@v3
        with:
          path: |
            **/node_modules
          key: node_modules-${{ runner.OS }}-node${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('packages/app/package.json') }}
          restore-keys: |
            node_modules-${{ runner.OS }}-node${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}-
            node_modules-${{ runner.OS }}-node${{ matrix.node-version }}-

      - name: lerna bootstrap
        run: |
          npx lerna bootstrap -- --frozen-lockfile

      - name: yarn test
        working-directory: ./packages/app
        run: |
          yarn test:ci --selectProjects unit server ; yarn test:ci --selectProjects server-v5
        env:
          MONGO_URI: mongodb://localhost:${{ job.services.mongodb.ports['27017'] }}/growi_test

      - name: Upload coverage report as artifact
        uses: actions/upload-artifact@v3
        with:
          name: Coverage Report
          path: packages/app/coverage

      - name: Slack Notification
        uses: weseek/ghaction-slack-notification@master
        if: failure()
        with:
          type: ${{ job.status }}
          job_name: '*Node CI for growi - test (${{ matrix.node-version }})*'
          channel: '#ci'
          isCompactMode: true
          url: ${{ secrets.SLACK_WEBHOOK_URL }}

  launch-dev:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]

    services:
      mongodb:
        image: mongo:4.4
        ports:
          - 27017/tcp

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'yarn'
          cache-dependency-path: '**/yarn.lock'

      - name: Cache/Restore node_modules
        id: cache-dependencies
        uses: actions/cache@v3
        with:
          path: |
            **/node_modules
          key: node_modules-${{ runner.OS }}-node${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('packages/app/package.json') }}
          restore-keys: |
            node_modules-${{ runner.OS }}-node${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}-
            node_modules-${{ runner.OS }}-node${{ matrix.node-version }}-

      - name: lerna bootstrap
        run: |
          npx lerna bootstrap -- --frozen-lockfile

      - name: yarn dev:ci
        working-directory: ./packages/app
        run: |
          cp config/ci/.env.local.for-ci .env.development.local
          yarn dev:ci
        env:
          MONGO_URI: mongodb://localhost:${{ job.services.mongodb.ports['27017'] }}/growi_dev

      - name: Slack Notification
        uses: weseek/ghaction-slack-notification@master
        if: failure()
        with:
          type: ${{ job.status }}
          job_name: '*Node CI for growi - launch-dev (${{ matrix.node-version }})*'
          channel: '#ci'
          isCompactMode: true
          url: ${{ secrets.SLACK_WEBHOOK_URL }}
