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

Commit af17e04

Browse files
committed
New variables
* ${PULL_REQUEST_REVIEWERS_APPROVED_COUNT} Number of reviewers that approved the PR. * ${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT} Number of participants that approved the PR. * These can be used to, for example, show a trigger button only if there are non-zero number of approvals.
1 parent c29055c commit af17e04

File tree

7 files changed

+89
-12
lines changed

7 files changed

+89
-12
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
Changelog of Pull Request Notifier for Bitbucket.
44

5-
## 2.5
5+
## 2.7
6+
* New variables
7+
* ${PULL_REQUEST_REVIEWERS_APPROVED_COUNT} Number of reviewers that approved the PR.
8+
* ${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT} Number of participants that approved the PR.
9+
These can be used to, for example, show a trigger button only if there are non-zero number of approvals.
10+
11+
## 2.6
612
* Bugfix: Saving with checked checkboxes RESCOPED_FROM/TO and BUTTON_TRIGGER was not reflected in GUI.
713

814
## 2.5

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ The filter text as well as the URL support variables. These are:
5858
* ${PULL_REQUEST_AUTHOR_ID} Example: 1
5959
* ${PULL_REQUEST_AUTHOR_NAME} Example: admin
6060
* ${PULL_REQUEST_AUTHOR_SLUG} Example: admin
61+
* ${PULL_REQUEST_REVIEWERS_APPROVED_COUNT} Number of reviewers that approved the PR.
62+
* ${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT} Number of participants that approved the PR.
6163
* ${PULL_REQUEST_FROM_SSH_CLONE_URL} Example: ssh://git@localhost:7999/project_1/rep_1
6264
* ${PULL_REQUEST_FROM_HTTP_CLONE_URL} Example: http://admin@localhost:7990/bitbucket/scm/project_1/rep_1.git
6365
* ${PULL_REQUEST_FROM_HASH} Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad

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

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

3+
import static com.google.common.collect.Iterables.filter;
4+
import static com.google.common.collect.Lists.newArrayList;
35
import static java.util.logging.Logger.getLogger;
46
import static java.util.regex.Pattern.compile;
57
import static se.bjurr.prnfb.listener.PrnfbRenderer.REPO_PROTOCOL.http;
@@ -15,13 +17,15 @@
1517
import se.bjurr.prnfb.settings.PrnfbNotification;
1618

1719
import com.atlassian.bitbucket.pull.PullRequest;
20+
import com.atlassian.bitbucket.pull.PullRequestParticipant;
1821
import com.atlassian.bitbucket.repository.Repository;
1922
import com.atlassian.bitbucket.repository.RepositoryCloneLinksRequest;
2023
import com.atlassian.bitbucket.repository.RepositoryService;
2124
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
2225
import com.atlassian.bitbucket.user.ApplicationUser;
2326
import com.atlassian.bitbucket.util.NamedLink;
2427
import com.google.common.annotations.VisibleForTesting;
28+
import com.google.common.base.Predicate;
2529
import com.google.common.base.Supplier;
2630

2731
public class PrnfbRenderer {
@@ -369,8 +373,31 @@ public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullReques
369373
Map<PrnfbVariable, Supplier<String>> variables) {
370374
return pullRequest.getTitle();
371375
}
376+
}), PULL_REQUEST_REVIEWERS_APPROVED_COUNT(new Resolver() {
377+
@Override
378+
public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullRequestAction,
379+
ApplicationUser ApplicationUser, RepositoryService repositoryService,
380+
ApplicationPropertiesService propertiesService, PrnfbNotification prnfbNotification,
381+
Map<PrnfbVariable, Supplier<String>> variables) {
382+
return Integer.toString(newArrayList(filter(pullRequest.getReviewers(), isApproved)).size());
383+
}
384+
}), PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT(new Resolver() {
385+
@Override
386+
public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullRequestAction,
387+
ApplicationUser ApplicationUser, RepositoryService repositoryService,
388+
ApplicationPropertiesService propertiesService, PrnfbNotification prnfbNotification,
389+
Map<PrnfbVariable, Supplier<String>> variables) {
390+
return Integer.toString(newArrayList(filter(pullRequest.getParticipants(), isApproved)).size());
391+
}
372392
});
373393

394+
private static final Predicate<PullRequestParticipant> isApproved = new Predicate<PullRequestParticipant>() {
395+
@Override
396+
public boolean apply(PullRequestParticipant input) {
397+
return input.isApproved();
398+
}
399+
};
400+
374401
private Resolver resolver;
375402

376403
private static String cloneUrlFromRepository(REPO_PROTOCOL protocol, Repository repository,

src/main/resources/admin.vm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
<li><b>${PULL_REQUEST_AUTHOR_ID}</b> Example: 1</li>
5757
<li><b>${PULL_REQUEST_AUTHOR_NAME}</b> Example: admin</li>
5858
<li><b>${PULL_REQUEST_AUTHOR_SLUG}</b> Example: admin</li>
59+
<li><b>${PULL_REQUEST_REVIEWERS_APPROVED_COUNT}</b> Number of reviewers that approved the PR.</li>
60+
<li><b>${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT}</b> Number of participants that approved the PR.</li>
5961
<li><b>${PULL_REQUEST_FROM_SSH_CLONE_URL}</b> Example: ssh://git@localhost:7999/project_1/rep_1</li>
6062
<li><b>${PULL_REQUEST_FROM_HTTP_CLONE_URL}</b> Example: http://admin@localhost:7990/bitbucket/scm/project_1/rep_1.git</li>
6163
<li><b>${PULL_REQUEST_FROM_HASH}</b> Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad</li>

src/main/resources/pr-triggerbutton.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ define('plugin/prnfb/pr-triggerbutton', [
1414
var $buttonTemplate = $(".triggerManualNotification");
1515
$buttonTemplate.empty().remove();
1616

17-
$.get(getResourceUrl(), function(settings) {
17+
function loadSettingsAndShowButtons() {
18+
$.get(getResourceUrl(), function(settings) {
1819
settings.forEach(function(item) {
1920
var $button = $buttonTemplate.clone();
2021
$button.html(item.title);
@@ -35,8 +36,18 @@ define('plugin/prnfb/pr-triggerbutton', [
3536

3637
$buttonArea.append($button);
3738
});
39+
});
40+
}
41+
42+
loadSettingsAndShowButtons();
43+
44+
//If a reviewer approves the PR, then a button may become visible
45+
$('.aui-button.approve').click(function () {
46+
setTimeout(function(){
47+
$(".triggerManualNotification").remove();
48+
loadSettingsAndShowButtons();
49+
}, 1000);
3850
});
39-
4051
});
4152

4253
AJS.$(document).ready(function() {

src/test/java/se/bjurr/prnbs/admin/utils/PullRequestEventBuilder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
import static com.atlassian.bitbucket.pull.PullRequestAction.COMMENTED;
44
import static com.atlassian.bitbucket.pull.PullRequestAction.RESCOPED;
5+
import static com.atlassian.bitbucket.pull.PullRequestRole.PARTICIPANT;
6+
import static com.google.common.collect.Sets.newHashSet;
57
import static java.lang.Boolean.TRUE;
68
import static org.mockito.Mockito.mock;
79
import static org.mockito.Mockito.when;
810
import static se.bjurr.prnbs.admin.utils.PullRequestRefBuilder.pullRequestRefBuilder;
911

12+
import java.util.Set;
13+
1014
import com.atlassian.bitbucket.comment.Comment;
1115
import com.atlassian.bitbucket.event.pull.PullRequestCommentAddedEvent;
1216
import com.atlassian.bitbucket.event.pull.PullRequestEvent;
1317
import com.atlassian.bitbucket.event.pull.PullRequestRescopedEvent;
1418
import com.atlassian.bitbucket.pull.PullRequest;
1519
import com.atlassian.bitbucket.pull.PullRequestAction;
1620
import com.atlassian.bitbucket.pull.PullRequestParticipant;
21+
import com.atlassian.bitbucket.pull.PullRequestRole;
1722
import com.atlassian.bitbucket.pull.PullRequestState;
1823

1924
public class PullRequestEventBuilder {
@@ -29,6 +34,8 @@ public class PullRequestEventBuilder {
2934
private final boolean beingOpen = TRUE;
3035
private Long pullRequestId = 0L;
3136
private PullRequestState pullRequestState;
37+
private final Set<PullRequestParticipant> participants = newHashSet();
38+
private final Set<PullRequestParticipant> reviewers = newHashSet();
3239

3340
private PullRequestEventBuilder(PrnfbTestBuilder prnfbTestBuilder) {
3441
this.prnfbTestBuilder = prnfbTestBuilder;
@@ -39,6 +46,17 @@ public PullRequestEventBuilder withFromRef(PullRequestRefBuilder fromRef) {
3946
return this;
4047
}
4148

49+
public PullRequestEventBuilder withParticipant(PullRequestRole role, Boolean isApproved) {
50+
PullRequestParticipant participant = mock(PullRequestParticipant.class);
51+
when(participant.isApproved()).thenReturn(isApproved);
52+
if (role == PARTICIPANT) {
53+
participants.add(participant);
54+
} else {
55+
reviewers.add(participant);
56+
}
57+
return this;
58+
}
59+
4260
public PullRequestEventBuilder withToRef(PullRequestRefBuilder toRef) {
4361
this.toRef = toRef;
4462
return this;
@@ -104,6 +122,8 @@ public PullRequestEvent build() {
104122
when(pullRequest.getState()).thenReturn(pullRequestState);
105123
when(pullRequestEvent.getAction()).thenReturn(pullRequestAction);
106124
when(pullRequestEvent.getPullRequest()).thenReturn(pullRequest);
125+
when(pullRequestEvent.getPullRequest().getParticipants()).thenReturn(participants);
126+
when(pullRequestEvent.getPullRequest().getReviewers()).thenReturn(reviewers);
107127
when(pullRequestEvent.getPullRequest().getAuthor()).thenReturn(author);
108128
when(pullRequestEvent.getPullRequest().getFromRef()).thenReturn(fromRef);
109129
when(pullRequestEvent.getPullRequest().getToRef()).thenReturn(toRef);

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import static com.atlassian.bitbucket.pull.PullRequestAction.MERGED;
55
import static com.atlassian.bitbucket.pull.PullRequestAction.OPENED;
66
import static com.atlassian.bitbucket.pull.PullRequestAction.RESCOPED;
7+
import static com.atlassian.bitbucket.pull.PullRequestRole.AUTHOR;
8+
import static com.atlassian.bitbucket.pull.PullRequestRole.PARTICIPANT;
79
import static com.atlassian.bitbucket.pull.PullRequestState.DECLINED;
810
import static com.google.common.base.Charsets.UTF_8;
911
import static com.google.common.base.Joiner.on;
1012
import static com.google.common.collect.Lists.newArrayList;
1113
import static com.google.common.io.Resources.getResource;
14+
import static java.lang.Boolean.FALSE;
15+
import static java.lang.Boolean.TRUE;
1216
import static java.util.Collections.sort;
1317
import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION;
1418
import static org.junit.Assert.assertEquals;
@@ -107,30 +111,35 @@ public void testThatAUrlCanHaveSeveralVariables() throws Exception {
107111
notificationBuilder() //
108112
.withFieldValue(
109113
url,
110-
"http://bjurr.se/?PULL_REQUEST_FROM_HASH=${PULL_REQUEST_FROM_HASH}&PULL_REQUEST_TO_HASH=${PULL_REQUEST_TO_HASH}&PULL_REQUEST_FROM_REPO_SLUG=${PULL_REQUEST_FROM_REPO_SLUG}&PULL_REQUEST_TO_REPO_SLUG=${PULL_REQUEST_TO_REPO_SLUG}") //
114+
"http://bjurr.se/?PULL_REQUEST_FROM_HASH=${PULL_REQUEST_FROM_HASH}&PULL_REQUEST_TO_HASH=${PULL_REQUEST_TO_HASH}&PULL_REQUEST_FROM_REPO_SLUG=${PULL_REQUEST_FROM_REPO_SLUG}&PULL_REQUEST_TO_REPO_SLUG=${PULL_REQUEST_TO_REPO_SLUG}&revapp=${PULL_REQUEST_REVIEWERS_APPROVED_COUNT}&partapp=${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT}") //
111115
.withFieldValue(events, OPENED.name()) //
112116
.build() //
113117
) //
114118
.store() //
115119
.trigger( //
116-
pullRequestEventBuilder() //
117-
.withFromRef( //
118-
pullRequestRefBuilder() //
119-
.withHash("cde456") //
120-
.withRepositorySlug("fromslug") //
121-
) //
122-
.withToRef( //
120+
pullRequestEventBuilder().withFromRef( //
121+
pullRequestRefBuilder() //
122+
.withHash("cde456") //
123+
.withRepositorySlug("fromslug") //
124+
).withToRef( //
123125
pullRequestRefBuilder() //
124126
.withHash("asd123") //
125127
.withRepositorySlug("toslug") //
126128
) //
127129
.withPullRequestId(10L) //
128130
.withPullRequestAction(OPENED) //
131+
.withParticipant(PARTICIPANT, TRUE) //
132+
.withParticipant(PARTICIPANT, TRUE) //
133+
.withParticipant(PARTICIPANT, TRUE) //
134+
.withParticipant(PARTICIPANT, FALSE) //
135+
.withParticipant(AUTHOR, TRUE) //
136+
.withParticipant(AUTHOR, TRUE) //
137+
.withParticipant(AUTHOR, FALSE) //
129138
.build() //
130139
) //
131140
.invokedUrl(
132141
0,
133-
"http://bjurr.se/?PULL_REQUEST_FROM_HASH=cde456&PULL_REQUEST_TO_HASH=asd123&PULL_REQUEST_FROM_REPO_SLUG=fromslug&PULL_REQUEST_TO_REPO_SLUG=toslug") //
142+
"http://bjurr.se/?PULL_REQUEST_FROM_HASH=cde456&PULL_REQUEST_TO_HASH=asd123&PULL_REQUEST_FROM_REPO_SLUG=fromslug&PULL_REQUEST_TO_REPO_SLUG=toslug&revapp=2&partapp=3") //
134143
.invokedMethod(GET);
135144
}
136145

0 commit comments

Comments
 (0)