Skip to content

Integrate Linting into CI Pipeline and pre-commit to Enhance Code Quality Checks #4

Description

@SverreNystad

We need to add a linting step to our Continuous Integration (CI) pipeline to ensure that all pushed code adheres to our coding standards and is free of syntax errors and undefined names. This addition will help prevent common coding issues from being merged into the main codebase, enhancing overall code quality and maintainability.

Proposed Changes

  1. Update CI Workflow: Integrate flake8 linting into the CI pipeline. This will be set up to fail the pipeline if linting errors are detected, specifically targeting Python syntax errors or undefined names. Here's is a possible update to the GitHub Actions workflow file:
- name: Install dependencies
  run: |
    python -m pip install --upgrade pip
    pip install flake8 pytest
    if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: Lint with flake8
  run: |
    # stop the build if there are Python syntax errors or undefined names
    flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
    # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
    flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

This configuration ensures that critical linting errors will fail the build, prompting developers to fix issues before merging their changes.

  1. Pre-commit Hook: Additionally, consider setting up a pre-commit hook to run linting locally before changes are pushed to the repository. This can help catch issues earlier in the development cycle, reducing CI failures due to linting errors.
    Here's is a possible solution:
#!/bin/sh

# Pre-commit hook to run flake8 linting with specific settings

# Stash non-staged changes to include only staged changes in the flake8 check
git stash -q --keep-index

# Strict check: stop the commit if there are Python syntax errors or undefined names
echo "Running strict flake8 checks..."
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics

# Save the return value from the strict check
STRICT_RETVAL=$?

# General linting with settings from the CI pipeline
echo "Running general flake8 linting..."
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

# Unstash the non-staged changes stashed earlier
git stash pop -q

# Check if strict linting failed
if [ $STRICT_RETVAL -ne 0 ]
then
    echo "Strict flake8 linting failed. Your commit will be aborted."
    echo "Fix the reported issues before committing."
    exit 1
fi

echo "Linting passed."
exit 0

Acceptance Criteria:

  • Update the CI pipeline to use linting.
  • Add scripts/pre-commit.sh for local linting.
  • Document the setup process for using the pre-commit hooks in the developer guide.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions