Skip to content

Fixed homepage links #702

Fixed homepage links

Fixed homepage links #702

Workflow file for this run

##############################################################################
##############################################################################
#
# 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
with:
fetch-depth: 0
- 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]' }}
name: Checks if sensitive files have been changed without authorization
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches and tags
- name: Get PR labels
id: check-labels
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [ -z "${{ github.event.pull_request.number }}" ]; then
echo "skip=false" >> $GITHUB_OUTPUT
exit 0
fi
LABELS="$(gh api repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels --jq '.[].name' | tr '\n' ' ')"
if echo "$LABELS" | grep -qw "ignore-sensitive-files-pr"; then
echo "::notice::Skipping sensitive files check due to 'ignore-sensitive-files-pr' label."
echo "skip=true" >> $GITHUB_OUTPUT
else
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Get Changed Unauthorized files
if: steps.check-labels.outputs.skip != 'true'
id: changed-unauth-files
run: |
# Skip if not in PR context
if [ -z "${{ github.event.pull_request.base.sha }}" ]; then
echo "any_changed=false" >> $GITHUB_OUTPUT
exit 0
fi
# Determine base and head commits for comparison
HEAD_SHA="${{ github.event.pull_request.head.sha || github.sha }}"
BASE_SHA=$(git merge-base "${{ github.event.pull_request.base.sha }}" "$HEAD_SHA")
# Define sensitive files patterns as a bash array
SENSITIVE_PATTERNS=(
"./*.md"
".coderabbit.yaml"
"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"
"yaml.lock"
"package.json"
"package-lock.json"
".pre-commit-config.yaml"
)
# Check for changes in sensitive files
CHANGED_UNAUTH_FILES=""
for pattern in "${SENSITIVE_PATTERNS[@]}"; do
FILES=$(git diff --name-only --diff-filter=ACMRD "$BASE_SHA" "$HEAD_SHA" | grep -E "$pattern" || true)
if [ ! -z "$FILES" ]; then
CHANGED_UNAUTH_FILES="$CHANGED_UNAUTH_FILES $FILES"
fi
done
# Trim and format output
CHANGED_UNAUTH_FILES=$(echo "$CHANGED_UNAUTH_FILES" | xargs)
echo "all_changed_files=$CHANGED_UNAUTH_FILES" >> $GITHUB_OUTPUT
# Check if any unauthorized files changed
if [ ! -z "$CHANGED_UNAUTH_FILES" ]; then
echo "any_changed=true" >> $GITHUB_OUTPUT
else
echo "any_changed=false" >> $GITHUB_OUTPUT
fi
- name: List all changed unauthorized files
if: steps.changed-unauth-files.outputs.any_changed == 'true'
env:
CHANGED_UNAUTH_FILES: ${{ steps.changed-unauth-files.outputs.all_changed_files }}
run: |
echo "::error::Unauthorized changes detected in sensitive files:"
echo ""
for file in $CHANGED_UNAUTH_FILES; do
echo "- $file"
done
echo ""
echo "To override:"
echo "Add the 'ignore-sensitive-files-pr' label to this PR."
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
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
run: |
CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})
# Use multiline token syntax to safely output file names
echo "changed_files<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGED_FILES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
CHANGED_FILES_COUNT=$(echo "$CHANGED_FILES" | wc -l)
echo "changed_files_count=$CHANGED_FILES_COUNT" >> $GITHUB_OUTPUT
- name: Echo number of changed files
run: |
echo "Number of changed files: ${{ steps.changed-files.outputs.changed_files_count }}"
- name: Check if the number of changed files is less than 10000
if: ${{ steps.changed-files.outputs.changed_files_count > 10000 }}
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 the source branch."
exit 1
Test-Application:
name: Test Application
needs: [Code-Quality-Checks, Docstring-Compliance]
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: Updated Ubuntu package sources
run: sudo apt-get -y update
- 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: 80.0
Test-Frontend:
name: Test Frontend and Coverage
runs-on: ubuntu-latest
needs: [Code-Quality-Checks] # or [Test-Application] if you want backend ready
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 22
- name: Cache node modules
uses: actions/cache@v4
with:
path: frontend/node_modules
key: ${{ runner.os }}-node-${{ hashFiles('frontend/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install frontend dependencies
working-directory: frontend
run: npm install
- name: Run frontend tests with coverage
working-directory: frontend
run: npm run test -- --coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: frontend/coverage/lcov.info
fail_ci_if_error: true
verbose: true
- name: Test acceptable level of code coverage
uses: VeryGoodOpenSource/very_good_coverage@v3
with:
path: frontend/coverage/lcov.info
min_coverage: 95.0
Test-Docusaurus-Deployment:
name: Test Deployment to https://docs.switchmap-ng.io
runs-on: ubuntu-latest
needs: [Test-Application]
# Run only if the develop branch and not dependabot
if: ${{ github.actor != 'dependabot[bot]' && github.event.pull_request.base.ref == 'develop' }}
steps:
- name: Checkout the Repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "24.x"
- name: Install dependencies
working-directory: ./docs
run: npm install
- name: Test building the website
working-directory: ./docs
run: npm run 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
with:
fetch-depth: 0
- 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 docstring_parser
- name: Run docstring compliance check
run: |
source venv/bin/activate
python .github/workflows/scripts/check_docstrings.py --directories switchmap setup bin tests .github