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

Commit

Permalink
Hiding project-level buttons from other projects #139
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Aug 12, 2016
1 parent 0230d6d commit ad97c91
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 45 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### 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
Hiding project-level buttons from other projects

[47b1aaea2806d9d](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/47b1aaea2806d9d) Tomas Bjerre *2016-08-12 15:32:23*

### No issue
doc

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

## 2.35
### GitHub [#132](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/132) How to trigger Jenkins 2.1 with parameters
Expand Down
54 changes: 31 additions & 23 deletions src/main/java/se/bjurr/prnfb/service/ButtonsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,6 @@ private boolean isVisibleOnPullRequest(PrnfbButton button, PullRequest pullReque
|| (pullRequest.getToRef() != null && isVisibleOnRepository(button, pullRequest.getToRef().getRepository()));
}

/**
* Checks if the given button is visible in the given repository.
*
* @param button
* Button under test
* @param repository
* Repository to check for
* @return True if the button is either globally visible or matches with the
* given repository
*/
private boolean isVisibleOnRepository(PrnfbButton button, Repository repository) {
if (button.getRepositorySlug().isPresent()) {
boolean visible = false;
do {
visible |= button.getProjectKey().get().equals(repository.getProject().getKey())
&& button.getRepositorySlug().get().equals(repository.getSlug());
} while (!visible && (repository = repository.getOrigin()) != null);
return visible;
} else {
return TRUE;
}
}

@VisibleForTesting
List<PrnfbButton> doGetButtons(List<PrnfbNotification> notifications, ClientKeyStore clientKeyStore,
final PullRequest pullRequest, boolean shouldAcceptAnyCertificate) {
Expand Down Expand Up @@ -156,4 +133,35 @@ Map<PrnfbVariable, Supplier<String>> getVariables(final UUID uuid) {
return variables;
}

/**
* Checks if the given button is visible in the given repository.
*
* @param button
* Button under test
* @param repository
* Repository to check for
* @return True if the button is either globally visible or matches with the
* given repository
*/
@VisibleForTesting
boolean isVisibleOnRepository(PrnfbButton button, Repository repository) {
boolean projectOk = false;
boolean repoOk = false;

do {
if (button.getProjectKey().isPresent()) {
projectOk |= button.getProjectKey().get().equals(repository.getProject().getKey());
} else {
projectOk = true;
}
if (button.getRepositorySlug().isPresent()) {
repoOk |= button.getRepositorySlug().get().equals(repository.getSlug());
} else {
repoOk = true;
}
} while (!(projectOk && repoOk) && (repository = repository.getOrigin()) != null);

return projectOk && repoOk;
}

}
99 changes: 78 additions & 21 deletions src/test/java/se/bjurr/prnfb/service/ButtonsServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import se.bjurr.prnfb.listener.PrnfbPullRequestEventListener;
import se.bjurr.prnfb.presentation.dto.ButtonDTO;
import se.bjurr.prnfb.presentation.dto.NotificationDTO;
import se.bjurr.prnfb.presentation.dto.ON_OR_OFF;
import se.bjurr.prnfb.settings.PrnfbButton;
import se.bjurr.prnfb.settings.PrnfbNotification;
import se.bjurr.prnfb.settings.USER_LEVEL;
import se.bjurr.prnfb.settings.ValidationException;

import com.atlassian.bitbucket.auth.AuthenticationContext;
Expand All @@ -39,7 +41,6 @@
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.repository.RepositoryService;
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;

public class ButtonsServiceTest {
Expand All @@ -54,40 +55,44 @@ public class ButtonsServiceTest {
private ButtonDTO buttonDto3;
@Mock
private ClientKeyStore clientKeyStore;
private final ON_OR_OFF confirmation = ON_OR_OFF.on;
private final String name = "name";
private PrnfbNotification notification1;
private PrnfbNotification notification2;
private NotificationDTO notificationDto1;
private NotificationDTO notificationDto2;
private List<PrnfbNotification> notifications;
@Mock
private Repository originRepo;
@Mock
private PrnfbPullRequestEventListener prnfbPullRequestEventListener;
@Mock
private PrnfbRendererFactory prnfbRendererFactory;
@Mock
private Project project;
@Mock
private ApplicationPropertiesService propertiesService;
@Mock
private PullRequestRef prRef;
@Mock
private PullRequest pullRequest;
private final PrnfbPullRequestAction pullRequestAction = BUTTON_TRIGGER;
@Mock
private PullRequestService pullRequestService;
@Mock
private PrnfbRenderer renderer;
@Mock
private Repository repository;
@Mock
private RepositoryService repositoryService;
@Mock
private SettingsService settingsService;
private final Boolean shouldAcceptAnyCertificate = true;
private ButtonsService sut;
@Mock
private UserCheckService userCheckService;
@Mock
private PullRequestRef prRef;
@Mock
private Repository repository;
@Mock
private Repository originRepo;
@Mock
private Project project;
private final USER_LEVEL userLevel = USER_LEVEL.ADMIN;
private final UUID uuid = UUID.randomUUID();

@SuppressWarnings("unchecked")
@Before
Expand All @@ -114,9 +119,9 @@ public void before() throws ValidationException {
when(this.settingsService.getButton(this.button1.getUuid()))//
.thenReturn(this.button1);
when(this.settingsService.getButton(this.button2.getUuid()))//
.thenReturn(this.button2);
.thenReturn(this.button2);
when(this.settingsService.getButton(this.button3.getUuid()))//
.thenReturn(this.button3);
.thenReturn(this.button3);

this.notificationDto1 = populatedInstanceOf(NotificationDTO.class);
this.notificationDto1.setUrl("http://hej.com");
Expand All @@ -143,9 +148,9 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
when(this.userCheckService.isAllowedUseButton(this.button1))//
.thenReturn(true);
when(this.userCheckService.isAllowedUseButton(this.button2))//
.thenReturn(true);
.thenReturn(true);
when(this.userCheckService.isAllowedUseButton(this.button3))//
.thenReturn(true);
.thenReturn(true);
when(
this.prnfbPullRequestEventListener.isNotificationTriggeredByAction(this.notification1, this.pullRequestAction,
this.renderer, this.pullRequest, this.clientKeyStore, this.shouldAcceptAnyCertificate))//
Expand All @@ -154,11 +159,11 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
this.prnfbPullRequestEventListener.isNotificationTriggeredByAction(this.notification2, this.pullRequestAction,
this.renderer, this.pullRequest, this.clientKeyStore, this.shouldAcceptAnyCertificate))//
.thenReturn(true);
when(this.pullRequest.getToRef()).thenReturn(prRef);
when(this.prRef.getRepository()).thenReturn(repository);
when(this.repository.getSlug()).thenReturn(button3.getRepositorySlug().get());
when(this.repository.getProject()).thenReturn(project);
when(this.project.getKey()).thenReturn(button3.getProjectKey().get());
when(this.pullRequest.getToRef()).thenReturn(this.prRef);
when(this.prRef.getRepository()).thenReturn(this.repository);
when(this.repository.getSlug()).thenReturn(this.button3.getRepositorySlug().get());
when(this.repository.getProject()).thenReturn(this.project);
when(this.project.getKey()).thenReturn(this.button3.getProjectKey().get());

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

// Now check if the button is inherited from the origin repo
when(this.repository.getOrigin()).thenReturn(originRepo);
when(this.originRepo.getSlug()).thenReturn(button3.getRepositorySlug().get());
when(this.originRepo.getProject()).thenReturn(project);
when(this.repository.getOrigin()).thenReturn(this.originRepo);
when(this.originRepo.getSlug()).thenReturn(this.button3.getRepositorySlug().get());
when(this.originRepo.getProject()).thenReturn(this.project);
actual = this.sut.doGetButtons(this.notifications, this.clientKeyStore, this.pullRequest,
this.shouldAcceptAnyCertificate);
assertThat(actual)//
Expand Down Expand Up @@ -221,4 +226,56 @@ public void testThatPressedButtonDoesTriggerIfMatchingNotification() {
verify(this.prnfbPullRequestEventListener, times(2))//
.notify(any(), any(), any(), any(), any(), any());
}

@Test
public void testVisibilityOnPullRequest() {
String buttonProjectKey = "proj";
String buttonRepositorySlug = "repo";
String repoProjectKey = "proj";
String repoRepositorySlug = "repo";
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, true);

buttonProjectKey = "proj";
buttonRepositorySlug = "repo";
repoProjectKey = "proj2";
repoRepositorySlug = "repo";
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, false);

buttonProjectKey = "proj";
buttonRepositorySlug = null;
repoProjectKey = "proj";
repoRepositorySlug = "repo";
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, true);

buttonProjectKey = "proj";
buttonRepositorySlug = null;
repoProjectKey = "proj2";
repoRepositorySlug = "repo";
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, false);

buttonProjectKey = "proj";
buttonRepositorySlug = "repo";
repoProjectKey = "proj";
repoRepositorySlug = "repo2";
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, false);

buttonProjectKey = "proj";
buttonRepositorySlug = "repo";
repoProjectKey = "proj2";
repoRepositorySlug = "repo2";
testVisibilityOnRepository(buttonProjectKey, buttonRepositorySlug, repoProjectKey, repoRepositorySlug, false);
}

private void testVisibilityOnRepository(String buttonProjectKey, String buttonRepositorySlug, String repoProjectKey,
String repoRepoSlug, boolean expected) {
PrnfbButton button = new PrnfbButton(this.uuid, this.name, this.userLevel, this.confirmation, buttonProjectKey,
buttonRepositorySlug);
when(this.repository.getProject()).thenReturn(this.project);
when(this.repository.getProject().getKey())//
.thenReturn(repoProjectKey);
when(this.repository.getSlug())//
.thenReturn(repoRepoSlug);
assertThat(this.sut.isVisibleOnRepository(button, this.repository))//
.isEqualTo(expected);
}
}

0 comments on commit ad97c91

Please sign in to comment.