Skip to content

Commit

Permalink
Merge pull request #4 from zharinov/feature_6742_mvn_lookup_parents_n…
Browse files Browse the repository at this point in the history
…ull_guards

Handle empty fields
  • Loading branch information
twendelmuth committed May 27, 2021
2 parents fb6cde8 + d257e11 commit 6655340
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 17 deletions.
11 changes: 11 additions & 0 deletions lib/datasource/maven/__fixtures__/child-empty/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?><metadata>
<groupId>org.example</groupId>
<artifactId>child-no-info</artifactId>
<version>2.0.0</version>
<versioning>
<versions>
<version>2.0.0</version>
</versions>
<lastUpdated>20130301200000</lastUpdated>
</versioning>
</metadata>
4 changes: 4 additions & 0 deletions lib/datasource/maven/__fixtures__/child-empty/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
</parent>
</project>
32 changes: 32 additions & 0 deletions lib/datasource/maven/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,38 @@ Array [
]
`;

exports[`datasource/maven/index fetching parent info should deal with missing parent fields 1`] = `
Array [
Object {
"headers": Object {
"accept-encoding": "gzip, deflate, br",
"host": "repo.maven.apache.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://repo.maven.apache.org/maven2/org/example/package/maven-metadata.xml",
},
Object {
"headers": Object {
"accept-encoding": "gzip, deflate, br",
"host": "repo.maven.apache.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "HEAD",
"url": "https://repo.maven.apache.org/maven2/org/example/package/2.0.0/package-2.0.0.pom",
},
Object {
"headers": Object {
"accept-encoding": "gzip, deflate, br",
"host": "repo.maven.apache.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://repo.maven.apache.org/maven2/org/example/package/2.0.0/package-2.0.0.pom",
},
]
`;

exports[`datasource/maven/index fetching parent info should get homepage and source from own pom 1`] = `
Array [
Object {
Expand Down
20 changes: 20 additions & 0 deletions lib/datasource/maven/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,26 @@ describe(getName(), () => {
expect(httpMock.getTrace()).toMatchSnapshot();
});

it('should deal with missing parent fields', async () => {
mockGenericPackage({
meta: loadFixture('child-empty/meta.xml'),
pom: loadFixture('child-empty/pom.xml'),
latest: '2.0.0',
jars: { '2.0.0': 200 },
});

const res = await get();

expect(res).toMatchObject({
display: 'org.example:package',
group: 'org.example',
name: 'package',
});
expect(res).not.toHaveProperty('homepage');
expect(res).not.toHaveProperty('sourceUrl');
expect(httpMock.getTrace()).toMatchSnapshot();
});

it('should deal with circular hierarchy', async () => {
const parentPom = loadFixture('child-parent-cycle/parent.pom.xml');
const parentPomMock = {
Expand Down
37 changes: 20 additions & 17 deletions lib/datasource/maven/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,23 +227,26 @@ export async function getDependencyInfo(
if (recursionLimit > 0 && parent && (!result.sourceUrl || !result.homepage)) {
// if we found a parent and are missing some information
// trying to get the scm/homepage information from it
const parentGroupId = parent.valueWithPath('groupId').replace(/\s/g, '');
const parentArtifactId = parent.valueWithPath('artifactId').replace(/\s/g, ''); // prettier-ignore
const parentVersion = parent.valueWithPath('version').replace(/\s/g, '');
const parentDisplayId = `${parentGroupId}:${parentArtifactId}`;
const parentDependency = getDependencyParts(parentDisplayId);

const parentInformation = await getDependencyInfo(
parentDependency,
repoUrl,
parentVersion,
recursionLimit - 1
);
if (!result.sourceUrl && parentInformation.sourceUrl) {
result.sourceUrl = parentInformation.sourceUrl;
}
if (!result.homepage && parentInformation.homepage) {
result.homepage = parentInformation.homepage;
const [parentGroupId, parentArtifactId, parentVersion] = [
'groupId',
'artifactId',
'version',
].map((k) => parent.valueWithPath(k)?.replace(/\s+/g, ''));
if (parentGroupId && parentArtifactId && parentVersion) {
const parentDisplayId = `${parentGroupId}:${parentArtifactId}`;
const parentDependency = getDependencyParts(parentDisplayId);
const parentInformation = await getDependencyInfo(
parentDependency,
repoUrl,
parentVersion,
recursionLimit - 1
);
if (!result.sourceUrl && parentInformation.sourceUrl) {
result.sourceUrl = parentInformation.sourceUrl;
}
if (!result.homepage && parentInformation.homepage) {
result.homepage = parentInformation.homepage;
}
}
}

Expand Down

0 comments on commit 6655340

Please sign in to comment.