# Runs the entire testsuite
name: QA
on:
  schedule:
    - cron:  '0 17 * * *'
  workflow_dispatch:
    inputs:
      pcp_qa_args:
        description: '(optional) overwrite ./check args (e.g. "-g pmproxy -g pmseries")'

jobs:
  qa:
    name: ${{ matrix.platform }}
    if: github.repository == 'performancecopilot/pcp' || github.event_name == 'workflow_dispatch'
    runs-on: ${{ matrix.os }}
    continue-on-error: ${{ matrix.experimental }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - {platform: debian10-container,        os: ubuntu-20.04, experimental: false}
          - {platform: debian11-container,        os: ubuntu-20.04, experimental: false}
          - {platform: ubuntu1604-container,      os: ubuntu-20.04, experimental: false}
          - {platform: ubuntu1804-container,      os: ubuntu-20.04, experimental: false}
          - {platform: ubuntu1804-i386-container, os: ubuntu-20.04, experimental: false}
          - {platform: ubuntu2004-container,      os: ubuntu-20.04, experimental: false}
          - {platform: ubuntu2004-btf,            os: ubuntu-20.04, experimental: false}
          - {platform: fedora33-container,        os: ubuntu-20.04, experimental: false}
          - {platform: fedora34-container,        os: ubuntu-20.04, experimental: false}
          - {platform: fedora35-container,        os: ubuntu-20.04, experimental: true }
          - {platform: fedora-rawhide-container,  os: ubuntu-20.04, experimental: true }
          - {platform: centos6-container,         os: ubuntu-20.04, experimental: false}
          - {platform: centos7-container,         os: ubuntu-20.04, experimental: false}
          - {platform: centos8-container,         os: ubuntu-20.04, experimental: false}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Fix local hostname DNS lookup
        run: echo 127.0.1.1 $(hostname --fqdn) $(hostname) | sudo tee -a /etc/hosts

      - name: Update version information
        run: |
          . VERSION.pcp
          PACKAGE_BUILD="0.$(date +'%Y%m%d').$(git rev-parse --short HEAD)"
          PCP_VERSION=${PACKAGE_MAJOR}.${PACKAGE_MINOR}.${PACKAGE_REVISION}
          PCP_BUILD_VERSION=${PCP_VERSION}-${PACKAGE_BUILD}
          sed -i "s/PACKAGE_BUILD=.*/PACKAGE_BUILD=${PACKAGE_BUILD}/" VERSION.pcp
          sed -i "1 s/(.*)/(${PCP_BUILD_VERSION})/" debian/changelog

      - name: Setup
        run: |
          python3 -c 'import yaml' || pip3 install pyyaml
          mkdir -p artifacts/build artifacts/test

          build/ci/ci-run.py ${{ matrix.platform }} setup

      - name: Build
        run: build/ci/ci-run.py ${{ matrix.platform }} task build

      - name: Copy build artifacts
        run: build/ci/ci-run.py ${{ matrix.platform }} artifacts build --path artifacts/build

      - name: Publish build artifacts
        uses: actions/upload-artifact@v2
        with:
          name: build-${{ matrix.platform }}
          path: artifacts/build

      - name: Install
        run: build/ci/ci-run.py ${{ matrix.platform }} task install

      - name: Initialize QA
        id: init_qa
        run: build/ci/ci-run.py ${{ matrix.platform }} task init_qa

      - name: QA
        run: build/ci/ci-run.py ${{ matrix.platform }} task qa
        continue-on-error: true
        env:
          PCP_QA_ARGS: ${{ github.event.inputs.pcp_qa_args }}

      - name: Copy test results
        # always() is required here to run this step even if the QA step fails
        if: always() && steps.init_qa.outcome == 'success'
        run: build/ci/ci-run.py ${{ matrix.platform }} artifacts test --path artifacts/test

      - name: Publish test results
        if: always() && steps.init_qa.outcome == 'success'
        uses: actions/upload-artifact@v2
        with:
          name: test-${{ matrix.platform }}
          path: artifacts/test

  report:
    name: Generate Test Report
    # always() is required here to run this job even if any of the QA jobs fails
    if: always() && (github.repository == 'performancecopilot/pcp' || github.event_name == 'workflow_dispatch')
    needs: qa
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Setup Allure2
        run: |
          wget -q -O allure-commandline.zip https://repo1.maven.org/maven2/io/qameta/allure/allure-commandline/2.14.0/allure-commandline-2.14.0.zip
          unzip -d /opt allure-commandline.zip

      - name: Download all artifacts
        uses: actions/download-artifact@v2
        with:
          path: artifacts

      - name: Generate test report
        run: |
          build/ci/test-report.py \
            --artifacts artifacts \
            --commit ${GITHUB_SHA} \
            --allure-results allure-results \
            --summary
          cp build/ci/allure/categories.json allure-results
          /opt/allure-*/bin/allure generate
          cp build/ci/allure/README.md allure-report
          cp build/ci/allure/open_report.sh allure-report
          # zip report before uploading, because GitHub uploads files in individual HTTP calls
          cd allure-report && zip -r ../qareport.zip . && cd ..

      - name: Publish test report
        uses: actions/upload-artifact@v2
        with:
          name: qareport
          path: qareport.zip

      - name: Checkout qa-reports
        if: github.repository == 'performancecopilot/pcp'
        uses: actions/checkout@v2
        with:
          repository: ${{ github.repository_owner }}/qa-reports
          path: qa-reports
          ssh-key: ${{ secrets.QA_REPORT_DEPLOY_KEY }}

      - name: Publish test report on GitHub Pages
        if: github.repository == 'performancecopilot/pcp'
        run: |
          rm -rf qa-reports/test-*
          cp -r artifacts/test-* qa-reports/

          current_report=$(date +'%Y%m%d_%H%M%S')-${GITHUB_SHA::8}
          cp -r allure-report qa-reports/reports/${current_report}

          pushd qa-reports/reports
          rm latest
          rm README.md
          ls -1r | tail -n +16 | xargs --no-run-if-empty rm -r # keep last 15 reports
          ln -s ${current_report} latest
          ls -1r -I README.md | awk 'BEGIN {print "# Recent Reports"} { print "## [" $1 "](" $1 "/)" }' > README.md
          popd

          pushd qa-reports
          git config --local user.name "GitHub Actions"
          git config --local user.email "action@github.com"
          git add .
          git commit --amend -m "Added QA report of ${{ github.repository_owner }}/pcp@${GITHUB_SHA}"
          git push -f

          echo "::warning::Complete test report is available at https://${{ github.repository_owner }}.github.io/qa-reports/reports/latest/"

      - name: Send test report to Slack
        if: github.repository == 'performancecopilot/pcp'
        run: |
          build/ci/test-report.py \
            --artifacts artifacts \
            --slack-channel ${SLACK_CHANNEL} \
            --github-run-url "https://github.com/${{ github.repository_owner }}/pcp/actions/runs/${{ github.run_id }}" \
            --qa-report-url "https://${{ github.repository_owner }}.github.io/qa-reports/reports/latest/"
        env:
          SLACK_CHANNEL: C011ERBFQGN
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
