Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(REST): Exclude release version from license info #2496

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ public LicenseInfoFile getLicenseInfoFile(Project project, User user, String out
Map<String, Map<String, Boolean>> releaseIdsToSelectedAttachmentIds,
Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachment, String externalIds, String fileName)
throws TException {
return getLicenseInfoFileWithoutReleaseVersion(project, user, outputGenerator,
releaseIdsToSelectedAttachmentIds, excludedLicensesPerAttachment, externalIds, fileName, false);
}

@Override
public LicenseInfoFile getLicenseInfoFileWithoutReleaseVersion(Project project, User user, String outputGenerator,
Map<String, Map<String, Boolean>> releaseIdsToSelectedAttachmentIds,
Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachment, String externalIds, String fileName,
boolean excludeReleaseVersion)
throws TException {
assertNotNull(project);
assertNotNull(user);
assertNotNull(outputGenerator);
Expand Down Expand Up @@ -170,7 +180,7 @@ public LicenseInfoFile getLicenseInfoFile(Project project, User user, String out
}

Object output = generator.generateOutputFile(projectLicenseInfoResults, project, obligationsResults, user,
filteredExtIdMap, obligationsStatusInfoMap, fileName);
filteredExtIdMap, obligationsStatusInfoMap, fileName, excludeReleaseVersion);
if (output instanceof byte[]) {
licenseInfoFile.setGeneratedOutput((byte[]) output);
} else if (output instanceof String) {
Expand All @@ -182,6 +192,7 @@ public LicenseInfoFile getLicenseInfoFile(Project project, User user, String out
return licenseInfoFile;
}

@Override
public Map<String, Map<String, String>> evaluateAttachments(String releaseId, User user) throws TException {
Release release = componentDatabaseHandler.getRelease(releaseId, user);
Map<Attachment, LicenseInfoParsingResult> parsedResults = new HashMap<Attachment, LicenseInfoParsingResult>();
Expand Down Expand Up @@ -619,7 +630,7 @@ public List<LicenseInfoParsingResult> getLicenseInfoForAttachment(Release releas
}

@Override
public LicenseObligationsStatusInfo getProjectObligationStatus(Map<String, ObligationStatusInfo> obligationStatusMap, List<LicenseInfoParsingResult> licenseResults,
public LicenseObligationsStatusInfo getProjectObligationStatus(Map<String, ObligationStatusInfo> obligationStatusMap, List<LicenseInfoParsingResult> licenseResults,
Map<String, String> excludedReleaseIdToAcceptedCLI) {

Map<String, ObligationStatusInfo> filteredObligationStatusMap = obligationStatusMap.isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public DocxGenerator(OutputFormatVariant outputFormatVariant, String description
}

@Override
public byte[] generateOutputFile(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, Project project, Collection<ObligationParsingResult> obligationResults, User user, Map<String, String> externalIds, Map<String, ObligationStatusInfo> obligationsStatus, String fileName) throws SW360Exception {
public byte[] generateOutputFile(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, Project project,
Collection<ObligationParsingResult> obligationResults, User user, Map<String, String> externalIds,
Map<String, ObligationStatusInfo> obligationsStatus, String fileName, boolean excludeReleaseVersion) throws SW360Exception {
String licenseInfoHeaderText = project.getLicenseInfoHeaderText();

ByteArrayOutputStream docxOutputStream = new ByteArrayOutputStream();
Expand All @@ -119,7 +121,8 @@ public byte[] generateOutputFile(Collection<LicenseInfoParsingResult> projectLic
project,
licenseInfoHeaderText,
false,
externalIds
externalIds,
excludeReleaseVersion
);
} else {
throw new SW360Exception("Could not load the template for xwpf document: " + DOCX_TEMPLATE_FILE);
Expand Down Expand Up @@ -168,7 +171,8 @@ private void fillDisclosureDocument(
Collection<LicenseInfoParsingResult> projectLicenseInfoResults,
Project project,
String licenseInfoHeaderText,
boolean includeObligations, Map<String, String> externalIds) throws XmlException, TException {
boolean includeObligations, Map<String, String> externalIds, boolean excludeReleaseVersion)
throws XmlException, TException {
if (CommonUtils.isNotEmpty(projectLicenseInfoResults)) {
projectLicenseInfoResults = projectLicenseInfoResults.stream().filter(Objects::nonNull)
.sorted(Comparator.comparing(li -> getComponentLongName(li), String.CASE_INSENSITIVE_ORDER))
Expand All @@ -188,8 +192,8 @@ private void fillDisclosureDocument(
return;
}

fillReleaseBulletList(document, projectLicenseInfoResults);
fillReleaseDetailList(document, projectLicenseInfoResults, includeObligations, licenseToReferenceId);
fillReleaseBulletList(document, projectLicenseInfoResults, excludeReleaseVersion);
fillReleaseDetailList(document, projectLicenseInfoResults, includeObligations, licenseToReferenceId, excludeReleaseVersion);
fillLicenseList(document, projectLicenseInfoResults, licenseToReferenceId);
}

Expand Down Expand Up @@ -308,9 +312,9 @@ private void fillReportDocument(
List<Obligation> obligations = SW360Utils.getObligations();
fillProjectComponentOrganisationObligationsTable(document, obligationsStatus, obligations,
ObligationLevel.ORGANISATION_OBLIGATION, COMMON_RULES_TABLE_INDEX, NO_ORGANISATION_OBLIGATIONS);
fillProjectComponentOrganisationObligationsTable(document, obligationsStatus, obligations,
fillProjectComponentOrganisationObligationsTable(document, obligationsStatus, obligations,
ObligationLevel.PROJECT_OBLIGATION, PROJECT_OBLIGATIONS_TABLE_INDEX, NO_PROJECT_OBLIGATIONS);
fillProjectComponentOrganisationObligationsTable(document, obligationsStatus, obligations,
fillProjectComponentOrganisationObligationsTable(document, obligationsStatus, obligations,
ObligationLevel.COMPONENT_OBLIGATION, COMPONENT_OBLIGATIONS_TABLE_INDEX, NO_COMPONENT_OBLIGATIONS);
fillComponentObligationsTable(document, obligationResults, mostLicenses, project);

Expand Down Expand Up @@ -720,24 +724,38 @@ private XmlCursor moveCursor(XmlCursor cursor) throws SW360Exception {
return cursor;
}

private void fillReleaseBulletList(XWPFDocument document, Collection<LicenseInfoParsingResult> projectLicenseInfoResults) throws XmlException {
private void fillReleaseBulletList(XWPFDocument document, Collection<LicenseInfoParsingResult> projectLicenseInfoResults,
boolean excludeReleaseVersion) throws XmlException {
List<String> releaseList = new ArrayList<>();
for (LicenseInfoParsingResult result : projectLicenseInfoResults) {
releaseList.add(getComponentLongName(result));

if (excludeReleaseVersion) {
for (LicenseInfoParsingResult result : projectLicenseInfoResults) {
releaseList.add(getComponentLongNameWithoutVersion(result));
}
} else {
for (LicenseInfoParsingResult result : projectLicenseInfoResults) {
releaseList.add(getComponentLongName(result));
}
}
addBulletList(document, releaseList, true);
addPageBreak(document);
}

private void fillReleaseDetailList(XWPFDocument document, Collection<LicenseInfoParsingResult> projectLicenseInfoResults, boolean includeObligations, Map<LicenseNameWithText, Integer> licenseToReferenceId) throws TException {
private void fillReleaseDetailList(XWPFDocument document,
Collection<LicenseInfoParsingResult> projectLicenseInfoResults, boolean includeObligations,
Map<LicenseNameWithText, Integer> licenseToReferenceId, boolean excludeReleaseVersion) throws TException {
addFormattedText(document.createParagraph().createRun(), "Detailed Releases Information", FONT_SIZE + 2, true);
setText(document.createParagraph().createRun(), "Please note the following license conditions and copyright " +
"notices applicable to Open Source Software and/or other components (or parts thereof):");
addNewLines(document, 0);
Map<String, Set<String>> sortedAcknowledgement = getAcknowledgement(projectLicenseInfoResults);
Map<String, Set<String>> sortedAcknowledgement = getAcknowledgement(projectLicenseInfoResults, excludeReleaseVersion);
for (LicenseInfoParsingResult parsingResult : projectLicenseInfoResults) {
addReleaseTitle(document, parsingResult);
addAcknowledgement(document, sortedAcknowledgement.get(getComponentLongName(parsingResult)));
addReleaseTitle(document, parsingResult, excludeReleaseVersion);
if (excludeReleaseVersion) {
addAcknowledgement(document, sortedAcknowledgement.get(getComponentLongNameWithoutVersion(parsingResult)));
} else {
addAcknowledgement(document, sortedAcknowledgement.get(getComponentLongName(parsingResult)));
}
if (parsingResult.getStatus() == LicenseInfoRequestStatus.SUCCESS) {
addLicenses(document, parsingResult, includeObligations, licenseToReferenceId);
addNewLines(document, 1);
Expand Down Expand Up @@ -766,18 +784,17 @@ private void addAcknowledgement(XWPFDocument document, Set<String> acknowledgeme
}

private SortedMap<String, Set<String>> getAcknowledgement(
Collection<LicenseInfoParsingResult> projectLicenseInfoResults) {
Collection<LicenseInfoParsingResult> projectLicenseInfoResults, boolean excludeReleaseVersion) {

Map<Boolean, List<LicenseInfoParsingResult>> partitionedResults = projectLicenseInfoResults.stream()
.collect(Collectors.partitioningBy(r -> r.getStatus() == LicenseInfoRequestStatus.SUCCESS));
List<LicenseInfoParsingResult> goodResults = partitionedResults.get(true);

return getSortedAcknowledgements(getSortedLicenseInfos(goodResults));

return getSortedAcknowledgements(getSortedLicenseInfos(goodResults, excludeReleaseVersion));
}

private void addReleaseTitle(XWPFDocument document, LicenseInfoParsingResult parsingResult) {
String releaseTitle = getComponentLongName(parsingResult);
private void addReleaseTitle(XWPFDocument document, LicenseInfoParsingResult parsingResult, boolean excludeReleaseVersion) {
String releaseTitle = excludeReleaseVersion ? getComponentLongNameWithoutVersion(parsingResult) : getComponentLongName(parsingResult);
XWPFParagraph releaseTitleParagraph = document.createParagraph();
releaseTitleParagraph.setStyle(STYLE_HEADING);
addBookmark(releaseTitleParagraph, releaseTitle, releaseTitle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public JsonGenerator(OutputFormatVariant outputFormatVariant, String description
}

@Override
public String generateOutputFile(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, Project project, Collection<ObligationParsingResult> obligationResults, User user, Map<String, String> externalIds, Map<String, ObligationStatusInfo> obligationsStatus, String fileName) throws SW360Exception {
public String generateOutputFile(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, Project project,
Collection<ObligationParsingResult> obligationResults, User user, Map<String, String> externalIds,
Map<String, ObligationStatusInfo> obligationsStatus, String fileName, boolean excludeReleaseVersion) throws SW360Exception {
return "";

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public abstract class OutputGenerator<T> {
this.outputVariant = variant;
}

public abstract T generateOutputFile(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, Project project, Collection<ObligationParsingResult> obligationResults, User user, Map<String,String> externalIds, Map<String, ObligationStatusInfo> obligationsStatus, String fileName) throws SW360Exception;
public abstract T generateOutputFile(Collection<LicenseInfoParsingResult> projectLicenseInfoResults,
Project project, Collection<ObligationParsingResult> obligationResults, User user,
Map<String, String> externalIds, Map<String, ObligationStatusInfo> obligationsStatus, String fileName,
boolean excludeReleaseVersion)
throws SW360Exception;

public String getOutputType() {
return outputType;
Expand Down Expand Up @@ -100,6 +104,10 @@ public String getComponentLongName(LicenseInfoParsingResult li) {
return SW360Utils.getReleaseFullname(li.getVendor(), li.getName(), li.getVersion());
}

public String getComponentLongNameWithoutVersion(LicenseInfoParsingResult li) {
return SW360Utils.getReleaseFullname(li.getVendor(), li.getName(), "");
}

public String getComponentShortName(LicenseInfoParsingResult li) {
return SW360Utils.getReleaseFullname("", li.getName(), li.getVersion());
}
Expand All @@ -116,17 +124,25 @@ public VelocityContext getConfiguredVelocityContext() {
return new VelocityContext(velocityToolManager.createContext());
}

@NotNull
protected SortedMap<String, LicenseInfoParsingResult> getSortedLicenseInfos(Collection<LicenseInfoParsingResult> projectLicenseInfoResults) {
Map<String, LicenseInfoParsingResult> licenseInfos = projectLicenseInfoResults.stream()
.collect(Collectors.toMap(this::getComponentLongName, li -> li, this::mergeLicenseInfoParsingResults));
return sortStringKeyedMap(licenseInfos);
}
@NotNull
protected SortedMap<String, LicenseInfoParsingResult> getSortedLicenseInfos(
Collection<LicenseInfoParsingResult> projectLicenseInfoResults, boolean excludeReleaseVersion) {
Map<String, LicenseInfoParsingResult> licenseInfos = excludeReleaseVersion
? projectLicenseInfoResults.stream()
.collect(Collectors.toMap(this::getComponentLongNameWithoutVersion, li -> li,
(l1, l2) -> mergeLicenseInfoParsingResults(l1, l2, true)))
: projectLicenseInfoResults.stream().collect(Collectors.toMap(this::getComponentLongName, li -> li,
(l1, l2) -> mergeLicenseInfoParsingResults(l1, l2, false)));
return sortStringKeyedMap(licenseInfos);
}

@NotNull
protected LicenseInfoParsingResult mergeLicenseInfoParsingResults(LicenseInfoParsingResult r1, LicenseInfoParsingResult r2){
protected LicenseInfoParsingResult mergeLicenseInfoParsingResults(LicenseInfoParsingResult r1, LicenseInfoParsingResult r2, boolean excludeReleaseVersion){
String r1_name = excludeReleaseVersion ? getComponentLongNameWithoutVersion(r1) : getComponentLongName(r1);
String r2_name = excludeReleaseVersion ? getComponentLongNameWithoutVersion(r2) : getComponentLongName(r2);

if (r1.getStatus() != LicenseInfoRequestStatus.SUCCESS || r2.getStatus() != LicenseInfoRequestStatus.SUCCESS ||
!getComponentLongName(r1).equals(getComponentLongName(r2))){
!r1_name.equals(r2_name)){
throw new IllegalArgumentException("Only successful parsing results for the same release can be merged");
}

Expand Down Expand Up @@ -230,8 +246,9 @@ private static <U> SortedMap<String, U> sortStringKeyedMap(Map<String, U> unsort
* @param externalIds
* @return rendered template
*/
protected String renderTemplateWithDefaultValues(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, String file,
String projectTitle, String licenseInfoHeaderText, String obligationsText, Map<String, String> externalIds) {
protected String renderTemplateWithDefaultValues(Collection<LicenseInfoParsingResult> projectLicenseInfoResults,
String file, String projectTitle, String licenseInfoHeaderText, String obligationsText,
Map<String, String> externalIds, boolean excludeReleaseVersion) {
VelocityContext vc = getConfiguredVelocityContext();
// set header
vc.put(LICENSE_INFO_PROJECT_TITLE, projectTitle);
Expand All @@ -254,12 +271,15 @@ protected String renderTemplateWithDefaultValues(Collection<LicenseInfoParsingRe
Map<Boolean, List<LicenseInfoParsingResult>> partitionedResults =
projectLicenseInfoResults.stream().collect(Collectors.partitioningBy(r -> r.getStatus() == LicenseInfoRequestStatus.SUCCESS));
List<LicenseInfoParsingResult> goodResults = partitionedResults.get(true);
Map<String, List<LicenseInfoParsingResult>> badResultsPerRelease =
partitionedResults.get(false).stream().collect(Collectors.groupingBy(this::getComponentLongName));

Map<String, List<LicenseInfoParsingResult>> badResultsPerRelease = excludeReleaseVersion
? partitionedResults.get(false).stream()
.collect(Collectors.groupingBy(this::getComponentLongNameWithoutVersion))
: partitionedResults.get(false).stream().collect(Collectors.groupingBy(this::getComponentLongName));
vc.put(LICENSE_INFO_ERROR_RESULTS_CONTEXT_PROPERTY, badResultsPerRelease);

// be sure that the licenses inside a release are sorted by id. This looks nicer
SortedMap<String, LicenseInfoParsingResult> sortedLicenseInfos = getSortedLicenseInfos(goodResults);
SortedMap<String, LicenseInfoParsingResult> sortedLicenseInfos = getSortedLicenseInfos(goodResults, excludeReleaseVersion);
// this will effectively change the objects in the collection and therefore the
// objects in the sorted map above
sortLicenseNamesWithinEachLicenseInfoById(sortedLicenseInfos.values(), licenseToReferenceId);
Expand Down
Loading