Skip to content

Commit

Permalink
feat(manager/poetry): support git rev dependencies (#26367)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
3 people committed Jan 4, 2024
1 parent 5130d5b commit 72fe67d
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 23 deletions.
103 changes: 103 additions & 0 deletions lib/modules/manager/poetry/extract.spec.ts
@@ -1,8 +1,10 @@
import { codeBlock } from 'common-tags';
import { Fixtures } from '../../../../test/fixtures';
import { fs } from '../../../../test/util';
import { GitRefsDatasource } from '../../datasource/git-refs';
import { GithubReleasesDatasource } from '../../datasource/github-releases';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { PypiDatasource } from '../../datasource/pypi';
import { extractPackageFile } from '.';

jest.mock('../../../util/fs');
Expand Down Expand Up @@ -169,6 +171,107 @@ describe('modules/manager/poetry/extract', () => {
});
});

it('parses git dependencies long commit hashs on http urls', async () => {
const content = codeBlock`
[tool.poetry.dependencies]
fastapi = {git = "https://github.com/tiangolo/fastapi.git", rev="6f5aa81c076d22e38afbe7d602db6730e28bc3cc"}
dep = "^2.0"
`;
const res = await extractPackageFile(content, filename);
expect(res?.deps).toMatchObject([
{
depType: 'dependencies',
depName: 'fastapi',
datasource: GitRefsDatasource.id,
currentDigest: '6f5aa81c076d22e38afbe7d602db6730e28bc3cc',
replaceString: '6f5aa81c076d22e38afbe7d602db6730e28bc3cc',
packageName: 'https://github.com/tiangolo/fastapi.git',
},
{
depType: 'dependencies',
depName: 'dep',
datasource: PypiDatasource.id,
currentValue: '^2.0',
},
]);
});

it('parses git dependencies short commit hashs on http urls', async () => {
const content = codeBlock`
[tool.poetry.dependencies]
fastapi = {git = "https://github.com/tiangolo/fastapi.git", rev="6f5aa81"}
dep = "^2.0"
`;
const res = await extractPackageFile(content, filename);
expect(res?.deps).toMatchObject([
{
depType: 'dependencies',
depName: 'fastapi',
datasource: GitRefsDatasource.id,
currentDigest: '6f5aa81',
replaceString: '6f5aa81',
packageName: 'https://github.com/tiangolo/fastapi.git',
},
{
depType: 'dependencies',
depName: 'dep',
datasource: PypiDatasource.id,
currentValue: '^2.0',
},
]);
});

it('parses git dependencies long commit hashs on ssh urls', async () => {
const content = codeBlock`
[tool.poetry.dependencies]
fastapi = {git = "git@github.com:tiangolo/fastapi.git", rev="6f5aa81c076d22e38afbe7d602db6730e28bc3cc"}
dep = "^2.0"
`;
const res = await extractPackageFile(content, filename);
expect(res?.deps).toMatchObject([
{
depType: 'dependencies',
depName: 'fastapi',
datasource: GitRefsDatasource.id,
currentDigest: '6f5aa81c076d22e38afbe7d602db6730e28bc3cc',
replaceString: '6f5aa81c076d22e38afbe7d602db6730e28bc3cc',
packageName: 'git@github.com:tiangolo/fastapi.git',
},
{
depType: 'dependencies',
depName: 'dep',
datasource: PypiDatasource.id,
currentValue: '^2.0',
},
]);
});

it('parses git dependencies long commit hashs on http urls with branch marker', async () => {
const content = codeBlock`
[tool.poetry.dependencies]
fastapi = {git = "https://github.com/tiangolo/fastapi.git", branch="develop", rev="6f5aa81c076d22e38afbe7d602db6730e28bc3cc"}
dep = "^2.0"
`;
const res = await extractPackageFile(content, filename);
expect(res?.deps).toMatchObject([
{
depType: 'dependencies',
depName: 'fastapi',
datasource: GitRefsDatasource.id,
currentValue: 'develop',
currentDigest: '6f5aa81c076d22e38afbe7d602db6730e28bc3cc',
replaceString: '6f5aa81c076d22e38afbe7d602db6730e28bc3cc',
packageName: 'https://github.com/tiangolo/fastapi.git',
},
{
depType: 'dependencies',
depName: 'dep',
datasource: PypiDatasource.id,
currentValue: '^2.0',
},
]);
});

it('parses github dependencies tags on ssh urls', async () => {
const content = codeBlock`
[tool.poetry.dependencies]
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/manager/poetry/index.ts
@@ -1,4 +1,5 @@
import type { Category } from '../../../constants';
import { GitRefsDatasource } from '../../datasource/git-refs';
import { GithubReleasesDatasource } from '../../datasource/github-releases';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { PypiDatasource } from '../../datasource/pypi';
Expand All @@ -12,6 +13,7 @@ export const supportedDatasources = [
PypiDatasource.id,
GithubTagsDatasource.id,
GithubReleasesDatasource.id,
GitRefsDatasource.id,
];

export const supportsLockFileMaintenance = true;
Expand Down
58 changes: 35 additions & 23 deletions lib/modules/manager/poetry/schema.ts
Expand Up @@ -7,6 +7,7 @@ import { uniq } from '../../../util/uniq';
import { GitRefsDatasource } from '../../datasource/git-refs';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { PypiDatasource } from '../../datasource/pypi';
import * as gitVersioning from '../../versioning/git';
import * as pep440Versioning from '../../versioning/pep440';
import * as poetryVersioning from '../../versioning/poetry';
import { dependencyPattern } from '../pip_requirements/extract';
Expand Down Expand Up @@ -35,39 +36,45 @@ const PoetryGitDependency = z
git: z.string(),
tag: z.string().optional().catch(undefined),
version: z.string().optional().catch(undefined),
branch: z.string().optional().catch(undefined),
rev: z.string().optional().catch(undefined),
})
.transform(({ git, tag, version }): PackageDependency => {
if (!tag) {
const res: PackageDependency = {
datasource: GitRefsDatasource.id,
packageName: git,
skipReason: 'git-dependency',
};

if (version) {
res.currentValue = version;
.transform(({ git, tag, version, branch, rev }): PackageDependency => {
if (tag) {
const { source, owner, name } = parseGitUrl(git);
if (source === 'github.com') {
const repo = `${owner}/${name}`;
return {
datasource: GithubTagsDatasource.id,
currentValue: tag,
packageName: repo,
};
} else {
return {
datasource: GitRefsDatasource.id,
currentValue: tag,
packageName: git,
skipReason: 'git-dependency',
};
}

return res;
}

const parsedUrl = parseGitUrl(git);
if (parsedUrl.source !== 'github.com') {
if (rev) {
return {
datasource: GitRefsDatasource.id,
currentValue: branch,
currentDigest: rev,
replaceString: rev,
packageName: git,
};
} else {
return {
datasource: GitRefsDatasource.id,
currentValue: tag,
currentValue: version,
packageName: git,
skipReason: 'git-dependency',
};
}

const { owner, name } = parsedUrl;
const repo = `${owner}/${name}`;
return {
datasource: GithubTagsDatasource.id,
currentValue: tag,
packageName: repo,
};
});

const PoetryPypiDependency = z.union([
Expand Down Expand Up @@ -114,6 +121,11 @@ export const PoetryDependencies = LooseRecord(
return dep;
}

if (dep.datasource === GitRefsDatasource.id && dep.currentDigest) {
dep.versioning = gitVersioning.id;
return dep;
}

// istanbul ignore if: normaly should not happen
if (!dep.currentValue) {
dep.skipReason = 'unspecified-version';
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/manager/poetry/types.ts
Expand Up @@ -22,6 +22,8 @@ export interface PoetryDependency {
git?: string;
tag?: string;
version?: string;
branch?: string;
rev?: string;
}

export interface PoetrySource {
Expand Down

0 comments on commit 72fe67d

Please sign in to comment.