From f5e2ed5cfaabeb9d0aa1bbb4764d9ddc5a06c369 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 6 Jan 2025 06:27:49 +0800 Subject: [PATCH] feat: support pre-commit local python hooks additional_dependencies --- .../complex.pre-commit-config.yaml | 6 ++++++ lib/modules/manager/pre-commit/extract.ts | 19 +++++++++++++++++++ lib/modules/manager/pre-commit/types.ts | 6 ++++++ 3 files changed, 31 insertions(+) diff --git a/lib/modules/manager/pre-commit/__fixtures__/complex.pre-commit-config.yaml b/lib/modules/manager/pre-commit/__fixtures__/complex.pre-commit-config.yaml index 1d569aaa676f17b..199d78ba94edf79 100644 --- a/lib/modules/manager/pre-commit/__fixtures__/complex.pre-commit-config.yaml +++ b/lib/modules/manager/pre-commit/__fixtures__/complex.pre-commit-config.yaml @@ -13,11 +13,17 @@ repos: rev: 19.3b0 hooks: - id: black + language: python + additional_dependencies: + - "request==1.1.1" - repo: https://gitlab.com/psf/black # should also detect gitlab rev: 19.3b0 hooks: - id: black + # missing language, not extracted + additional_dependencies: + - "urllib==24.9.0" - repo: http://gitlab.com/psf/black # should also detect http rev: 19.3b0 diff --git a/lib/modules/manager/pre-commit/extract.ts b/lib/modules/manager/pre-commit/extract.ts index 8dc89e4e70a269f..67c46714d077df7 100644 --- a/lib/modules/manager/pre-commit/extract.ts +++ b/lib/modules/manager/pre-commit/extract.ts @@ -13,6 +13,7 @@ import { matchesPrecommitDependencyHeuristic, } from './parsing'; import type { PreCommitConfig } from './types'; +import { pep508ToPackageDependency } from '../pep621/utils'; /** * Determines the datasource(id) to be used for this dependency @@ -136,6 +137,24 @@ function findDependencies(precommitFile: PreCommitConfig): PackageDependency[] { return []; } const packageDependencies: PackageDependency[] = []; + precommitFile.repos.forEach((item) => { + if (item.repo !== 'meta') { + return; + } + + item.hooks?.forEach((hook) => { + // normally language are not defined in yaml + // only support it when it's explicitly python + if (hook.language === 'python') { + hook.additional_dependencies?.map((req) => { + const dep = pep508ToPackageDependency('pre-commit-python', req); + if (dep) { + packageDependencies.push(dep); + } + }); + } + }); + }); precommitFile.repos.forEach((item) => { if (matchesPrecommitDependencyHeuristic(item)) { logger.trace(item, 'Matched pre-commit dependency spec'); diff --git a/lib/modules/manager/pre-commit/types.ts b/lib/modules/manager/pre-commit/types.ts index dadcf068e61e975..8af0db9c81be190 100644 --- a/lib/modules/manager/pre-commit/types.ts +++ b/lib/modules/manager/pre-commit/types.ts @@ -2,7 +2,13 @@ export interface PreCommitConfig { repos: PreCommitDependency[]; } +export interface PreCommitHook { + language?: 'python' | string; + additional_dependencies?: Array; +} + export interface PreCommitDependency { repo: string; + hooks?: Array; rev: string; }