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

Commit ad97c91

Browse files
committed
Hiding project-level buttons from other projects #139
1 parent 0230d6d commit ad97c91

File tree

3 files changed

+115
-45
lines changed

3 files changed

+115
-45
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
Changelog of Pull Request Notifier for Bitbucket.
44

55
## Unreleased
6+
### GitHub [#139](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/139) Bitbucket - PR Button created at project level shows up for other projects in the same host
7+
Hiding project-level buttons from other projects
8+
9+
[47b1aaea2806d9d](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/47b1aaea2806d9d) Tomas Bjerre *2016-08-12 15:32:23*
10+
611
### No issue
712
doc
813

9-
[8a3438c4f5d045e](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/8a3438c4f5d045e) Tomas Bjerre *2016-08-11 18:07:30*
14+
[0230d6dd0ea44f2](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/0230d6dd0ea44f2) Tomas Bjerre *2016-08-11 18:07:42*
1015

1116
## 2.35
1217
### GitHub [#132](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/132) How to trigger Jenkins 2.1 with parameters

src/main/java/se/bjurr/prnfb/service/ButtonsService.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,6 @@ private boolean isVisibleOnPullRequest(PrnfbButton button, PullRequest pullReque
8585
|| (pullRequest.getToRef() != null && isVisibleOnRepository(button, pullRequest.getToRef().getRepository()));
8686
}
8787

88-
/**
89-
* Checks if the given button is visible in the given repository.
90-
*
91-
* @param button
92-
* Button under test
93-
* @param repository
94-
* Repository to check for
95-
* @return True if the button is either globally visible or matches with the
96-
* given repository
97-
*/
98-
private boolean isVisibleOnRepository(PrnfbButton button, Repository repository) {
99-
if (button.getRepositorySlug().isPresent()) {
100-
boolean visible = false;
101-
do {
102-
visible |= button.getProjectKey().get().equals(repository.getProject().getKey())
103-
&& button.getRepositorySlug().get().equals(repository.getSlug());
104-
} while (!visible && (repository = repository.getOrigin()) != null);
105-
return visible;
106-
} else {
107-
return TRUE;
108-
}
109-
}
110-
11188
@VisibleForTesting
11289
List<PrnfbButton> doGetButtons(List<PrnfbNotification> notifications, ClientKeyStore clientKeyStore,
11390
final PullRequest pullRequest, boolean shouldAcceptAnyCertificate) {
@@ -156,4 +133,35 @@ Map<PrnfbVariable, Supplier<String>> getVariables(final UUID uuid) {
156133
return variables;
157134
}
158135

136+
/**
137+
* Checks if the given button is visible in the given repository.
138+
*
139+
* @param button
140+
* Button under test
141+
* @param repository
142+
* Repository to check for
143+
* @return True if the button is either globally visible or matches with the
144+
* given repository
145+
*/
146+
@VisibleForTesting
147+
boolean isVisibleOnRepository(PrnfbButton button, Repository repository) {
148+
boolean projectOk = false;
149+
boolean repoOk = false;
150+
151+
do {
152+
if (button.getProjectKey().isPresent()) {
153+
projectOk |= button.getProjectKey().get().equals(repository.getProject().getKey());
154+
} else {
155+
projectOk = true;
156+
}
157+
if (button.getRepositorySlug().isPresent()) {
158+
repoOk |= button.getRepositorySlug().get().equals(repository.getSlug());
159+
} else {
160+
repoOk = true;
161+
}
162+
} while (!(projectOk && repoOk) && (repository = repository.getOrigin()) != null);
163+
164+
return projectOk && repoOk;
165+
}
166+
159167
}

src/test/java/se/bjurr/prnfb/service/ButtonsServiceTest.java

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import se.bjurr.prnfb.listener.PrnfbPullRequestEventListener;
2828
import se.bjurr.prnfb.presentation.dto.ButtonDTO;
2929
import se.bjurr.prnfb.presentation.dto.NotificationDTO;
30+
import se.bjurr.prnfb.presentation.dto.ON_OR_OFF;
3031
import se.bjurr.prnfb.settings.PrnfbButton;
3132
import se.bjurr.prnfb.settings.PrnfbNotification;
33+
import se.bjurr.prnfb.settings.USER_LEVEL;
3234
import se.bjurr.prnfb.settings.ValidationException;
3335

3436
import com.atlassian.bitbucket.auth.AuthenticationContext;
@@ -39,7 +41,6 @@
3941
import com.atlassian.bitbucket.repository.Repository;
4042
import com.atlassian.bitbucket.repository.RepositoryService;
4143
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
42-
import com.google.common.base.Optional;
4344
import com.google.common.collect.Lists;
4445

4546
public class ButtonsServiceTest {
@@ -54,40 +55,44 @@ public class ButtonsServiceTest {
5455
private ButtonDTO buttonDto3;
5556
@Mock
5657
private ClientKeyStore clientKeyStore;
58+
private final ON_OR_OFF confirmation = ON_OR_OFF.on;
59+
private final String name = "name";
5760
private PrnfbNotification notification1;
5861
private PrnfbNotification notification2;
5962
private NotificationDTO notificationDto1;
6063
private NotificationDTO notificationDto2;
6164
private List<PrnfbNotification> notifications;
6265
@Mock
66+
private Repository originRepo;
67+
@Mock
6368
private PrnfbPullRequestEventListener prnfbPullRequestEventListener;
6469
@Mock
6570
private PrnfbRendererFactory prnfbRendererFactory;
6671
@Mock
72+
private Project project;
73+
@Mock
6774
private ApplicationPropertiesService propertiesService;
6875
@Mock
76+
private PullRequestRef prRef;
77+
@Mock
6978
private PullRequest pullRequest;
7079
private final PrnfbPullRequestAction pullRequestAction = BUTTON_TRIGGER;
7180
@Mock
7281
private PullRequestService pullRequestService;
7382
@Mock
7483
private PrnfbRenderer renderer;
7584
@Mock
85+
private Repository repository;
86+
@Mock
7687
private RepositoryService repositoryService;
7788
@Mock
7889
private SettingsService settingsService;
7990
private final Boolean shouldAcceptAnyCertificate = true;
8091
private ButtonsService sut;
8192
@Mock
8293
private UserCheckService userCheckService;
83-
@Mock
84-
private PullRequestRef prRef;
85-
@Mock
86-
private Repository repository;
87-
@Mock
88-
private Repository originRepo;
89-
@Mock
90-
private Project project;
94+
private final USER_LEVEL userLevel = USER_LEVEL.ADMIN;
95+
private final UUID uuid = UUID.randomUUID();
9196

9297
@SuppressWarnings("unchecked")
9398
@Before
@@ -114,9 +119,9 @@ public void before() throws ValidationException {
114119
when(this.settingsService.getButton(this.button1.getUuid()))//
115120
.thenReturn(this.button1);
116121
when(this.settingsService.getButton(this.button2.getUuid()))//
117-
.thenReturn(this.button2);
122+
.thenReturn(this.button2);
118123
when(this.settingsService.getButton(this.button3.getUuid()))//
119-
.thenReturn(this.button3);
124+
.thenReturn(this.button3);
120125

121126
this.notificationDto1 = populatedInstanceOf(NotificationDTO.class);
122127
this.notificationDto1.setUrl("http://hej.com");
@@ -143,9 +148,9 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
143148
when(this.userCheckService.isAllowedUseButton(this.button1))//
144149
.thenReturn(true);
145150
when(this.userCheckService.isAllowedUseButton(this.button2))//
146-
.thenReturn(true);
151+
.thenReturn(true);
147152
when(this.userCheckService.isAllowedUseButton(this.button3))//
148-
.thenReturn(true);
153+
.thenReturn(true);
149154
when(
150155
this.prnfbPullRequestEventListener.isNotificationTriggeredByAction(this.notification1, this.pullRequestAction,
151156
this.renderer, this.pullRequest, this.clientKeyStore, this.shouldAcceptAnyCertificate))//
@@ -154,11 +159,11 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
154159
this.prnfbPullRequestEventListener.isNotificationTriggeredByAction(this.notification2, this.pullRequestAction,
155160
this.renderer, this.pullRequest, this.clientKeyStore, this.shouldAcceptAnyCertificate))//
156161
.thenReturn(true);
157-
when(this.pullRequest.getToRef()).thenReturn(prRef);
158-
when(this.prRef.getRepository()).thenReturn(repository);
159-
when(this.repository.getSlug()).thenReturn(button3.getRepositorySlug().get());
160-
when(this.repository.getProject()).thenReturn(project);
161-
when(this.project.getKey()).thenReturn(button3.getProjectKey().get());
162+
when(this.pullRequest.getToRef()).thenReturn(this.prRef);
163+
when(this.prRef.getRepository()).thenReturn(this.repository);
164+
when(this.repository.getSlug()).thenReturn(this.button3.getRepositorySlug().get());
165+
when(this.repository.getProject()).thenReturn(this.project);
166+
when(this.project.getKey()).thenReturn(this.button3.getProjectKey().get());
162167

163168
List<PrnfbButton> actual = this.sut.doGetButtons(this.notifications, this.clientKeyStore, this.pullRequest,
164169
this.shouldAcceptAnyCertificate);
@@ -173,9 +178,9 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
173178
.containsOnly(this.button1, this.button2);
174179

175180
// Now check if the button is inherited from the origin repo
176-
when(this.repository.getOrigin()).thenReturn(originRepo);
177-
when(this.originRepo.getSlug()).thenReturn(button3.getRepositorySlug().get());
178-
when(this.originRepo.getProject()).thenReturn(project);
181+
when(this.repository.getOrigin()).thenReturn(this.originRepo);
182+
when(this.originRepo.getSlug()).thenReturn(this.button3.getRepositorySlug().get());
183+
when(this.originRepo.getProject()).thenReturn(this.project);
179184
actual = this.sut.doGetButtons(this.notifications, this.clientKeyStore, this.pullRequest,
180185
this.shouldAcceptAnyCertificate);
181186
assertThat(actual)//
@@ -221,4 +226,56 @@ public void testThatPressedButtonDoesTriggerIfMatchingNotification() {
221226
verify(this.prnfbPullRequestEventListener, times(2))//
222227
.notify(any(), any(), any(), any(), any(), any());
223228
}
229+
230+
@Test
231+
public void testVisibilityOnPullRequest() {
232+
String buttonProjectKey = "proj";
233+
String buttonRepositorySlug = "repo";
234+
String repoProjectKey = "proj";
235+
String repoRepositorySlug = "repo";
236+
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, true);
237+
238+
buttonProjectKey = "proj";
239+
buttonRepositorySlug = "repo";
240+
repoProjectKey = "proj2";
241+
repoRepositorySlug = "repo";
242+
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, false);
243+
244+
buttonProjectKey = "proj";
245+
buttonRepositorySlug = null;
246+
repoProjectKey = "proj";
247+
repoRepositorySlug = "repo";
248+
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, true);
249+
250+
buttonProjectKey = "proj";
251+
buttonRepositorySlug = null;
252+
repoProjectKey = "proj2";
253+
repoRepositorySlug = "repo";
254+
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, false);
255+
256+
buttonProjectKey = "proj";
257+
buttonRepositorySlug = "repo";
258+
repoProjectKey = "proj";
259+
repoRepositorySlug = "repo2";
260+
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, false);
261+
262+
buttonProjectKey = "proj";
263+
buttonRepositorySlug = "repo";
264+
repoProjectKey = "proj2";
265+
repoRepositorySlug = "repo2";
266+
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, false);
267+
}
268+
269+
private void testVisibilityOnRepository(String buttonProjectKey, String buttonRepositorySlug, String repoProjectKey,
270+
String repoRepoSlug, boolean expected) {
271+
PrnfbButton button = new PrnfbButton(this.uuid, this.name, this.userLevel, this.confirmation, buttonProjectKey,
272+
buttonRepositorySlug);
273+
when(this.repository.getProject()).thenReturn(this.project);
274+
when(this.repository.getProject().getKey())//
275+
.thenReturn(repoProjectKey);
276+
when(this.repository.getSlug())//
277+
.thenReturn(repoRepoSlug);
278+
assertThat(this.sut.isVisibleOnRepository(button, this.repository))//
279+
.isEqualTo(expected);
280+
}
224281
}

0 commit comments

Comments
 (0)