Skip to content
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

feat(manager/poetry): Support tag as git-dependency on github #21949

Merged
merged 7 commits into from May 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 75 additions & 10 deletions lib/modules/manager/poetry/extract.spec.ts
@@ -1,5 +1,7 @@
import { codeBlock } from 'common-tags';
import { Fixtures } from '../../../../test/fixtures';
import { fs } from '../../../../test/util';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { extractPackageFile } from '.';

jest.mock('../../../util/fs');
Expand Down Expand Up @@ -141,8 +143,17 @@ describe('modules/manager/poetry/extract', () => {
});

it('extracts dependencies from dependency groups', async () => {
const content =
'[tool.poetry.dependencies]\ndep = "^2.0"\n\n[tool.poetry.group.dev.dependencies]\ndev_dep = "^3.0"\n\n[tool.poetry.group.typing.dependencies]\ntyping_dep = "^4.0"';
const content = codeBlock`
[tool.poetry.dependencies]
dep = "^2.0"


[tool.poetry.group.dev.dependencies]
dev_dep = "^3.0"

[tool.poetry.group.typing.dependencies]
typing_dep = "^4.0"
`;
const res = await extractPackageFile(content, filename);
expect(res?.deps).toMatchObject([
{
Expand Down Expand Up @@ -177,9 +188,42 @@ describe('modules/manager/poetry/extract', () => {
});
});

it('parses github dependencies tags on ssh urls', async () => {
const content = codeBlock`
[tool.poetry.dependencies]
fastapi = {git = "git@github.com:tiangolo/fastapi.git", tag="1.2.3"}
werkzeug = ">=0.14"
`;
const res = (await extractPackageFile(content, filename))!.deps;
expect(res[0].depName).toBe('fastapi');
expect(res[0].packageName).toBe('tiangolo/fastapi');
expect(res[0].currentValue).toBe('1.2.3');
expect(res[0].skipReason).toBeUndefined();
expect(res[0].datasource).toBe(GithubTagsDatasource.id);
expect(res).toHaveLength(2);
});

it('parses github dependencies tags on http urls', async () => {
const content = codeBlock`
[tool.poetry.dependencies]
fastapi = {git = "https://github.com/tiangolo/fastapi.git", tag="1.2.3"}
werkzeug = ">=0.14"
`;
const res = (await extractPackageFile(content, filename))!.deps;
expect(res[0].depName).toBe('fastapi');
expect(res[0].packageName).toBe('tiangolo/fastapi');
expect(res[0].currentValue).toBe('1.2.3');
expect(res[0].skipReason).toBeUndefined();
expect(res[0].datasource).toBe(GithubTagsDatasource.id);
expect(res).toHaveLength(2);
});

it('skips git dependencies', async () => {
const content =
'[tool.poetry.dependencies]\r\nflask = {git = "https://github.com/pallets/flask.git"}\r\nwerkzeug = ">=0.14"';
const content = codeBlock`
[tool.poetry.dependencies]
flask = {git = "https://github.com/pallets/flask.git"}
werkzeug = ">=0.14"
`;
const res = (await extractPackageFile(content, filename))!.deps;
expect(res[0].depName).toBe('flask');
expect(res[0].currentValue).toBeEmptyString();
Expand All @@ -188,18 +232,36 @@ describe('modules/manager/poetry/extract', () => {
});

it('skips git dependencies with version', async () => {
const content =
'[tool.poetry.dependencies]\r\nflask = {git = "https://github.com/pallets/flask.git", version="1.2.3"}\r\nwerkzeug = ">=0.14"';
const content = codeBlock`
[tool.poetry.dependencies]
flask = {git = "https://github.com/pallets/flask.git", version="1.2.3"}
werkzeug = ">=0.14"
`;
const res = (await extractPackageFile(content, filename))!.deps;
expect(res[0].depName).toBe('flask');
expect(res[0].currentValue).toBe('1.2.3');
expect(res[0].skipReason).toBe('git-dependency');
expect(res).toHaveLength(2);
});

it('skips git dependencies on tags that are not in github', async () => {
const content = codeBlock`
[tool.poetry.dependencies]
aws-sam = {git = "https://gitlab.com/gitlab-examples/aws-sam.git", tag="1.2.3"}
`;
const res = (await extractPackageFile(content, filename))!.deps;
expect(res[0].depName).toBe('aws-sam');
expect(res[0].currentValue).toBe('1.2.3');
expect(res[0].skipReason).toBe('git-dependency');
expect(res).toHaveLength(1);
});

it('skips path dependencies', async () => {
const content =
'[tool.poetry.dependencies]\r\nflask = {path = "/some/path/"}\r\nwerkzeug = ">=0.14"';
const content = codeBlock`
[tool.poetry.dependencies]
flask = {path = "/some/path/"}
werkzeug = ">=0.14"
`;
const res = (await extractPackageFile(content, filename))!.deps;
expect(res[0].depName).toBe('flask');
expect(res[0].currentValue).toBe('');
Expand All @@ -208,8 +270,11 @@ describe('modules/manager/poetry/extract', () => {
});

it('skips path dependencies with version', async () => {
const content =
'[tool.poetry.dependencies]\r\nflask = {path = "/some/path/", version = "1.2.3"}\r\nwerkzeug = ">=0.14"';
const content = codeBlock`
[tool.poetry.dependencies]
flask = {path = "/some/path/", version = "1.2.3"}
werkzeug = ">=0.14"
`;
const res = (await extractPackageFile(content, filename))!.deps;
expect(res[0].depName).toBe('flask');
expect(res[0].currentValue).toBe('1.2.3');
Expand Down
40 changes: 32 additions & 8 deletions lib/modules/manager/poetry/extract.ts
Expand Up @@ -7,7 +7,9 @@ import {
localPathExists,
readLocalFile,
} from '../../../util/fs';
import { parseGitUrl } from '../../../util/git/url';
import { regEx } from '../../../util/regex';
import { GithubTagsDatasource } from '../../datasource/github-tags';
yetanotherion marked this conversation as resolved.
Show resolved Hide resolved
import { PypiDatasource } from '../../datasource/pypi';
import * as pep440Versioning from '../../versioning/pep440';
import * as poetryVersioning from '../../versioning/poetry';
Expand Down Expand Up @@ -56,12 +58,15 @@ function extractFromSection(
}

const pep503NormalizeRegex = regEx(/[-_.]+/g);
const packageName = depName
.toLowerCase()
.replace(pep503NormalizeRegex, '-');
let packageName = depName.toLowerCase().replace(pep503NormalizeRegex, '-');
let skipReason: SkipReason | null = null;
let currentValue = sectionContent[depName];
let nestedVersion = false;
let datasource = PypiDatasource.id;
let lockedVersion: string | null = null;
if (packageName in poetryLockfile) {
lockedVersion = poetryLockfile[packageName];
}
rarkins marked this conversation as resolved.
Show resolved Hide resolved
if (!is.string(currentValue)) {
const version = currentValue.version;
const path = currentValue.path;
Expand All @@ -76,8 +81,19 @@ function extractFromSection(
currentValue = '';
skipReason = 'path-dependency';
} else if (git) {
currentValue = '';
skipReason = 'git-dependency';
if (currentValue.tag) {
currentValue = currentValue.tag;
datasource = GithubTagsDatasource.id;
const githubPackageName = extractGithubPackageName(git);
if (githubPackageName) {
packageName = githubPackageName;
} else {
skipReason = 'git-dependency';
}
} else {
currentValue = '';
skipReason = 'git-dependency';
}
} else {
currentValue = '';
skipReason = 'multiple-constraint-dep';
Expand All @@ -88,10 +104,10 @@ function extractFromSection(
depType,
currentValue,
managerData: { nestedVersion },
datasource: PypiDatasource.id,
datasource,
};
if (packageName in poetryLockfile) {
dep.lockedVersion = poetryLockfile[packageName];
if (lockedVersion) {
dep.lockedVersion = lockedVersion;
}
if (depName !== packageName) {
dep.packageName = packageName;
Expand Down Expand Up @@ -199,3 +215,11 @@ export async function extractPackageFile(
}
return res;
}

function extractGithubPackageName(url: string): string | null {
const parsedUrl = parseGitUrl(url);
if (parsedUrl.source !== 'github.com') {
return null;
}
return `${parsedUrl.owner}/${parsedUrl.name}`;
}
6 changes: 5 additions & 1 deletion lib/modules/manager/poetry/index.ts
@@ -1,11 +1,15 @@
import type { ProgrammingLanguage } from '../../../constants';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { PypiDatasource } from '../../datasource/pypi';

export { extractPackageFile } from './extract';
export { updateArtifacts } from './artifacts';
export { updateLockedDependency } from './update-locked';

export const supportedDatasources = [PypiDatasource.id];
export const supportedDatasources = [
PypiDatasource.id,
GithubTagsDatasource.id,
];

export const language: ProgrammingLanguage = 'python';
export const supportsLockFileMaintenance = true;
Expand Down
1 change: 1 addition & 0 deletions lib/modules/manager/poetry/types.ts
Expand Up @@ -20,6 +20,7 @@ export interface PoetryFile {
export interface PoetryDependency {
path?: string;
git?: string;
tag?: string;
version?: string;
}

Expand Down