Skip to content

Commit

Permalink
Added GitHub Actions python script linter (PalisadoesFoundation#2850)
Browse files Browse the repository at this point in the history
* Added GitHub Actions python script linter

* Fixed reference

* Linted
  • Loading branch information
palisadoes authored and PurnenduMIshra129th committed Jan 14, 2025
1 parent a7c27ea commit 51a9532
Show file tree
Hide file tree
Showing 8 changed files with 899 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
ignore = E402,E722,E203,F401,W503
max-line-length = 80
53 changes: 52 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
echo "Error: Close this PR and try again."
exit 1
check_code_quality:
Code-Quality-Checks:
name: Checking code quality
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -124,6 +124,7 @@ jobs:
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
needs: [Code-Quality-Checks]
runs-on: ubuntu-latest
steps:
- name: Checkout this repository
Expand Down Expand Up @@ -238,3 +239,53 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_REPOSITORY: ${{ github.repository }}

Python-Compliance:
name: Check Python Code Style
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 .github/workflows/requirements.txt
- name: Run Black Formatter Check
run: |
source venv/bin/activate
black --check .
- name: Run Flake8 Linter
run: |
source venv/bin/activate
flake8 --docstring-convention google --ignore E402,E722,E203,F401,W503 .github
- name: Run pydocstyle
run: |
source venv/bin/activate
pydocstyle --convention=google --add-ignore=D415,D205 .github
- name: Run docstring compliance check
run: |
source venv/bin/activate
python .github/workflows/scripts/check_docstrings.py --directories .github
14 changes: 14 additions & 0 deletions .github/workflows/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#############################################################################
# DO NOT DELETE
#############################################################################
#
# Required for GitHub Action workflow python checks
#
#############################################################################
#############################################################################

black
pydocstyle
flake8
flake8-docstrings
docstring_parser
43 changes: 29 additions & 14 deletions .github/workflows/scripts/biome_disable_check.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""Biome Checker Script.
Methodology:
Recursively analyzes specified files or directories to ensure they do
not contain biome-ignore statements, enforcing code quality practices.
NOTE:
Note:
This script complies with our python3 coding and documentation standards.
It complies with:
Expand All @@ -25,16 +24,17 @@


def has_biome_ignore(file_path: str) -> bool:
"""
Check if a file contains biome-ignore statements.
"""Check if a file contains biome-ignore statements.
Args:
file_path (str): Path to the file.
file_path: Path to the file.
Returns:
bool: True if biome-ignore statement is found, False otherwise.
"""
biome_ignore_pattern = re.compile(r"//\s*biome-ignore.*$", re.IGNORECASE | re.MULTILINE)
biome_ignore_pattern = re.compile(
r"//\s*biome-ignore.*$", re.IGNORECASE | re.MULTILINE
)

try:
with open(file_path, encoding="utf-8") as file:
Expand All @@ -52,20 +52,27 @@ def has_biome_ignore(file_path: str) -> bool:


def check_biome(files_or_directories: list[str]) -> bool:
"""
Check files for biome-ignore statements.
"""Check files for biome-ignore statements.
Args:
files_or_directories (list): List of files or directories to check.
files_or_directories: List of files or directories to check.
Returns:
bool: True if biome-ignore statement is found, False otherwise.
"""
biome_found = False

for item in files_or_directories:
if os.path.isfile(item) and item.endswith((".ts", ".tsx")) and has_biome_ignore(item):
print(f"File {item} contains biome-ignore statement. Please remove them and ensure the code adheres to the specified Biome rules.")
if (
os.path.isfile(item)
and item.endswith((".ts", ".tsx"))
and has_biome_ignore(item)
):
print(
f"""\
File {item} contains biome-ignore statement. Please remove them and ensure \
the code adheres to the specified Biome rules."""
)
biome_found = True
elif os.path.isdir(item):
# If it's a directory, walk through it and check all
Expand All @@ -89,6 +96,9 @@ def check_biome(files_or_directories: list[str]) -> bool:
def arg_parser_resolver():
"""Resolve the CLI arguments provided by the user.
Args:
None
Returns:
result: Parsed argument object
"""
Expand All @@ -113,8 +123,7 @@ def arg_parser_resolver():


def main():
"""
Execute the script's main functionality.
"""Execute the script's main functionality.
This function serves as the entry point for the script. It performs
the following tasks:
Expand All @@ -124,6 +133,12 @@ def main():
3. Provides informative messages based on the analysis.
4. Exits with an error if biome-ignore statements are found.
Args:
None
Returns:
None
Raises:
SystemExit: If an error occurs during execution.
"""
Expand All @@ -142,4 +157,4 @@ def main():


if __name__ == "__main__":
main()
main()
Loading

0 comments on commit 51a9532

Please sign in to comment.