diff --git a/docs/pages/guides/style.md b/docs/pages/guides/style.md index 2e1e352e..f580c571 100644 --- a/docs/pages/guides/style.md +++ b/docs/pages/guides/style.md @@ -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 diff --git a/src/sp_repo_review/checks/precommit.py b/src/sp_repo_review/checks/precommit.py index d6c2ee5c..2ced9229 100644 --- a/src/sp_repo_review/checks/precommit.py +++ b/src/sp_repo_review/checks/precommit.py @@ -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`: @@ -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", {}) @@ -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`: @@ -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", {}) @@ -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`. @@ -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", {}) diff --git a/tests/test_precommit.py b/tests/test_precommit.py index 595c6bc3..88501447 100644 --- a/tests/test_precommit.py +++ b/tests/test_precommit.py @@ -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 @@ -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.