Skip to content

Commit

Permalink
feat(pre-commit): support python additional_dependencies (#33417)
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 authored Jan 8, 2025
1 parent 147b620 commit 59455c0
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ repos:
rev: 19.3b0
hooks:
- id: black
language: python
additional_dependencies:
- "request==1.1.1"
- "" # broken pypi package
- 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
Expand Down Expand Up @@ -48,3 +55,7 @@ repos:
- repo: some_invalid_url
# case with invlalid url.
rev: v1.0.0

# pre-commit meta hooks
- repo: meta
hooks: []
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ exports[`modules/manager/pre-commit/extract extractPackageFile() extracts from c
"depType": "repository",
"packageName": "pre-commit/pre-commit-hooks",
},
{
"currentValue": "==1.1.1",
"currentVersion": "1.1.1",
"datasource": "pypi",
"depName": "request",
"depType": "pre-commit-python",
"packageName": "request",
},
{
"currentValue": "19.3b0",
"datasource": "github-tags",
Expand Down
9 changes: 9 additions & 0 deletions lib/modules/manager/pre-commit/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mockDeep } from 'jest-mock-extended';
import { Fixtures } from '../../../../test/fixtures';
import { mocked } from '../../../../test/util';
import * as _hostRules from '../../../util/host-rules';
import { PypiDatasource } from '../../datasource/pypi';
import { extractPackageFile } from '.';

jest.mock('../../../util/host-rules', () => mockDeep());
Expand Down Expand Up @@ -81,6 +82,14 @@ describe('modules/manager/pre-commit/extract', () => {
expect(result).toMatchSnapshot({
deps: [
{ depName: 'pre-commit/pre-commit-hooks', currentValue: 'v3.3.0' },
{
currentValue: '==1.1.1',
currentVersion: '1.1.1',
datasource: PypiDatasource.id,
depName: 'request',
depType: 'pre-commit-python',
packageName: 'request',
},
{ depName: 'psf/black', currentValue: '19.3b0' },
{ depName: 'psf/black', currentValue: '19.3b0' },
{ depName: 'psf/black', currentValue: '19.3b0' },
Expand Down
18 changes: 18 additions & 0 deletions lib/modules/manager/pre-commit/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { regEx } from '../../../util/regex';
import { parseSingleYaml } from '../../../util/yaml';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { GitlabTagsDatasource } from '../../datasource/gitlab-tags';
import { pep508ToPackageDependency } from '../pep621/utils';
import type { PackageDependency, PackageFileContent } from '../types';
import {
matchesPrecommitConfigHeuristic,
Expand Down Expand Up @@ -137,6 +138,23 @@ function findDependencies(precommitFile: PreCommitConfig): PackageDependency[] {
}
const packageDependencies: PackageDependency[] = [];
precommitFile.repos.forEach((item) => {
// meta hooks is defined from pre-commit and doesn't support `additional_dependencies`
if (item.repo !== 'meta') {
item.hooks?.forEach((hook) => {
// normally language are not defined in yaml
// only support it when it's explicitly defined.
// this avoid to parse hooks from pre-commit-hooks.yaml from git repo
if (hook.language === 'python') {
hook.additional_dependencies?.map((req) => {
const dep = pep508ToPackageDependency('pre-commit-python', req);
if (dep) {
packageDependencies.push(dep);
}
});
}
});
}

if (matchesPrecommitDependencyHeuristic(item)) {
logger.trace(item, 'Matched pre-commit dependency spec');
const repository = String(item.repo);
Expand Down
30 changes: 30 additions & 0 deletions lib/modules/manager/pre-commit/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,33 @@ To enable the `pre-commit` manager, add the following config:
```

Alternatively, add `:enablePreCommit` to your `extends` array.

### Additional Dependencies

renovate has partial support for `additional_dependencies`, currently python only.

for python hooks, you will need to **explicitly add language** to your hooks with `additional_dependencies`
to let renovatebot know what kind of dependencies they are.

For example, this work for `request`:

```yaml
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black
language: python
additional_dependencies:
- 'request==1.1.1'
```

this won't work:

```yaml
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black
additional_dependencies:
- 'request==1.1.1'
```
6 changes: 6 additions & 0 deletions lib/modules/manager/pre-commit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ export interface PreCommitConfig {
repos: PreCommitDependency[];
}

export interface PreCommitHook {
language?: string;
additional_dependencies?: Array<string>;
}

export interface PreCommitDependency {
repo: string;
hooks?: Array<PreCommitHook>;
rev: string;
}

0 comments on commit 59455c0

Please sign in to comment.