Skip to content

Commit

Permalink
simplify external references addition
Browse files Browse the repository at this point in the history
fixes #331

Signed-off-by: Hervé Boutemy <[email protected]>
  • Loading branch information
hboutemy committed Apr 16, 2023
1 parent 6574473 commit 25dbd4e
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions src/main/java/org/cyclonedx/maven/DefaultModelConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ public class DefaultModelConverter implements ModelConverter {
public DefaultModelConverter() {
}

@Override
public String generatePackageUrl(final Artifact artifact) {
return generatePackageUrl(artifact, true);
}

@Override
public String generateVersionlessPackageUrl(final Artifact artifact) {
return generatePackageUrl(artifact, false);
}
Expand All @@ -104,14 +106,17 @@ private String generatePackageUrl(final Artifact artifact, final boolean include
return generatePackageUrl(artifact.getGroupId(), artifact.getArtifactId(), version, qualifiers, null);
}

@Override
public String generatePackageUrl(final org.eclipse.aether.artifact.Artifact artifact) {
return generatePackageUrl(artifact, true, true);
}

@Override
public String generateVersionlessPackageUrl(final org.eclipse.aether.artifact.Artifact artifact) {
return generatePackageUrl(artifact, false, true);
}

@Override
public String generateClassifierlessPackageUrl(final org.eclipse.aether.artifact.Artifact artifact) {
return generatePackageUrl(artifact, true, false);
}
Expand Down Expand Up @@ -147,6 +152,7 @@ private String generatePackageUrl(String groupId, String artifactId, String vers
return null;
}

@Override
public Component convert(Artifact artifact, CycloneDxSchema.Version schemaVersion, boolean includeLicenseText) {
final Component component = new Component();
component.setGroup(artifact.getGroupId());
Expand Down Expand Up @@ -221,48 +227,30 @@ private void extractComponentMetadata(MavenProject project, Component component,
}
}
if (CycloneDxSchema.Version.VERSION_10 != schemaVersion) {
if (project.getUrl() != null) {
if (!doesComponentHaveExternalReference(component, ExternalReference.Type.WEBSITE)) {
addExternalReference(ExternalReference.Type.WEBSITE, project.getUrl(), component);
}
addExternalReference(ExternalReference.Type.WEBSITE, project.getUrl(), component);
if (project.getCiManagement() != null) {
addExternalReference(ExternalReference.Type.BUILD_SYSTEM, project.getCiManagement().getUrl(), component);
}
if (project.getCiManagement() != null && project.getCiManagement().getUrl() != null) {
if (!doesComponentHaveExternalReference(component, ExternalReference.Type.BUILD_SYSTEM)) {
addExternalReference(ExternalReference.Type.BUILD_SYSTEM, project.getCiManagement().getUrl(), component);
}
}
if (project.getDistributionManagement() != null && project.getDistributionManagement().getDownloadUrl() != null) {
if (!doesComponentHaveExternalReference(component, ExternalReference.Type.DISTRIBUTION)) {
addExternalReference(ExternalReference.Type.DISTRIBUTION, project.getDistributionManagement().getDownloadUrl(), component);
}
}
if (project.getDistributionManagement() != null && project.getDistributionManagement().getRepository() != null) {
if (!doesComponentHaveExternalReference(component, ExternalReference.Type.DISTRIBUTION)) {
if (project.getDistributionManagement() != null) {
addExternalReference(ExternalReference.Type.DISTRIBUTION, project.getDistributionManagement().getDownloadUrl(), component);
if (project.getDistributionManagement().getRepository() != null) {
addExternalReference(ExternalReference.Type.DISTRIBUTION, project.getDistributionManagement().getRepository().getUrl(), component);
}
}
if (project.getIssueManagement() != null && project.getIssueManagement().getUrl() != null) {
if (!doesComponentHaveExternalReference(component, ExternalReference.Type.ISSUE_TRACKER)) {
addExternalReference(ExternalReference.Type.ISSUE_TRACKER, project.getIssueManagement().getUrl(), component);
}
if (project.getIssueManagement() != null) {
addExternalReference(ExternalReference.Type.ISSUE_TRACKER, project.getIssueManagement().getUrl(), component);
}
if (project.getMailingLists() != null && project.getMailingLists().size() > 0) {
for (MailingList list : project.getMailingLists()) {
if (list.getArchive() != null) {
if (!doesComponentHaveExternalReference(component, ExternalReference.Type.MAILING_LIST)) {
addExternalReference(ExternalReference.Type.MAILING_LIST, list.getArchive(), component);
}
} else if (list.getSubscribe() != null) {
if (!doesComponentHaveExternalReference(component, ExternalReference.Type.MAILING_LIST)) {
addExternalReference(ExternalReference.Type.MAILING_LIST, list.getSubscribe(), component);
}
String url = list.getArchive();
if (url == null) {
url = list.getSubscribe();
}
addExternalReference(ExternalReference.Type.MAILING_LIST, url, component);
}
}
if (project.getScm() != null && project.getScm().getUrl() != null) {
if (!doesComponentHaveExternalReference(component, ExternalReference.Type.VCS)) {
addExternalReference(ExternalReference.Type.VCS, project.getScm().getUrl(), component);
}
if (project.getScm() != null) {
addExternalReference(ExternalReference.Type.VCS, project.getScm().getUrl(), component);
}
}
}
Expand All @@ -281,6 +269,9 @@ private MavenProject getEffectiveMavenProject(final Artifact artifact) throws Pr
}

private void addExternalReference(final ExternalReference.Type referenceType, final String url, final Component component) {
if (url == null || doesComponentHaveExternalReference(component, referenceType)) {
return;
}
try {
final URI uri = new URI(url.trim());
final ExternalReference ref = new ExternalReference();
Expand All @@ -292,10 +283,10 @@ private void addExternalReference(final ExternalReference.Type referenceType, fi
}
}

private boolean doesComponentHaveExternalReference(final Component component, final ExternalReference.Type type) {
private boolean doesComponentHaveExternalReference(final Component component, final ExternalReference.Type referenceType) {
if (component.getExternalReferences() != null && !component.getExternalReferences().isEmpty()) {
for (final ExternalReference ref : component.getExternalReferences()) {
if (type == ref.getType()) {
if (referenceType == ref.getType()) {
return true;
}
}
Expand Down

0 comments on commit 25dbd4e

Please sign in to comment.