Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
sw360(purl): using purl instead of coordinates
Browse files Browse the repository at this point in the history
    adding purl
    using purl instead of coordinates, removing coordinates from sw360 release
    change from purl to purls
    changed purl sw360 representation to map with purl type as key

Signed-off-by: Stephanie Neubauer <Stephanie.Neubauer@bosch-si.com>
  • Loading branch information
Stephanie Neubauer authored and Stephanie Neubauer committed Sep 24, 2019
1 parent ab4be97 commit c18c7ad
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 147 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.eclipse.sw360.antenna.sw360.rest.resource.*;
import org.eclipse.sw360.antenna.sw360.rest.resource.SW360HalResource;
import org.eclipse.sw360.antenna.sw360.rest.resource.SW360HalResourceUtility;
import org.eclipse.sw360.antenna.sw360.rest.resource.Self;
import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360SparseLicense;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SW360Release extends SW360HalResource<SW360ReleaseLinkObjects, SW360ReleaseEmbedded> {

Expand All @@ -31,6 +34,10 @@ public class SW360Release extends SW360HalResource<SW360ReleaseLinkObjects, SW36
private static final String CHANGESTATUS_KEY = "change_status";
private static final String COPYRIGHTS_KEY = "copyrights";
private static final String CLEARINGSTATE_KEY = "clearingState";
private static final Set<String> PURL_TYPE = Stream.of(
"bitbucket", "composer", "deb", "docker",
"gem", "generic", "github", "golang", "maven",
"npm", "nuget", "p2", "pypi", "rpm").collect(Collectors.toSet());

private String name;
private String version;
Expand Down Expand Up @@ -129,19 +136,27 @@ public void setDownloadurl(String downloadurl) {
this.downloadurl = downloadurl;
}


@JsonIgnore
public Map<String, String> getCoordinates() {
public Map<String, String> getPurls() {
return externalIds.entrySet().stream()
.filter(e -> SW360CoordinateKeysToArtifactCoordinates.getValues().contains(e.getKey()))
.filter(e -> PURL_TYPE.contains(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

public SW360Release setCoordinates(Map<String, String> coordinates) {
externalIds.putAll(coordinates);
public SW360Release setPurls(Map<String, String> purls) {
dropAllPurls();

externalIds.putAll(purls);

return this;
}

private void dropAllPurls() {
externalIds.keySet().stream()
.filter(PURL_TYPE::contains)
.forEach(externalIds::remove);
}

@JsonIgnore
public String getFinalLicense() {
return externalIds.get(FINAL_LICENSES_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
*/
package org.eclipse.sw360.antenna.sw360.utils;

import com.github.packageurl.PackageURL;
import org.eclipse.sw360.antenna.model.artifact.Artifact;
import org.eclipse.sw360.antenna.model.artifact.facts.*;
import org.eclipse.sw360.antenna.model.util.ArtifactLicenseUtils;
import org.eclipse.sw360.antenna.model.xml.generated.LicenseInformation;
import org.eclipse.sw360.antenna.sw360.rest.resource.SW360CoordinateKeysToArtifactCoordinates;
import org.eclipse.sw360.antenna.sw360.rest.resource.SW360HalResourceUtility;
import org.eclipse.sw360.antenna.sw360.rest.resource.components.SW360Component;
import org.eclipse.sw360.antenna.sw360.rest.resource.releases.SW360Release;

import java.util.*;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class SW360ReleaseAdapterUtils {
Expand All @@ -34,23 +37,23 @@ public static SW360Release prepareRelease(SW360Component component, Set<String>
release.setComponentId(componentId);
release.setMainLicenseIds(sw360LicenseIds);

SW360ReleaseAdapterUtils.setCoordinates(release, artifact);
SW360ReleaseAdapterUtils.setPackageUrls(release, artifact);
SW360ReleaseAdapterUtils.setFinalLicense(release, artifact);
SW360ReleaseAdapterUtils.setDeclaredLicense(release, artifact);
SW360ReleaseAdapterUtils.setObservedLicense(release, artifact);
SW360ReleaseAdapterUtils.setSources(release, artifact);
SW360ReleaseAdapterUtils.setOriginalRepo(release, artifact);
SW360ReleaseAdapterUtils.setSwhId(release, artifact);
SW360ReleaseAdapterUtils.setHashes(release, artifact);
SW360ReleaseAdapterUtils.setClearingStatus(release,artifact);
SW360ReleaseAdapterUtils.setClearingStatus(release, artifact);
SW360ReleaseAdapterUtils.setChangeStatus(release, artifact);
SW360ReleaseAdapterUtils.setCopyrights(release, artifact);

return release;
}

public static boolean isValidRelease(SW360Release release) {
if(release.getName() == null || release.getName().isEmpty()) {
if (release.getName() == null || release.getName().isEmpty()) {
return false;
}
return release.getVersion() != null && !release.getVersion().isEmpty();
Expand All @@ -72,18 +75,12 @@ private static void setCPEId(SW360Release release, Artifact artifact) {
.ifPresent(release::setCpeId);
}

private static void setCoordinates(SW360Release release, Artifact artifact) {
release.setCoordinates(getMapOfCoordinates(artifact));
}
private static void setPackageUrls(SW360Release sw360Release, Artifact artifact) {
Map<String, String> packageURLS = artifact.askForAll(ArtifactCoordinates.class).stream()
.map(ArtifactCoordinates::getPurl)
.collect(Collectors.toMap(PackageURL::getType, PackageURL::canonicalize));

private static Map<String, String> getMapOfCoordinates(Artifact artifact) {
Map<String, String> coordinates = new HashMap<>();
artifact.askForAll(ArtifactCoordinates.class).forEach(coordinate ->
coordinates.put(
SW360CoordinateKeysToArtifactCoordinates.get(coordinate.getClass()),
coordinate.toString())
);
return coordinates;
sw360Release.setPurls(packageURLS);
}

private static void setFinalLicense(SW360Release release, Artifact artifact) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import org.eclipse.sw360.antenna.model.artifact.facts.*;
import org.eclipse.sw360.antenna.model.reporting.MessageType;
import org.eclipse.sw360.antenna.model.util.ArtifactLicenseUtils;
import org.eclipse.sw360.antenna.model.util.ArtifactUtils;
import org.eclipse.sw360.antenna.model.xml.generated.License;
import org.eclipse.sw360.antenna.model.xml.generated.LicenseOperator;
import org.eclipse.sw360.antenna.model.xml.generated.LicenseStatement;
import org.eclipse.sw360.antenna.sw360.SW360MetaDataReceiver;
import org.eclipse.sw360.antenna.sw360.rest.resource.SW360CoordinateKeysToArtifactCoordinates;
import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360License;
import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360SparseLicense;
import org.eclipse.sw360.antenna.sw360.rest.resource.releases.SW360Release;
Expand Down Expand Up @@ -70,7 +70,7 @@ private void addClearingStateIfAvailable(Artifact artifact, SW360Release release
}

private void mapExternalIdsOnSW360Release(SW360Release sw360Release, Artifact artifact) {
mapCoordinates(sw360Release)
mapCoordinatesFromPurls(sw360Release)
.forEach(artifact::addFact);

artifact.addFact(new DeclaredLicenseInformation(makeLicenseStatementFromString(sw360Release.getDeclaredLicense())));
Expand All @@ -91,35 +91,19 @@ private void mapExternalIdsOnSW360Release(SW360Release sw360Release, Artifact ar
artifact.addFact(new CopyrightStatement(sw360Release.getCopyrights()));
}

private Stream<ArtifactCoordinates> mapCoordinatesFromPurls(SW360Release sw360Release) {
final Map<String, String> purls = sw360Release.getPurls();
return purls.values().stream()
.map(ArtifactUtils::createArtifactCoordinatesFromPurl);
}

private License makeLicenseStatementFromString(String license) {
License license1 = new License();
license1.setName(license);

return license1;
}

private Stream<ArtifactCoordinates> mapCoordinates(SW360Release sw360Release) {
final Map<String, String> coordinates = sw360Release.getCoordinates();
return SW360CoordinateKeysToArtifactCoordinates.getKeys()
.stream()
.map(key -> {
String coordinateType = SW360CoordinateKeysToArtifactCoordinates.get(key);
final String coordinatesString = coordinates.get(coordinateType);
if(coordinatesString == null) {
return null;
}

String[] splitCoordiantes = coordinatesString.split(":");

if (splitCoordiantes.length == 3) {
return SW360CoordinateKeysToArtifactCoordinates.createArtifactCoordinates(splitCoordiantes[0], sw360Release.getName(), sw360Release.getVersion(), key);
} else {
return SW360CoordinateKeysToArtifactCoordinates.createArtifactCoordinates("", sw360Release.getName(), sw360Release.getVersion(), key);
}
})
.filter(Objects::nonNull);
}

private void addSourceUrlIfAvailable(Artifact artifact, SW360Release release) {
if (release.getDownloadurl() != null && !release.getDownloadurl().isEmpty()) {
artifact.addFact(new ArtifactSourceUrl(release.getDownloadurl()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ public void artifactIsMappedToSw360ReleaseCorrectly() {
assertThat(false).isTrue();
}

assertThat(release.getCoordinates()).containsKeys("mvn");
assertThat(release.getCoordinates()).hasSize(1);
assertThat(release.getCoordinates()).containsValue("org.group.id:artifactId(test1):1.2.3");
assertThat(release.getPurls()).hasSize(1);
assertThat(release.getPurls().containsKey("maven")).isTrue();

assertThat(release.getHashes()).hasSize(1);
assertThat(release.getHashes()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

package org.eclipse.sw360.antenna.sw360.workflow.processors;

import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import com.github.packageurl.PackageURLBuilder;
import org.eclipse.sw360.antenna.api.exceptions.AntennaConfigurationException;
import org.eclipse.sw360.antenna.api.exceptions.AntennaException;
import org.eclipse.sw360.antenna.model.artifact.Artifact;
Expand All @@ -22,14 +25,11 @@
import org.eclipse.sw360.antenna.model.xml.generated.LicenseStatement;
import org.eclipse.sw360.antenna.sw360.SW360MetaDataReceiver;
import org.eclipse.sw360.antenna.sw360.rest.resource.LinkObjects;
import org.eclipse.sw360.antenna.sw360.rest.resource.SW360Attributes;
import org.eclipse.sw360.antenna.sw360.rest.resource.SW360CoordinateKeysToArtifactCoordinates;
import org.eclipse.sw360.antenna.sw360.rest.resource.Self;
import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360License;
import org.eclipse.sw360.antenna.sw360.rest.resource.licenses.SW360SparseLicense;
import org.eclipse.sw360.antenna.sw360.rest.resource.releases.SW360Release;
import org.eclipse.sw360.antenna.sw360.rest.resource.releases.SW360ReleaseEmbedded;
import org.eclipse.sw360.antenna.sw360.workflow.processors.SW360Enricher;
import org.eclipse.sw360.antenna.testing.AntennaTestWithMockedContext;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -80,7 +80,7 @@ public void setUp() throws AntennaConfigurationException {
}

@After
public void after(){
public void after() {
temporaryFolder.delete();

verify(toolConfigMock, atLeast(0)).getProxyHost();
Expand All @@ -89,7 +89,7 @@ public void after(){


@Test
public void releaseIsMappedToArtifactCorrectly() throws AntennaException {
public void releaseIsMappedToArtifactCorrectly() throws AntennaException, MalformedPackageURLException {
SW360Release release0 = mkSW360Release("test1");
release0.set_Embedded(new SW360ReleaseEmbedded());
release0.get_Embedded().setLicenses(Collections.emptyList());
Expand All @@ -101,6 +101,9 @@ public void releaseIsMappedToArtifactCorrectly() throws AntennaException {
assertThat(artifacts.size()).isEqualTo(1);
Artifact artifact0 = artifacts.get(0);

assertThat(artifact0.askFor(MavenCoordinates.class).isPresent()).isTrue();
assertThat(artifact0.askFor(MavenCoordinates.class).get().getVersion()).isEqualTo("1.2.3");

assertThat(artifact0.askFor(ArtifactSourceUrl.class).isPresent()).isTrue();
assertThat(artifact0.askForGet(ArtifactSourceUrl.class).get()).isEqualTo(sourceUrl);
assertThat(artifact0.askFor(ArtifactReleaseTagURL.class).isPresent()).isTrue();
Expand Down Expand Up @@ -231,7 +234,7 @@ private SW360License createLicenseFromSparseLicense(SW360SparseLicense sparseLic
return license;
}

private SW360Release mkSW360Release(String name) {
private SW360Release mkSW360Release(String name) throws MalformedPackageURLException {
SW360Release sw360Release = new SW360Release();

sw360Release.setName(name);
Expand All @@ -242,7 +245,14 @@ private SW360Release mkSW360Release(String name) {

sw360Release.setDeclaredLicense("Apache-2.0");
sw360Release.setObservedLicense("A Test License");
sw360Release.setCoordinates(Collections.singletonMap(SW360CoordinateKeysToArtifactCoordinates.get(MavenCoordinates.class), "test:test1:1.2.3"));
sw360Release.setPurls(Collections.singletonMap(PackageURL.StandardTypes.MAVEN,
PackageURLBuilder.aPackageURL()
.withName("test")
.withType("maven")
.withNamespace("test")
.withVersion("1.2.3")
.build()
.canonicalize()));
sw360Release.setReleaseTagUrl(releaseTagUrl);
sw360Release.setSoftwareHeritageId(swhID);
sw360Release.setHashes(Collections.singleton(hashString));
Expand Down

0 comments on commit c18c7ad

Please sign in to comment.