Skip to content

Commit

Permalink
feat(REST): Exclude release version from license info
Browse files Browse the repository at this point in the history
Signed-off-by: afsahsyeda <[email protected]>
  • Loading branch information
afsahsyeda committed Jun 28, 2024
1 parent 20c818a commit e2f4d3a
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 54 deletions.
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,12 +124,17 @@ 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,
this::mergeLicenseInfoParsingResults))
: projectLicenseInfoResults.stream().collect(
Collectors.toMap(this::getComponentLongName, li -> li, this::mergeLicenseInfoParsingResults));
return sortStringKeyedMap(licenseInfos);
}

@NotNull
protected LicenseInfoParsingResult mergeLicenseInfoParsingResults(LicenseInfoParsingResult r1, LicenseInfoParsingResult r2){
Expand Down Expand Up @@ -230,8 +243,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 +268,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
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,30 @@ public TextGenerator(OutputFormatVariant outputFormatVariant, String outputDescr
}

@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 {
String projectName = project.getName();
String projectVersion = project.getVersion();
String licenseInfoHeaderText = project.getLicenseInfoHeaderText();
String obligationsText = project.getObligationsText();

switch (getOutputVariant()) {
case DISCLOSURE:
return generateDisclosure(projectLicenseInfoResults, projectName + " " + projectVersion, licenseInfoHeaderText, obligationsText, externalIds);
return generateDisclosure(projectLicenseInfoResults, projectName + " " + projectVersion,
licenseInfoHeaderText, obligationsText, externalIds, excludeReleaseVersion);
default:
throw new IllegalArgumentException("Unknown generator variant type: " + getOutputVariant());
}
}
private String generateDisclosure(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, String projectTitle, String licenseInfoHeaderText, String obligationsText, Map<String, String> externalIds) {

private String generateDisclosure(Collection<LicenseInfoParsingResult> projectLicenseInfoResults,
String projectTitle, String licenseInfoHeaderText, String obligationsText,
Map<String, String> externalIds, boolean excludeReleaseVersion) {
try {
return renderTemplateWithDefaultValues(projectLicenseInfoResults, TXT_TEMPLATE_FILE, projectTitle, licenseInfoHeaderText, obligationsText, externalIds);
return renderTemplateWithDefaultValues(projectLicenseInfoResults, TXT_TEMPLATE_FILE, projectTitle,
licenseInfoHeaderText, obligationsText, externalIds, excludeReleaseVersion);
} catch (Exception e) {
LOGGER.error("Could not generate text licenseinfo file for project " + projectTitle, e);
return "License information could not be generated.\nAn exception occurred: " + e.toString();
Expand Down
Loading

0 comments on commit e2f4d3a

Please sign in to comment.