name: Publish Unit Test Results

on:
  workflow_run:
    workflows: ["Build Equinox"]
    types:
      - completed

permissions: {}

jobs:
  unit-test-results:
    name: Unit Test Results
    runs-on: ubuntu-latest
    if: github.event.workflow_run.conclusion != 'skipped'
    permissions:
      checks: write
      pull-requests: write
      actions: read
    steps:
      - name: Download and Extract Unit Test Results
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        run: |
           mkdir -p artifacts && cd artifacts

           artifacts_url=${{ github.event.workflow_run.artifacts_url }}

           gh api "$artifacts_url" -q '.artifacts[] | [.name, .archive_download_url] | @tsv' | while read artifact
           do
             IFS=$'\t' read name url <<< "$artifact"
             gh api $url > "$name.zip"
             unzip -d "$name" "$name.zip"
           done

      - name: Publish Unit Test Results
        uses: EnricoMi/publish-unit-test-result-action@v2
        with:
          commit:      ${{ github.event.workflow_run.head_sha }}
          junit_files: "artifacts/**/*-reports/*.xml"
          event_name:  ${{ github.event.workflow_run.event }}
          event_file:  artifacts/Event File/event.json
  tck-results:
    name: TCK Test Results for ${{ matrix.tck.name }}
    runs-on: ubuntu-latest
    if: github.event.workflow_run.conclusion != 'skipped'
    permissions:
      checks: write
      pull-requests: write
      actions: read
    strategy:
      fail-fast: false
      matrix:
        tck: 
          - { name: framework,   chapter:  '10', label: 'Framework',           suffix: ' API',                   pattern: "TEST-org.osgi.test.cases.framework-*.xml" }
          - { name: url,         chapter:  '52', label: 'URL Handlers',        suffix: ' Service Specification', pattern: "TEST-org.osgi.test.cases.url-*.xml" }
          - { name: resolver,    chapter:  '59', label: 'Resolver',            suffix: ' Service Specification', pattern: "TEST-org.osgi.test.cases.resolver-*.xml" }
          - { name: log,         chapter: '101', label: 'Log',                 suffix: ' Service Specification', pattern: "TEST-org.osgi.test.cases.log-*.xml" }
          - { name: device,      chapter: '103', label: 'Device Access',       suffix: ' Specification',         pattern: "TEST-org.osgi.test.cases.device-*.xml" }
          - { name: cm,          chapter: '104', label: 'Configuration Admin', suffix: ' Service Specification', pattern: "TEST-org.osgi.test.cases.cm-*.xml"}
          - { name: metatype,    chapter: '105', label: 'Metatype',            suffix: ' Service Specification', pattern: "TEST-org.osgi.test.cases.metatype-*.xml" }
          - { name: preferences, chapter: '106', label: 'Preferences',         suffix: 'Service Specification',  pattern: "TEST-org.osgi.test.cases.prefs-*.xml" }
          - { name: useradmin,   chapter: '107', label: 'User Admin',          suffix: ' Service Specification', pattern: "TEST-org.osgi.test.cases.useradmin-*.xml" }
          - { name: event,       chapter: '113', label: 'Event Admin',         suffix: ' Service Specification', pattern: "TEST-org.osgi.test.cases.event-*.xml" }
          - { name: coordinator, chapter: '130', label: 'Coordinator',         suffix: ' Service Specification', pattern: "TEST-org.osgi.test.cases.coordinator-*.xml" }
          - { name: tracker,     chapter: '701', label: 'Tracker',             suffix: ' Specification',         pattern: "TEST-org.osgi.test.cases.tracker-*.xml" }
          
    steps:  
      - name: Download and Extract Unit Test Results
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        run: |
           mkdir -p artifacts && cd artifacts

           artifacts_url=${{ github.event.workflow_run.artifacts_url }}

           gh api "$artifacts_url" -q '.artifacts[] | [.name, .archive_download_url] | @tsv' | while read artifact
           do
             IFS=$'\t' read name url <<< "$artifact"
             gh api $url > "$name.zip"
             unzip -d "$name" "$name.zip"
           done

      - name: Parse TCK Results of ${{ matrix.tck.name }}
        uses: EnricoMi/publish-unit-test-result-action@v2
        id: test-results
        if: always()
        with:
          commit:       ${{ github.event.workflow_run.head_sha }}
          junit_files:  artifacts/**/tck-results/${{ matrix.tck.pattern }}
          comment_mode: off
          check_name:   ${{ matrix.tck.label }} TCK
          event_name:   ${{ github.event.workflow_run.event }}
          event_file:   artifacts/Event File/event.json
      - name: Set badge color of ${{ matrix.tck.name }}
        shell: bash
        run: |
           case ${{ fromJSON( steps.test-results.outputs.json ).conclusion }} in
           success)
           echo "BADGE_COLOR=31c653" >> $GITHUB_ENV
           ;;
           failure)
           echo "BADGE_COLOR=800000" >> $GITHUB_ENV
           ;;
           neutral)
           echo "BADGE_COLOR=696969" >> $GITHUB_ENV
           ;;
           esac
      - name: Create badge of ${{ matrix.tck.name }}
        uses: emibcn/badge-action@4209421db54c8764d8932070ffd0f81715a629bf
        with:
          # label: ${{ matrix.tck.chapter }} - ${{ matrix.tck.label }} ${{ matrix.tck.suffix }}
          label:  '${{ fromJSON( steps.test-results.outputs.json ).formatted.stats.tests }} tests'
          status: '${{ fromJSON( steps.test-results.outputs.json ).formatted.stats.tests_succ }} passed, ${{ fromJSON( steps.test-results.outputs.json ).formatted.stats.tests_fail }} failed'
          color: ${{ env.BADGE_COLOR }}
          path: tck-badge-${{ matrix.tck.name }}.svg
      - name: Upload badge of ${{ matrix.tck.name }} to Gist
        if: github.ref == 'refs/heads/master'
        uses: andymckay/append-gist-action@6e8d64427fe47cbacf4ab6b890411f1d67c07f3e
        with:
          token: ${{ secrets.GIST_TOKEN }}
          gistURL: https://gist.github.com/eclipse-equinox-bot/d941fe2a4992a018d88e778b48ee3135
          file: tck-badge-${{ matrix.tck.name }}.svg
          
