# for each folder in anda/
# generate a new workflow for each folder in anda/
name: Automatically build packages
permissions:
  contents: read
  attestations: write
  artifact-metadata: write
  id-token: write
on:
  push:
    paths:
      - anda/**
    branches:
      - frawhide
  pull_request:
    branches:
      - frawhide
  merge_group:
    branches:
      - frawhide
  workflow_dispatch:
  workflow_call:

jobs:
  manifest:
    runs-on: ubuntu-24.04-arm
    outputs:
      build_matrix: ${{ steps.generate_build_matrix.outputs.build_matrix }}
    container:
      image: ghcr.io/terrapkg/builder:frawhide
      options: --cap-add=SYS_ADMIN --privileged
    steps:
      - name: Set workspace as safe
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
      - name: Generate build matrix
        id: generate_build_matrix
        run: anda ci >> $GITHUB_OUTPUT
  build:
    needs: manifest
    uses: ./.github/workflows/json-build.yml
    secrets: inherit
    with:
      packages: ${{ needs.manifest.outputs.build_matrix }}
      publish: ${{ github.event_name == 'push' }}

  appstream:
    needs: build
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/terrapkg/appstream-generator:main
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          merge-multiple: true
          path: ./artifacts
      - name: Generate test catalog
        id: catalog
        # run appstream-builder, then add step summary
        run: |
          set -x
          appstream-builder -v \
            --packages-dir=artifacts/rpms \
            --icons-dir=icons \
            --include-failed \
            --output-dir=output \
            --uncompressed-icons \
            --origin=test \
            --basename=test \
            --veto-ignore=missing-parents \
            --veto-ignore=missing-info 2>&1 | tee asb.log

      - name: Run appstreamcli validate
        if: steps.catalog.outcome == 'success'
        run: |
          if stat output/test.xml.gz &>/dev/null; then
            echo "## AppStream MetaInfo Validation" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo '```xml' >> $GITHUB_STEP_SUMMARY
            appstreamcli validate output/test.xml.gz >> $GITHUB_STEP_SUMMARY | true
            echo "" >> $GITHUB_STEP_SUMMARY
            echo '```' >> $GITHUB_STEP_SUMMARY
          else
            echo "Nothing to do."
          fi
          
          
      - name: Export logs
        id: export_logs
        if: steps.catalog.outcome == 'success'
        run: |
          if stat output/*.xml.gz &>/dev/null; then
            echo "## AppStream Builder Log" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo '```log' >> $GITHUB_STEP_SUMMARY
            cat asb.log >> $GITHUB_STEP_SUMMARY
            echo '```' >> $GITHUB_STEP_SUMMARY
            echo '---' >> $GITHUB_STEP_SUMMARY
          else
            echo "Nothing to do."
          fi

      - name: Report Summary
        id: report_summary
        if: steps.export_logs.outcome == 'success'
        run: |
          echo "## AppStream Builder Report" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          if stat output/*.xml.gz &>/dev/null; then
            if grep -q "veto" asb.log; then
              echo "::group::Vetoed packages"
              echo "### Vetoed packages" >> $GITHUB_STEP_SUMMARY
              echo "" >> $GITHUB_STEP_SUMMARY
              echo '```xml' >> $GITHUB_STEP_SUMMARY
              echo "$(grep -i 'veto' asb.log)" >> $GITHUB_STEP_SUMMARY
              echo '```' >> $GITHUB_STEP_SUMMARY
              echo "::warning file=asb.log::Some packages were vetoed during AppStream generation. Please review the 'Vetoed packages' section in the summary for details."
              echo "::endgroup::"
            fi
            echo "## Full Data Summary" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "### Generated Appstream files:" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            for file in output/*.xml.gz; do
              echo "#### \`$file\`" >> $GITHUB_STEP_SUMMARY
              echo "" >> $GITHUB_STEP_SUMMARY
              echo '```xml' >> $GITHUB_STEP_SUMMARY
              zcat "$file" >> $GITHUB_STEP_SUMMARY || true
              echo '```' >> $GITHUB_STEP_SUMMARY
              echo "" >> $GITHUB_STEP_SUMMARY
            done
          else
            echo "No appstream files found." >> $GITHUB_STEP_SUMMARY
          fi
