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

Commit

Permalink
New variables
Browse files Browse the repository at this point in the history
 * ${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.
  • Loading branch information
tomasbjerre committed Oct 13, 2015
1 parent c29055c commit af17e04
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 12 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

Changelog of Pull Request Notifier for Bitbucket.

## 2.5
## 2.7
* 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.

## 2.6
* Bugfix: Saving with checked checkboxes RESCOPED_FROM/TO and BUTTON_TRIGGER was not reflected in GUI.

## 2.5
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ The filter text as well as the URL support variables. These are:
* ${PULL_REQUEST_AUTHOR_ID} Example: 1
* ${PULL_REQUEST_AUTHOR_NAME} Example: admin
* ${PULL_REQUEST_AUTHOR_SLUG} Example: admin
* ${PULL_REQUEST_REVIEWERS_APPROVED_COUNT} Number of reviewers that approved the PR.
* ${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT} Number of participants that approved the PR.
* ${PULL_REQUEST_FROM_SSH_CLONE_URL} Example: ssh://git@localhost:7999/project_1/rep_1
* ${PULL_REQUEST_FROM_HTTP_CLONE_URL} Example: http://admin@localhost:7990/bitbucket/scm/project_1/rep_1.git
* ${PULL_REQUEST_FROM_HASH} Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/se/bjurr/prnfb/listener/PrnfbRenderer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package se.bjurr.prnfb.listener;

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

import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.pull.PullRequestParticipant;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.repository.RepositoryCloneLinksRequest;
import com.atlassian.bitbucket.repository.RepositoryService;
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.util.NamedLink;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;

public class PrnfbRenderer {
Expand Down Expand Up @@ -369,8 +373,31 @@ public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullReques
Map<PrnfbVariable, Supplier<String>> variables) {
return pullRequest.getTitle();
}
}), PULL_REQUEST_REVIEWERS_APPROVED_COUNT(new Resolver() {
@Override
public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullRequestAction,
ApplicationUser ApplicationUser, RepositoryService repositoryService,
ApplicationPropertiesService propertiesService, PrnfbNotification prnfbNotification,
Map<PrnfbVariable, Supplier<String>> variables) {
return Integer.toString(newArrayList(filter(pullRequest.getReviewers(), isApproved)).size());
}
}), PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT(new Resolver() {
@Override
public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullRequestAction,
ApplicationUser ApplicationUser, RepositoryService repositoryService,
ApplicationPropertiesService propertiesService, PrnfbNotification prnfbNotification,
Map<PrnfbVariable, Supplier<String>> variables) {
return Integer.toString(newArrayList(filter(pullRequest.getParticipants(), isApproved)).size());
}
});

private static final Predicate<PullRequestParticipant> isApproved = new Predicate<PullRequestParticipant>() {
@Override
public boolean apply(PullRequestParticipant input) {
return input.isApproved();
}
};

private Resolver resolver;

private static String cloneUrlFromRepository(REPO_PROTOCOL protocol, Repository repository,
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
<li><b>${PULL_REQUEST_AUTHOR_ID}</b> Example: 1</li>
<li><b>${PULL_REQUEST_AUTHOR_NAME}</b> Example: admin</li>
<li><b>${PULL_REQUEST_AUTHOR_SLUG}</b> Example: admin</li>
<li><b>${PULL_REQUEST_REVIEWERS_APPROVED_COUNT}</b> Number of reviewers that approved the PR.</li>
<li><b>${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT}</b> Number of participants that approved the PR.</li>
<li><b>${PULL_REQUEST_FROM_SSH_CLONE_URL}</b> Example: ssh://git@localhost:7999/project_1/rep_1</li>
<li><b>${PULL_REQUEST_FROM_HTTP_CLONE_URL}</b> Example: http://admin@localhost:7990/bitbucket/scm/project_1/rep_1.git</li>
<li><b>${PULL_REQUEST_FROM_HASH}</b> Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad</li>
Expand Down
15 changes: 13 additions & 2 deletions src/main/resources/pr-triggerbutton.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ define('plugin/prnfb/pr-triggerbutton', [
var $buttonTemplate = $(".triggerManualNotification");
$buttonTemplate.empty().remove();

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

$buttonArea.append($button);
});
});
}

loadSettingsAndShowButtons();

//If a reviewer approves the PR, then a button may become visible
$('.aui-button.approve').click(function () {
setTimeout(function(){
$(".triggerManualNotification").remove();
loadSettingsAndShowButtons();
}, 1000);
});

});

AJS.$(document).ready(function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import static com.atlassian.bitbucket.pull.PullRequestAction.COMMENTED;
import static com.atlassian.bitbucket.pull.PullRequestAction.RESCOPED;
import static com.atlassian.bitbucket.pull.PullRequestRole.PARTICIPANT;
import static com.google.common.collect.Sets.newHashSet;
import static java.lang.Boolean.TRUE;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static se.bjurr.prnbs.admin.utils.PullRequestRefBuilder.pullRequestRefBuilder;

import java.util.Set;

import com.atlassian.bitbucket.comment.Comment;
import com.atlassian.bitbucket.event.pull.PullRequestCommentAddedEvent;
import com.atlassian.bitbucket.event.pull.PullRequestEvent;
import com.atlassian.bitbucket.event.pull.PullRequestRescopedEvent;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.pull.PullRequestAction;
import com.atlassian.bitbucket.pull.PullRequestParticipant;
import com.atlassian.bitbucket.pull.PullRequestRole;
import com.atlassian.bitbucket.pull.PullRequestState;

public class PullRequestEventBuilder {
Expand All @@ -29,6 +34,8 @@ public class PullRequestEventBuilder {
private final boolean beingOpen = TRUE;
private Long pullRequestId = 0L;
private PullRequestState pullRequestState;
private final Set<PullRequestParticipant> participants = newHashSet();
private final Set<PullRequestParticipant> reviewers = newHashSet();

private PullRequestEventBuilder(PrnfbTestBuilder prnfbTestBuilder) {
this.prnfbTestBuilder = prnfbTestBuilder;
Expand All @@ -39,6 +46,17 @@ public PullRequestEventBuilder withFromRef(PullRequestRefBuilder fromRef) {
return this;
}

public PullRequestEventBuilder withParticipant(PullRequestRole role, Boolean isApproved) {
PullRequestParticipant participant = mock(PullRequestParticipant.class);
when(participant.isApproved()).thenReturn(isApproved);
if (role == PARTICIPANT) {
participants.add(participant);
} else {
reviewers.add(participant);
}
return this;
}

public PullRequestEventBuilder withToRef(PullRequestRefBuilder toRef) {
this.toRef = toRef;
return this;
Expand Down Expand Up @@ -104,6 +122,8 @@ public PullRequestEvent build() {
when(pullRequest.getState()).thenReturn(pullRequestState);
when(pullRequestEvent.getAction()).thenReturn(pullRequestAction);
when(pullRequestEvent.getPullRequest()).thenReturn(pullRequest);
when(pullRequestEvent.getPullRequest().getParticipants()).thenReturn(participants);
when(pullRequestEvent.getPullRequest().getReviewers()).thenReturn(reviewers);
when(pullRequestEvent.getPullRequest().getAuthor()).thenReturn(author);
when(pullRequestEvent.getPullRequest().getFromRef()).thenReturn(fromRef);
when(pullRequestEvent.getPullRequest().getToRef()).thenReturn(toRef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import static com.atlassian.bitbucket.pull.PullRequestAction.MERGED;
import static com.atlassian.bitbucket.pull.PullRequestAction.OPENED;
import static com.atlassian.bitbucket.pull.PullRequestAction.RESCOPED;
import static com.atlassian.bitbucket.pull.PullRequestRole.AUTHOR;
import static com.atlassian.bitbucket.pull.PullRequestRole.PARTICIPANT;
import static com.atlassian.bitbucket.pull.PullRequestState.DECLINED;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.io.Resources.getResource;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static java.util.Collections.sort;
import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -107,30 +111,35 @@ public void testThatAUrlCanHaveSeveralVariables() throws Exception {
notificationBuilder() //
.withFieldValue(
url,
"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}") //
"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}") //
.withFieldValue(events, OPENED.name()) //
.build() //
) //
.store() //
.trigger( //
pullRequestEventBuilder() //
.withFromRef( //
pullRequestRefBuilder() //
.withHash("cde456") //
.withRepositorySlug("fromslug") //
) //
.withToRef( //
pullRequestEventBuilder().withFromRef( //
pullRequestRefBuilder() //
.withHash("cde456") //
.withRepositorySlug("fromslug") //
).withToRef( //
pullRequestRefBuilder() //
.withHash("asd123") //
.withRepositorySlug("toslug") //
) //
.withPullRequestId(10L) //
.withPullRequestAction(OPENED) //
.withParticipant(PARTICIPANT, TRUE) //
.withParticipant(PARTICIPANT, TRUE) //
.withParticipant(PARTICIPANT, TRUE) //
.withParticipant(PARTICIPANT, FALSE) //
.withParticipant(AUTHOR, TRUE) //
.withParticipant(AUTHOR, TRUE) //
.withParticipant(AUTHOR, FALSE) //
.build() //
) //
.invokedUrl(
0,
"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") //
"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") //
.invokedMethod(GET);
}

Expand Down

0 comments on commit af17e04

Please sign in to comment.