name: Litmus-CI
on:
  issue_comment:
    types: [created]
    branches:
      - master

jobs:
  tests:
    if: contains(github.event.comment.html_url, '/pull/') && startsWith(github.event.comment.body, '/run')
    runs-on: ubuntu-latest
    steps:

      - name: Notification for Starting Testing.
        uses: peter-evans/create-or-update-comment@v1
        with:
          comment-id: "${{ github.event.comment.id }}"
          body: |
            ****
            **Test Status:** The testing has been started please wait for the results ...

      #Using the last commit id of pull request
      - uses: octokit/request-action@v2.x
        id: get_PR_commits
        with:
          route: GET /repos/:repo/pulls/:pull_number/commits
          repo: ${{ github.repository }}
          pull_number: ${{ github.event.issue.number }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: set commit to output
        id: getcommit
        run: |
           prsha=$(echo $response | jq '.[-1].sha'  | tr -d '"')
           echo "::set-output name=sha::$prsha"
        env:
          response:  ${{ steps.get_PR_commits.outputs.data }}

      - uses: actions/checkout@v2
        with:
          ref: ${{steps.getcommit.outputs.sha}}

      - name: Setting Dependencies
        uses: actions/setup-node@v1
        with:
          working-directory: litmus-portal/frontend

      - name: Installing Dependencies for frontend.
        run: |
          HUSKY_SKIP_INSTALL=1 npm i
        working-directory: litmus-portal/frontend

      # Step for running all frontend Cypress unit tests.
      - name: Starting cypress unit tests
        if: startsWith(github.event.comment.body, '/run-unit') || startsWith(github.event.comment.body, '/run-all')
        uses: cypress-io/github-action@v2
        continue-on-error: false
        with:
          command: npm run unit:ci
          install: false
          working-directory: litmus-portal/frontend

      # Cloning the litmus-e2e repo for E2E tests.
      - name: Cloning the litmus-e2e Repo
        if: startsWith(github.event.comment.body, '/run-e2e') || startsWith(github.event.comment.body, '/run-all')
        run: |
           git clone https://github.com/litmuschaos/litmus-e2e.git -b litmus-portal

      # Starting the React-Server for E2E tests.
      - name: Starting the server
        if: startsWith(github.event.comment.body, '/run-e2e') || startsWith(github.event.comment.body, '/run-all')
        run: |
          CHOKIDAR_USEPOLLING=1 npm start &
        working-directory: litmus-portal/frontend

      - name: Installing Cypress and running tests for login page
        if: startsWith(github.event.comment.body, '/run-e2e-login')
        uses: cypress-io/github-action@v2
        continue-on-error: false
        with:
          wait-on: http://localhost:3000
          spec: cypress/integration/Login/*
          working-directory: litmus-e2e/CypressE2E/

      - name: Install Cypress and run all tests
        if: startsWith(github.event.comment.body, '/run-e2e-all') || startsWith(github.event.comment.body, '/run-all')
        uses: cypress-io/github-action@v2
        continue-on-error: false
        with:
          wait-on: http://localhost:3000
          spec: cypress/integration/**/*
          working-directory: litmus-e2e/CypressE2E

      - name: Check the test run
        if: |
         startsWith(github.event.comment.body, '/run-unit') || startsWith(github.event.comment.body, '/run-all') ||
         startsWith(github.event.comment.body, '/run-e2e-login') || startsWith(github.event.comment.body, '/run-e2e-workflow') ||
         startsWith(github.event.comment.body, '/run-e2e-all')
        run: |
          echo ::set-env name=TEST_RUN::true

      - name: Check for all the jobs are succeeded
        if: ${{ success() && env.TEST_RUN == 'true' }}
        uses: peter-evans/create-or-update-comment@v1
        with:
          comment-id: "${{ github.event.comment.id }}"
          body: |
            **Test Result:** All tests are passed
            **Run ID:** [${{ env.RUN_ID }}](https://github.com/litmuschaos/litmus/actions/runs/${{ env.RUN_ID }})
          reactions: hooray
        env:
          RUN_ID: ${{ github.run_id }}

      - name: Check for any job failed
        if: ${{ failure() }}
        uses: peter-evans/create-or-update-comment@v1
        with:
          comment-id: "${{ github.event.comment.id }}"
          body: |
            **Test Failed:** Some tests are failed please check
            **Run ID:** [${{ env.RUN_ID }}](https://github.com/litmuschaos/litmus/actions/runs/${{ env.RUN_ID }})
          reactions: confused
        env:
          RUN_ID: ${{ github.run_id }}

      - name: Check if any test ran or not
        if: env.TEST_RUN != 'true'
        uses: peter-evans/create-or-update-comment@v1
        with:
          comment-id: "${{ github.event.comment.id }}"
          body: |
            **Test Result:** No test found
            **Run ID:** [${{ env.RUN_ID }}](https://github.com/litmuschaos/litmus/actions/runs/${{ env.RUN_ID }})
          reactions: eyes
        env:
          RUN_ID: ${{ github.run_id }}

  merge:
    if: contains(github.event.comment.html_url, '/pull/') && startsWith(github.event.comment.body, '/merge')
    runs-on: ubuntu-latest
    steps:
      - name: Add a merge label when all jobs are passed
        uses: actions-ecosystem/action-add-labels@v1
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          labels: merge