name: Deploy Blockstack Apps
on: [pull_request]

jobs:
  blockstack-app-deployments:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set Node Version
        uses: actions/setup-node@v1.4.2
        with:
          node-version: 12.16.1
      - name: Restore lerna cache
        id: lerna-cache
        uses: actions/cache@v2
        with:
          path: |
            node_modules
            */*/node_modules
          key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
      - name: Install monorepo deps
        run: yarn --frozen-lockfile
        if: steps.lerna-cache.outputs.cache-hit != 'true'
      - name: Build Blockstack App
        run: yarn prod:web
        working-directory: packages/app
      - name: Copy Blockstack App vercel.json
        uses: canastro/copy-file-action@master
        with:
          source: 'packages/app/vercel.json'
          target: 'packages/app/dist/vercel.json'
      - name: Deploy Blockstack App with Vercel
        uses: aulneau/vercel-action@v19.0.2+3
        id: vercel-deployment-blockstack-app
        if: github.event_name == 'pull_request'
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.ORG_ID }}
          vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID_APP }}
          scope: ${{ secrets.VERCEL_SCOPE }}
          working-directory: packages/app/dist
          github-comment: false
          alias-domains: pr-{{ PR_NUMBER }}.app.stacks.engineering
      - name: Build Blockstack Test App
        env:
          AUTH_ORIGIN: ${{ steps.vercel-deployment-blockstack-app.outputs.preview-url }}
        run: yarn prod:web
        working-directory: packages/test-app
      - name: Copy Blockstack Test App vercel.json
        uses: canastro/copy-file-action@master
        with:
          source: 'packages/test-app/vercel.json'
          target: 'packages/test-app/dist/vercel.json'
      - name: Deploy Blockstack Test App with Vercel
        uses: aulneau/vercel-action@v19.0.2+3
        id: vercel-deployment-blockstack-test-app
        if: github.event_name == 'pull_request'
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.ORG_ID }}
          vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID_TEST_APP }}
          scope: ${{ secrets.VERCEL_SCOPE }}
          working-directory: packages/test-app/dist
          github-comment: false
          alias-domains: pr-{{ PR_NUMBER }}.testnet-demo.stacks.engineering
      - name: Comment on PR
        uses: actions/github-script@v2
        id: comment
        if: github.event_name == 'pull_request'
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const firstLine = `Deploy previews for _${{ steps.vercel-deployment-blockstack-app.outputs.preview-name }}_ and _${{ steps.vercel-deployment-blockstack-test-app.outputs.preview-name }}_ are ready!`;
            const { data } = await github.issues.listComments({
              ...context.repo,
              issue_number: context.issue.number,
            });
            const vercelPreviewURLComment = data.find((comment) =>
              comment.body.includes(firstLine)
            );
            const commentId = vercelPreviewURLComment && vercelPreviewURLComment.id || undefined;
            const commentBody = `
            #### Deploy previews
            ${firstLine}

            - [Blockstack App](${{ steps.vercel-deployment-blockstack-app.outputs.preview-url }})
            - [Blockstack Testnet Demo App](${{ steps.vercel-deployment-blockstack-test-app.outputs.preview-url }})

            Built with commit ${context.sha}.
            `;
            if(context.issue.number){
              if (commentId) {
                await github.issues.updateComment({
                  ...context.repo,
                  comment_id: commentId,
                  body: commentBody,
                });
              } else {
                await github.issues.createComment({
                  ...context.repo,
                  issue_number: context.issue.number,
                  body: commentBody,
                });
              }
            }
