name: Daily QA
on:
  schedule:
    - cron:  '0 19 * * *'
  workflow_dispatch:

jobs:
  qa:
    name: ${{ matrix.platform }}-${{ matrix.runner }}
    if: github.repository == 'performancecopilot/pcp' || github.event_name == 'workflow_dispatch'
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - {platform: ubuntu1604,     runner: container, os: ubuntu-latest}
          - {platform: ubuntu1804,     runner: direct,    os: ubuntu-18.04}
          - {platform: ubuntu1804i386, runner: container, os: ubuntu-latest}
          - {platform: ubuntu2004,     runner: direct,    os: ubuntu-20.04}
          - {platform: debian10,       runner: container, os: ubuntu-latest}
          - {platform: debian11,       runner: container, os: ubuntu-latest}
          - {platform: fedora31,       runner: container, os: ubuntu-latest}
          - {platform: fedora32,       runner: container, os: ubuntu-latest}
          - {platform: fedora33,       runner: container, os: ubuntu-latest}
          - {platform: fedora_rawhide, runner: container, os: ubuntu-latest}
          - {platform: centos6,        runner: container, os: ubuntu-latest}
          - {platform: centos7,        runner: container, os: ubuntu-latest}
          - {platform: centos8,        runner: container, os: ubuntu-latest}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Set up Python 3.6+
        if: matrix.platform == 'ubuntu1604'
        uses: actions/setup-python@v2
        with:
          python-version: '>=3.6'

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

          for i in $(seq 1 10)
          do
            if [ $i -gt 1 ]; then
              echo && echo "Probably network error, wait 5 min and try again ($i/10)..."
              sleep 300
              sudo podman system prune -a -f
            fi

            ./build/ci/run.py --runner ${{ matrix.runner }} --platform ${{ matrix.platform }} setup && exit 0 || true
          done
          exit 1

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

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

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

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

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

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

      - name: QA
        run: ./build/ci/run.py --runner ${{ matrix.runner }} --platform ${{ matrix.platform }} task qa || true

      - name: Copy test results
        if: always()
        run: ./build/ci/run.py --runner ${{ matrix.runner }} --platform ${{ matrix.platform }} artifacts test --path ./artifacts/test

      - name: Publish test results
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: test-${{ matrix.platform }}-${{ matrix.runner }}
          path: ./artifacts/test

  report:
    name: Publish Test Report
    if: always() && github.repository == 'performancecopilot/pcp'
    needs: qa
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

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

      - name: Setup
        run: |
          wget -q -O allure-commandline.zip https://repo1.maven.org/maven2/io/qameta/allure/allure-commandline/2.13.5/allure-commandline-2.13.5.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 \
            --commit ${GITHUB_SHA} \
            --allure-results ./allure-results \
            --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"
          cp ./build/ci/allure/categories.json ./allure-results

          # disable history, because the history.json file is getting bigger than 100 MB (max GitHub file size)
          # [ -d qa-reports/docs/history ] && cp -r qa-reports/docs/history ./allure-results

          /opt/allure-*/bin/allure generate
        env:
          SLACK_CHANNEL: C011ERBFQGN
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

      - name: Publish QA report
        run: |
          rm -rf qa-reports/test-* qa-reports/docs
          mv artifacts/test-* qa-reports/
          mv allure-report qa-reports/docs

          cd qa-reports
          git config --local user.name "GitHub Actions"
          git config --local user.email "action@github.com"
          git add .
          git commit --amend -m "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/"
