Skip to content

Commit

Permalink
[GLT-4299] fixed posting JIRA comments that were too long
Browse files Browse the repository at this point in the history
  • Loading branch information
djcooke committed Nov 11, 2024
1 parent 709c406 commit 79f564a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changes/fix_jira_maxLength.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Creating/updating notifications in JIRA failed if there were too many items because the message was
too long. Lists containing over 100 items will now be reduced to a summary of the library counts per
project. If there are over 100 projects, then only a count of the libraries and projects will be
included
23 changes: 20 additions & 3 deletions src/main/java/ca/on/oicr/gsi/dimsum/data/Notification.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package ca.on.oicr.gsi.dimsum.data;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.concurrent.Immutable;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -13,6 +16,7 @@ public class Notification {

private static final String PERIOD = ".";
private static final String COLON_AND_LINE_BREAK = ":\n";
private static final int MAX_LIST_ITEMS = 100;

private final Run run;
private final Set<Sample> pendingAnalysisSamples;
Expand Down Expand Up @@ -128,13 +132,26 @@ private String makeRunMessage() {
}
}

private String makePunctuationAndList(Set<Sample> samples) {
protected static String makePunctuationAndList(Set<Sample> samples) {
if (samples.isEmpty()) {
return PERIOD;
} else {
StringBuilder sb = new StringBuilder(COLON_AND_LINE_BREAK);
for (Sample sample : samples) {
sb.append("\n* %s (L%s)".formatted(sample.getName(), sample.getSequencingLane()));
if (samples.size() <= MAX_LIST_ITEMS) {
for (Sample sample : samples) {
sb.append("\n* %s (L%s)".formatted(sample.getName(), sample.getSequencingLane()));
}
} else {
Map<String, Integer> projects = samples.stream()
.map(Sample::getProject)
.collect(Collectors.toMap(Function.identity(), x -> 1, (x, y) -> x + y));
if (projects.size() <= MAX_LIST_ITEMS) {
projects.entrySet().forEach((entry) -> sb
.append("\n* %d %s libraries".formatted(entry.getValue(), entry.getKey())));
} else {
Integer libraryCount = projects.values().stream().reduce(0, (x, y) -> x + y);
sb.append("\n* %d libraries in %d projects".formatted(libraryCount, projects.size()));
}
}
return sb.toString();
}
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/ca/on/oicr/gsi/dimsum/data/NotificationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ca.on.oicr.gsi.dimsum.data;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
import ca.on.oicr.gsi.cardea.data.Sample;

public class NotificationTest {

private static String LIST_PREFIX = ":\n\n";

@Test
public void testMakePunctuationAndListSamples() {
Set<Sample> samples = new HashSet<>();
samples.add(mockSample(1, 1));
samples.add(mockSample(1, 2));
samples.add(mockSample(1, 3));

String result = Notification.makePunctuationAndList(samples);
String[] lines = result.split("\n");
// expecting starting punctuation (2 lines) + 3 library lines
assertEquals(5, lines.length);
assertTrue(result.startsWith(LIST_PREFIX));
for (int i = 2; i < lines.length; i++) {
assertTrue(lines[2].matches("^\\* Library \\d+ \\(L1\\)$"));
}
}

@Test
public void testMakePunctuationAndListProjects() {
Set<Sample> samples = new HashSet<>();
for (int projectNumber = 1; projectNumber <= 5; projectNumber++) {
for (int sampleNumber = 1; sampleNumber <= 25; sampleNumber++) {
samples.add(mockSample(projectNumber, sampleNumber));
}
}

String result = Notification.makePunctuationAndList(samples);
String[] lines = result.split("\n");
// expecting starting punctuation (2 lines) + 5 project lines
assertEquals(7, lines.length);
assertTrue(result.startsWith(LIST_PREFIX));
for (int i = 2; i < lines.length; i++) {
assertTrue(lines[2].matches("^\\* \\d+ Project \\d libraries$"));
}
}

@Test
public void testMakePunctuationAndListSummaryOnly() {
Set<Sample> samples = new HashSet<>();
for (int projectNumber = 1; projectNumber <= 105; projectNumber++) {
samples.add(mockSample(projectNumber, 1));
samples.add(mockSample(projectNumber, 2));
}

String result = Notification.makePunctuationAndList(samples);
String[] lines = result.split("\n");
// expecting starting punctuation (2 lines) + 1 summary line
assertEquals(3, lines.length);
assertTrue(result.startsWith(LIST_PREFIX));
assertEquals("* 210 libraries in 105 projects", lines[2]);
}

private static Sample mockSample(int projectNumber, int libraryNumber) {
Sample sample = mock(Sample.class);
when(sample.getProject()).thenReturn("Project " + projectNumber);
when(sample.getName()).thenReturn("Library " + libraryNumber);
when(sample.getSequencingLane()).thenReturn("1");
return sample;
}

}

0 comments on commit 79f564a

Please sign in to comment.