Autofix hook — ruff fix + format before gate evaluation.
AutofixHook
Run ruff fix + format as a pre-gate hook.
Source code in packages/axm-audit/src/axm_audit/hooks/autofix.py
| class AutofixHook:
"""Run ruff fix + format as a pre-gate hook."""
def execute(self, context: dict[str, Any], **params: Any) -> HookResult:
"""Run ``ruff check --fix .`` then ``ruff format .``.
Args:
context: Hook context with ``working_dir``.
**params: Ignored.
Returns:
HookResult with ``fixed`` count in metadata.
"""
project_path = Path(context.get("working_dir", "."))
try:
fix_result = run_in_project(
["ruff", "check", "--fix", "."],
project_path,
capture_output=True,
text=True,
)
except FileNotFoundError:
return HookResult.ok(skipped=True)
if fix_result.returncode == _RUFF_CONFIG_ERROR:
logger.warning("ruff config error: %s", fix_result.stderr[:500])
return HookResult.ok(fixed=0)
fixed = _parse_fixed_count(fix_result.stdout)
try:
run_in_project(
["ruff", "format", "."],
project_path,
capture_output=True,
text=True,
)
except FileNotFoundError:
return HookResult.ok(skipped=True)
return HookResult.ok(fixed=fixed)
|
execute(context, **params)
Run ruff check --fix . then ruff format ..
Parameters:
| Name |
Type |
Description |
Default |
context
|
dict[str, Any]
|
Hook context with working_dir.
|
required
|
**params
|
Any
|
|
{}
|
Returns:
| Type |
Description |
HookResult
|
HookResult with fixed count in metadata.
|
Source code in packages/axm-audit/src/axm_audit/hooks/autofix.py
| def execute(self, context: dict[str, Any], **params: Any) -> HookResult:
"""Run ``ruff check --fix .`` then ``ruff format .``.
Args:
context: Hook context with ``working_dir``.
**params: Ignored.
Returns:
HookResult with ``fixed`` count in metadata.
"""
project_path = Path(context.get("working_dir", "."))
try:
fix_result = run_in_project(
["ruff", "check", "--fix", "."],
project_path,
capture_output=True,
text=True,
)
except FileNotFoundError:
return HookResult.ok(skipped=True)
if fix_result.returncode == _RUFF_CONFIG_ERROR:
logger.warning("ruff config error: %s", fix_result.stderr[:500])
return HookResult.ok(fixed=0)
fixed = _parse_fixed_count(fix_result.stdout)
try:
run_in_project(
["ruff", "format", "."],
project_path,
capture_output=True,
text=True,
)
except FileNotFoundError:
return HookResult.ok(skipped=True)
return HookResult.ok(fixed=fixed)
|