Skip to content

Commit 616d7b8

Browse files
Generate build targets in comment id order
1 parent 71fd537 commit 616d7b8

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private boolean isStatusMessage(String content) {
159159
return false;
160160
}
161161

162-
public Collection<StashPullRequestBuildTarget> getBuildTargets(
162+
public List<StashPullRequestBuildTarget> getBuildTargets(
163163
StashPullRequestResponseValue pullRequest) {
164164
if (shouldSkip(pullRequest)) {
165165
return new ArrayList<>();
@@ -186,19 +186,18 @@ public Collection<StashPullRequestBuildTarget> getBuildTargets(
186186
if (!isOnlyBuildOnComment) {
187187
return getBuildTargetsWithoutOnlyBuildOnCommentLogic(pullRequest, comments);
188188
}
189-
Collection<StashPullRequestBuildTarget> buildTargets =
189+
List<StashPullRequestBuildTarget> buildTargets =
190190
getBuildTargetsWithOnlyBuildOnCommentLogic(pullRequest, comments);
191191

192192
if (!trigger.getCancelOutdatedJobsEnabled()) {
193193
return buildTargets;
194194
}
195-
List<StashPullRequestBuildTarget> buildTargetsList = new ArrayList<>(buildTargets);
196-
buildTargetsList.sort(
195+
buildTargets.sort(
197196
Comparator.comparing(StashPullRequestBuildTarget::getBuildCommandCommentId).reversed());
198-
Collection<StashPullRequestBuildTarget> buildTargetsToKeep =
199-
buildTargetsList.stream().limit(1).collect(Collectors.toList());
200-
Collection<StashPullRequestBuildTarget> buildTargetsToCancel =
201-
buildTargetsList.stream().skip(1).collect(Collectors.toList());
197+
List<StashPullRequestBuildTarget> buildTargetsToKeep =
198+
buildTargets.stream().limit(1).collect(Collectors.toList());
199+
List<StashPullRequestBuildTarget> buildTargetsToCancel =
200+
buildTargets.stream().skip(1).collect(Collectors.toList());
202201
try {
203202
for (StashPullRequestBuildTarget buildTarget : buildTargetsToCancel) {
204203
postBuildCancelComment(pullRequest, buildTarget.getBuildCommandCommentId());
@@ -219,10 +218,13 @@ public Collection<StashPullRequestBuildTarget> getBuildTargets(
219218
return buildTargetsToKeep;
220219
}
221220

222-
private Collection<StashPullRequestBuildTarget> getBuildTargetsWithOnlyBuildOnCommentLogic(
221+
private List<StashPullRequestBuildTarget> getBuildTargetsWithOnlyBuildOnCommentLogic(
223222
StashPullRequestResponseValue pullRequest, List<StashPullRequestComment> comments) {
224223
List<StashPullRequestBuildTarget> buildTargets = new ArrayList<>();
225224

225+
// Start with least recent comments
226+
comments.sort(Comparator.naturalOrder());
227+
226228
for (StashPullRequestComment comment : comments) {
227229
String content = comment.getText();
228230
if (StringUtils.isEmpty(content)) {
@@ -244,7 +246,7 @@ private Collection<StashPullRequestBuildTarget> getBuildTargetsWithOnlyBuildOnCo
244246
return buildTargets;
245247
}
246248

247-
private Collection<StashPullRequestBuildTarget> getBuildTargetsWithoutOnlyBuildOnCommentLogic(
249+
private List<StashPullRequestBuildTarget> getBuildTargetsWithoutOnlyBuildOnCommentLogic(
248250
StashPullRequestResponseValue pullRequest, List<StashPullRequestComment> comments) {
249251
String sourceCommit = pullRequest.getFromRef().getLatestCommit();
250252
String destinationCommit = pullRequest.getToRef().getLatestCommit();

src/test/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepositoryTest.java

+30-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import static org.hamcrest.Matchers.anEmptyMap;
77
import static org.hamcrest.Matchers.arrayWithSize;
88
import static org.hamcrest.Matchers.contains;
9-
import static org.hamcrest.Matchers.containsInAnyOrder;
109
import static org.hamcrest.Matchers.containsString;
1110
import static org.hamcrest.Matchers.empty;
1211
import static org.hamcrest.Matchers.emptyArray;
@@ -453,20 +452,40 @@ public void getBuildTargets_onlyBuildOnComment_multiple_comments_generate_multip
453452
when(stashApiClient.getPullRequestComments(any(), any(), any())).thenReturn(comments);
454453
when(trigger.getCiBuildPhrases()).thenReturn("DO TEST");
455454
when(trigger.getOnlyBuildOnComment()).thenReturn(true);
455+
when(trigger.getCancelOutdatedJobsEnabled()).thenReturn(false);
456456

457-
Collection<StashPullRequestBuildTarget> buildTargets =
458-
stashRepository.getBuildTargets(pullRequest);
457+
List<StashPullRequestBuildTarget> buildTargets = stashRepository.getBuildTargets(pullRequest);
459458

460459
assertThat(buildTargets, hasSize(2));
461460
assertThat(
462-
buildTargets,
463-
containsInAnyOrder(
464-
allOf(
465-
hasProperty("additionalParameters", aMapWithSize(2)),
466-
hasProperty(
467-
"additionalParameters",
468-
allOf(hasEntry("key1", "value1"), hasEntry("key2", "value2")))),
469-
hasProperty("additionalParameters", anEmptyMap())));
461+
buildTargets.get(0),
462+
allOf(
463+
hasProperty("additionalParameters", aMapWithSize(2)),
464+
hasProperty(
465+
"additionalParameters",
466+
allOf(hasEntry("key1", "value1"), hasEntry("key2", "value2")))));
467+
assertThat(buildTargets.get(1), hasProperty("additionalParameters", anEmptyMap()));
468+
}
469+
470+
@Test
471+
public void
472+
getBuildTargets_onlyBuildOnComment_cancelOutdatedJobsDisabled_generates_targets_in_comment_id_order()
473+
throws Exception {
474+
StashPullRequestComment comment1 = new StashPullRequestComment(1, "DO TEST\np:key1=value1");
475+
StashPullRequestComment comment2 = new StashPullRequestComment(2, "DO TEST\np:key2=value2");
476+
List<StashPullRequestComment> comments = Arrays.asList(comment2, comment1);
477+
when(stashApiClient.getPullRequestComments(any(), any(), any())).thenReturn(comments);
478+
when(trigger.getCiBuildPhrases()).thenReturn("DO TEST");
479+
when(trigger.getOnlyBuildOnComment()).thenReturn(true);
480+
when(trigger.getCancelOutdatedJobsEnabled()).thenReturn(false);
481+
482+
List<StashPullRequestBuildTarget> buildTargets = stashRepository.getBuildTargets(pullRequest);
483+
484+
assertThat(buildTargets, hasSize(2));
485+
assertThat(
486+
buildTargets.get(0), hasProperty("additionalParameters", hasEntry("key1", "value1")));
487+
assertThat(
488+
buildTargets.get(1), hasProperty("additionalParameters", hasEntry("key2", "value2")));
470489
}
471490

472491
@Test

0 commit comments

Comments
 (0)