Skip to content
Merged
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
15 changes: 14 additions & 1 deletion src/main/java/org/jlab/sim/business/service/SyncService.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private static List<Software> parseProjects(
String description = null;

if (!item.isNull(DESCRIPTION)) {
description = item.getString(DESCRIPTION);
description = normalizeLineEndings(item.getString(DESCRIPTION));
}

JsonArray topics = item.getJsonArray("topics");
Expand Down Expand Up @@ -180,6 +180,19 @@ private static List<Software> parseProjects(
return softwareList;
}

/**
* Replaces CRLF and CR line endings with LF. GitLab returns a description entered in its web form
* with CRLF line endings, but the sync page passes it through an HTML attribute, which the
* browser reads with LF line endings, so sim saves LF. Without this a synced description with a
* line break never matches.
*
* @param text The text
* @return The text with LF line endings
*/
static String normalizeLineEndings(String text) {
return text.replace("\r\n", "\n").replace('\r', '\n');
}

private static final Pattern LINK_PATTERN = Pattern.compile("<([^>]+)>\\s*;\\s*rel=\"([^\"]+)\"");

/** The next page's URL from a response's first Link header, or null on the last page. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jlab.sim.persistence.entity.Repository;
import org.jlab.sim.persistence.entity.Software;
import org.jlab.sim.persistence.enumeration.SoftwareType;
import org.jlab.sim.persistence.view.SoftwareDiff;
import org.jlab.smoothness.business.exception.UserFriendlyException;
import org.jsoup.Jsoup;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -89,6 +90,40 @@ void gitLabProjectsBecomeSoftware() throws Exception {
assertTrue(notes.isArchived());
}

@Test
void multiLineDescriptionMatchesWhatSimSaved() throws Exception {
// GitLab returns a description entered in its web form with CRLF line endings; the sync page
// passes it through an HTML attribute, which the browser reads with LF line endings (sim#31)
String json =
"""
[
{"name": "fsdlib", "web_url": "https://code.jlab.org/acc/ops/fsdlib",
"description": "FSD library\\r\\nfor apps\\rand scripts\\n", "topics": [],
"archived": false}
]
""";

List<Software> remoteList = SyncService.parseGitLabProjects(json, REPOSITORY);

assertEquals("FSD library\nfor apps\nand scripts\n", remoteList.get(0).getDescription());

Software saved =
new Software(
REPOSITORY,
"fsdlib",
SoftwareType.APP,
"FSD library\nfor apps\nand scripts\n",
null,
"https://code.jlab.org/acc/ops/fsdlib",
false,
null);

SoftwareDiff diff = new SyncService().diff(List.of(saved), Map.of("fsdlib", remoteList.get(0)));

assertEquals(1, diff.getMatchList().size(), "matched, not updated again");
assertFalse(diff.hasChanges());
}

@Test
void emptyResultsAreNoSoftware() throws Exception {
assertEquals(
Expand Down
Loading