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

Commit

Permalink
Fixing IndexOutOfBoundsException if no reviewers and using reviewers …
Browse files Browse the repository at this point in the history
…variable

 * Adding tests to reviewers variables.
 * Also changing changelog.md to specify exact names of new variables in 2.13.
  • Loading branch information
tomasbjerre committed Jan 25, 2016
1 parent 7364987 commit d84ea71
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 31 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ Changelog of Pull Request Notifier for Bitbucket.
## 2.13
* Allowing SSL certificates to be ignored.
* Adding settings to configure custom keystore.
* Add attribute for list of reviewers, reviewers' IDs and reviewers' names.
* New variables:
* ${PULL_REQUEST_REVIEWERS} Example: Administrator,User
* ${PULL_REQUEST_REVIEWERS_ID} Example: 1,2
* ${PULL_REQUEST_REVIEWERS_SLUG} Example: admin,user

## 2.12
* Fixing PULL_REQUEST_URL-bug correctly with getSlug.
Expand Down
35 changes: 19 additions & 16 deletions src/main/java/se/bjurr/prnfb/listener/PrnfbRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import static se.bjurr.prnfb.listener.PrnfbRenderer.REPO_PROTOCOL.ssh;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;

Expand Down Expand Up @@ -448,14 +448,15 @@ public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullReques
ApplicationPropertiesService propertiesService, PrnfbNotification prnfbNotification,
Map<PrnfbVariable, Supplier<String>> variables, ClientKeyStore clientKeyStore, boolean shouldAcceptAnyCertificate) {
List<PullRequestParticipant> slist = newArrayList(pullRequest.getReviewers());
StringBuilder rString = new StringBuilder();

String sep = ",";
if (slist.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
for (PullRequestParticipant each : slist) {
rString.append(sep).append(each.getUser().getDisplayName());
sb.append("," + each.getUser().getDisplayName());
}

return rString.substring(1);
return sb.substring(1);
}
}), PULL_REQUEST_REVIEWERS_ID(new Resolver() {
@Override
Expand All @@ -464,14 +465,15 @@ public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullReques
ApplicationPropertiesService propertiesService, PrnfbNotification prnfbNotification,
Map<PrnfbVariable, Supplier<String>> variables, ClientKeyStore clientKeyStore, boolean shouldAcceptAnyCertificate) {
List<PullRequestParticipant> slist = newArrayList(pullRequest.getReviewers());
StringBuilder rString = new StringBuilder();

String sep = ",";
if (slist.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
for (PullRequestParticipant each : slist) {
rString.append(sep).append(Integer.toString(each.getUser().getId()));
sb.append("," + each.getUser().getId());
}

return rString.substring(1);
return sb.substring(1);
}
}), PULL_REQUEST_REVIEWERS_SLUG(new Resolver() {
@Override
Expand All @@ -480,14 +482,15 @@ public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullReques
ApplicationPropertiesService propertiesService, PrnfbNotification prnfbNotification,
Map<PrnfbVariable, Supplier<String>> variables, ClientKeyStore clientKeyStore, boolean shouldAcceptAnyCertificate) {
List<PullRequestParticipant> slist = newArrayList(pullRequest.getReviewers());
StringBuilder rString = new StringBuilder();

String sep = ",";
if (slist.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
for (PullRequestParticipant each : slist) {
rString.append(sep).append(each.getUser().getSlug());
sb.append("," + each.getUser().getSlug());
}

return rString.substring(1);
return sb.substring(1);
}
}), PULL_REQUEST_REVIEWERS_APPROVED_COUNT(new Resolver() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.atlassian.bitbucket.pull.PullRequestParticipant;
import com.atlassian.bitbucket.pull.PullRequestRole;
import com.atlassian.bitbucket.pull.PullRequestState;
import com.atlassian.bitbucket.user.ApplicationUser;

public class PullRequestEventBuilder {
public static final String PREVIOUS_TO_HASH = "previousToHash";
Expand All @@ -46,9 +47,29 @@ public PullRequestEventBuilder withFromRef(PullRequestRefBuilder fromRef) {
return this;
}

public PullRequestEventBuilder withParticipant(PullRequestRole role, Boolean isApproved) {
public PullRequestEventBuilder withParticipantReviewer(PullRequestRole role, Boolean isApproved) {
return withParticipantReviewer(role, isApproved, "name", "username", 1);
}

public PullRequestEventBuilder withParticipantReviewer(PullRequestRole role, Boolean isApproved, String name,
String username, Integer id) {
PullRequestParticipant participant = mock(PullRequestParticipant.class);
when(participant.isApproved()).thenReturn(isApproved);
ApplicationUser user = mock(ApplicationUser.class);
when(participant.getUser()).thenReturn(user);
when(participant.getUser().getDisplayName()).thenReturn(name);
when(participant.getUser().getSlug()).thenReturn(username);
when(participant.getUser().getId()).thenReturn(id);
if (role == PARTICIPANT) {
participants.add(participant);
} else {
reviewers.add(participant);
}
return this;
}

public PullRequestEventBuilder withParticipant(PullRequestRole role) {
PullRequestParticipant participant = mock(PullRequestParticipant.class);
if (role == PARTICIPANT) {
participants.add(participant);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.PullRequestRole.REVIEWER;
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;
Expand Down Expand Up @@ -107,39 +108,83 @@ public void testThatAdminFormFieldsAreUsedInAdminGUI() throws IOException {
public void testThatAUrlCanHaveSeveralVariables() throws Exception {
prnfbTestBuilder()
.isLoggedInAsAdmin()
.withPullRequest(//
pullRequestEventBuilder()//
.withParticipantReviewer(REVIEWER, false, "First Last", "firstlast", 1)//
.withParticipantReviewer(REVIEWER, false, "First2 Last2", "firstlast2", 5)//
.withParticipant(PARTICIPANT)//
.build()//
.getPullRequest())
.withNotification( //
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}&revapp=${PULL_REQUEST_REVIEWERS_APPROVED_COUNT}&partapp=${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT}") //
"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}&reviewers=${PULL_REQUEST_REVIEWERS}&reviewersid=${PULL_REQUEST_REVIEWERS_ID}&reviewersslug=${PULL_REQUEST_REVIEWERS_SLUG}") //
.withFieldValue(events, OPENED.name()) //
.build() //
) //
.store() //
.trigger( //
pullRequestEventBuilder().withFromRef( //
pullRequestRefBuilder() //
.withHash("cde456") //
.withRepositorySlug("fromslug") //
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) //
.withParticipantReviewer(PARTICIPANT, TRUE) //
.withParticipantReviewer(PARTICIPANT, TRUE) //
.withParticipantReviewer(PARTICIPANT, TRUE) //
.withParticipantReviewer(PARTICIPANT, FALSE) //
.withParticipantReviewer(AUTHOR, TRUE) //
.withParticipantReviewer(AUTHOR, TRUE) //
.withParticipantReviewer(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&revapp=2&partapp=3") //
"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&reviewers=name%2Cname%2Cname&reviewersid=1%2C1%2C1&reviewersslug=username%2Cusername%2Cusername") //
.invokedMethod(GET);
}

@Test
public void testThatAUrlCanHaveReviewersVariablesWhenNoReviewer() throws Exception {
prnfbTestBuilder()
.isLoggedInAsAdmin()
.withPullRequest(//
pullRequestEventBuilder()//
.build()//
.getPullRequest())
.withNotification( //
notificationBuilder() //
.withFieldValue(
url,
"http://bjurr.se/?revapp=${PULL_REQUEST_REVIEWERS_APPROVED_COUNT}&partapp=${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT}&reviewers=${PULL_REQUEST_REVIEWERS}&reviewersid=${PULL_REQUEST_REVIEWERS_ID}&reviewersslug=${PULL_REQUEST_REVIEWERS_SLUG}") //
.withFieldValue(events, OPENED.name()) //
.build() //
) //
.store() //
.trigger( //
pullRequestEventBuilder()//
.withFromRef( //
pullRequestRefBuilder() //
.withHash("cde456") //
.withRepositorySlug("fromslug") //
).withToRef( //
pullRequestRefBuilder() //
.withHash("asd123") //
.withRepositorySlug("toslug") //
) //
.withPullRequestId(10L) //
.withPullRequestAction(OPENED) //
.build() //
) //
.invokedUrl(0, "http://bjurr.se/?revapp=0&partapp=0&reviewers=&reviewersid=&reviewersslug=") //
.invokedMethod(GET);
}

Expand Down

0 comments on commit d84ea71

Please sign in to comment.