From 3bfb5c99d4c1b59f0bfaed8a99aea2ddc14cc13d 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 | 60 ++++++++++++++++++++++++++++++++++ .github/workflows/tests.yml | 4 ++- README.md | 2 +- 4 files changed, 65 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..44d22dd --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,60 @@ +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 report + + - name: Upload coverage report + uses: actions/upload-artifact@v2 + with: + name: coverage-report + path: coverage_html_report/ + + - name: Set up coverage badge + uses: actions/github-script@v4 + with: + script: | + const fs = require('fs'); + const coverageReport = fs.readFileSync('coverage_report.txt', 'utf8'); + const coveragePercentage = coverageReport.match(/TOTAL\s+\d+\s+\d+\s+(\d+)%/)[1]; + 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})`; + const readme = fs.readFileSync('README.md', 'utf8'); + const updatedReadme = readme.replace(/!\[Coverage\]\(.*\)/, badgeMarkdown); + 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.