Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ruff linter to workflows #33

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ on:
required: false
type: string
default: '3.2.2'
ruff-version:
required: false
type: string
default: '0.7.0'
BASH_SEVERITY:
required: false
type: string
Expand Down Expand Up @@ -141,6 +145,28 @@ jobs:
- name: Check code style with Black
run: |
black --check --diff --line-length 79 .
ruff:
name: ruff
if: ${{ inputs.ruff-version != '' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install apt dependencies
run: |
sudo apt-get install -y -qq python3 python3-pip
- name: Install pip dependencies
run: |
python -m pip install --upgrade pip
pip3 install ruff==${{ inputs.ruff-version }}
ruff --version
- name: Configure ruff
if: ${{ hashFiles('ruff.toml') == '' }}
run: |
echo "ruff.toml does not exists. Will be downloaded."
wget https://raw.githubusercontent.com/mundialis/github-workflows/main/linting-config-examples/ruff.toml
- name: Lint with ruff
run: |
ruff check --config ruff.toml .
super-linter:
name: super-linter
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ COPY .github/workflows/linting.yml $WORKFLOW_LINTING_WORKFLOW
RUN echo black==$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."black-version"'.default) >> /requirements.txt
RUN echo flake8==$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."flake8-version"'.default) >> /requirements.txt
RUN echo pylint==$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."pylint-version"'.default) >> /requirements.txt
RUN echo ruff==$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."ruff-version"'.default) >> /requirements.txt
RUN pip install -r requirements.txt

# Copy script to be executed on docker startup
Expand Down
4 changes: 4 additions & 0 deletions linting-config-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ same way and place them in your directory root to adjust.
`pylint .`
`pylint --rc-file=.pylintrc_allowed_to_fail .`

## ruff
ruff contains rules inspired by black, flake8, pylint and more and is extremely fast in linting and formatting Python code. Simply add a ruff.toml config file to the root of your repository.
See example here or visit [official documentation](https://docs.astral.sh/ruff/). `ruff check --config ruff.toml .`

## superlinter
Superlinter is a wrapper for many different linters. Each has a different way to be configured.
Example config files exist for [markdownlint](https://github.com/DavidAnson/markdownlint)
Expand Down
44 changes: 44 additions & 0 deletions linting-config-examples/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Ruff configuration file: ruff.toml

# Define the required version for Ruff
required-version = ">=0.7.0"

line-length = 79

# Specify directories or files to be excluded from Ruff linting, in addition to default exclusions
extend-exclude = [
".git",
"__pycache__",
".env",
".venv",
"env",
"venv",
"ENV",
"env.bak",
"venv.bak",
"ctypes",
"pydispatch",
]

# Uncomment the following sections as needed

# [format]
# Format settings for Ruff (quote-style and indent-style)
# quote-style = "double"
# indent-style= "tab"

# [lint]
# Define linting rules selection and ignore list
# select = [
# "A", # flake8-builtins (A)
# "COM", # flake8-commas
# "PL", # Pylint
# ]
# ignore = [
# "E402", # module-import-not-at-top-of-file
# "E501", # line-too-long
# ]

# [lint.per-file-ignores]
# Define file-specific linting rule ignores
# "lib_dop/r_dop_import_lib.py" = ["ERA001", "PLR2004"]
43 changes: 43 additions & 0 deletions pre_commit_hooks/linting.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fi
RUN_BLACK=TRUE
RUN_FLAKE8=TRUE
RUN_PYLINT=TRUE
RUN_RUFF=TRUE

####################################
# Overwrite linter versions if set #
Expand Down Expand Up @@ -83,6 +84,24 @@ else
echo "- pylint version not overwritten in code workflow"
fi

if [ $(yq '.jobs.lint.with | has("ruff-version")' $CODE_LINTING_WORKFLOW) == true ]
then
DEFAULT_RUFF_VERSION=$(cat /requirements.txt | grep ruff== | cut -d "=" -f 3)
NEW_RUFF_VERSION=$(yq '.jobs.lint.with."ruff-version"' $CODE_LINTING_WORKFLOW)
if [ "$(echo $NEW_RUFF_VERSION | tr '"' 'x')" = "xx" ]
then
RUN_RUFF=FALSE
echo "- ruff configured to be skipped (empty string)"
elif [ $DEFAULT_RUFF_VERSION != $NEW_RUFF_VERSION ]
then
# sed would fail with Permission denied
# sed -i "s+$DEFAULT_RUFF_VERSION+$NEW_RUFF_VERSION+g" /requirements.txt
echo "- Warning: overwritten ruff version will not be installed!"
fi
else
echo "- ruff version not overwritten in code workflow"
fi

echo

###################
Expand All @@ -94,6 +113,7 @@ echo
echo "flake8: `flake8 --version`"
echo "pylint: `pylint --version`"
echo "black: `black --version`"
echo "ruff: `ruff --version`"

########
# lint #
Expand Down Expand Up @@ -168,6 +188,29 @@ else
echo "BLACK configured to be skipped"
fi

if [ $RUN_RUFF != "FALSE" ]
then
echo
echo "RUFF:"
if test -f "ruff.toml"
then
echo "ruff.toml exists"
else
# Same behaviour as in workflow
echo "ruff.toml does not exists. Will be downloaded."
wget https://raw.githubusercontent.com/mundialis/github-workflows/main/linting-config-examples/ruff.toml
fi
ruff check --config ruff.toml .
if [ $? -ne 0 ]
then
RETURNCODE=1
FAILINGSTEP="$FAILINGSTEP RUFF (run 'ruff check --config ruff.toml .')"
fi
else
echo
echo "RUFF configured to be skipped"
fi

if [ $RETURNCODE -ne 0 ]
then
echo "Failing steps: ${FAILINGSTEP}"
Expand Down