Repository: YangCatalog/yang-validator-extractor (language: python)

Issue:
Generate code coverage automatically
Update the coverage badge and generate a full coverage report summary on every Pull Request


Discussion / hints:

Fix patch:
diff --git a/.coveragerc b/.coveragerc
new file mode 100644
index 0000000..d4fa8be
--- /dev/null
+++ b/.coveragerc
@@ -0,0 +1,2 @@
+[run]
+relative_files = true
\ No newline at end of file
diff --git a/.github/images/coverage.svg b/.github/images/coverage.svg
new file mode 100644
index 0000000..91a5e77
--- /dev/null
+++ b/.github/images/coverage.svg
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" width="99" height="20">
+    <linearGradient id="b" x2="0" y2="100%">
+        <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
+        <stop offset="1" stop-opacity=".1"/>
+    </linearGradient>
+    <mask id="a">
+        <rect width="99" height="20" rx="3" fill="#fff"/>
+    </mask>
+    <g mask="url(#a)">
+        <path fill="#555" d="M0 0h63v20H0z"/>
+        <path fill="#dfb317" d="M63 0h36v20H63z"/>
+        <path fill="url(#b)" d="M0 0h99v20H0z"/>
+    </g>
+    <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
+        <text x="31.5" y="15" fill="#010101" fill-opacity=".3">coverage</text>
+        <text x="31.5" y="14">coverage</text>
+        <text x="80" y="15" fill="#010101" fill-opacity=".3">68%</text>
+        <text x="80" y="14">68%</text>
+    </g>
+</svg>
diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml
new file mode 100644
index 0000000..8b855aa
--- /dev/null
+++ b/.github/workflows/coverage.yml
@@ -0,0 +1,26 @@
+name: Post coverage comment
+
+on:
+  workflow_run:
+    workflows: ['Python tests']
+    types:
+      - completed
+
+jobs:
+
+  coverage-comment:
+    runs-on: ubuntu-18.04
+    if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}
+
+    steps:
+      - uses: actions/checkout@v2
+
+      - uses: actions/setup-python@v4
+        with:
+          python-version: '3.9'
+
+      - name: Post comment
+        uses: py-cov-action/python-coverage-comment-action@v3
+        with:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+          GITHUB_PR_RUN_ID: ${{ github.event.workflow_run.id }}
\ No newline at end of file
diff --git a/README.md b/README.md
index afc1487..7eb0268 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,9 @@
 # Django-based YANG Extractor and Validator
 
+<img src=".github/images/coverage.svg" alt="">
+
+---
+
 A web application that allows you to fetch, extract and validate YANG modules by RFC number, by IETF draft name, or by uploading YANG files. 
 It is built on top of the [Django](https://www.djangoproject.com/) Python web framework. [xym](https://github.com/xym-tool/xym) tool is used to fetch and extract YANG modules from IETF specifications. Combination of the following YANG compilators/validators are used on the extracted modules:
 - [pyang](https://github.com/mbj4668/pyang)
@@ -24,7 +28,7 @@ Information about the YANG Validator API can be found here:
 
 ## Building and Deploying Docker Image
 
-The NSO configuration is setup to listen to port 8080 to avoid requiring root to run it, so remember to use portmapping when you start the container:
+The NSO configuration is set up to listen to port 8080 to avoid requiring root to run it, so remember to use port mapping when you start the container:
 
 ```console
 docker run -p 0.0.0.0:80:8080


Test patch:
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 5750b1a..a32579c 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -1,6 +1,9 @@
 name: Python tests
 
-on: [push]
+on:
+  push:
+  pull_request:
+    types: [opened, reopened, ready_for_review, synchronize, edited]
 
 jobs:
   build:
@@ -31,6 +34,7 @@ jobs:
           pip install --upgrade pip
           pip install -r requirements.txt
           pip install -r lint_requirements.txt
+          pip install -r tests_requirements.txt
 
       - name: Check isort
         uses: jamescurtin/isort-action@master
@@ -46,5 +50,79 @@ jobs:
           YANGCATALOG_CONFIG_PATH: tests/resources/test.conf
           DJANGO_SETTINGS_MODULE: tests.resources.settings
           CONFD_VERSION: 0
-        run: python3 manage.py test tests
+        run: coverage run manage.py test tests
+
+      - name: Store coverage file
+        uses: actions/upload-artifact@v3
+        with:
+          name: coverage
+          path: .coverage
+
+      - name: Write coverage comment
+        id: coverage_comment
+        uses: py-cov-action/python-coverage-comment-action@v3
+        with:
+          GITHUB_TOKEN: ${{ github.token }}
+          COMMENT_TEMPLATE: '
+            {% extends "base" %}
+            {% block title %}
+            {{ super() }}
+  
+            <details>
+            <summary>Full coverage report summary (click to unfold)</summary>
+            {% for file_coverage in coverage.files.values() %}
+            {{ file_coverage.path }}: <b>{{ ((file_coverage.info.percent_covered * 100)|int) }}%</b><br>
+            {% endfor %}
+            </details>
+  
+            {% endblock title %}
+          '
+
+      - name: Store Pull Request comment to be posted
+        uses: actions/upload-artifact@v3
+        if: ${{ steps.coverage_comment.outputs.COMMENT_FILE_WRITTEN == 'true' }}
+        with:
+          name: python-coverage-comment-action
+          path: python-coverage-comment-action.txt
+
+  update-coverage-badge:
+    needs: build
+    runs-on: ubuntu-18.04
+    if: ${{ github.event_name == 'push' && github.event.push.head.repo.full_name != 'YangCatalog/yang-validator-extractor' }}
+
+    steps:
+      - uses: actions/checkout@v3
+        with:
+          fetch-depth: 0
+          ref: ${{ github.event.push.head.ref }}
+          repository: ${{ github.event.push.head.repo.full_name }}
+
+      - uses: actions/setup-python@v4
+        with:
+          python-version: '3.9'
+
+      - uses: actions/download-artifact@v3
+        with:
+          name: coverage
+
+      - name: Coverage badge
+        uses: tj-actions/coverage-badge-py@v1.8
+
+      - name: Move coverage badge to images folder
+        run: mv coverage.svg .github/images
+
+      - name: Verify Changed files
+        uses: tj-actions/verify-changed-files@v9
+        id: changed_files
+        with:
+          files: .github/images/coverage.svg
+
+      - name: Commit files
+        if: ${{ steps.changed_files.outputs.files_changed == 'true' }}
+        run: |
+          git config --local user.email "github-actions[bot]@users.noreply.github.com"
+          git config --local user.name "github-actions[bot]"
+          git add .github/images/coverage.svg
+          git commit -m "Updated coverage.svg"
+          git push
   
\ No newline at end of file
diff --git a/tests_requirements.txt b/tests_requirements.txt
new file mode 100644
index 0000000..7db431e
--- /dev/null
+++ b/tests_requirements.txt
@@ -0,0 +1,1 @@
+coverage==6.5.0
\ No newline at end of file