From 731b0a70b3a1fdf0c6062e27b8013d151c6cc2ed Mon Sep 17 00:00:00 2001 From: Bhargava Shastry Date: Fri, 6 Oct 2023 12:21:38 +0200 Subject: [PATCH] Add coverage and badge --- .github/workflows/bandit.yaml | 1 + .github/workflows/coverage.yml | 71 ++++++++++++++++++++++++++++++++++ .github/workflows/tests.yml | 4 +- README.md | 2 +- 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/coverage.yml diff --git a/.github/workflows/bandit.yaml b/.github/workflows/bandit.yaml index 004045d..a3034cd 100644 --- a/.github/workflows/bandit.yaml +++ b/.github/workflows/bandit.yaml @@ -23,6 +23,7 @@ jobs: - name: Install dependencies run: | + python -m pip install --upgrade pip pip install -r requirements.txt pip install bandit diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 0000000..8458d77 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,71 @@ +name: Run Coverage + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build-and-test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install coverage + + - name: Run tests with coverage + run: | + coverage run -m unittest discover -s tests -p "test_*.py" + + - name: Generate coverage report + run: | + coverage html --directory coverage_html_report + coverage report -m > coverage_report.txt + + - name: Upload coverage report + uses: actions/upload-artifact@v2 + with: + name: coverage-report + path: | + coverage_html_report/ + coverage_report.txt + + - name: Set up coverage badge + uses: actions/github-script@v4 + with: + script: | + const fs = require('fs'); + const coverageReport = fs.readFileSync('coverage_report.txt', 'utf8'); + // Extract the total coverage percentage + const regex = /TOTAL\s+(\d+)\s+(\d+)\s+(\d+)%/; + const matches = report.match(regex); + const totalStatements = parseInt(matches[1]); + const missedStatements = parseInt(matches[2]); + const coveragePercentage = parseInt(matches[3]); + let badgeColor = 'brightgreen'; + + if (coveragePercentage < 80) { + badgeColor = 'red'; + } else if (coveragePercentage < 90) { + badgeColor = 'yellow'; + } + const badgeMarkdown = `![Coverage](https://img.shields.io/badge/Coverage-${coveragePercentage}%25-${badgeColor})`; + console.log(badgeMarkdown); + const readme = fs.readFileSync('README.md', 'utf8'); + const updatedReadme = readme.replace(/!\[Coverage\]\(.*\)/, badgeMarkdown); + console.log('Updated README:', updatedReadme); + fs.writeFileSync('README.md', updatedReadme); diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 08bb049..3fcf3f4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -19,7 +19,9 @@ jobs: python-version: 3.8 - name: Install dependencies - run: pip install -r requirements.txt + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt - name: Run tests run: python test_runner.py diff --git a/README.md b/README.md index 1d4e09b..7dee3d8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # DocuBot -[![Run Tests](https://github.com/bshastry/docubot/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/bshastry/docubot/actions/workflows/tests.yml)![Bandit Workflow](https://img.shields.io/badge/Bandit-No%20Issues%20Found-brightgreen) +[![Run Tests](https://github.com/bshastry/docubot/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/bshastry/docubot/actions/workflows/tests.yml)![Bandit Workflow](https://img.shields.io/badge/Bandit-No%20Issues%20Found-brightgreen)![Coverage](https://img.shields.io/badge/Coverage-90%25-brightgreen.svg) DocuBot is a command-line chatbot that answers questions using a knowledge base of documents provided by you. It allows you to interactively get answers to questions with citations from the documents provided.