name: CI
on:
  push:
    branches:
      - v7
  pull_request:

# This configuration cancels previous runs if a new run is started on the same PR. Only one run at a time per PR.
# This does not affect pushes to the v7 branch itself, only PRs.
# from https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-a-fallback-value
concurrency:
  group: ${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  install-and-build:
    strategy:
      fail-fast: false
      matrix:
        node-version: [ 14, 18 ]
    name: Upload install and build artifact (Node ${{ matrix.node-version }})
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
      - uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: yarn
      - name: Install dependencies and build sequelize
        run: yarn install --frozen-lockfile
      - name: Build sequelize
        run: yarn build
      - name: Compress artifact
        run: tar -cf install-build-node-${{ matrix.node-version }}.tar ./packages/core/lib ./packages/core/node_modules ./packages/core/types ./packages/validator-js/lib ./packages/validator-js/node_modules ./packages/validator-js/types ./node_modules
      - uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
        with:
          name: install-build-artifact-node-${{ matrix.node-version }}
          path: install-build-node-${{ matrix.node-version }}.tar
          retention-days: 1
  lint:
    name: Lint code
    runs-on: ubuntu-latest
    needs: install-and-build
    steps:
      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
      - uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
        with:
          node-version: 18.x
          cache: yarn
      - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
        with:
          name: install-build-artifact-node-18
      - name: Extract artifact
        run: tar -xf install-build-node-18.tar
      - run: yarn lint-no-fix
  unit-test:
    strategy:
      fail-fast: false
      matrix:
        node-version: [14, 18]
    name: Unit test all dialects (Node ${{ matrix.node-version }})
    runs-on: ubuntu-latest
    needs: lint
    steps:
      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
      - uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: yarn
      - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
        with:
          name: install-build-artifact-node-${{ matrix.node-version }}
      - name: Extract artifact
        run: tar -xf install-build-node-${{ matrix.node-version }}.tar
      - name: ESM / CJS export equivalence
        run: yarn test-unit-esm
      - name: Unit tests (validator.js)
        run: yarn workspace @sequelize/validator.js test-unit
      - name: Unit tests (mariadb)
        run: yarn workspace @sequelize/core test-unit-mariadb
      - name: Unit tests (mysql)
        run: yarn workspace @sequelize/core test-unit-mysql
      - name: Unit tests (postgres)
        run: yarn workspace @sequelize/core test-unit-postgres
      - name: Unit tests (sqlite)
        run: yarn workspace @sequelize/core test-unit-sqlite
      - name: Unit tests (mssql)
        run: yarn workspace @sequelize/core test-unit-mssql
      - name: Unit tests (db2)
        run: yarn workspace @sequelize/core test-unit-db2
      - name: Unit tests (ibmi)
        run: yarn workspace @sequelize/core test-unit-ibmi
      - name: Unit tests (snowflake)
        run: yarn workspace @sequelize/core test-unit-snowflake
  docs:
    name: Generate TypeDoc
    runs-on: ubuntu-latest
    needs: lint
    steps:
      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
      - uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
        with:
          node-version: 18.x
          cache: yarn
      - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
        with:
          name: install-build-artifact-node-18
      - name: Extract artifact
        run: tar -xf install-build-node-18.tar
      - run: yarn docs
  test-typings:
    strategy:
      fail-fast: false
      matrix:
        ts-version: ["4.7", "4.8", "4.9", "5.0"]
    name: TS Typings (${{ matrix.ts-version }})
    runs-on: ubuntu-latest
    needs: lint
    steps:
      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
      - uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
        with:
          node-version: 18.x
          cache: yarn
      - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
        with:
          name: install-build-artifact-node-18
      - name: Extract artifact
        run: tar -xf install-build-node-18.tar
      - name: Install TypeScript
        run: yarn add typescript@~${{ matrix.ts-version }} -W
      - name: Typing Tests
        run: yarn workspace @sequelize/core test-typings
  test-sqlite:
    strategy:
      fail-fast: false
      matrix:
        node-version: [14, 18]
    name: sqlite (Node ${{ matrix.node-version }})
    runs-on: ubuntu-latest
    needs: [ unit-test, test-typings ]
    env:
      DIALECT: sqlite
    steps:
      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
      - uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: yarn
      - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
        with:
          name: install-build-artifact-node-${{ matrix.node-version }}
      - name: Extract artifact
        run: tar -xf install-build-node-${{ matrix.node-version }}.tar
      - name: Integration Tests
        run: yarn workspace @sequelize/core test-integration
  test-postgres:
    strategy:
      fail-fast: false
      matrix:
        node-version: [14, 18]
        postgres-version: [oldest, latest]
        minify-aliases: [true, false]
        native: [true, false]
    name: postgres ${{ matrix.postgres-version }}${{ matrix.native && ' (native)' || '' }} (Node ${{ matrix.node-version }})${{ matrix.minify-aliases && ' (minified aliases)' || '' }}
    runs-on: ubuntu-latest
    needs: [ unit-test, test-typings ]
    env:
      DIALECT: ${{ matrix.native && 'postgres-native' || 'postgres' }}
      SEQ_PG_MINIFY_ALIASES: ${{ matrix.minify-aliases && '1' || '' }}
    steps:
      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
      - uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: yarn
      - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
        with:
          name: install-build-artifact-node-${{ matrix.node-version }}
      - name: Extract artifact
        run: tar -xf install-build-node-${{ matrix.node-version }}.tar
      - name: Install pg-native
        run: yarn workspace @sequelize/core add pg-native --ignore-engines
      - run: yarn start-postgres-${{ matrix.postgres-version }}
      - name: Integration Tests
        run: yarn workspace @sequelize/core test-integration
  test-oldest-latest:
    strategy:
      fail-fast: false
      matrix:
        node-version: [14, 18]
        database-version: [oldest, latest]
        dialect: [mysql, mariadb, mssql, db2]
    name: ${{ matrix.dialect }} ${{ matrix.database-version }} (Node ${{ matrix.node-version }})
    runs-on: ubuntu-latest
    needs: [ unit-test, test-typings ]
    env:
      DIALECT: ${{ matrix.dialect }}
    steps:
      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
      - uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: yarn
      - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
        with:
          name: install-build-artifact-node-${{ matrix.node-version }}
      - name: Extract artifact
        run: tar -xf install-build-node-${{ matrix.node-version }}.tar
      - run: yarn start-${{ matrix.dialect }}-${{ matrix.database-version }}
      - name: Integration Tests
        run: yarn workspace @sequelize/core test-integration
  release:
    name: Release
    runs-on: ubuntu-latest
    needs:
      [
        docs,
        test-sqlite,
        test-postgres,
        test-oldest-latest
      ]
    if: github.event_name == 'push'
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
        with:
          # Number of commits to fetch. 0 indicates all history for all branches and tags.
          # We need the entire history to generate the changelog properly
          fetch-depth: 0
      - uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
        with:
          node-version: 18.x
          cache: yarn
      - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
        with:
          name: install-build-artifact-node-18
      - name: Extract artifact
        run: tar -xf install-build-node-18.tar
      - name: Configure git
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "bot@sequelize.org"
      - name: Set npm auth token
        run: npm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
      - run: yarn publish-all
      - id: sequelize
        uses: sdepold/github-action-get-latest-release@aa12fcb2943e8899cbcc29ff6f73409b32b48fa1 # master
        with:
          repository: sequelize/sequelize
      - name: Notify channels
        run: |
          curl -XPOST -u "sdepold:${{ secrets.GH_TOKEN }}" -H "Accept: application/vnd.github.v3+json" -H "Content-Type: application/json" https://api.github.com/repos/sequelize/sequelize/dispatches --data '{"event_type":"Release notifier","client_payload":{"release-id": ${{ steps.sequelize.outputs.id }}}}'
      - name: Notify docs repo
        run: |
          curl -XPOST -u "sdepold:${{ secrets.GH_TOKEN }}" -H "Accept: application/vnd.github.v3+json" -H "Content-Type: application/json" https://api.github.com/repos/sequelize/website/dispatches --data '{"event_type":"Build website"}'
