Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow_run as eventName #153

Merged
merged 1 commit into from
Feb 13, 2024
Merged

Add workflow_run as eventName #153

merged 1 commit into from
Feb 13, 2024

Conversation

Bouni
Copy link
Contributor

@Bouni Bouni commented Feb 8, 2024

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
Copy link
Contributor Author

Bouni commented Feb 8, 2024

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

@MishaKav
Copy link
Owner

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
Copy link
Contributor Author

Bouni commented Feb 12, 2024

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 MishaKav merged commit 5dbfef7 into MishaKav:main Feb 13, 2024
1 check failed
MishaKav added a commit that referenced this pull request Feb 13, 2024
* 1.1.51

* changelog

* build
@MishaKav
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants