name: Review app pipeline

concurrency: ${{ github.workflow }}-${{ github.event.pull_request.number }}

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

defaults:
  run:
    shell: bash

jobs:
  postgres:
    name: Provision postgres
    runs-on: ubuntu-latest
    environment: review

    steps:
      - name: Install Cloud Foundry CLI
        run: |
          wget --user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15" -q -O - https://packages.cloudfoundry.org/debian/cli.cloudfoundry.org.key | sudo apt-key add -
          echo "deb https://packages.cloudfoundry.org/debian stable main" | sudo tee /etc/apt/sources.list.d/cloudfoundry-cli.list
          sudo apt-get update
          sudo apt-get install cf8-cli

      - name: Provision postgres
        env:
          CF_USERNAME: ${{ secrets.CF_USERNAME }}
          CF_PASSWORD: ${{ secrets.CF_PASSWORD }}
          CF_API_ENDPOINT: ${{ secrets.CF_API_ENDPOINT }}
          CF_SPACE: dev
          CF_ORG: ${{ secrets.CF_ORG }}
        run: |
          cf api $CF_API_ENDPOINT
          cf auth
          cf target -o $CF_ORG -s $CF_SPACE
          cf create-service postgres tiny-unencrypted-13 dluhc-core-review-${{ github.event.pull_request.number }}-postgres --wait

  redis:
    name: Provision redis
    runs-on: ubuntu-latest
    environment: review

    steps:
      - name: Install Cloud Foundry CLI
        run: |
          wget --user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15" -q -O - https://packages.cloudfoundry.org/debian/cli.cloudfoundry.org.key | sudo apt-key add -
          echo "deb https://packages.cloudfoundry.org/debian stable main" | sudo tee /etc/apt/sources.list.d/cloudfoundry-cli.list
          sudo apt-get update
          sudo apt-get install cf8-cli

      - name: Provision redis
        env:
          CF_USERNAME: ${{ secrets.CF_USERNAME }}
          CF_PASSWORD: ${{ secrets.CF_PASSWORD }}
          CF_API_ENDPOINT: ${{ secrets.CF_API_ENDPOINT }}
          CF_SPACE: dev
          CF_ORG: ${{ secrets.CF_ORG }}
        run: |
          cf api $CF_API_ENDPOINT
          cf auth
          cf target -o $CF_ORG -s $CF_SPACE
          cf create-service redis micro-6.x dluhc-core-review-${{ github.event.pull_request.number }}-redis --wait

  deploy:
    name: Deploy review app
    runs-on: ubuntu-latest
    environment: review
    needs: [postgres, redis]
    permissions:
      issues: write
      pull-requests: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Install Cloud Foundry CLI
        run: |
          wget --user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15" -q -O - https://packages.cloudfoundry.org/debian/cli.cloudfoundry.org.key | sudo apt-key add -
          echo "deb https://packages.cloudfoundry.org/debian stable main" | sudo tee /etc/apt/sources.list.d/cloudfoundry-cli.list
          sudo apt-get update
          sudo apt-get install cf8-cli

      - name: Setup review app without starting
        env:
          CF_USERNAME: ${{ secrets.CF_USERNAME }}
          CF_PASSWORD: ${{ secrets.CF_PASSWORD }}
          CF_API_ENDPOINT: ${{ secrets.CF_API_ENDPOINT }}
          CF_SPACE: dev
          CF_ORG: ${{ secrets.CF_ORG }}
          APP_NAME: dluhc-core-review-${{ github.event.pull_request.number }}
        run: |
          cf api $CF_API_ENDPOINT
          cf auth
          cf target -o $CF_ORG -s $CF_SPACE
          cf push $APP_NAME \
            --manifest ./config/cloud_foundry/review_manifest.yml \
            --no-start

      - name: Set environment variables
        env:
          APP_NAME: dluhc-core-review-${{ github.event.pull_request.number }}
          API_USER: ${{ secrets.API_USER }}
          API_KEY: ${{ secrets.API_KEY }}
          GOVUK_NOTIFY_API_KEY: ${{ secrets.GOVUK_NOTIFY_API_KEY }}
          RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
          OS_DATA_KEY: ${{ secrets.OS_DATA_KEY }}
          IMPORT_PAAS_INSTANCE: ${{ secrets.IMPORT_PAAS_INSTANCE }}
          EXPORT_PAAS_INSTANCE: ${{ secrets.EXPORT_PAAS_INSTANCE }}
          S3_CONFIG: ${{ secrets.S3_CONFIG }}
          CSV_DOWNLOAD_PAAS_INSTANCE: ${{ secrets.CSV_DOWNLOAD_PAAS_INSTANCE }}
          SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
        run: |
          cf set-env $APP_NAME API_USER $API_USER
          cf set-env $APP_NAME API_KEY $API_KEY
          cf set-env $APP_NAME GOVUK_NOTIFY_API_KEY $GOVUK_NOTIFY_API_KEY
          cf set-env $APP_NAME RAILS_MASTER_KEY $RAILS_MASTER_KEY
          cf set-env $APP_NAME OS_DATA_KEY $OS_DATA_KEY
          cf set-env $APP_NAME IMPORT_PAAS_INSTANCE $IMPORT_PAAS_INSTANCE
          cf set-env $APP_NAME EXPORT_PAAS_INSTANCE "dluhc-core-review-export-bucket"
          cf set-env $APP_NAME S3_CONFIG $S3_CONFIG
          cf set-env $APP_NAME CSV_DOWNLOAD_PAAS_INSTANCE "dluhc-core-review-csv-bucket"
          cf set-env $APP_NAME SENTRY_DSN $SENTRY_DSN
          cf set-env $APP_NAME APP_HOST "https://dluhc-core-review-${{ github.event.pull_request.number }}.london.cloudapps.digital"

      - name: Bind postgres service
        env:
          APP_NAME: dluhc-core-review-${{ github.event.pull_request.number }}
          SERVICE_NAME: dluhc-core-review-${{ github.event.pull_request.number }}-postgres
        run: |
          cf bind-service $APP_NAME $SERVICE_NAME --wait

      - name: Bind redis service
        env:
          APP_NAME: dluhc-core-review-${{ github.event.pull_request.number }}
          SERVICE_NAME: dluhc-core-review-${{ github.event.pull_request.number }}-redis
        run: |
          cf bind-service $APP_NAME $SERVICE_NAME --wait

      - name: Bind logit drain service
        env:
          APP_NAME: dluhc-core-review-${{ github.event.pull_request.number }}
          SERVICE_NAME: logit-ssl-drain
        run: |
          cf bind-service $APP_NAME $SERVICE_NAME --wait

      - name: Bind S3 buckets services
        env:
          APP_NAME: dluhc-core-review-${{ github.event.pull_request.number }}
        run: |
          cf bind-service $APP_NAME dluhc-core-review-csv-bucket --wait
          cf bind-service $APP_NAME dluhc-core-review-export-bucket --wait
          cf bind-service $APP_NAME dluhc-core-review-import-bucket --wait

      - name: Start review app
        env:
          APP_NAME: dluhc-core-review-${{ github.event.pull_request.number }}
        run: |
          cf restage $APP_NAME

      - 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://dluhc-core-review-${{ github.event.pull_request.number }}.london.cloudapps.digital"
          check_for_duplicate_msg: true
          duplicate_msg_pattern: Created review app at*