MishaKav / pytest-coverage-comment

Comments a pull request with the pytest code coverage badge and full report
MIT License
173 stars 57 forks source link

Add workflow_run as eventName #153

Closed Bouni closed 7 months ago

Bouni commented 7 months ago

This PR adds workflow_run to the events that can trigger this action. That way #68 can be solved by using a two staged actions setup like this for example:

First a pytest action that generates pytest-coverage.txt and pytest.xml and uploads these amoung a textfile with the PR number as workflow artifacts.

---
name: Run unit tests
on: # yamllint disable-line rule:truthy
  push:
    branches:
      - main
  pull_request:
jobs:
  pytest:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - name: Install pytest
        run: pip install pytest pytest-cov
      - name: Run pytest
        # yamllint disable rule:line-length
        run: |
          set -o pipefail
          PYTHONPATH=. pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered --cov=luxtronik tests/ | tee pytest-coverage.txt
        # yamllint enable rule:line-length
      - name: Save PR number and coverage results
        run: |
          mkdir -p ./pr
          echo ${{ github.event.number }} > ./pr/PR-number.txt
          cp ./pytest-coverage.txt ./pr/pytest-coverage.txt
          cp ./pytest.xml ./pr/pytest.xml
      - uses: actions/upload-artifact@v4
        with:
          name: pr
          path: pr/

And the second stage that actually creates the comment.

---
name: Comment coverage report on the pull request
on: # yamllint disable-line rule:truthy
  workflow_run:
    workflows: ["Run unit tests"]
    types:
      - completed

jobs:
  post-coverage-report:
    runs-on: ubuntu-latest
    if: >
      github.event.workflow_run.event == 'pull_request' &&
      github.event.workflow_run.conclusion == 'success'
    steps:
      - name: 'Download artifact'
        uses: actions/github-script@v7
        # yamllint disable rule:line-length
        with:
          script: |
            var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
               owner: context.repo.owner,
               repo: context.repo.repo,
               run_id: ${{github.event.workflow_run.id }},
            });
            var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
              return artifact.name == "pr"
            })[0];
            var download = await github.rest.actions.downloadArtifact({
               owner: context.repo.owner,
               repo: context.repo.repo,
               artifact_id: matchArtifact.id,
               archive_format: 'zip',
            });
            var fs = require('fs');
            fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data));
        # yamllint enable rule:line-length
      - name: Unzip artifact
        run: unzip pr.zip
      - name: Read the PR number from file
        id: pr_number
        uses: juliangruber/read-file-action@v1
        with:
          path: ./PR-number.txt
      - name: Publish pytest coverage as comment
        uses: bouni/pytest-coverage-comment@workflow_run
        with:
          issue-number: ${{ steps.pr_number.outputs.content }}
          pytest-coverage-path: ./pytest-coverage.txt
          junitxml-path: ./pytest.xml

Important is that the name of the first worflow name: Run unit tests must be identical to workflows: ["Run unit tests"] in the second workflow.

@asumagic did actually post a solution like the above in #68 but that failed because workflow_run was not an allowd trigger.

The underlayng problem why the comment can not be created directly from a PR that comes from a forked repo is described in GitHubs Blog: https://securitylab.github.com/research/github-actions-preventing-pwn-requests/

Bouni commented 7 months ago

😏 The My Pytest Coverage Comment / Live Test (pull_request) action failed exactly because of #68 which will be solved by this PR.

MishaKav commented 7 months ago

Looks amazing, thank you so much for your contribution! 🚀 Your PR addressing such a complex problem is truly impressive and immensely valuable to this action. Will do a more deep review in the next few days and will release a new version :)

Bouni commented 7 months ago

To be honset, It took me dozens of trail an error attempts on one of my projects and with each iteration I though, now I finally fixed it 😅 I was so happy to see that you already had the workflow_dispatch case in you code, otherwise I would have been completely lost.

An thank you for providing this action by the way 👍🏽

MishaKav commented 7 months ago

Big thanks again for the effort, I release new version with your code https://github.com/MishaKav/pytest-coverage-comment/releases/tag/v1.1.51

You can use it:

- name: Pytest Coverage Comment
  uses: MishaKav/pytest-coverage-comment@main

or use a specific version

- name: Pytest Coverage Comment
  uses: MishaKav/pytest-coverage-comment@v1.1.51