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

Commit 2cfaea0

Browse files
author
Stefan Anzinger
committed
Show buttons only on PR where the button belongs to
1 parent eb2b592 commit 2cfaea0

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.atlassian.bitbucket.pull.PullRequest;
2323
import com.atlassian.bitbucket.pull.PullRequestService;
24+
import com.atlassian.bitbucket.repository.Repository;
2425
import com.google.common.annotations.VisibleForTesting;
2526
import com.google.common.base.Supplier;
2627
import com.google.common.base.Suppliers;
@@ -74,6 +75,35 @@ private boolean isTriggeredByAction(ClientKeyStore clientKeyStore, List<PrnfbNot
7475
return FALSE;
7576
}
7677

78+
/**
79+
* Checks if the given button is visible in the given repository.
80+
*
81+
* @param button Button under test
82+
* @param repository Repository to check for
83+
* @return True if the button is either globally visible or matches with the given repository
84+
*/
85+
private boolean isVisibleOnRepository(PrnfbButton button, Repository repository) {
86+
if(button.getRepositorySlug().isPresent()) {
87+
boolean visible = false;
88+
do {
89+
visible |= button.getProjectKey().get().equals(repository.getProject().getKey())
90+
&& button.getRepositorySlug().get().equals(repository.getSlug());
91+
} while(!visible && (repository = repository.getOrigin()) != null);
92+
return visible;
93+
} else {
94+
return TRUE;
95+
}
96+
}
97+
98+
/**
99+
* Checks if the given button is visible on the pull request by either the from or to repository.
100+
*/
101+
private boolean isVisibleOnPullRequest(PrnfbButton button, PullRequest pullRequest) {
102+
return
103+
(pullRequest.getFromRef() != null && isVisibleOnRepository(button, pullRequest.getFromRef().getRepository()))
104+
|| (pullRequest.getToRef() != null && isVisibleOnRepository(button, pullRequest.getToRef().getRepository()));
105+
}
106+
77107
@VisibleForTesting
78108
List<PrnfbButton> doGetButtons(List<PrnfbNotification> notifications, ClientKeyStore clientKeyStore,
79109
final PullRequest pullRequest, boolean shouldAcceptAnyCertificate) {
@@ -83,7 +113,8 @@ List<PrnfbButton> doGetButtons(List<PrnfbNotification> notifications, ClientKeyS
83113
PrnfbPullRequestAction pullRequestAction = BUTTON_TRIGGER;
84114
if (this.userCheckService.isAllowedUseButton(candidate)
85115
&& isTriggeredByAction(clientKeyStore, notifications, shouldAcceptAnyCertificate, pullRequestAction, pullRequest,
86-
variables)) {
116+
variables)
117+
&& (isVisibleOnPullRequest(candidate, pullRequest))) {
87118
allFoundButtons.add(candidate);
88119
}
89120
}

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@
3232
import se.bjurr.prnfb.settings.ValidationException;
3333

3434
import com.atlassian.bitbucket.auth.AuthenticationContext;
35+
import com.atlassian.bitbucket.project.Project;
3536
import com.atlassian.bitbucket.pull.PullRequest;
37+
import com.atlassian.bitbucket.pull.PullRequestRef;
3638
import com.atlassian.bitbucket.pull.PullRequestService;
39+
import com.atlassian.bitbucket.repository.Repository;
3740
import com.atlassian.bitbucket.repository.RepositoryService;
3841
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
42+
import com.google.common.base.Optional;
3943
import com.google.common.collect.Lists;
4044

4145
public class ButtonsServiceTest {
@@ -44,8 +48,10 @@ public class ButtonsServiceTest {
4448
private AuthenticationContext authenticationContext;
4549
private PrnfbButton button1;
4650
private PrnfbButton button2;
51+
private PrnfbButton button3;
4752
private ButtonDTO buttonDto1;
4853
private ButtonDTO buttonDto2;
54+
private ButtonDTO buttonDto3;
4955
@Mock
5056
private ClientKeyStore clientKeyStore;
5157
private PrnfbNotification notification1;
@@ -74,6 +80,14 @@ public class ButtonsServiceTest {
7480
private ButtonsService sut;
7581
@Mock
7682
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;
7791

7892
@SuppressWarnings("unchecked")
7993
@Before
@@ -87,16 +101,22 @@ public void before() throws ValidationException {
87101
.thenReturn(this.renderer);
88102

89103
this.buttonDto1 = populatedInstanceOf(ButtonDTO.class);
90-
this.buttonDto1.setProjectKey("a");
104+
this.buttonDto1.setProjectKey(null);
105+
this.buttonDto1.setRepositorySlug(null);
91106
this.button1 = toPrnfbButton(this.buttonDto1);
92107
this.buttonDto2 = populatedInstanceOf(ButtonDTO.class);
93-
this.buttonDto2.setProjectKey("b");
108+
this.buttonDto2.setProjectKey(null);
109+
this.buttonDto2.setRepositorySlug(null);
94110
this.button2 = toPrnfbButton(this.buttonDto2);
111+
this.buttonDto3 = populatedInstanceOf(ButtonDTO.class);
112+
this.button3 = toPrnfbButton(this.buttonDto3);
95113

96114
when(this.settingsService.getButton(this.button1.getUuid()))//
97115
.thenReturn(this.button1);
98116
when(this.settingsService.getButton(this.button2.getUuid()))//
99-
.thenReturn(this.button2);
117+
.thenReturn(this.button2);
118+
when(this.settingsService.getButton(this.button3.getUuid()))//
119+
.thenReturn(this.button3);
100120

101121
this.notificationDto1 = populatedInstanceOf(NotificationDTO.class);
102122
this.notificationDto1.setUrl("http://hej.com");
@@ -117,14 +137,15 @@ public void before() throws ValidationException {
117137

118138
@Test
119139
public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
120-
121-
List<PrnfbButton> candidates = newArrayList(this.button1, this.button2);
140+
List<PrnfbButton> candidates = newArrayList(this.button1, this.button2, this.button3);
122141
when(this.settingsService.getButtons())//
123142
.thenReturn(candidates);
124143
when(this.userCheckService.isAllowedUseButton(this.button1))//
125144
.thenReturn(true);
126145
when(this.userCheckService.isAllowedUseButton(this.button2))//
127-
.thenReturn(true);
146+
.thenReturn(true);
147+
when(this.userCheckService.isAllowedUseButton(this.button3))//
148+
.thenReturn(true);
128149
when(
129150
this.prnfbPullRequestEventListener.isNotificationTriggeredByAction(this.notification1, this.pullRequestAction,
130151
this.renderer, this.pullRequest, this.clientKeyStore, this.shouldAcceptAnyCertificate))//
@@ -133,12 +154,32 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() {
133154
this.prnfbPullRequestEventListener.isNotificationTriggeredByAction(this.notification2, this.pullRequestAction,
134155
this.renderer, this.pullRequest, this.clientKeyStore, this.shouldAcceptAnyCertificate))//
135156
.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());
136162

137163
List<PrnfbButton> actual = this.sut.doGetButtons(this.notifications, this.clientKeyStore, this.pullRequest,
138164
this.shouldAcceptAnyCertificate);
165+
assertThat(actual)//
166+
.containsOnly(this.button1, this.button2, this.button3);
167+
168+
// Now do the same with another repository - button3 should disappear
169+
when(this.repository.getSlug()).thenReturn("otherrepository");
170+
actual = this.sut.doGetButtons(this.notifications, this.clientKeyStore, this.pullRequest,
171+
this.shouldAcceptAnyCertificate);
172+
assertThat(actual)//
173+
.containsOnly(this.button1, this.button2);
139174

175+
// 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);
179+
actual = this.sut.doGetButtons(this.notifications, this.clientKeyStore, this.pullRequest,
180+
this.shouldAcceptAnyCertificate);
140181
assertThat(actual)//
141-
.containsExactly(this.button1, this.button2);
182+
.containsOnly(this.button1, this.button2, this.button3);
142183
}
143184

144185
@Test

0 commit comments

Comments
 (0)