lenskit/lkpy

View on GitHub
.github/workflows/post-test-results.yml

Summary

Maintainability
Test Coverage
name: Post test results

on:
  workflow_run:
    workflows: [Automatic Tests]
    types:
      - completed

jobs:
  report-coverage:
    runs-on: ubuntu-latest
    if: github.event.workflow_run.event == 'pull_request'
    continue-on-error: true
    steps:
      - name: 'Download artifact'
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            let artifact = null;
            let attempts = 0;
            while (!artifact) {
              attempts += 1;
              if (attempts > 5) {
                console.error('too many attempts to find report artifact, abandoning');
                throw new Error('cannot find report artifact')
              }
              console.info("attemping to find artifact for run %s", context.payload.workflow_run.id);
              let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
                owner: context.repo.owner,
                repo: context.repo.repo,
                run_id: context.payload.workflow_run.id,
                name: "coverage-report"
              });
              console.info("found %d artifacts", allArtifacts.data.artifacts.length)
              artifact = allArtifacts.data.artifacts[0];
              if (!artifact) {
                console.info('could not find artifact, waiting and trying again')
                await new Promise((ok) => setTimeout(() => ok(), 15000));
              }
            }

            let download = await github.rest.actions.downloadArtifact({
               owner: context.repo.owner,
               repo: context.repo.repo,
               artifact_id: artifact.id,
               archive_format: 'zip',
            });
            let fs = require('fs');
            fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/coverage-report.zip`, Buffer.from(download.data));

      - name: 'Unzip artifact'
        run: unzip coverage-report.zip

      - name: 'Comment on PR'
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            let fs = require('fs');
            let issue_number = Number.parseInt(fs.readFileSync('./pr-number'));
            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issue_number,
              body: await fs.readFileSync('./report.md', {encoding: 'utf8'})
            });