Skip to content

Commit

Permalink
SBOMER-85: Store licenses in archives instead of builds (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck authored Jun 21, 2024
1 parent 7d6a165 commit e297274
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
39 changes: 24 additions & 15 deletions core/src/main/java/org/jboss/pnc/build/finder/core/BuildFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,10 @@ public Map<BuildSystemInteger, KojiBuild> call() throws KojiClientException {
green(String.join(", ", uniqueLicenses)));
Collection<KojiBuild> values = allBuilds.values();
List<KojiBuild> buildsWithLicenses = values.stream()
.filter(kojiBuild -> !kojiBuild.getLicenses().isEmpty())
.filter(
kojiBuild -> kojiBuild.getArchives()
.stream()
.anyMatch(kojiLocalArchive -> !kojiLocalArchive.getLicenses().isEmpty()))
.collect(Collectors.toUnmodifiableList());
int numBuildsWithLicenses = buildsWithLicenses.size();
LOGGER.info(
Expand All @@ -1420,6 +1423,19 @@ public Map<BuildSystemInteger, KojiBuild> call() throws KojiClientException {
green(numBuilds),
green(Math.round(((double) numBuildsWithLicenses / (double) numBuilds) * 100D)));

List<KojiLocalArchive> archives = values.stream()
.flatMap(kojiBuild -> kojiBuild.getArchives().stream())
.collect(Collectors.toUnmodifiableList());
int numArchives = archives.size();
long numArchivesWithLicenses = archives.stream()
.filter(kojiLocalArchive -> !kojiLocalArchive.getLicenses().isEmpty())
.count();
LOGGER.info(
"{} / {} = {}% of archives have license information",
green(numArchivesWithLicenses),
green(numArchives),
green(Math.round(((double) numArchivesWithLicenses / (double) numArchives) * 100D)));

if (LOGGER.isWarnEnabled()) {
List<KojiBuild> allbuildsList = new ArrayList<>(values);
LOGGER.warn(
Expand All @@ -1446,29 +1462,22 @@ private static Set<LicenseInfo> addLicensesToBuilds(

for (Entry<String, Collection<LicenseInfo>> licenseEntry : entries) {
String filename = StringUtils.removeEnd(licenseEntry.getKey(), BANG_SLASH);
String parentFilename = filename;
Optional<KojiBuild> optKojiBuild = findBuildForFilename(parentFilename, allBuilds);
int index;

while (optKojiBuild.isEmpty() && (index = parentFilename.lastIndexOf(BANG_SLASH)) != -1) {
parentFilename = parentFilename.substring(0, index);
optKojiBuild = findBuildForFilename(parentFilename, allBuilds);
}
Optional<KojiLocalArchive> optLocalArchive = findLocalArchiveForFilename(filename, allBuilds);

if (optKojiBuild.isPresent()) {
KojiBuild build = optKojiBuild.get();
if (optLocalArchive.isPresent()) {
KojiLocalArchive localArchive = optLocalArchive.get();
Collection<LicenseInfo> licenseInfos = licenseEntry.getValue();
build.getLicenses().addAll(licenseInfos);
localArchive.getLicenses().addAll(licenseInfos);
allLicenses.addAll(licenseInfos);
} else {
LOGGER.error("No matching build found for file {}", boldRed(filename));
LOGGER.error("No matching archive found for file {}", boldRed(filename));
}
}

return Collections.unmodifiableSet(allLicenses);
}

private static Optional<KojiBuild> findBuildForFilename(
private static Optional<KojiLocalArchive> findLocalArchiveForFilename(
String filename,
Map<BuildSystemInteger, KojiBuild> allBuilds) {
for (KojiBuild build : allBuilds.values()) {
Expand All @@ -1478,7 +1487,7 @@ private static Optional<KojiBuild> findBuildForFilename(
.findFirst();

if (optArchive.isPresent()) {
return Optional.of(build);
return optArchive;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.collections4.MapUtils;
import org.jboss.pnc.build.finder.core.LicenseInfo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo;
Expand All @@ -49,8 +46,6 @@ public class KojiBuild {

private KojiTaskInfo taskInfo;

private Set<LicenseInfo> licenses;

private transient KojiTaskRequest taskRequest;

private transient List<KojiLocalArchive> archives;
Expand All @@ -68,14 +63,12 @@ public class KojiBuild {
public KojiBuild() {
archives = new ArrayList<>();
duplicateArchives = new ArrayList<>();
licenses = new TreeSet<>();
}

public KojiBuild(KojiBuildInfo buildInfo) {
this.buildInfo = buildInfo;
archives = new ArrayList<>();
duplicateArchives = new ArrayList<>();
licenses = new TreeSet<>();
}

public KojiBuild(
Expand All @@ -95,7 +88,6 @@ public KojiBuild(
this.tags = tags;
this.types = types;
this.remoteRpms = remoteRpms;
licenses = new TreeSet<>();
}

@JsonIgnore
Expand Down Expand Up @@ -188,14 +180,6 @@ public void setDuplicateArchives(List<KojiArchiveInfo> duplicateArchives) {
this.duplicateArchives = duplicateArchives;
}

public Set<LicenseInfo> getLicenses() {
return licenses;
}

public void setLicenses(Set<LicenseInfo> licenses) {
this.licenses = licenses;
}

@JsonIgnore
public boolean isPnc() {
return buildInfo != null && PNC.equals(MapUtils.getString(buildInfo.getExtra(), BUILD_SYSTEM));
Expand Down Expand Up @@ -312,6 +296,6 @@ public Optional<String> getMethod() {
public String toString() {
return "KojiBuild [buildInfo=" + buildInfo + ", taskInfo=" + taskInfo + ", taskRequest=" + taskRequest
+ ", archives=" + archives + ", remoteArchives=" + remoteArchives + ", tags=" + tags + ", remoteRpms="
+ remoteRpms + ", duplicateArchives=" + duplicateArchives + ", licenses=" + licenses + "]";
+ remoteRpms + ", duplicateArchives=" + duplicateArchives + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.TreeSet;

import org.jboss.pnc.build.finder.core.Checksum;
import org.jboss.pnc.build.finder.core.LicenseInfo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -38,24 +39,29 @@ public class KojiLocalArchive {
@JsonProperty("unmatchedFiles")
private Collection<String> unmatchedFilenames;

private Collection<LicenseInfo> licenses;

public KojiLocalArchive() {
this.filenames = new TreeSet<>();
this.checksums = new TreeSet<>();
this.unmatchedFilenames = new TreeSet<>();
this.licenses = new TreeSet<>();
}

public KojiLocalArchive(KojiArchiveInfo archive, Collection<String> filenames, Collection<Checksum> checksums) {
this.archive = archive;
this.filenames = new TreeSet<>(filenames);
this.checksums = new TreeSet<>(checksums);
this.unmatchedFilenames = new TreeSet<>();
this.licenses = new TreeSet<>();
}

public KojiLocalArchive(KojiRpmInfo rpm, Collection<String> filenames, Collection<Checksum> checksums) {
this.rpm = rpm;
this.filenames = new TreeSet<>(filenames);
this.checksums = new TreeSet<>(checksums);
this.unmatchedFilenames = new TreeSet<>();
this.licenses = new TreeSet<>();
}

public static boolean isMissingBuildTypeInfo(KojiArchiveInfo archive) {
Expand Down Expand Up @@ -127,9 +133,17 @@ public void setUnmatchedFilenames(Collection<String> unmatchedFilenames) {
this.unmatchedFilenames = new TreeSet<>(unmatchedFilenames);
}

public Collection<LicenseInfo> getLicenses() {
return licenses;
}

public void setLicenses(Collection<LicenseInfo> licenses) {
this.licenses = licenses;
}

@Override
public String toString() {
return "KojiLocalArchive [archive=" + archive + ", rpm=" + rpm + ", filenames=" + filenames + ", checksums="
+ checksums + ", unmatchedFilenames=" + unmatchedFilenames + "]";
+ checksums + ", unmatchedFilenames=" + unmatchedFilenames + ", licenses=" + licenses + "]";
}
}

0 comments on commit e297274

Please sign in to comment.