on:
  push:
  pull_request:
  release:
    types:
      - created

name: CI
jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      decentraland_ecs_s3_bucket_key: ${{ steps.publish_decentraland_ecs.outputs.s3-bucket-key }}
    steps:
      - uses: actions/checkout@master
      - name: Use Node.js 16.x
        uses: actions/setup-node@v1
        with:
          node-version: 16.x
      - name: install
        run: make install
      - name: update version of packages/@dcl/dcl-rollup
        uses: menduz/oddish-action@master
        with:
          cwd: packages/@dcl/dcl-rollup
          deterministic-snapshot: true
          only-update-versions: true
      - name: update version of packages/@dcl/build-ecs
        uses: menduz/oddish-action@master
        with:
          cwd: packages/@dcl/build-ecs
          deterministic-snapshot: true
          only-update-versions: true
      - name: update version of packages/@dcl/amd
        uses: menduz/oddish-action@master
        with:
          cwd: packages/@dcl/amd
          deterministic-snapshot: true
          only-update-versions: true
      - name: update version of packages/@dcl/js-runtime
        uses: menduz/oddish-action@master
        with:
          cwd: packages/@dcl/js-runtime
          deterministic-snapshot: true
          only-update-versions: true
      - name: update version of packages/@dcl/ecs
        uses: menduz/oddish-action@master
        with:
          cwd: packages/@dcl/ecs
          deterministic-snapshot: true
          only-update-versions: true
      - name: update version of packages/@dcl/sdk
        uses: menduz/oddish-action@master
        with:
          cwd: packages/@dcl/sdk
          deterministic-snapshot: true
          only-update-versions: true
      - name: build
        run: make build
      - name: test
        run: make test
      - name: lint
        run: make lint
      - name: prepare releasable packages
        run: make prepare
      - name: 'Upload Artifact'
        uses: actions/upload-artifact@v2
        with:
          name: built-artifacts
          path: 'packages/**/*.tgz'
          retention-days: 5
      - name: publish @dcl/dcl-rollup package
        uses: menduz/oddish-action@master
        with:
          registry-url: 'https://registry.npmjs.org'
          access: public
          cwd: ./packages/@dcl/dcl-rollup/dist
          ## use action runId instead of current date to generate snapshot numbers
          deterministic-snapshot: true

          ## publish every package to s3
          s3-bucket: ${{ secrets.SDK_TEAM_S3_BUCKET }}
          s3-bucket-key-prefix: '@dcl/js-sdk-toolchain/branch/${{ github.head_ref || github.ref }}/@dcl/dcl-rollup'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          AWS_ACCESS_KEY_ID: ${{ secrets.SDK_TEAM_AWS_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.SDK_TEAM_AWS_SECRET }}
      - name: publish @dcl/ecs package
        uses: menduz/oddish-action@master
        with:
          registry-url: 'https://registry.npmjs.org'
          access: public
          cwd: ./packages/@dcl/ecs
          ## use action runId instead of current date to generate snapshot numbers
          deterministic-snapshot: true

          ## publish every package to s3
          s3-bucket: ${{ secrets.SDK_TEAM_S3_BUCKET }}
          s3-bucket-key-prefix: '@dcl/js-sdk-toolchain/branch/${{ github.head_ref || github.ref }}/@dcl/ecs'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          AWS_ACCESS_KEY_ID: ${{ secrets.SDK_TEAM_AWS_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.SDK_TEAM_AWS_SECRET }}
      - name: publish @dcl/amd package
        id: publish_dcl_amd
        uses: menduz/oddish-action@master
        with:
          registry-url: 'https://registry.npmjs.org'
          access: public
          cwd: ./packages/@dcl/amd
          ## use action runId instead of current date to generate snapshot numbers
          deterministic-snapshot: true

          ## publish every package to s3
          s3-bucket: ${{ secrets.SDK_TEAM_S3_BUCKET }}
          s3-bucket-key-prefix: '@dcl/js-sdk-toolchain/branch/${{ github.head_ref || github.ref }}/@dcl/amd'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          AWS_ACCESS_KEY_ID: ${{ secrets.SDK_TEAM_AWS_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.SDK_TEAM_AWS_SECRET }}
      - name: publish @dcl/js-runtime package
        id: publish_dcl_js_runtime
        uses: menduz/oddish-action@master
        with:
          registry-url: 'https://registry.npmjs.org'
          access: public
          cwd: ./packages/@dcl/js-runtime
          ## use action runId instead of current date to generate snapshot numbers
          deterministic-snapshot: true

          ## publish every package to s3
          s3-bucket: ${{ secrets.SDK_TEAM_S3_BUCKET }}
          s3-bucket-key-prefix: '@dcl/js-sdk-toolchain/branch/${{ github.head_ref || github.ref }}/@dcl/js-runtime'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          AWS_ACCESS_KEY_ID: ${{ secrets.SDK_TEAM_AWS_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.SDK_TEAM_AWS_SECRET }}
      - name: publish @dcl/build-ecs package
        uses: menduz/oddish-action@master
        id: publish_dcl_build_ecs
        with:
          registry-url: 'https://registry.npmjs.org'
          access: public
          cwd: ./packages/@dcl/build-ecs/dist
          ## use action runId instead of current date to generate snapshot numbers
          deterministic-snapshot: true

          ## publish every package to s3
          s3-bucket: ${{ secrets.SDK_TEAM_S3_BUCKET }}
          s3-bucket-key-prefix: '@dcl/js-sdk-toolchain/branch/${{ github.head_ref || github.ref }}/@dcl/build-ecs'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          AWS_ACCESS_KEY_ID: ${{ secrets.SDK_TEAM_AWS_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.SDK_TEAM_AWS_SECRET }}
      - name: (PR) change @dcl/sdk version to s3 url
        working-directory: ./packages/@dcl/sdk
        if: ${{ github.event.pull_request.number }}
        run: node -e "let pkg=require('./package.json'); pkg.dependencies['@dcl/build-ecs']='$BUILD_ECS_S3_URL'; pkg.dependencies['@dcl/amd']='$AMD_S3_URL'; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));"
        env:
          AMD_S3_URL: ${{ secrets.SDK_TEAM_S3_BASE_URL }}/${{ steps.publish_dcl_amd.outputs.s3-bucket-key }}
          BUILD_ECS_S3_URL: ${{ secrets.SDK_TEAM_S3_BASE_URL }}/${{ steps.publish_dcl_build_ecs.outputs.s3-bucket-key }}
      - name: publish @dcl/sdk package
        uses: menduz/oddish-action@master
        id: publish_decentraland_ecs
        with:
          registry-url: 'https://registry.npmjs.org'
          access: public
          cwd: ./packages/@dcl/sdk
          ## use action runId instead of current date to generate snapshot numbers
          deterministic-snapshot: true

          ## publish every package to s3
          s3-bucket: ${{ secrets.SDK_TEAM_S3_BUCKET }}
          s3-bucket-key-prefix: '@dcl/js-sdk-toolchain/branch/${{ github.head_ref || github.ref }}'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          AWS_ACCESS_KEY_ID: ${{ secrets.SDK_TEAM_AWS_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.SDK_TEAM_AWS_SECRET }}

  notify_deployment:
    needs: [build]
    if: ${{ github.event.pull_request.number }}
    runs-on: ubuntu-latest
    name: Deployment Notification
    steps:
      - name: Find Comment
        uses: peter-evans/find-comment@v1
        id: fc
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: 'github-actions[bot]'
          body-includes: Test this pull request
      - name: Generate S3 URL
        id: url-generator
        run: |
          body=$(echo ${{ secrets.SDK_TEAM_S3_BASE_URL }})/${{ needs.build.outputs.decentraland_ecs_s3_bucket_key }}
          echo ::set-output name=body::$body
      - name: Test Install S3 URL
        run: mkdir -p test_s3 && cd test_s3 && npm init --yes && npm install "${{ steps.url-generator.outputs.body }}"
      - name: Create or update comment
        uses: peter-evans/create-or-update-comment@v1
        with:
          comment-id: ${{ steps.fc.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            # Test this pull request
            - The `@dcl/sdk` package can be tested in scenes by running
               ```bash
               npm install "${{ steps.url-generator.outputs.body }}"
               ```
          edit-mode: replace
