# SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
#
# SPDX-License-Identifier: AGPL-3.0-only

name: Frontend / Deploy PR to Netlify

on:
  pull_request:
    branches: [ develop ]
    types:
      - labeled
      - opened
      - synchronize
      - reopened
      - ready_for_review
  pull_request_target:
    branches: [ develop ]
    types:
      - labeled
      - opened
      - synchronize
      - reopened
      - ready_for_review

permissions:
  contents: read
  deployments: write

env:
  NETLIFY_VERSION: 13.2.2
  NETLIFY_NEXTJS_PLUGIN_VERSION: 4.33.0
  NODEJS_VERSION: 20

defaults:
  run:
    working-directory: frontend

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.head.label }}-${{ github.event_name }}-${{ github.job }}
  cancel-in-progress: true

jobs:
  deploy:
    if: "contains(github.event.pull_request.labels.*.name, 'ci: force deployment') && ((github.event_name == 'pull_request_target') == github.event.pull_request.head.repo.fork)"
    runs-on: ubuntu-latest
    name: Deploys to netlify
    env:
      DEPLOY_URL: "https://${{ github.event.number }}--hedgedoc-ui-test.netlify.app/"
    steps:
      - name: Checkout repository
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: Setup node
        uses: ./.github/actions/setup-node
        with:
          NODEJS_VERSION: ${{ env.NODEJS_VERSION }}

      - name: Patch intro.md to include netlify banner.
        run: cp netlify/intro.md public/public/intro.md

      - name: Patch motd.md to include privacy policy.
        run: cp netlify/motd.md public/public/motd.md

      - name: Patch base URL
        run: echo "HD_BASE_URL=\"${{ env.DEPLOY_URL }}\"" >> .env.production

      - name: Build app
        run: yarn build --filter=frontend
        shell: bash
        working-directory: .
        env:
          NEXT_PUBLIC_USE_MOCK_API: true
          TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
          TURBO_API: ${{ vars.TURBO_API }}
          TURBO_TEAM: ${{ vars.TURBO_TEAM }}

      - name: Remove Next.js cache to avoid it being deployed
        run: rm -r .next/cache

      - name: Mark GitHub deployment as started
        uses: bobheadxi/deployments@88ce5600046c82542f8246ac287d0a53c461bca3 # v1.4.0
        id: github-deployment
        with:
          step: start
          token: ${{ secrets.GITHUB_TOKEN }}
          env: "PR #${{ github.event.number }}"
          override: true
          ref: ${{ github.head_ref }}

      - name: Install netlify CLI
        run: "yarn add --dev netlify-cli@${{ env.NETLIFY_VERSION }} @netlify/plugin-nextjs@${{ env.NETLIFY_NEXTJS_PLUGIN_VERSION }}"

      - name: Run netlify CLI
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
        run: "netlify deploy --build --context deploy-preview --alias \"${{ github.event.number }}\" --json --message \"PR #${{ github.event.number }}\""

      - name: Mark GitHub deployment as finished
        uses: bobheadxi/deployments@88ce5600046c82542f8246ac287d0a53c461bca3 # v1.4.0
        with:
          step: finish
          token: ${{ secrets.GITHUB_TOKEN }}
          env_url: ${{ env.DEPLOY_URL }}
          env: ${{ steps.github-deployment.outputs.env }}
          status: ${{ job.status }}
          deployment_id: ${{ steps.github-deployment.outputs.deployment_id }}
          override: true
