Skip to content

Commit e2362b3

Browse files
authored
fix: disable pre-commit.ci checks if using dependabot pre-commit (#758)
* fix: disable pre-commit.ci checks if using dependabot pre-commit Signed-off-by: Henry Schreiner <henryfs@princeton.edu> * docs: also mention in guide Signed-off-by: Henry Schreiner <henryfs@princeton.edu> --------- Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
1 parent 77746d7 commit e2362b3

3 files changed

Lines changed: 66 additions & 9 deletions

File tree

docs/pages/guides/style.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ ci:
108108

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

111+
If you prefer, you can use Dependabot, which will also auto-update, though it
112+
doesn't run the checks. The config looks like this:
113+
114+
```yaml
115+
version: 2
116+
updates:
117+
- package-ecosystem: "pre-commit"
118+
directory: "/"
119+
schedule:
120+
interval: "monthly"
121+
groups:
122+
pre-commit:
123+
patterns:
124+
- "*"
125+
cooldown:
126+
default-days: 7
127+
```
128+
111129
## Format
112130

113131
{% rr PC110 %} [Black](https://black.readthedocs.io/en/latest/) is a popular

src/sp_repo_review/checks/precommit.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class PC901(PreCommit):
204204
"Custom pre-commit CI update message"
205205

206206
@staticmethod
207-
def check(precommit: dict[str, Any]) -> bool:
207+
def check(precommit: dict[str, Any], dependabot: dict[str, Any]) -> bool | None: # type: ignore[override]
208208
"""
209209
Should have something like this in `.pre-commit-config.yaml`:
210210
@@ -213,6 +213,11 @@ def check(precommit: dict[str, Any]) -> bool:
213213
autoupdate_commit_msg: 'chore(deps): update pre-commit hooks'
214214
```
215215
"""
216+
if any(
217+
ecosystem.get("package-ecosystem", "") == "github-actions"
218+
for ecosystem in dependabot.get("updates", [])
219+
):
220+
return None
216221

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

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

223228
@staticmethod
224-
def check(precommit: dict[str, Any]) -> bool:
229+
def check(precommit: dict[str, Any], dependabot: dict[str, Any]) -> bool | None: # type: ignore[override]
225230
"""
226231
Should have something like this in `.pre-commit-config.yaml`:
227232
@@ -230,6 +235,11 @@ def check(precommit: dict[str, Any]) -> bool:
230235
autofix_commit_msg: "style: pre-commit fixes"
231236
```
232237
"""
238+
if any(
239+
ecosystem.get("package-ecosystem", "") == "github-actions"
240+
for ecosystem in dependabot.get("updates", [])
241+
):
242+
return None
233243

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

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

240250
@staticmethod
241-
def check(precommit: dict[str, Any]) -> bool:
251+
def check(precommit: dict[str, Any], dependabot: dict[str, Any]) -> bool | None: # type: ignore[override]
242252
"""
243253
Should set some schedule: `weekly` (default), `monthly`, or `quarterly`.
244254
@@ -247,6 +257,11 @@ def check(precommit: dict[str, Any]) -> bool:
247257
autoupdate_schedule: "monthly"
248258
```
249259
"""
260+
if any(
261+
ecosystem.get("package-ecosystem", "") == "github-actions"
262+
for ecosystem in dependabot.get("updates", [])
263+
):
264+
return None
250265

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

tests/test_precommit.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,22 @@ def test_pc901():
243243
ci:
244244
autoupdate_commit_msg: 'chore: update pre-commit hooks'
245245
""")
246-
assert compute_check("PC901", precommit=precommit).result
246+
assert compute_check("PC901", precommit=precommit, dependabot={}).result
247+
248+
249+
def test_pc901_not_needed():
250+
dependabot = yaml.safe_load("""
251+
updates:
252+
- package-ecosystem: "github-actions"
253+
""")
254+
assert compute_check("PC901", precommit={}, dependabot=dependabot).result is None
247255

248256

249257
def test_pc901_no_msg():
250258
precommit = yaml.safe_load("""
251259
repos:
252260
""")
253-
res = compute_check("PC901", precommit=precommit)
261+
res = compute_check("PC901", precommit=precommit, dependabot={})
254262
assert not res.result
255263
assert "autoupdate_commit_msg" in res.err_msg
256264

@@ -260,36 +268,52 @@ def test_pc902():
260268
ci:
261269
autofix_commit_msg: 'style: pre-commit fixes'
262270
""")
263-
assert compute_check("PC902", precommit=precommit).result
271+
assert compute_check("PC902", precommit=precommit, dependabot={}).result
264272

265273

266274
def test_pc902_no_msg():
267275
precommit = yaml.safe_load("""
268276
repos:
269277
""")
270-
res = compute_check("PC902", precommit=precommit)
278+
res = compute_check("PC902", precommit=precommit, dependabot={})
271279
assert not res.result
272280
assert "autofix_commit_msg" in res.err_msg
273281

274282

283+
def test_pc902_not_needed():
284+
dependabot = yaml.safe_load("""
285+
updates:
286+
- package-ecosystem: "github-actions"
287+
""")
288+
assert compute_check("PC902", precommit={}, dependabot=dependabot).result is None
289+
290+
275291
def test_pc903():
276292
precommit = yaml.safe_load("""
277293
ci:
278294
autoupdate_schedule: "monthly"
279295
280296
""")
281-
assert compute_check("PC903", precommit=precommit).result
297+
assert compute_check("PC903", precommit=precommit, dependabot={}).result
282298

283299

284300
def test_pc903_no_msg():
285301
precommit = yaml.safe_load("""
286302
repos:
287303
""")
288-
res = compute_check("PC903", precommit=precommit)
304+
res = compute_check("PC903", precommit=precommit, dependabot={})
289305
assert not res.result
290306
assert "autoupdate_schedule" in res.err_msg
291307

292308

309+
def test_pc903_not_needed():
310+
dependabot = yaml.safe_load("""
311+
updates:
312+
- package-ecosystem: "github-actions"
313+
""")
314+
assert compute_check("PC903", precommit={}, dependabot=dependabot).result is None
315+
316+
293317
def test_repo_review_checks_skips_with_lefthook_only(tmp_path: Path) -> None:
294318
"""PreCommit checks should be omitted if only lefthook.yml is present.
295319

0 commit comments

Comments
 (0)