name: Validate pull request modifications
on:
  pull_request:
    paths:
      - 'front-packages/akeneo-design-system/**'

jobs:
  test:
    name: Launch tests on the DSM
    runs-on: ubuntu-latest

    steps:
      - name: Checkout the current branch
        uses: actions/checkout@v2

      - name: Install node 18 for the rest of the workflow
        uses: actions/setup-node@v2
        with:
          node-version: '18'

      - name: Cache node modules
        uses: actions/cache@v2
        env:
          cache-name: cache-node-modules
        with:
          path: '**/front-packages/akeneo-design-system/node_modules'
          key: ${{ runner.os }}-node-modules-${{ hashFiles('**/front-packages/akeneo-design-system/yarn.lock') }}

      - name: Install peer dependencies
        # TODO RAC-594 Do not used fixed peer dependencies
        run: cd front-packages/akeneo-design-system && yarn add --dev react@^16.14.0 react-dom@^16.9.12 styled-components@^5.1.1

      - name: Install node modules
        run: cd front-packages/akeneo-design-system && yarn install

      - name: Generate a dummy component to test it with the others
        run: cd front-packages/akeneo-design-system && ./bin/dsm generate Dummy

      - name: Cache TypeScript build
        uses: actions/cache@v2
        env:
          cache-name: cache-typescript-build
        with:
          path: '**/front-packages/akeneo-design-system/tsconfig.build.tsbuildinfo'
          key: ${{ runner.os }}-typescript-${{ hashFiles('**/front-packages/akeneo-design-system/src') }}

      - name: Cache lib build
        uses: actions/cache@v2
        env:
          cache-name: cache-lib-build
        with:
          path: '**/front-packages/akeneo-design-system/lib'
          key: ${{ runner.os }}-lib-${{ hashFiles('**/front-packages/akeneo-design-system/src') }}

      - name: Make sure the lib build is successful
        run: cd front-packages/akeneo-design-system && yarn lib:build

      - name: Launch lint checks
        run: cd front-packages/akeneo-design-system && yarn lint:check

      - uses: actions/cache@v2
        name: Cache jest cache
        env:
          cache-name: cache-jest-cache
        with:
          path: /tmp/jest/**
          key: ${{ runner.os }}-jest-cache

      - name: Launch unit tests
        run: cd front-packages/akeneo-design-system && yarn test:unit:run

      - name: Build storybook
        id: build_storybook
        run: cd front-packages/akeneo-design-system && yarn storybook:build && echo ::set-output name=status::success

      - name: Launch visual tests
        run: cd front-packages/akeneo-design-system && yarn test:visual:run && echo ::set-output name=status::success
        id: visual_test_execution

      - name: Cache example node modules
        uses: actions/cache@v2
        env:
          cache-name: cache-example-node-modules
        with:
          path: '**/front-packages/akeneo-design-system/example/node_modules'
          key: ${{ runner.os }}-example-project-node-modules-${{ hashFiles('**/front-packages/akeneo-design-system/example/yarn.lock') }}

      # To check if the build is working, the directory `example` contains an integration of the lib
      # with a few tests. In the next step we will build this example and validate that it's working properly
      - name: Install example project dependencies
        run: cd front-packages/akeneo-design-system/example && rm -rf node_modules/akeneo-design-system && yarn install --check-files

      - name: Launch example integration tests
        run: cd front-packages/akeneo-design-system/example && yarn test

      - name: Upload the src folder on failure for debugging purposes
        uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: src-folder
          path: front-packages/akeneo-design-system/src

      # In this next section (only launched if the visual regression failed) we will update the visual snapshots
      # to provide a pull request to the developer contributing to the DSM.
      - name: Remove files generated by previous steps
        if: failure() && steps.visual_test_execution.outputs.status != 'success' && steps.build_storybook.outputs.status == 'success'
        run: cd front-packages/akeneo-design-system && git reset HEAD --hard

      - name: Update visual tests snapshots
        if: failure() && steps.visual_test_execution.outputs.status != 'success' && steps.build_storybook.outputs.status == 'success'
        run: cd front-packages/akeneo-design-system && yarn test:visual:update

      - name: Get the current branch name to expose it later on
        uses: mdecoleman/pr-branch-name@1.0.0
        if: failure() && steps.visual_test_execution.outputs.status != 'success' && steps.build_storybook.outputs.status == 'success'
        id: extract_branch
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Remove Dummy component
        if: failure() && steps.visual_test_execution.outputs.status != 'success' && steps.build_storybook.outputs.status == 'success'
        run: rm -rf front-packages/akeneo-design-system/src/components/Dummy

      - name: Push to a new branch and create pull request targeting the current one
        if: failure() && steps.visual_test_execution.outputs.status != 'success' && steps.build_storybook.outputs.status == 'success'
        id: create_visual_pull_request
        uses: peter-evans/create-pull-request@v3
        with:
          commit-message: Update visual tests for branch ${{ steps.extract_branch.outputs.branch }}
          branch: ${{ steps.extract_branch.outputs.branch }}-visual-update
          base: ${{ steps.extract_branch.outputs.branch }}
          title: Update visual tests for branch ${{ steps.extract_branch.outputs.branch }}
          body: This PR updates the visual tests for the changes in the branch ${{ steps.extract_branch.outputs.branch }}
          assignees: ${{ github.actor }}

      - name: Comment current PR to notify that new visual snapshots are available
        uses: thollander/actions-comment-pull-request@main
        if: failure() && steps.visual_test_execution.outputs.status != 'success' && steps.build_storybook.outputs.status == 'success'
        with:
          message: |
            Your visual regression tests are not passing <img width="25" src="https://emoji.slack-edge.com/T031L1UKF/sad-dog/ac79218e153de6de.jpg" />
            Here is a pull request to fix them: https://github.com/${{ github.repository }}/pull/${{ steps.create_visual_pull_request.outputs.pull-request-number }} :tada:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Deploy to gh-pages on the external repository (in a subfolder)
        uses: peaceiris/actions-gh-pages@v3.7.0-6
        with:
          personal_token: ${{ secrets.DSM_EXTERNAL_TOKEN }}
          publish_dir: ./front-packages/akeneo-design-system/storybook-static
          destination_dir: ${{ github.event.number }}
          keep_files: true
          external_repository: akeneo/akeneo-design-system

      - name: Comment PR to say that the staging env is available
        uses: thollander/actions-comment-pull-request@main
        with:
          message: |
            A new version of the staging env has been deployed :tada:
            Check it out here: https://akeneo.github.io/akeneo-design-system/${{ github.event.number }}/
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
