Skip to content

Commit

Permalink
add pre-commit, output to file, as_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
ScDor committed Jan 5, 2024
1 parent c9dad6f commit 366a946
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 38 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: pre-commit

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
- uses: pre-commit/action@v3.0.0
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
__pycache__
/rules.json
/violations.json
/result.md
/result.md
/artifacts
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.9
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
45 changes: 16 additions & 29 deletions adopt_ruff.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
from __future__ import annotations

import json
from pathlib import Path
from typing import TYPE_CHECKING

from mdutils.mdutils import MdUtils
from tabulate import tabulate
from pydantic import BaseModel

from models.ruff_output import Violation
from models.rule import Rule
from utils import output_table

if TYPE_CHECKING:
from pydantic import BaseModel

ARTIFACTS_PATH = Path("artifacts")
ARTIFACTS_PATH.mkdir(exist_ok=True)
(ARTIFACTS_PATH := Path("artifacts")).mkdir(exist_ok=True)


def load(
Expand All @@ -41,34 +35,27 @@ def run(
# TODO ignore configured/ignored?
if already_respected := respected_rules(violations, rules):
md.new_header(1, "Respected Ruff rules")
md.new_line(f"{len(already_respected)} Ruff rules can be added right away 🚀")
md.new_line(f"{repo_name} already respects them - enforcing will be seamless.")
md.new_paragraph(tabulate(already_respected, tablefmt="github", headers="keys"))
# TODO save a CSV artifact

# TODO add autofixable table (always/sometimes)
md.new_line(
f"{len(already_respected)} Ruff rules are respected in the repo - "
"They can be added right away 🚀"
)
output_table(
already_respected,
ARTIFACTS_PATH / "respected.csv",
md=md,
collapsible=True,
)
return md.get_md_text()


def respected_rules(
violations: tuple[Violation, ...],
rules: tuple[Rule, ...],
) -> tuple[dict]:
violation_codes = {v.code for v in violations}
respected = [rule for rule in rules if rule.code not in violation_codes]
return tuple(
{
"Linter": rule.linter,
"Code": rule.code,
"Name": rule.name,
"Fixable": rule.fix.one_word,
"Preview": rule.preview,
}
for rule in sorted(respected, key=lambda r: r.code)
)
) -> tuple[Rule, ...]:
violated_codes = {v.code for v in violations}
return tuple(rule for rule in rules if rule.code not in violated_codes)


rules, violations = load()
result = run(rules, violations)
Path("result.md").write_text(result)
print(result)
11 changes: 11 additions & 0 deletions models/rule.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from enum import Enum
from typing import Any

from pydantic import BaseModel

Expand Down Expand Up @@ -35,3 +36,13 @@ class Config:
fix: FixAvailability
explanation: str
preview: bool

@property
def as_dict(self) -> dict[str, Any]:
return {
"Linter": self.linter,
"Code": self.code,
"Name": self.name,
"Fixable": self.fix.one_word,
"Preview": self.preview,
}
208 changes: 206 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ description = "Adopt Ruff faster"
# readme = "README.md" # TODO

[tool.poetry.dependencies]
python = "^3.10"
python = "^3.11"
pydantic = "^2.5.3"
ruff = "^0.1.9"
tabulate = "^0.9.0"
mdutils = "^1.6.0"


[tool.poetry.group.dev.dependencies]
pre-commit = "^3.6.0"

[tool.ruff]
target-version = "py311"
select = ["ALL"]
ignore = [
"ANN",
Expand All @@ -34,6 +37,8 @@ ignore = [
"TD003",
"TD004",
"FIX002",
"EM101",
"FBT001",
]
[build-system]
requires = ["poetry-core"]
Expand Down
Loading

0 comments on commit 366a946

Please sign in to comment.