Skip to content

feat(pre-commit): support python additional_dependencies #33417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2025
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
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;
}
Loading