name: Review app pipeline

concurrency:
  group: review-${{ github.event.pull_request.number }}

on:
  pull_request:
    types:
      - opened
      - synchronize
      - reopened
  workflow_dispatch:

defaults:
  run:
    shell: bash

jobs:
  infra:
    name: Deploy review app infrastructure
    uses: communitiesuk/submit-social-housing-lettings-and-sales-data-infrastructure/.github/workflows/create_review_app_infra.yml@main
    with:
      key: ${{ github.event.pull_request.number }}
      app_repo_role: arn:aws:iam::815624722760:role/core-application-repo
    permissions:
      id-token: write

  code:
    name: Deploy review app code
    needs: [infra]
    uses: ./.github/workflows/aws_deploy.yml
    with:
      aws_account_id: 837698168072
      aws_role_prefix: core-dev
      aws_task_prefix: core-review-${{ github.event.pull_request.number }}
      concurrency_tag: ${{ github.event.pull_request.number }}
      environment: review
    permissions:
      id-token: write

  comment:
    name: Add link to PR
    needs: [code]
    runs-on: ubuntu-latest
    permissions:
      issues: write
      pull-requests: write

    steps:
      - name: Comment on PR with URL
        uses: unsplash/comment-on-pr@v1.3.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          msg: "Created review app at https://review.submit-social-housing-data.levellingup.gov.uk/${{ github.event.pull_request.number }}"
          check_for_duplicate_msg: true
          duplicate_msg_pattern: Created review app at*