Skip to content

Commit 71fd537

Browse files
Format
1 parent 98853c8 commit 71fd537

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.regex.Matcher;
3333
import java.util.regex.Pattern;
3434
import java.util.stream.Collectors;
35-
3635
import javax.annotation.Nonnull;
3736
import javax.annotation.Nullable;
3837
import jenkins.model.Jenkins;
@@ -148,16 +147,13 @@ private boolean shouldSkip(StashPullRequestResponseValue pullRequest) {
148147

149148
private boolean isStatusMessage(String content) {
150149
String escapedBuildName = Pattern.quote(job.getDisplayName());
151-
String[] patterns = {
152-
BUILD_START_REGEX,
153-
BUILD_FINISH_REGEX,
154-
BUILD_CANCEL_REGEX
155-
};
150+
String[] patterns = {BUILD_START_REGEX, BUILD_FINISH_REGEX, BUILD_CANCEL_REGEX};
156151
for (String pattern : patterns) {
157152
String buildStatusMessage = String.format(pattern, escapedBuildName);
158-
Matcher matcher = Pattern.compile(buildStatusMessage, Pattern.CASE_INSENSITIVE).matcher(content);
153+
Matcher matcher =
154+
Pattern.compile(buildStatusMessage, Pattern.CASE_INSENSITIVE).matcher(content);
159155
if (matcher.find()) {
160-
return true;
156+
return true;
161157
}
162158
}
163159
return false;
@@ -190,26 +186,34 @@ public Collection<StashPullRequestBuildTarget> getBuildTargets(
190186
if (!isOnlyBuildOnComment) {
191187
return getBuildTargetsWithoutOnlyBuildOnCommentLogic(pullRequest, comments);
192188
}
193-
Collection<StashPullRequestBuildTarget> buildTargets = getBuildTargetsWithOnlyBuildOnCommentLogic(pullRequest,
194-
comments);
189+
Collection<StashPullRequestBuildTarget> buildTargets =
190+
getBuildTargetsWithOnlyBuildOnCommentLogic(pullRequest, comments);
195191

196192
if (!trigger.getCancelOutdatedJobsEnabled()) {
197193
return buildTargets;
198194
}
199195
List<StashPullRequestBuildTarget> buildTargetsList = new ArrayList<>(buildTargets);
200-
buildTargetsList.sort(Comparator.comparing(StashPullRequestBuildTarget::getBuildCommandCommentId).reversed());
201-
Collection<StashPullRequestBuildTarget> buildTargetsToKeep = buildTargetsList.stream().limit(1)
202-
.collect(Collectors.toList());
203-
Collection<StashPullRequestBuildTarget> buildTargetsToCancel = buildTargetsList.stream().skip(1)
204-
.collect(Collectors.toList());
196+
buildTargetsList.sort(
197+
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());
205202
try {
206203
for (StashPullRequestBuildTarget buildTarget : buildTargetsToCancel) {
207204
postBuildCancelComment(pullRequest, buildTarget.getBuildCommandCommentId());
208205
}
209206
} catch (StashApiException e) {
210-
pollLog.log("Cannot post \\\"BuildCanceled\\\" comment for PR #{}, not building", pullRequest.getId(), e);
211-
logger.log(Level.INFO, format("%s: cannot post Build Cancel comment for pull request %s, not building",
212-
job.getFullName(), pullRequest.getId()), e);
207+
pollLog.log(
208+
"Cannot post \\\"BuildCanceled\\\" comment for PR #{}, not building",
209+
pullRequest.getId(),
210+
e);
211+
logger.log(
212+
Level.INFO,
213+
format(
214+
"%s: cannot post Build Cancel comment for pull request %s, not building",
215+
job.getFullName(), pullRequest.getId()),
216+
e);
213217
return new ArrayList<>();
214218
}
215219
return buildTargetsToKeep;
@@ -228,8 +232,7 @@ private Collection<StashPullRequestBuildTarget> getBuildTargetsWithOnlyBuildOnCo
228232
if (isPhrasesContain(content, this.trigger.getCiBuildPhrases())) {
229233
if (comment.getReplies() != null
230234
&& !comment.getReplies().isEmpty()
231-
&& comment.getReplies().stream()
232-
.anyMatch(reply -> isStatusMessage(reply.getText()))) {
235+
&& comment.getReplies().stream().anyMatch(reply -> isStatusMessage(reply.getText()))) {
233236
continue;
234237
}
235238

@@ -329,12 +332,10 @@ private String postBuildStatusComment(
329332
throws StashApiException {
330333
String sourceCommit = pullRequest.getFromRef().getLatestCommit();
331334
String destinationCommit = pullRequest.getToRef().getLatestCommit();
332-
String comment =
333-
format(marker, job.getDisplayName(), sourceCommit, destinationCommit);
335+
String comment = format(marker, job.getDisplayName(), sourceCommit, destinationCommit);
334336
StashPullRequestComment commentResponse;
335337
commentResponse =
336-
this.client.postPullRequestComment(
337-
pullRequest.getId(), comment, buildCommandCommentId);
338+
this.client.postPullRequestComment(pullRequest.getId(), comment, buildCommandCommentId);
338339
return commentResponse.getCommentId().toString();
339340
}
340341

src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ public StashPullRequestComment postPullRequestComment(String pullRequestId, Stri
161161
}
162162

163163
@Nullable
164-
public StashPullRequestComment postPullRequestComment(String pullRequestId, String comment, Integer replyCommentId)
165-
throws StashApiException {
164+
public StashPullRequestComment postPullRequestComment(
165+
String pullRequestId, String comment, Integer replyCommentId) throws StashApiException {
166166
String path = pullRequestPath(pullRequestId) + "/comments";
167167
ObjectNode payload = mapper.getNodeFactory().objectNode();
168168
payload.put("text", comment);

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

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import static org.hamcrest.Matchers.hasSize;
1717
import static org.hamcrest.Matchers.is;
1818
import static org.hamcrest.Matchers.notNullValue;
19-
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
2019
import static org.mockito.Mockito.any;
2120
import static org.mockito.Mockito.doThrow;
2221
import static org.mockito.Mockito.eq;
@@ -26,6 +25,7 @@
2625
import static org.mockito.Mockito.times;
2726
import static org.mockito.Mockito.verify;
2827
import static org.mockito.Mockito.when;
28+
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
2929

3030
import hudson.model.BooleanParameterDefinition;
3131
import hudson.model.Executor;
@@ -41,7 +41,6 @@
4141
import hudson.model.StringParameterDefinition;
4242
import hudson.model.StringParameterValue;
4343
import hudson.util.RunList;
44-
4544
import java.util.Arrays;
4645
import java.util.Collection;
4746
import java.util.Collections;
@@ -471,8 +470,7 @@ public void getBuildTargets_onlyBuildOnComment_multiple_comments_generate_multip
471470
}
472471

473472
@Test
474-
public void getBuildTargets_onlyBuildOnComment_skip_if_build_status_message()
475-
throws Exception {
473+
public void getBuildTargets_onlyBuildOnComment_skip_if_build_status_message() throws Exception {
476474
when(trigger.getOnlyBuildOnComment()).thenReturn(true);
477475
when(trigger.getCiBuildPhrases()).thenReturn("DO TEST");
478476
when(project.getFullName()).thenReturn("MyProject");
@@ -502,15 +500,15 @@ public void getBuildTargets_onlyBuildOnComment_skip_if_build_status_message()
502500
}
503501

504502
@Test
505-
public void getBuildTargets_onlyBuildOnComment_cancelOutdatedJobsEnabled_multiple_comments_generate_single_target()
506-
throws Exception {
507-
StashPullRequestComment comment1 =
508-
new StashPullRequestComment(1, "DO TEST\np:key1=value1");
509-
StashPullRequestComment comment2 =
510-
new StashPullRequestComment(2, "DO TEST\np:key2=value2");
503+
public void
504+
getBuildTargets_onlyBuildOnComment_cancelOutdatedJobsEnabled_multiple_comments_generate_single_target()
505+
throws Exception {
506+
StashPullRequestComment comment1 = new StashPullRequestComment(1, "DO TEST\np:key1=value1");
507+
StashPullRequestComment comment2 = new StashPullRequestComment(2, "DO TEST\np:key2=value2");
511508
List<StashPullRequestComment> comments = Arrays.asList(comment1, comment2);
512509
when(stashApiClient.getPullRequestComments(any(), any(), any())).thenReturn(comments);
513-
when(stashApiClient.postPullRequestComment(any(), any(), any())).thenReturn(new StashPullRequestComment(3, null));
510+
when(stashApiClient.postPullRequestComment(any(), any(), any()))
511+
.thenReturn(new StashPullRequestComment(3, null));
514512
when(trigger.getCiBuildPhrases()).thenReturn("DO TEST");
515513
when(trigger.getOnlyBuildOnComment()).thenReturn(true);
516514
when(trigger.getCancelOutdatedJobsEnabled()).thenReturn(true);
@@ -524,29 +522,27 @@ public void getBuildTargets_onlyBuildOnComment_cancelOutdatedJobsEnabled_multipl
524522
contains(
525523
allOf(
526524
hasProperty("additionalParameters", aMapWithSize(1)),
527-
hasProperty(
528-
"additionalParameters",
529-
hasEntry("key2", "value2")))
530-
));
525+
hasProperty("additionalParameters", hasEntry("key2", "value2")))));
531526
}
532527

533-
@Test
534-
public void getBuildTargets_onlyBuildOnComment_cancelOutdatedJobsEnabled_posts_build_cancel_comment()
535-
throws Exception {
536-
StashPullRequestComment comment1 =
537-
new StashPullRequestComment(1, "DO TEST\np:key1=value1");
538-
StashPullRequestComment comment2 =
539-
new StashPullRequestComment(2, "DO TEST\np:key2=value2");
528+
@Test
529+
public void
530+
getBuildTargets_onlyBuildOnComment_cancelOutdatedJobsEnabled_posts_build_cancel_comment()
531+
throws Exception {
532+
StashPullRequestComment comment1 = new StashPullRequestComment(1, "DO TEST\np:key1=value1");
533+
StashPullRequestComment comment2 = new StashPullRequestComment(2, "DO TEST\np:key2=value2");
540534
List<StashPullRequestComment> comments = Arrays.asList(comment1, comment2);
541535
when(stashApiClient.getPullRequestComments(any(), any(), any())).thenReturn(comments);
542-
when(stashApiClient.postPullRequestComment(any(), any(), any())).thenReturn(new StashPullRequestComment(3, null));
536+
when(stashApiClient.postPullRequestComment(any(), any(), any()))
537+
.thenReturn(new StashPullRequestComment(3, null));
543538
when(trigger.getCiBuildPhrases()).thenReturn("DO TEST");
544539
when(trigger.getOnlyBuildOnComment()).thenReturn(true);
545540
when(trigger.getCancelOutdatedJobsEnabled()).thenReturn(true);
546541

547542
stashRepository.getBuildTargets(pullRequest);
548543

549-
verify(stashApiClient).postPullRequestComment(any(), argThat(containsString("BuildCanceled")), eq(1));
544+
verify(stashApiClient)
545+
.postPullRequestComment(any(), argThat(containsString("BuildCanceled")), eq(1));
550546
}
551547

552548
@Test
@@ -585,7 +581,8 @@ public void addFutureBuildTasks_removes_outdated_jobs_if_enabled() throws Except
585581
}
586582

587583
@Test
588-
public void addFutureBuildTasks_does_not_remove_old_jobs_if_cancelOutdatedJobs_is_disabled() throws Exception {
584+
public void addFutureBuildTasks_does_not_remove_old_jobs_if_cancelOutdatedJobs_is_disabled()
585+
throws Exception {
589586
when(trigger.getCancelOutdatedJobsEnabled()).thenReturn(false);
590587
when(trigger.getStashHost()).thenReturn("StashHost");
591588
when(project.getFullName()).thenReturn("MyProject");
@@ -598,16 +595,20 @@ public void addFutureBuildTasks_does_not_remove_old_jobs_if_cancelOutdatedJobs_i
598595
assertThat(Jenkins.getInstance().getQueue().getItems(), is(arrayWithSize(2)));
599596
}
600597

601-
@Test
598+
@Test
602599
public void addFutureBuildTasks_cancels_running_job_if_enabled() throws Exception {
603600
when(trigger.getCancelOutdatedJobsEnabled()).thenReturn(true);
604601
when(trigger.getStashHost()).thenReturn("StashHost");
605602
StashPullRequestComment response = new StashPullRequestComment(3, null);
606603
when(stashApiClient.postPullRequestComment(any(), any(), any())).thenReturn(response);
607604
FreeStyleBuild runningBuild = mock(FreeStyleBuild.class);
608605
when(runningBuild.isBuilding()).thenReturn(true);
609-
when(runningBuild.getCauses()).thenReturn(Arrays.asList(
610-
new StashCause("", null, null, null, null, null, null, null, null, null, null, null, null, null, null)));
606+
when(runningBuild.getCauses())
607+
.thenReturn(
608+
Arrays.asList(
609+
new StashCause(
610+
"", null, null, null, null, null, null, null, null, null, null, null, null,
611+
null, null)));
611612
when(project.getBuilds()).thenReturn(RunList.fromRuns(Arrays.asList(runningBuild)));
612613
Executor executor = mock(Executor.class);
613614
when(runningBuild.getExecutor()).thenReturn(executor);
@@ -618,8 +619,9 @@ public void addFutureBuildTasks_cancels_running_job_if_enabled() throws Exceptio
618619
assertThat(Jenkins.getInstance().getQueue().getItems(), is(arrayWithSize(1)));
619620
}
620621

621-
@Test
622-
public void addFutureBuildTasks_does_not_cancel_running_job_if_cancelOutdatedJobs_is_disabled() throws Exception {
622+
@Test
623+
public void addFutureBuildTasks_does_not_cancel_running_job_if_cancelOutdatedJobs_is_disabled()
624+
throws Exception {
623625
when(trigger.getCancelOutdatedJobsEnabled()).thenReturn(false);
624626
when(trigger.getStashHost()).thenReturn("StashHost");
625627
StashPullRequestComment response = new StashPullRequestComment(3, null);

0 commit comments

Comments
 (0)