name: Build

on:
  push:
    branches:
      - v5-develop
      - v5-main
    paths-ignore:
      - "src/Sonarr.Api.*/openapi.json"
  pull_request:
    branches:
      - v5-develop
    paths-ignore:
      - "src/NzbDrone.Core/Localization/Core/**"
      - "src/Sonarr.Api.*/openapi.json"

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

env:
  FRAMEWORK: net10.0
  RAW_BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
  SONARR_MAJOR_VERSION: 5
  VERSION: 5.0.0

jobs:
  prepare:
    runs-on: ubuntu-latest
    outputs:
      framework: ${{ steps.variables.outputs.framework }}
      major_version: ${{ steps.variables.outputs.major_version }}
      version: ${{ steps.variables.outputs.version }}
      branch: ${{ steps.variables.outputs.branch }}
    steps:
      - name: Setup Environment Variables
        id: variables
        shell: bash
        run: |
          echo "framework=${{ env.FRAMEWORK }}" >> "$GITHUB_OUTPUT"
          echo "major_version=${{ env.SONARR_MAJOR_VERSION }}" >> "$GITHUB_OUTPUT"
          echo "version=${{ env.VERSION }}.$((${{ github.run_number }}))" >> "$GITHUB_OUTPUT"
          echo "branch=${RAW_BRANCH_NAME//\//-}" >> "$GITHUB_OUTPUT"

  backend:
    needs: prepare
    strategy:
      fail-fast: false
      matrix:
        include:
          - runtime: freebsd-x64
            package_tests: false
            os: ubuntu-latest
          - runtime: linux-arm
            package_tests: false
            os: ubuntu-latest
          - runtime: linux-arm64
            package_tests: false
            os: ubuntu-latest
          - runtime: linux-musl-arm64
            package_tests: false
            os: ubuntu-latest
          - runtime: linux-musl-x64
            package_tests: false
            os: ubuntu-latest
          - runtime: linux-x64
            package_tests: true
            os: ubuntu-latest
          - runtime: osx-arm64
            package_tests: true
            os: ubuntu-latest
          - runtime: osx-x64
            package_tests: false
            os: ubuntu-latest
          - runtime: win-x64
            package_tests: true
            os: ubuntu-latest
          - runtime: win-x86
            package_tests: false
            os: ubuntu-latest

    runs-on: ${{ matrix.os }}
    steps:
      - name: Check out
        uses: actions/checkout@v5

      - name: Build
        uses: ./.github/actions/build
        with:
          branch: ${{ needs.prepare.outputs.branch }}
          version: ${{ needs.prepare.outputs.version }}
          framework: ${{ needs.prepare.outputs.framework }}
          runtime: ${{ matrix.runtime }}
          package_tests: ${{ matrix.package_tests }}

  frontend:
    runs-on: ubuntu-latest
    steps:
      - name: Check out
        uses: actions/checkout@v5

      - name: Volta
        uses: volta-cli/action@v4

      - name: Yarn Install
        run: yarn install

      - name: Lint
        run: yarn lint

      - name: Stylelint
        run: yarn stylelint -f github

      - name: Build
        run: yarn build --env production

      - name: Publish UI Artifact
        uses: actions/upload-artifact@v4
        with:
          name: build_ui
          path: _output/UI/**/*

  unit_test:
    needs: backend
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        include:
          - os: ubuntu-latest
            artifact: tests-linux-x64
            filter: TestCategory!=ManualTest&TestCategory!=WINDOWS&TestCategory!=IntegrationTest&TestCategory!=AutomationTest
          - os: macos-latest
            artifact: tests-osx-arm64
            filter: TestCategory!=ManualTest&TestCategory!=WINDOWS&TestCategory!=IntegrationTest&TestCategory!=AutomationTest
          - os: windows-latest
            artifact: tests-win-x64
            filter: TestCategory!=ManualTest&TestCategory!=LINUX&TestCategory!=IntegrationTest&TestCategory!=AutomationTest
    runs-on: ${{ matrix.os }}
    steps:
      - name: Check out
        uses: actions/checkout@v5

      - name: Test
        uses: ./.github/actions/test
        with:
          os: ${{ matrix.os }}
          artifact: ${{ matrix.artifact }}
          pattern: Sonarr.*.Test.dll
          filter: ${{ matrix.filter }}

  unit_test_postgres:
    needs: backend
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        postgres-version: [16, 17, 18]
    steps:
      - name: Check out
        uses: actions/checkout@v5

      - name: Test
        uses: ./.github/actions/test
        with:
          os: ubuntu-latest
          artifact: tests-linux-x64
          pattern: Sonarr.*.Test.dll
          filter: TestCategory!=ManualTest&TestCategory!=WINDOWS&TestCategory!=IntegrationTest&TestCategory!=AutomationTest
          use_postgres: true
          postgres-version: ${{ matrix.postgres-version }}

  integration_test:
    needs: [prepare, backend]
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        include:
          - os: ubuntu-latest
            artifact: tests-linux-x64
            filter: TestCategory!=ManualTest&TestCategory!=WINDOWS&TestCategory=IntegrationTest
            binary_artifact: build-linux-x64
            binary_path: linux-x64/${{ needs.prepare.outputs.framework }}/Sonarr
          - os: macos-latest
            artifact: tests-osx-arm64
            filter: TestCategory!=ManualTest&TestCategory!=WINDOWS&TestCategory=IntegrationTest
            binary_artifact: build-osx-arm64
            binary_path: osx-arm64/${{ needs.prepare.outputs.framework }}/Sonarr
          - os: windows-latest
            artifact: tests-win-x64
            filter: TestCategory!=ManualTest&TestCategory!=LINUX&TestCategory=IntegrationTest
            binary_artifact: build-win-x64
            binary_path: win-x64/${{ needs.prepare.outputs.framework }}/Sonarr
    runs-on: ${{ matrix.os }}
    steps:
      - name: Check out
        uses: actions/checkout@v5

      - name: Test
        uses: ./.github/actions/test
        with:
          os: ${{ matrix.os }}
          artifact: ${{ matrix.artifact }}
          pattern: Sonarr.*.Test.dll
          filter: ${{ matrix.filter }}
          integration_tests: true
          binary_artifact: ${{ matrix.binary_artifact }}
          binary_path: ${{ matrix.binary_path }}

  deploy:
    if: ${{ github.ref_name == 'v5-develop-final' || github.ref_name == 'v5-main-final' }}
    needs:
      [
        prepare,
        backend,
        frontend,
        unit_test,
        unit_test_postgres,
        integration_test,
      ]
    secrets: inherit
    uses: ./.github/workflows/deploy.yml
    with:
      framework: ${{ needs.prepare.outputs.framework }}
      branch: ${{ github.ref_name }}
      major_version: ${{ needs.prepare.outputs.major_version }}
      version: ${{ needs.prepare.outputs.version }}

  notify:
    name: Discord Notification
    needs:
      [
        prepare,
        backend,
        frontend,
        unit_test,
        unit_test_postgres,
        integration_test,
        deploy,
      ]
    if: ${{ !cancelled() && (github.ref_name == 'v5-develop-final' || github.ref_name == 'v5-main-final') }}
    env:
      STATUS: ${{ contains(needs.*.result, 'failure') && 'failure' || 'success' }}
    runs-on: ubuntu-latest

    steps:
      - name: Notify
        uses: tsickert/discord-webhook@v6.0.0
        with:
          webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }}
          username: "GitHub Actions"
          avatar-url: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
          embed-title: "${{ github.workflow }}: ${{ env.STATUS == 'success' && 'Success' || 'Failure' }}"
          embed-url: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
          embed-description: |
            **Branch** ${{ github.ref }}
            **Build** ${{ needs.prepare.outputs.version }}
          embed-color: ${{ env.STATUS == 'success' && '3066993' || '15158332' }}
