Skip to content

Commit

Permalink
Add support for Windows artifacts to Deliverables Analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Dec 9, 2024
1 parent d1665d5 commit 773d4b8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import com.github.packageurl.PackageURLBuilder;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.jboss.pnc.api.deliverablesanalyzer.dto.Artifact;
import org.jboss.pnc.api.deliverablesanalyzer.dto.ArtifactType;
Expand All @@ -29,6 +28,7 @@
import org.jboss.pnc.api.deliverablesanalyzer.dto.FinderResult;
import org.jboss.pnc.api.deliverablesanalyzer.dto.LicenseInfo;
import org.jboss.pnc.api.deliverablesanalyzer.dto.MavenArtifact;
import org.jboss.pnc.api.deliverablesanalyzer.dto.WindowsArtifact;
import org.jboss.pnc.api.dto.Request;
import org.jboss.pnc.api.enums.DeliverableAnalyzerReportLabel;
import org.jboss.pnc.api.enums.LabelOperation;
Expand All @@ -48,7 +48,6 @@
import org.jboss.pnc.enums.RepositoryType;
import org.jboss.pnc.facade.OperationsManager;
import org.jboss.pnc.facade.deliverables.api.AnalysisResult;
import org.jboss.pnc.facade.util.UserService;
import org.jboss.pnc.mapper.api.ArtifactMapper;
import org.jboss.pnc.mapper.api.DeliverableAnalyzerOperationMapper;
import org.jboss.pnc.model.Base32LongID;
Expand Down Expand Up @@ -95,6 +94,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -543,9 +543,8 @@ private org.jboss.pnc.model.Artifact findOrCreateNotFoundArtifact(
private org.jboss.pnc.model.Artifact createArtifact(
org.jboss.pnc.model.Artifact artifact,
TargetRepository targetRepo) {

artifact.setTargetRepository(targetRepo);
artifact.setPurl(createGenericPurl(artifact.getFilename().toString(), artifact.getSha256()));
artifact.setPurl(createGenericPurl(artifact.getFilename(), artifact.getSha256()));
org.jboss.pnc.model.Artifact savedArtifact = artifactRepository.save(artifact);
targetRepo.getArtifacts().add(savedArtifact);
return savedArtifact;
Expand All @@ -567,17 +566,12 @@ private org.jboss.pnc.model.Artifact mapBrewArtifact(
String nvr,
TargetRepository targetRepository,
User user) {
if (artifact.getArtifactType() != ArtifactType.MAVEN) {
throw new UnsupportedOperationException("Brew artifact " + artifact + " is not Maven!");
}
MavenArtifact mavenArtifact = (MavenArtifact) artifact;

org.jboss.pnc.model.Artifact.Builder builder = mapArtifact(mavenArtifact, user);
builder.identifier(createIdentifier(mavenArtifact));
builder.filename(createFileName(mavenArtifact));
builder.deployPath(createDeployPath(mavenArtifact));
builder.originUrl(createBrewOriginURL(mavenArtifact, nvr));
builder.purl(createPURL(mavenArtifact));
org.jboss.pnc.model.Artifact.Builder builder = mapArtifact(artifact, user);
builder.identifier(createIdentifier(artifact));
builder.filename(createFileName(artifact));
builder.deployPath(createDeployPath(artifact));
builder.originUrl(createBrewOriginURL(artifact, nvr));
builder.purl(createPURL(artifact));
builder.targetRepository(targetRepository);

return builder.build();
Expand All @@ -603,49 +597,82 @@ private org.jboss.pnc.model.Artifact.Builder mapArtifact(Artifact artifact, User
return builder;
}

private static String createDeployPath(MavenArtifact mavenArt) {
String filename = createFileName(mavenArt);
String deployPath = "/" + mavenArt.getGroupId().replace('.', '/') + "/" + mavenArt.getArtifactId() + "/"
+ mavenArt.getVersion() + "/" + filename;
private static String createDeployPath(Artifact artifact) {
String filename = createFileName(artifact);
String deployPath;

if (artifact instanceof MavenArtifact) {
MavenArtifact mavenArtifact = (MavenArtifact) artifact;
deployPath = "/" + mavenArtifact.getGroupId().replace('.', '/') + "/" + mavenArtifact.getArtifactId() + "/"
+ mavenArtifact.getVersion() + "/" + filename;
} else if (artifact instanceof WindowsArtifact) {
deployPath = "/" + filename;
} else {
throw new IllegalArgumentException("Unsupported artifact type: " + artifact.getArtifactType());
}

return deployPath;
}

private static String createFileName(MavenArtifact mavenArt) {
String filename = mavenArt.getArtifactId() + "-" + mavenArt.getVersion();
if (!Strings.isEmpty(mavenArt.getClassifier())) {
filename += "-" + mavenArt.getClassifier();
private static String createFileName(Artifact artifact) {
String filename;

if (artifact instanceof MavenArtifact) {
MavenArtifact mavenArtifact = (MavenArtifact) artifact;
filename = mavenArtifact.getArtifactId() + "-" + mavenArtifact.getVersion();
if (!Strings.isEmpty(mavenArtifact.getClassifier())) {
filename += "-" + mavenArtifact.getClassifier();
}
filename += "." + mavenArtifact.getType();
} else if (artifact instanceof WindowsArtifact) {
WindowsArtifact windowsArtifact = (WindowsArtifact) artifact;
filename = windowsArtifact.getFilename();
} else {
throw new IllegalArgumentException("Unsupported artifact type: " + artifact.getArtifactType());
}
filename += "." + mavenArt.getType();

return filename;
}

private String createBrewOriginURL(MavenArtifact mavenArt, String nvr) {
private String createBrewOriginURL(Artifact artifact, String nvr) {
String brewContentUrl = globalConfig.getBrewContentUrl();

Matcher matcher = NVR_PATTERN.matcher(nvr);
if (!matcher.matches()) {
throw new IllegalArgumentException("NVR " + nvr + " does not match expected format.");
}
String name = matcher.group(1);
String version = matcher.group(2);
String release = matcher.group(3);

return brewContentUrl + "/" + name + "/" + version + "/" + release + "/maven" + createDeployPath(mavenArt);
return brewContentUrl + "/" + name + "/" + version + "/" + release + "/"
+ artifact.getArtifactType().name().toLowerCase(Locale.ENGLISH) + createDeployPath(artifact);
}

private String createPURL(MavenArtifact mavenArtifact) {
private String createPURL(Artifact artifact) {
try {
PackageURLBuilder purlBuilder = PackageURLBuilder.aPackageURL()
.withType(PackageURL.StandardTypes.MAVEN)
.withNamespace(mavenArtifact.getGroupId())
.withName(mavenArtifact.getArtifactId())
.withVersion(mavenArtifact.getVersion())
.withQualifier(
"type",
StringUtils.isEmpty(mavenArtifact.getType()) ? "jar" : mavenArtifact.getType());

if (!StringUtils.isEmpty(mavenArtifact.getClassifier())) {
purlBuilder.withQualifier("classifier", mavenArtifact.getClassifier());
PackageURLBuilder purlBuilder;
if (artifact instanceof MavenArtifact) {
MavenArtifact mavenArtifact = (MavenArtifact) artifact;
purlBuilder = PackageURLBuilder.aPackageURL()
.withType(PackageURL.StandardTypes.MAVEN)
.withNamespace(mavenArtifact.getGroupId())
.withName(mavenArtifact.getArtifactId())
.withVersion(mavenArtifact.getVersion())
.withQualifier(
"type",
StringUtils.isEmpty(mavenArtifact.getType()) ? "jar" : mavenArtifact.getType());

if (!StringUtils.isEmpty(mavenArtifact.getClassifier())) {
purlBuilder.withQualifier("classifier", mavenArtifact.getClassifier());
}

} else if (artifact instanceof WindowsArtifact) {
WindowsArtifact windowsArtifact = (WindowsArtifact) artifact;
purlBuilder = PackageURLBuilder.aPackageURL()
.withType(PackageURL.StandardTypes.GENERIC)
.withName(windowsArtifact.getName())
.withVersion(windowsArtifact.getVersion());
} else {
throw new IllegalArgumentException("Unsupported artifact type: " + artifact.getArtifactType());
}
return purlBuilder.build().toString();
} catch (MalformedPackageURLException e) {
Expand Down Expand Up @@ -673,17 +700,25 @@ private String createGenericPurl(String filename, String sha256) {
}
}

private String createIdentifier(MavenArtifact mavenArtifact) {
return Arrays
.asList(
mavenArtifact.getGroupId(),
mavenArtifact.getArtifactId(),
mavenArtifact.getType(),
mavenArtifact.getVersion(),
mavenArtifact.getClassifier())
.stream()
.filter(Objects::nonNull)
.collect(Collectors.joining(":"));
private String createIdentifier(Artifact artifact) {
if (artifact instanceof MavenArtifact) {
MavenArtifact mavenArtifact = (MavenArtifact) artifact;
return Arrays
.asList(
mavenArtifact.getGroupId(),
mavenArtifact.getArtifactId(),
mavenArtifact.getType(),
mavenArtifact.getVersion(),
mavenArtifact.getClassifier())
.stream()
.filter(Objects::nonNull)
.collect(Collectors.joining(":"));
} else if (artifact instanceof WindowsArtifact) {
WindowsArtifact windowsArtifact = (WindowsArtifact) artifact;
return String.join("-", windowsArtifact.getName(), windowsArtifact.getVersion());
} else {
throw new IllegalArgumentException("Unsupported artifact type: " + artifact.getArtifactType());
}
}

private TargetRepository getDistributionRepository(String distURL) {
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<version.lombok>1.18.22</version.lombok>
<version.mapstruct>1.5.3.Final</version.mapstruct>
<version.pncmetrics>1.1.3</version.pncmetrics>
<version.pnc-api>3.0.5</version.pnc-api>
<version.pnc-api>3.0.6-SNAPSHOT</version.pnc-api>
<version.pnc-common>3.0.1</version.pnc-common>
<version.rex-api>1.0.1</version.rex-api>
<bifrost-client.version>3.0.0</bifrost-client.version>
Expand Down Expand Up @@ -1671,6 +1671,7 @@
<version>2.12.2</version>
<configuration>
<configFile>../eclipse-codeStyle.xml</configFile>
<lineEnding>LF</lineEnding>
</configuration>
<executions>
<execution>
Expand Down

0 comments on commit 773d4b8

Please sign in to comment.