name: Deploy UI Docs
on:
  pull_request:
    paths:
      - packages/ui-docs/**
      - packages/ui/**

jobs:
  vercel-deployment:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: aulneau/vercel-action@v19.0.2+3
        id: vercel-deployment-preview
        if: github.event_name == 'pull_request'
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.ORG_ID }}
          vercel-project-id: ${{ secrets.PROJECT_ID }}
          scope: ${{ secrets.VERCEL_SCOPE }}
          alias-domains: pr-{{ PR_NUMBER }}.ui.stacks.engineering
          working-directory: packages/ui-docs
      - 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 = `The Blockstack UI docs have been deployed with Vercel using the code from this PR!`;
            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 = `
            #### Blockstack UI Documentation
            ${firstLine}

            - [Blockstack UI Docs](${{ steps.vercel-deployment-preview.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,
                });
              }
            }
