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

Commit f6a6165

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 43befa6 commit f6a6165

File tree

7 files changed

+81
-4
lines changed

7 files changed

+81
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Changelog of Pull Request Notifier for Stash.
44

5+
## 1.32
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+
511
## 1.31
612
* Bugfix: Saving with checked checkboxes RESCOPED_FROM/TO and BUTTON_TRIGGER was not reflected in GUI.
713
* Bugfix: Avoiding admin page to crash if entering quote as value of a field.

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/stash/scm/project_1/rep_1.git
6365
* ${PULL_REQUEST_FROM_HASH} Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad

src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package se.bjurr.prnfs.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.prnfs.listener.PrnfsRenderer.REPO_PROTOCOL.http;
@@ -15,13 +17,15 @@
1517
import se.bjurr.prnfs.settings.PrnfsNotification;
1618

1719
import com.atlassian.stash.pull.PullRequest;
20+
import com.atlassian.stash.pull.PullRequestParticipant;
1821
import com.atlassian.stash.repository.Repository;
1922
import com.atlassian.stash.repository.RepositoryCloneLinksRequest;
2023
import com.atlassian.stash.repository.RepositoryService;
2124
import com.atlassian.stash.server.ApplicationPropertiesService;
2225
import com.atlassian.stash.user.StashUser;
2326
import com.atlassian.stash.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 PrnfsRenderer {
@@ -331,7 +335,28 @@ public String resolve(PullRequest pullRequest, PrnfsPullRequestAction pullReques
331335
PrnfsNotification prnfsNotification, Map<PrnfsVariable, Supplier<String>> variables) {
332336
return pullRequest.getTitle();
333337
}
338+
}), PULL_REQUEST_REVIEWERS_APPROVED_COUNT(new Resolver() {
339+
@Override
340+
public String resolve(PullRequest pullRequest, PrnfsPullRequestAction pullRequestAction, StashUser ApplicationUser,
341+
RepositoryService repositoryService, ApplicationPropertiesService propertiesService,
342+
PrnfsNotification prnfbNotification, Map<PrnfsVariable, Supplier<String>> variables) {
343+
return Integer.toString(newArrayList(filter(pullRequest.getReviewers(), isApproved)).size());
344+
}
345+
}), PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT(new Resolver() {
346+
@Override
347+
public String resolve(PullRequest pullRequest, PrnfsPullRequestAction pullRequestAction, StashUser ApplicationUser,
348+
RepositoryService repositoryService, ApplicationPropertiesService propertiesService,
349+
PrnfsNotification prnfbNotification, Map<PrnfsVariable, Supplier<String>> variables) {
350+
return Integer.toString(newArrayList(filter(pullRequest.getParticipants(), isApproved)).size());
351+
}
334352
});
353+
;
354+
private static final Predicate<PullRequestParticipant> isApproved = new Predicate<PullRequestParticipant>() {
355+
@Override
356+
public boolean apply(PullRequestParticipant input) {
357+
return input.isApproved();
358+
}
359+
};
335360

336361
private Resolver resolver;
337362

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/stash/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/prnfs/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/prnfs/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/prnfs/admin/PrnfsPullRequestEventListenerTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import static com.atlassian.stash.pull.PullRequestAction.MERGED;
55
import static com.atlassian.stash.pull.PullRequestAction.OPENED;
66
import static com.atlassian.stash.pull.PullRequestAction.RESCOPED;
7+
import static com.atlassian.stash.pull.PullRequestRole.AUTHOR;
8+
import static com.atlassian.stash.pull.PullRequestRole.PARTICIPANT;
79
import static com.atlassian.stash.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;
@@ -108,7 +112,7 @@ public void testThatAUrlCanHaveSeveralVariables() throws Exception {
108112
notificationBuilder() //
109113
.withFieldValue(
110114
url,
111-
"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}") //
115+
"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}") //
112116
.withFieldValue(events, OPENED.name()) //
113117
.build() //
114118
) //
@@ -127,11 +131,18 @@ public void testThatAUrlCanHaveSeveralVariables() throws Exception {
127131
) //
128132
.withPullRequestId(10L) //
129133
.withPullRequestAction(OPENED) //
134+
.withParticipant(PARTICIPANT, TRUE) //
135+
.withParticipant(PARTICIPANT, TRUE) //
136+
.withParticipant(PARTICIPANT, TRUE) //
137+
.withParticipant(PARTICIPANT, FALSE) //
138+
.withParticipant(AUTHOR, TRUE) //
139+
.withParticipant(AUTHOR, TRUE) //
140+
.withParticipant(AUTHOR, FALSE) //
130141
.build() //
131142
) //
132143
.invokedUrl(
133144
0,
134-
"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") //
145+
"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") //
135146
.invokedMethod(GET);
136147
}
137148

src/test/java/se/bjurr/prnfs/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.stash.pull.PullRequestAction.COMMENTED;
44
import static com.atlassian.stash.pull.PullRequestAction.RESCOPED;
5+
import static com.atlassian.stash.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.prnfs.admin.utils.PullRequestRefBuilder.pullRequestRefBuilder;
911

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

1924
public class PullRequestEventBuilder {
@@ -28,12 +33,25 @@ public class PullRequestEventBuilder {
2833
private boolean beingClosed;
2934
private final boolean beingOpen = TRUE;
3035
private Long pullRequestId = 0L;
36+
private final Set<PullRequestParticipant> participants = newHashSet();
37+
private final Set<PullRequestParticipant> reviewers = newHashSet();
3138
private PullRequestState pullRequestState;
3239

3340
private PullRequestEventBuilder(PrnfsTestBuilder prnfsTestBuilder) {
3441
this.prnfsTestBuilder = prnfsTestBuilder;
3542
}
3643

44+
public PullRequestEventBuilder withParticipant(PullRequestRole role, Boolean isApproved) {
45+
PullRequestParticipant participant = mock(PullRequestParticipant.class);
46+
when(participant.isApproved()).thenReturn(isApproved);
47+
if (role == PARTICIPANT) {
48+
participants.add(participant);
49+
} else {
50+
reviewers.add(participant);
51+
}
52+
return this;
53+
}
54+
3755
public PullRequestEventBuilder withFromRef(PullRequestRefBuilder fromRef) {
3856
this.fromRef = fromRef;
3957
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().getReviewers()).thenReturn(reviewers);
126+
when(pullRequestEvent.getPullRequest().getParticipants()).thenReturn(participants);
107127
when(pullRequestEvent.getPullRequest().getAuthor()).thenReturn(author);
108128
when(pullRequestEvent.getPullRequest().getFromRef()).thenReturn(fromRef);
109129
when(pullRequestEvent.getPullRequest().getToRef()).thenReturn(toRef);

0 commit comments

Comments
 (0)