name: "Main Build Process"

# Runs on main branch commits,
# every commit in a pull request, any published release.
on:
  pull_request:
    branches: ["main"]

env:
  REGISTRY: gitea.borealian.ovh
  IMAGE_NAME: ${{ github.repository }}
jobs:
  build:
    name: "Build & Test"

    # Permissions this GitHub Action needs for other things in GitHub
    permissions: write-all

    runs-on: ubuntu-latest

    steps:
      - name: Check out the code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - name: Setup .NET
        uses: actions/setup-dotnet@v5
        with:
          dotnet-version: |
            10.x
      - name: "Restore/Build/Test"
        run: |
          dotnet test ./src/LittleTown.sln --configuration Release --verbosity normal --logger trx --collect:"XPlat Code Coverage" --logger:"trx;LogFileName=test_results.xml"
      - name: Combine Coverage Reports # This is because one report is produced per project, and we want one result for all of them.
        uses: danielpalme/ReportGenerator-GitHub-Action@v5.5.1
        with:
          reports: "**/*.cobertura.xml" # REQUIRED # The coverage reports that should be parsed (separated by semicolon). Globbing is supported.
          targetdir: "${{ github.workspace }}/coverage-report" # REQUIRED # The directory where the generated report should be saved.
          reporttypes: "Html;MarkdownSummary;Cobertura" # The output formats and scope (separated by semicolon) Values: Badges, Clover, Cobertura, CsvSummary, Html, Html_Dark, Html_Light, Html_BlueRed, HtmlChart, HtmlInline, HtmlInline_AzurePipelines, HtmlInline_AzurePipelines_Dark, HtmlInline_AzurePipelines_Light, HtmlSummary, JsonSummary, Latex, LatexSummary, lcov, MarkdownSummary, MarkdownSummaryGithub, MarkdownDeltaSummary, MHtml, PngChart, SonarQube, TeamCitySummary, TextSummary, TextDeltaSummary, Xml, XmlSummary
          verbosity: "Info" # The verbosity level of the log messages. Values: Verbose, Info, Warning, Error, Off
          title: "Code Coverage" # Optional title.
          tag: "${{ github.run_number }}_${{ github.run_id }}" # Optional tag or build version.
          customSettings: "" # Optional custom settings (separated by semicolon). See: https://github.com/danielpalme/ReportGenerator/wiki/Settings.
          toolpath: "reportgeneratortool" # Default directory for installing the dotnet tool.
      - name: Upload coverage via SSH
        uses: appleboy/scp-action@v1
        with:
          host: "192.168.1.103"
          username: "gitea"
          password: "password"
          port: 2222
          source: "coverage-report/*"
          target: "/config/docs/workspace/${{ github.repository_owner }}/${{ github.event.repository.name }}/${{ github.event.pull_request.number }}/coverage-report/"
          strip_components: 1
      - name: Publish Code Coverage Report
        uses: irongut/CodeCoverageSummary@v1.3.0
        with:
          filename: "coverage-report/Cobertura.xml"
          badge: true
          fail_below_min: false # just informative for now
          format: markdown
          hide_branch_rate: false
          hide_complexity: false
          indicators: true
          output: both
          thresholds: "10 30"
      - name: Comment on Gitea PR
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          GITEA_URL: https://gitea.borealian.ovh
          REPO_OWNER: mcmuzzle
          REPO_NAME: LittleTown
          PR_NUMBER: ${{ github.event.pull_request.number }}

        run: |
          COMMENT_CONTENT=$(cat code-coverage-results.md)
          COMMENT_CONTENT="${COMMENT_CONTENT}\n\n📊 [Rapport HTML complet](https://docs.borealian.ovh/workspace/${{ github.repository_owner }}//${{ github.event.repository.name }}/${{ github.event.pull_request.number }}/coverage-report/index.html)"
          curl -X POST \
            -H "Authorization: token ${GITEA_TOKEN}" \
            -H "Content-Type: application/json" \
            -d "{\"body\": \"${COMMENT_CONTENT}\"}" \
            ${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/issues/${PR_NUMBER}/comments

