GitHub Action: Complete conversion of testing to PyTest / CodeCov #208
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################## | |
############################################################################## | |
# | |
# PR Workflow | |
# | |
############################################################################## | |
############################################################################## | |
name: PR Workflow | |
on: | |
pull_request: | |
branches: | |
- "**" | |
env: | |
CODECOV_UNIQUE_NAME: CODECOV_UNIQUE_NAME-${{ github.run_id }}-${{ github.run_number }} | |
DATABASE_NAME: switchmap_unittest | |
DATABASE_USERNAME: switchmap_unittest | |
DATABASE_PASSWORD: switchmap_unittest | |
MYSQL_ROOT_USERNAME: root | |
MYSQL_ROOT_PASSWORD: root | |
jobs: | |
Code-Quality-Checks: | |
name: Performs linting, formatting, type-checking, checking for different source and target branch | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout the Repository | |
uses: actions/checkout@v4 | |
- name: Check if the source and target branches are different | |
if: ${{ github.event.pull_request.base.ref == github.event.pull_request.head.ref }} | |
run: | | |
echo "Source Branch ${{ github.event.pull_request.head.ref }}" | |
echo "Target Branch ${{ github.event.pull_request.base.ref }}" | |
echo "Error: Source and Target Branches are the same. Please ensure they are different." | |
echo "Error: Close this PR and try again." | |
exit 1 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: 3.9 | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install flake8 black pydocstyle flake8-docstrings | |
- name: Run Black Formatter Check | |
run: | | |
black --check . | |
- name: Run Flake8 Linter | |
run: | | |
flake8 --docstring-convention google switchmap bin setup tests .github --ignore E402,E722,E203,F401,W503 | |
- name: Run pydocstyle | |
run: | | |
pydocstyle switchmap setup bin tests --convention=google --add-ignore=D415,D205 | |
Check-Sensitive-Files: | |
if: ${{ github.actor != 'dependabot[bot]' && !contains(github.event.pull_request.labels.*.name, 'ignore-sensitive-files-pr') }} | |
name: Checks if sensitive files have been changed without authorization | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Get Changed Unauthorized files | |
id: changed-unauth-files | |
uses: tj-actions/changed-files@v45 | |
with: | |
files: | | |
CODE_OF_CONDUCT.md | |
CODE_STYLE.md | |
CONTRIBUTING.md | |
DOCUMENTATION.md | |
INSTALLATION.md | |
ISSUE_GUIDELINES.md | |
PR_GUIDELINES.md | |
README.md | |
.coderabbit.yaml | |
docs/CNAME | |
docs/static/CNAME | |
docs/package.json | |
docs/sidebar*.js | |
docs/docusaurus.config.js | |
docs/babel.config.js | |
docs/tsconfig.json | |
.gitignore | |
CODEOWNERS | |
LICENSE | |
.pydocstyle | |
pyproject.toml | |
.flake8 | |
requirements.txt | |
CNAME | |
yaml.lock | |
package.json | |
package-lock.json | |
- name: List all changed unauthorized files | |
if: steps.changed-unauth-files.outputs.any_changed == 'true' || steps.changed-unauth-files.outputs.any_deleted == 'true' | |
env: | |
CHANGED_UNAUTH_FILES: ${{ steps.changed-unauth-files.outputs.all_changed_files }} | |
run: | | |
for file in ${CHANGED_UNAUTH_FILES}; do | |
echo "$file is unauthorized to change/delete" | |
done | |
echo "To override this, apply the 'ignore-sensitive-files-pr' label" | |
exit 1 | |
Count-Changed-Files: | |
if: ${{ github.actor != 'dependabot[bot]' }} | |
name: Checks if number of files changed is acceptable | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Get changed files | |
id: changed-files | |
uses: tj-actions/changed-files@v45 | |
- name: Echo number of changed files | |
env: | |
CHANGED_FILES_COUNT: ${{ steps.changed-files.outputs.all_changed_files_count }} | |
run: | | |
echo "Number of files changed: $CHANGED_FILES_COUNT" | |
- name: Check if the number of changed files is less than 10000 | |
if: steps.changed-files.outputs.all_changed_files_count > 10000 | |
env: | |
CHANGED_FILES_COUNT: ${{ steps.changed-files.outputs.all_changed_files_count }} | |
run: | | |
echo "Error: Too many files (greater than 10000) changed in the pull request." | |
echo "Possible issues:" | |
echo "- Contributor may be merging into an incorrect branch." | |
echo "- Source branch may be incorrect please use develop as source branch." | |
exit 1 | |
Test-Application: | |
name: Test Application | |
needs: [Code-Quality-Checks] | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python 3.11 | |
uses: actions/setup-python@v3 | |
with: | |
python-version: 3.11 | |
- name: Install Ubuntu packages | |
run: sudo apt-get install -y python3 python3-pip snmp libsnmp-dev gcc python-dev-is-python3 python3-venv mysql-server | |
- name: Cache pip packages | |
uses: actions/cache@v4 | |
with: | |
path: ~/.cache/pip | |
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} | |
restore-keys: | | |
${{ runner.os }}-pip- | |
- name: Install dependencies and test | |
run: | | |
python3 -m venv venv | |
source venv/bin/activate | |
python -m pip install --upgrade pip | |
pip3 install -r requirements.txt | |
pip install pytest-cov coverage | |
tests/bin/error_code_report.py | |
tests/bin/test_db_config_setup.py | |
sudo systemctl start mysql.service | |
sudo mysql -u${{ env.MYSQL_ROOT_USERNAME }} -p${{ env.MYSQL_ROOT_PASSWORD }} -e "CREATE DATABASE ${{ env.DATABASE_NAME }};" | |
sudo mysql -u${{ env.MYSQL_ROOT_USERNAME }} -p${{ env.MYSQL_ROOT_PASSWORD }} -e "CREATE USER '${{ env.DATABASE_USERNAME }}'@'localhost' IDENTIFIED BY '${{ env.DATABASE_PASSWORD }}';" | |
sudo mysql -u${{ env.MYSQL_ROOT_USERNAME }} -p${{ env.MYSQL_ROOT_PASSWORD }} -e "GRANT ALL PRIVILEGES ON ${{ env.DATABASE_NAME }}.* TO '${{ env.DATABASE_USERNAME }}'@'localhost';" | |
sudo mysql -u${{ env.MYSQL_ROOT_USERNAME }} -p${{ env.MYSQL_ROOT_PASSWORD }} -e "FLUSH PRIVILEGES;" | |
pytest --cov=switchmap --cov-report=lcov:coverage/coverage.lcov --cov-report=term-missing tests/switchmap_ | |
- name: Upload coverage to Codecov | |
uses: codecov/codecov-action@v5 | |
with: | |
token: ${{ secrets.CODECOV_TOKEN }} | |
slug: PalisadoesFoundation/switchmap-ng | |
verbose: true | |
files: ./coverage/coverage.lcov | |
gcov_ignore: "docs/" | |
fail_ci_if_error: false | |
name: "${{env.CODECOV_UNIQUE_NAME}}" | |
- name: Test acceptable level of code coverage | |
uses: VeryGoodOpenSource/very_good_coverage@v3 | |
with: | |
path: ./coverage/coverage.lcov | |
min_coverage: 0.0 | |
Test-Docusaurus-Deployment: | |
name: Test Deployment to https://docs-legacy.switchmap-ng.io | |
runs-on: ubuntu-latest | |
needs: [Code-Quality-Checks] | |
# Run only if the develop branch and not dependabot | |
if: ${{ github.actor != 'dependabot[bot]' && github.event.pull_request.base.ref == 'develop' }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 22 | |
cache: yarn | |
cache-dependency-path: "docs/" | |
# Run Docusaurus in the ./docs directory | |
- name: Install dependencies | |
working-directory: ./docs | |
run: yarn install --frozen-lockfile | |
- name: Test building the website | |
working-directory: ./docs | |
run: yarn build | |
Check-Target-Branch: | |
if: ${{ github.actor != 'dependabot[bot]' }} | |
name: Check Target Branch | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check if the target branch is develop | |
if: github.event.pull_request.base.ref != 'develop' | |
run: | | |
echo "Error: Pull request target branch must be 'develop' not ${{github.event.pull_request.base.ref}}. Please refer PR_GUIDELINES.md" | |
echo "Error: Close this PR and try again." | |
exit 1 | |
Validate-CodeRabbit: | |
name: Validate CodeRabbit Approval | |
runs-on: ubuntu-latest | |
if: github.actor != 'dependabot[bot]' | |
needs: [Code-Quality-Checks, Test-Application] | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Validate CodeRabbit.ai Approval | |
run: | | |
chmod +x $GITHUB_WORKSPACE/.github/workflows/scripts/validate-coderabbit.sh | |
$GITHUB_WORKSPACE/.github/workflows/scripts/validate-coderabbit.sh | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
PR_NUMBER: ${{ github.event.pull_request.number }} | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
Docstring-Compliance: | |
name: Check Docstring Compliance | |
runs-on: ubuntu-latest | |
needs: [Code-Quality-Checks] | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python 3.11 | |
uses: actions/setup-python@v4 | |
with: | |
python-version: 3.11 | |
- name: Cache pip packages | |
uses: actions/cache@v4 | |
with: | |
path: ~/.cache/pip | |
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} | |
restore-keys: | | |
${{ runner.os }}-pip- | |
- name: Install dependencies | |
run: | | |
python3 -m venv venv | |
source venv/bin/activate | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Run docstring compliance check | |
run: | | |
source venv/bin/activate | |
python .github/workflows/scripts/check_docstrings.py --directories switchmap setup bin tests .github |