From 293d5da9b8dbb9bdfc8d91b60fd0b92403c00ac9 Mon Sep 17 00:00:00 2001 From: Uno-Takashi Date: Sun, 21 Jun 2026 10:42:36 +0000 Subject: [PATCH] chore: add pre-commit hooks and flake8-bandit ruff rules Introduce a pre-commit configuration running ruff (lint + format) and the standard pre-commit-hooks. Expand the ruff rule set to cover flake8 (E/W/F), flake8-django (DJ) and flake8-bandit (S); ignore S101 in tests/conftest. Vendored static assets under web/static/ are excluded from the generic whitespace/EOF hooks. Hand-maintained files are normalised one-off. Co-Authored-By: Claude Opus 4.8 --- .dockleignore | 2 +- .github/PULL_REQUEST_TEMPLATE.md | 2 +- .gitignore | 2 +- .pre-commit-config.yaml | 33 ++++++++++++++++++++++++++++++++ pyproject.toml | 11 ++++++++--- web/.gitignore | 2 +- web/templates/404.html | 2 +- web/templates/chart.html | 6 +++--- web/templates/layout.html | 10 +++++----- 9 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.dockleignore b/.dockleignore index 8b81aae..6828f4c 100644 --- a/.dockleignore +++ b/.dockleignore @@ -1 +1 @@ - CIS-DI-0010 \ No newline at end of file + CIS-DI-0010 diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index dfb4d25..1066dbc 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -13,4 +13,4 @@ ## その他・備考 -- (備考やレビュワーへの申し送り事項等があれば書いてください) \ No newline at end of file +- (備考やレビュワーへの申し送り事項等があれば書いてください) diff --git a/.gitignore b/.gitignore index 8f09f77..0996822 100644 --- a/.gitignore +++ b/.gitignore @@ -96,4 +96,4 @@ migrations/ prof/* -.coverage* \ No newline at end of file +.coverage* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..46c6cd2 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,33 @@ +# pre-commit hooks for the d-party Backend (Django). +# Install: `pre-commit install` (run from this directory). +# Run on everything: `pre-commit run --all-files` +# +# ruff の詳細ルール(flake8 / flake8-django / flake8-bandit 等)は pyproject.toml の +# [tool.ruff] を参照する。 +minimum_pre_commit_version: "3.5.0" + +# ベンダー製 / 配信用静的アセット(jQuery・Bootstrap・Flickity 等の minified ライブラリ)は +# 整形対象から除外する。アプリのソースコードのみフックを適用する。 +exclude: '^web/static/' + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v6.0.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + args: [--allow-multiple-documents] + - id: check-toml + - id: check-json + - id: check-merge-conflict + - id: check-added-large-files + + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.15.18 + hooks: + # Lint(flake8 / flake8-django / flake8-bandit 等を ruff で再現。設定は pyproject.toml) + - id: ruff-check + args: [--fix] + # Format(black 互換) + - id: ruff-format diff --git a/pyproject.toml b/pyproject.toml index 722f260..9aa573d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,7 +52,10 @@ line-length = 88 extend-exclude = ["**/migrations/*"] [tool.ruff.lint] -select = ["E", "F", "I", "UP", "B", "DJ", "C4"] +# E,W,F = flake8 本体 (pycodestyle + pyflakes) / DJ = flake8-django / +# S = flake8-bandit / I = isort / UP = pyupgrade / B = flake8-bugbear / +# C4 = flake8-comprehensions +select = ["E", "W", "F", "I", "UP", "B", "DJ", "C4", "S"] ignore = [ "E501", # line length is handled by the formatter "B008", # function calls in argument defaults are idiomatic in Django/DRF @@ -62,8 +65,10 @@ ignore = [ [tool.ruff.lint.per-file-ignores] # The Channels ASGI entrypoint must initialise Django before importing routing. "d_party/asgi.py" = ["E402"] -# Pre-existing test-only smells we intentionally leave untouched. -"**/tests.py" = ["E711", "E721", "E722", "F811", "F841"] +# Tests use bare asserts (flake8-bandit S101) plus pre-existing test-only smells. +"**/tests.py" = ["S101", "E711", "E721", "E722", "F811", "F841"] +"**/test_*.py" = ["S101"] +"conftest.py" = ["S101"] [tool.pytest.ini_options] DJANGO_SETTINGS_MODULE = "d_party.settings" diff --git a/web/.gitignore b/web/.gitignore index e25f19a..5016c4e 100644 --- a/web/.gitignore +++ b/web/.gitignore @@ -1 +1 @@ -!static/ \ No newline at end of file +!static/ diff --git a/web/templates/404.html b/web/templates/404.html index 4fb16a9..fbfafd8 100644 --- a/web/templates/404.html +++ b/web/templates/404.html @@ -2,7 +2,7 @@ {% block stylesheet %} {% load static %} -{% endblock %} +{% endblock %} {% block content %}
diff --git a/web/templates/chart.html b/web/templates/chart.html index ba6b232..c0aff38 100644 --- a/web/templates/chart.html +++ b/web/templates/chart.html @@ -1,9 +1,9 @@ {% extends "layout.html" %} {% load static %} -{%block stylesheet %} +{%block stylesheet %} -{% endblock %} +{% endblock %} {% block content %}

ユーザーログ統計情報

@@ -60,4 +60,4 @@
ルーム : 接続中
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/web/templates/layout.html b/web/templates/layout.html index 6b1e333..497fbe1 100644 --- a/web/templates/layout.html +++ b/web/templates/layout.html @@ -9,7 +9,7 @@ {%block head%}{% endblock %} {% load static %} + type="image/png" sizes="32x32" href="{% static "web/favicon/favicon-32x32.png" %}"> @@ -24,11 +24,11 @@ - + - - - + + + {%block stylesheet%}{% endblock %}