Skip to content
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
18 changes: 18 additions & 0 deletions docs/pages/guides/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ ci:

The frequencies can be "weekly" (the default), "monthly", and "quarterly".

If you prefer, you can use Dependabot, which will also auto-update, though it
doesn't run the checks. The config looks like this:

```yaml
version: 2
updates:
- package-ecosystem: "pre-commit"
directory: "/"
schedule:
interval: "monthly"
groups:
pre-commit:
patterns:
- "*"
cooldown:
default-days: 7
```

## Format

{% rr PC110 %} [Black](https://black.readthedocs.io/en/latest/) is a popular
Expand Down
21 changes: 18 additions & 3 deletions src/sp_repo_review/checks/precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class PC901(PreCommit):
"Custom pre-commit CI update message"

@staticmethod
def check(precommit: dict[str, Any]) -> bool:
def check(precommit: dict[str, Any], dependabot: dict[str, Any]) -> bool | None: # type: ignore[override]
"""
Should have something like this in `.pre-commit-config.yaml`:

Expand All @@ -213,6 +213,11 @@ def check(precommit: dict[str, Any]) -> bool:
autoupdate_commit_msg: 'chore(deps): update pre-commit hooks'
```
"""
if any(
ecosystem.get("package-ecosystem", "") == "github-actions"
for ecosystem in dependabot.get("updates", [])
):
return None

return "autoupdate_commit_msg" in precommit.get("ci", {})

Expand All @@ -221,7 +226,7 @@ class PC902(PreCommit):
"Custom pre-commit CI autofix message"

@staticmethod
def check(precommit: dict[str, Any]) -> bool:
def check(precommit: dict[str, Any], dependabot: dict[str, Any]) -> bool | None: # type: ignore[override]
"""
Should have something like this in `.pre-commit-config.yaml`:

Expand All @@ -230,6 +235,11 @@ def check(precommit: dict[str, Any]) -> bool:
autofix_commit_msg: "style: pre-commit fixes"
```
"""
if any(
ecosystem.get("package-ecosystem", "") == "github-actions"
for ecosystem in dependabot.get("updates", [])
):
return None

return "autofix_commit_msg" in precommit.get("ci", {})

Expand All @@ -238,7 +248,7 @@ class PC903(PreCommit):
"Specified pre-commit CI schedule"

@staticmethod
def check(precommit: dict[str, Any]) -> bool:
def check(precommit: dict[str, Any], dependabot: dict[str, Any]) -> bool | None: # type: ignore[override]
"""
Should set some schedule: `weekly` (default), `monthly`, or `quarterly`.

Expand All @@ -247,6 +257,11 @@ def check(precommit: dict[str, Any]) -> bool:
autoupdate_schedule: "monthly"
```
"""
if any(
ecosystem.get("package-ecosystem", "") == "github-actions"
for ecosystem in dependabot.get("updates", [])
):
return None

return "autoupdate_schedule" in precommit.get("ci", {})

Expand Down
36 changes: 30 additions & 6 deletions tests/test_precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,22 @@ def test_pc901():
ci:
autoupdate_commit_msg: 'chore: update pre-commit hooks'
""")
assert compute_check("PC901", precommit=precommit).result
assert compute_check("PC901", precommit=precommit, dependabot={}).result


def test_pc901_not_needed():
dependabot = yaml.safe_load("""
updates:
- package-ecosystem: "github-actions"
""")
assert compute_check("PC901", precommit={}, dependabot=dependabot).result is None


def test_pc901_no_msg():
precommit = yaml.safe_load("""
repos:
""")
res = compute_check("PC901", precommit=precommit)
res = compute_check("PC901", precommit=precommit, dependabot={})
assert not res.result
assert "autoupdate_commit_msg" in res.err_msg

Expand All @@ -260,36 +268,52 @@ def test_pc902():
ci:
autofix_commit_msg: 'style: pre-commit fixes'
""")
assert compute_check("PC902", precommit=precommit).result
assert compute_check("PC902", precommit=precommit, dependabot={}).result


def test_pc902_no_msg():
precommit = yaml.safe_load("""
repos:
""")
res = compute_check("PC902", precommit=precommit)
res = compute_check("PC902", precommit=precommit, dependabot={})
assert not res.result
assert "autofix_commit_msg" in res.err_msg


def test_pc902_not_needed():
dependabot = yaml.safe_load("""
updates:
- package-ecosystem: "github-actions"
""")
assert compute_check("PC902", precommit={}, dependabot=dependabot).result is None


def test_pc903():
precommit = yaml.safe_load("""
ci:
autoupdate_schedule: "monthly"

""")
assert compute_check("PC903", precommit=precommit).result
assert compute_check("PC903", precommit=precommit, dependabot={}).result


def test_pc903_no_msg():
precommit = yaml.safe_load("""
repos:
""")
res = compute_check("PC903", precommit=precommit)
res = compute_check("PC903", precommit=precommit, dependabot={})
assert not res.result
assert "autoupdate_schedule" in res.err_msg


def test_pc903_not_needed():
dependabot = yaml.safe_load("""
updates:
- package-ecosystem: "github-actions"
""")
assert compute_check("PC903", precommit={}, dependabot=dependabot).result is None


def test_repo_review_checks_skips_with_lefthook_only(tmp_path: Path) -> None:
"""PreCommit checks should be omitted if only lefthook.yml is present.

Expand Down
Loading