name: CI
on:
  push:
    branches:
      - master
      - hotfix/**
      - release/**
  pull_request:
    branches:
      - master
jobs:
  codemetrics:
    runs-on: ubuntu-latest
    name: "Log Code Metrics"
    if: github.ref == 'refs/heads/master'
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - run: npm ci
      - run: node utils/codeMetrics.js
        env:
          CODE_METRICS_APP_ID: ${{ secrets.CODE_METRICS_APP_ID }}
  compile:
    runs-on: ubuntu-latest
    name: "Compile TypeScript"
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - run: npm ci
      - run: npm run compile
      - run: npm run compile:strict
  format:
    runs-on: ubuntu-latest
    name: "Check Format"
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - run: npm ci
      - run: npm run format:check
  lint:
    runs-on: ubuntu-latest
    name: "Lint"
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - run: npm ci
      - run: npm run lint
  unittest:
    runs-on: ubuntu-latest
    name: "Unit Tests"
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - run: npm ci
      - run: npm run test
  build:
    runs-on: ubuntu-latest
    name: "Build"
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - run: npm ci
      - run: npm run build:contracts
      - name: Restore Build Cache
        uses: actions/cache@v2
        with:
          path: .cache
          key: ${{ runner.os }}-build-cache
      - run: npm run pack:prod
      - run: cp -r ./Contracts ./dist/contracts
      - run: cp -r ./configs ./dist/configs
      - uses: actions/upload-artifact@v2
        with:
          name: dist
          path: dist/
      - name: Upload build to preview blob storage
        run: az storage blob upload-batch -d '$web' -s 'dist' --account-name cosmosexplorerpreview --subscription cosmosdb-portalteam-generaldemo --destination-path "${{github.event.pull_request.head.sha || github.sha}}" --account-key="${PREVIEW_STORAGE_KEY}"
        env:
          PREVIEW_STORAGE_KEY: ${{ secrets.PREVIEW_STORAGE_KEY }}
      - name: Upload preview config to blob storage
        run: az storage blob upload -c '$web' -f ./preview/config.json --account-name cosmosexplorerpreview --subscription cosmosdb-portalteam-generaldemo --name "${{github.event.pull_request.head.sha || github.sha}}/config.json" --account-key="${PREVIEW_STORAGE_KEY}"
        env:
          PREVIEW_STORAGE_KEY: ${{ secrets.PREVIEW_STORAGE_KEY }}
  endtoendemulator:
    name: "End To End Emulator Tests"
    # Temporarily disabled. This test needs to be rewritten in playwright
    if: false
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - uses: southpolesteve/cosmos-emulator-github-action@v1
      - name: End to End Tests
        run: |
          npm ci
          npm start &
          npm run wait-for-server
          npx jest -c ./jest.config.e2e.js --detectOpenHandles test/sql/container.spec.ts
        shell: bash
        env:
          DATA_EXPLORER_ENDPOINT: "https://localhost:1234/explorer.html?platform=Emulator"
          PLATFORM: "Emulator"
          NODE_TLS_REJECT_UNAUTHORIZED: 0
      - uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: screenshots
          path: failed-*
  endtoend:
    name: "E2E"
    runs-on: ubuntu-latest
    env:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
      NOTEBOOKS_TEST_RUNNER_CLIENT_SECRET: ${{ secrets.NOTEBOOKS_TEST_RUNNER_CLIENT_SECRET }}
    strategy:
      fail-fast: false
      matrix:
        test-file:
          - ./test/cassandra/container.spec.ts
          - ./test/graph/container.spec.ts
          - ./test/sql/container.spec.ts
          - ./test/mongo/container.spec.ts
          - ./test/mongo/container32.spec.ts
          - ./test/selfServe/selfServeExample.spec.ts
          # - ./test/notebooks/upload.spec.ts // TEMP disabled since notebooks service is off
          - ./test/sql/resourceToken.spec.ts
          - ./test/tables/container.spec.ts
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - run: npm ci
      - run: npm start &
      - run: npm run wait-for-server
      - name: ${{ matrix['test-file'] }}
        run: |
          # Run tests up to three times
          for i in $(seq 1 3); do npx jest -c ./jest.config.playwright.js ${{ matrix['test-file'] }} && s=0 && break || s=$? && sleep 1; done; (exit $s)
        shell: bash
      - uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: screenshots
          path: screenshots/
  nuget:
    name: Publish Nuget
    if: github.ref == 'refs/heads/master' || contains(github.ref, 'hotfix/') || contains(github.ref, 'release/')
    needs: [build]
    runs-on: ubuntu-latest
    env:
      NUGET_SOURCE: ${{ secrets.NUGET_SOURCE }}
      AZURE_DEVOPS_PAT: ${{ secrets.AZURE_DEVOPS_PAT }}
    steps:
      - uses: nuget/setup-nuget@v1
        with:
          nuget-api-key: ${{ secrets.NUGET_API_KEY }}
      - name: Download Dist Folder
        uses: actions/download-artifact@v2
        with:
          name: dist
      - run: cp ./configs/prod.json config.json
      - run: nuget sources add -Name "ADO" -Source "$NUGET_SOURCE" -UserName "GitHub" -Password "$AZURE_DEVOPS_PAT"
      - run: nuget pack -Version "2.0.0-github-${GITHUB_SHA}"
      - run: nuget push -SkipDuplicate -Source "$NUGET_SOURCE" -ApiKey Az *.nupkg
      - uses: actions/upload-artifact@v2
        name: packages
        with:
          path: "*.nupkg"
  nugetmpac:
    name: Publish Nuget MPAC
    if: github.ref == 'refs/heads/master' || contains(github.ref, 'hotfix/') || contains(github.ref, 'release/')
    needs: [build]
    runs-on: ubuntu-latest
    env:
      NUGET_SOURCE: ${{ secrets.NUGET_SOURCE }}
      AZURE_DEVOPS_PAT: ${{ secrets.AZURE_DEVOPS_PAT }}
    steps:
      - uses: nuget/setup-nuget@v1
        with:
          nuget-api-key: ${{ secrets.NUGET_API_KEY }}
      - name: Download Dist Folder
        uses: actions/download-artifact@v2
        with:
          name: dist
      - run: cp ./configs/mpac.json config.json
      - run: sed -i 's/Azure.Cosmos.DB.Data.Explorer/Azure.Cosmos.DB.Data.Explorer.MPAC/g' DataExplorer.nuspec
      - run: nuget sources add -Name "ADO" -Source "$NUGET_SOURCE" -UserName "GitHub" -Password "$AZURE_DEVOPS_PAT"
      - run: nuget pack -Version "2.0.0-github-${GITHUB_SHA}"
      - run: nuget push -SkipDuplicate -Source "$NUGET_SOURCE" -ApiKey Az *.nupkg
      - uses: actions/upload-artifact@v2
        name: packages
        with:
          path: "*.nupkg"
