Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit 1cc9dc3

Browse files
committed
Checking if canMerge with escalated (ADMIN) permission #221
1 parent 5c6a81b commit 1cc9dc3

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

src/main/java/se/bjurr/prnfb/listener/PrnfbPullRequestEventListener.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package se.bjurr.prnfb.listener;
22

3+
import static com.atlassian.bitbucket.permission.Permission.ADMIN;
34
import static com.google.common.base.Optional.absent;
45
import static com.google.common.base.Optional.of;
56
import static java.lang.Boolean.FALSE;
@@ -16,6 +17,22 @@
1617

1718
import org.slf4j.Logger;
1819

20+
import se.bjurr.prnfb.http.ClientKeyStore;
21+
import se.bjurr.prnfb.http.HttpResponse;
22+
import se.bjurr.prnfb.http.Invoker;
23+
import se.bjurr.prnfb.http.NotificationResponse;
24+
import se.bjurr.prnfb.http.UrlInvoker;
25+
import se.bjurr.prnfb.service.PrnfbRenderer;
26+
import se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR;
27+
import se.bjurr.prnfb.service.PrnfbRendererFactory;
28+
import se.bjurr.prnfb.service.SettingsService;
29+
import se.bjurr.prnfb.service.VariablesContext;
30+
import se.bjurr.prnfb.service.VariablesContext.VariablesContextBuilder;
31+
import se.bjurr.prnfb.settings.PrnfbHeader;
32+
import se.bjurr.prnfb.settings.PrnfbNotification;
33+
import se.bjurr.prnfb.settings.PrnfbSettingsData;
34+
import se.bjurr.prnfb.settings.TRIGGER_IF_MERGE;
35+
1936
import com.atlassian.bitbucket.event.pull.PullRequestCommentAddedEvent;
2037
import com.atlassian.bitbucket.event.pull.PullRequestCommentDeletedEvent;
2138
import com.atlassian.bitbucket.event.pull.PullRequestCommentEditedEvent;
@@ -31,26 +48,12 @@
3148
import com.atlassian.bitbucket.event.pull.PullRequestUpdatedEvent;
3249
import com.atlassian.bitbucket.pull.PullRequest;
3350
import com.atlassian.bitbucket.pull.PullRequestService;
51+
import com.atlassian.bitbucket.user.SecurityService;
52+
import com.atlassian.bitbucket.util.Operation;
3453
import com.atlassian.event.api.EventListener;
3554
import com.google.common.annotations.VisibleForTesting;
3655
import com.google.common.base.Optional;
3756

38-
import se.bjurr.prnfb.http.ClientKeyStore;
39-
import se.bjurr.prnfb.http.HttpResponse;
40-
import se.bjurr.prnfb.http.Invoker;
41-
import se.bjurr.prnfb.http.NotificationResponse;
42-
import se.bjurr.prnfb.http.UrlInvoker;
43-
import se.bjurr.prnfb.service.PrnfbRenderer;
44-
import se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR;
45-
import se.bjurr.prnfb.service.PrnfbRendererFactory;
46-
import se.bjurr.prnfb.service.SettingsService;
47-
import se.bjurr.prnfb.service.VariablesContext;
48-
import se.bjurr.prnfb.service.VariablesContext.VariablesContextBuilder;
49-
import se.bjurr.prnfb.settings.PrnfbHeader;
50-
import se.bjurr.prnfb.settings.PrnfbNotification;
51-
import se.bjurr.prnfb.settings.PrnfbSettingsData;
52-
import se.bjurr.prnfb.settings.TRIGGER_IF_MERGE;
53-
5457
public class PrnfbPullRequestEventListener {
5558

5659
private static final Logger LOG = getLogger(PrnfbPullRequestEventListener.class);
@@ -64,18 +67,21 @@ public static void setInvoker(Invoker invoker) {
6467
private final ExecutorService executorService;
6568
private final PrnfbRendererFactory prnfbRendererFactory;
6669
private final PullRequestService pullRequestService;
70+
private final SecurityService securityService;
6771

6872
private final SettingsService settingsService;
6973

7074
public PrnfbPullRequestEventListener(
7175
PrnfbRendererFactory prnfbRendererFactory,
7276
PullRequestService pullRequestService,
7377
ExecutorService executorService,
74-
SettingsService settingsService) {
78+
SettingsService settingsService,
79+
SecurityService securityService) {
7580
this.prnfbRendererFactory = prnfbRendererFactory;
7681
this.pullRequestService = pullRequestService;
7782
this.executorService = executorService;
7883
this.settingsService = settingsService;
84+
this.securityService = securityService;
7985
}
8086

8187
private Invoker createInvoker() {
@@ -187,9 +193,19 @@ public boolean isNotificationTriggeredByAction(
187193
if (notification.getTriggerIfCanMerge() != ALWAYS && pullRequest.isOpen()) {
188194
// Cannot perform canMerge unless PR is open
189195
boolean isConflicted =
190-
pullRequestService
191-
.canMerge(pullRequest.getToRef().getRepository().getId(), pullRequest.getId())
192-
.isConflicted();
196+
securityService //
197+
.withPermission(ADMIN, "Can merge") //
198+
.call(
199+
new Operation<Boolean, RuntimeException>() {
200+
@Override
201+
public Boolean perform() throws RuntimeException {
202+
return pullRequestService //
203+
.canMerge(
204+
pullRequest.getToRef().getRepository().getId(),
205+
pullRequest.getId()) //
206+
.isConflicted();
207+
}
208+
});
193209
if (ignoreBecauseOfConflicting(notification.getTriggerIfCanMerge(), isConflicted)) {
194210
return FALSE;
195211
}

src/test/java/se/bjurr/prnfb/listener/PrnfbPullRequestEventListenerTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.Before;
2727
import org.junit.Test;
2828
import org.mockito.Mock;
29+
import org.mockito.Mockito;
2930
import org.mockito.invocation.InvocationOnMock;
3031
import org.mockito.stubbing.Answer;
3132

@@ -49,6 +50,8 @@
4950
import com.atlassian.bitbucket.pull.PullRequestService;
5051
import com.atlassian.bitbucket.repository.Repository;
5152
import com.atlassian.bitbucket.user.ApplicationUser;
53+
import com.atlassian.bitbucket.user.EscalatedSecurityContext;
54+
import com.atlassian.bitbucket.user.SecurityService;
5255
import com.google.common.base.Function;
5356

5457
public class PrnfbPullRequestEventListenerTest {
@@ -87,9 +90,26 @@ public String apply(UrlInvoker input) {
8790
@Before
8891
public void before() throws ValidationException {
8992
initMocks(this);
93+
SecurityService securityService = mock(SecurityService.class);
94+
EscalatedSecurityContext escalatedSecurityContext = mock(EscalatedSecurityContext.class);
95+
when(securityService.withPermission(Mockito.any(), Mockito.any())) //
96+
.thenReturn(escalatedSecurityContext);
97+
when(escalatedSecurityContext.call(Mockito.any())) //
98+
.thenAnswer(
99+
new Answer<Boolean>() {
100+
@Override
101+
public Boolean answer(InvocationOnMock invocation) throws Throwable {
102+
return (Boolean) invocation.callRealMethod();
103+
}
104+
});
105+
90106
sut =
91107
new PrnfbPullRequestEventListener(
92-
prnfbRendererFactory, pullRequestService, executorService, settingsService);
108+
prnfbRendererFactory,
109+
pullRequestService,
110+
executorService,
111+
settingsService,
112+
securityService);
93113
setInvoker(
94114
new Invoker() {
95115
@Override

0 commit comments

Comments
 (0)