name: "[e2e] prod-specs"

on:
  workflow_dispatch:
    inputs:
      url:
        required: true
        default: https://interactive-viewer-next.apps-dev.hbp.eu/
      sha:
        required: true
        default: ''

jobs:
  update-e2e-status:
    runs-on: ubuntu-latest
    env:
      URL_ROOT: https://api.github.com/repos/HumanBrainProject/interactive-viewer
      SHA: ${{ github.event.inputs.sha }}
    steps:
      - name: update commit status
        run: |
          curl -v \
            -X POST \
            -H "Authorization: Bearer ${{ github.token }}" \
            -H 'accept: application/vnd.github.v3+json' \
            ${URL_ROOT}/statuses/${SHA} \
            -d '{
              "target_url":"'$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID'",
              "description": "Running e2e",
              "state": "pending"
            }'
  e2e:
    needs: update-e2e-status
    runs-on: [self-hosted, headless]
    strategy:
      fail-fast: false
      matrix:
        protractor-spec: [
          './src/advanced/urlParsing.prod.e2e-spec.js',
          './src/advanced/pluginApi.prod.e2e-spec.js',
          './src/advanced/nonAtlasImages.prod.e2e-spec.js',
          # './src/advanced/browsingForDatasets.prod.e2e-spec.js',
          './src/advanced/favDatasets.prod.e2e-spec.js',
          './src/advanced/history.prod.e2e-spec.js',
          
          './src/selecting/region.prod.e2e-spec.js',
          './src/selecting/template.prod.e2e-spec.js',
          './src/selecting/atlas.prod.e2e-spec.js',

          './src/layout/home.prod.e2e-spec.js'
        ]
    env:
      PROTRACTOR_SPECS: ${{ matrix.protractor-spec }}
      ATLAS_URL: ${{ github.event.inputs.url }}
      PPTR_VERSION: "5.3.1"
      CHROMIUM_VERSION: "86.0.4240.0"

    outputs:
      failure-state: ${{ steps.failure-state-step.outputs.failure-state }}

    steps:
    - uses: actions/checkout@v2
      with:
        ref: ${{ github.event.ref }}

    - name: Install dep
      run: |
        npm i
        npm run wd -- update --versions.chrome=${CHROMIUM_VERSION}
        npm i --no-save puppeteer@${PPTR_VERSION}
  
    - name: 'Run e2e for ${{ matrix.protractor-spec }} on ${{ github.event.inputs.url }}'
      run: npm run e2e

    - name: Set output when workflow fails
      if: failure()
      run: echo '::set-output name=failure-state::true'
      id: failure-state-step

    - name: Define screenshot artefact
      if: failure()
      run: |
        strip_prefix=${PROTRACTOR_SPECS#./src/}
        strip_suffix=${strip_prefix%.e2e-spec.js}
        replace_slash=${strip_suffix//\//-}
        replace_dots=${replace_slash//\./-}
        echo 'ARTEFACT_NAME='$replace_dots >> $GITHUB_ENV

    - name: 'Upload screenshots artefact'
      if: failure()
      uses: actions/upload-artifact@v2
      with:
        name: ${{ env.ARTEFACT_NAME }}
        path: ./scrnsht/

  update-after-e2e-status:
    if: always()
    needs: e2e
    runs-on: ubuntu-latest
    env:
      URL_ROOT: https://api.github.com/repos/HumanBrainProject/interactive-viewer
      SHA: ${{ github.event.inputs.sha }}
    steps:
      - name: update badge
        run: |
          [[ '${{ needs.e2e.outputs.failure-state }}' = 'true' ]] && state=failure || state=success &&
          curl -v \
            -X POST \
            -H "Authorization: Bearer ${{ github.token }}" \
            -H 'accept: application/vnd.github.v3+json' \
            ${URL_ROOT}/statuses/${SHA} \
            -d '{
              "target_url":"'$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID'",
              "description": "e2e completed successfully.",
              "state": "'$state'"
            }'
