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

Commit

Permalink
PULL_REQUEST_COMMENT_ID
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Aug 14, 2017
1 parent b8fbfa3 commit 1d67435
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 39 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### No issue
PULL_REQUEST_COMMENT_ID

[52f0544250f1934](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/52f0544250f1934) Tomas Bjerre *2017-08-14 18:45:16*

doc

[b8fbfa325b8624a](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/b8fbfa325b8624a) Tomas Bjerre *2017-08-13 17:31:11*

## 3.12
### No issue
Avoid race condition on save in GUI

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The filter text as well as the URL support variables. These are:
| `${PULL_REQUEST_VERSION}` | Example: `1` |
| `${PULL_REQUEST_COMMENT_TEXT}` | Example: `A comment` |
| `${PULL_REQUEST_COMMENT_ACTION}` | Example: `ADDED`, `DELETED`, `EDITED`, `REPLIED` |
| `${PULL_REQUEST_COMMENT_ID}` | Example: `1234` |
| `${PULL_REQUEST_ACTION}` | Example: `OPENED` |
| `${PULL_REQUEST_STATE}` | Example: `DECLINED`, `MERGED`, `OPEN` |
| `${BUTTON_TRIGGER_TITLE}` | Example: `Trigger Notification` |
Expand Down
65 changes: 41 additions & 24 deletions src/main/java/se/bjurr/prnfb/service/PrnfbVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
List<String> parts = newArrayList();
for (PrnfbVariable v : PrnfbVariable.values()) {
final List<String> parts = newArrayList();
for (final PrnfbVariable v : PrnfbVariable.values()) {
if (v != EVERYTHING_URL //
&& v != PULL_REQUEST_DESCRIPTION) {
parts.add(v.name() + "=${" + v.name() + "}");
Expand All @@ -106,7 +106,7 @@ public String resolve(
if (prnfbNotification == null || !prnfbNotification.getInjectionUrl().isPresent()) {
return "";
}
PrnfbRenderer renderer =
final PrnfbRenderer renderer =
new PrnfbRenderer(
pullRequest,
pullRequestAction,
Expand All @@ -116,13 +116,13 @@ public String resolve(
prnfbNotification,
variables,
securityService);
String renderedUrlParam =
final String renderedUrlParam =
renderer.render(
prnfbNotification.getInjectionUrl().get(),
ENCODE_FOR.URL,
clientKeyStore,
shouldAcceptAnyCertificate);
UrlInvoker urlInvoker =
final UrlInvoker urlInvoker =
urlInvoker() //
.withUrlParam(renderedUrlParam) //
.withMethod(GET) //
Expand All @@ -136,9 +136,9 @@ public String resolve(
.shouldAcceptAnyCertificate(shouldAcceptAnyCertificate);
createInvoker() //
.invoke(urlInvoker);
String rawResponse = urlInvoker.getResponse().getContent().trim();
final String rawResponse = urlInvoker.getResponse().getContent().trim();
if (prnfbNotification.getInjectionUrlRegexp().isPresent()) {
Matcher m =
final Matcher m =
compile(prnfbNotification.getInjectionUrlRegexp().get()).matcher(rawResponse);
if (!m.find()) {
return "";
Expand Down Expand Up @@ -288,6 +288,23 @@ public String resolve(
return getOrEmpty(variables, PULL_REQUEST_COMMENT_TEXT);
}
}),
PULL_REQUEST_COMMENT_ID(
new PrnfbVariableResolver() {
@Override
public String resolve(
PullRequest pullRequest,
PrnfbPullRequestAction prnfbPullRequestAction,
ApplicationUser applicationUser,
RepositoryService repositoryService,
ApplicationPropertiesService propertiesService,
PrnfbNotification prnfbNotification,
Map<PrnfbVariable, Supplier<String>> variables,
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
return getOrEmpty(variables, PULL_REQUEST_COMMENT_ID);
}
}),
PULL_REQUEST_DESCRIPTION(
new PrnfbVariableResolver() {
@Override
Expand Down Expand Up @@ -649,7 +666,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == NEEDS_WORK);
return iterableToString(transform(reviewers, (p) -> p.getUser().getSlug()));
}
Expand All @@ -668,7 +685,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == NEEDS_WORK);
return iterableToString(transform(reviewers, (p) -> p.getUser().getEmailAddress()));
}
Expand All @@ -687,7 +704,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == NEEDS_WORK);
return iterableToString(transform(reviewers, (p) -> p.getUser().getName()));
}
Expand All @@ -706,7 +723,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == NEEDS_WORK);
return iterableToString(transform(reviewers, (p) -> p.getUser().getDisplayName()));
}
Expand All @@ -725,7 +742,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == UNAPPROVED);
return iterableToString(transform(reviewers, (p) -> p.getUser().getSlug()));
}
Expand All @@ -744,7 +761,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == UNAPPROVED);
return iterableToString(transform(reviewers, (p) -> p.getUser().getEmailAddress()));
}
Expand All @@ -763,7 +780,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == UNAPPROVED);
return iterableToString(transform(reviewers, (p) -> p.getUser().getName()));
}
Expand All @@ -782,7 +799,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == UNAPPROVED);
return iterableToString(transform(reviewers, (p) -> p.getUser().getDisplayName()));
}
Expand All @@ -801,7 +818,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == APPROVED);
return iterableToString(transform(reviewers, (p) -> p.getUser().getSlug()));
}
Expand All @@ -820,7 +837,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == APPROVED);
return iterableToString(transform(reviewers, (p) -> p.getUser().getEmailAddress()));
}
Expand All @@ -839,7 +856,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == APPROVED);
return iterableToString(transform(reviewers, (p) -> p.getUser().getName()));
}
Expand All @@ -858,7 +875,7 @@ public String resolve(
ClientKeyStore clientKeyStore,
boolean shouldAcceptAnyCertificate,
SecurityService securityService) {
Iterable<PullRequestParticipant> reviewers =
final Iterable<PullRequestParticipant> reviewers =
filter(pullRequest.getReviewers(), (r) -> r.getStatus() == APPROVED);
return iterableToString(transform(reviewers, (p) -> p.getUser().getDisplayName()));
}
Expand Down Expand Up @@ -1296,7 +1313,7 @@ public String resolve(
private static String countParticipantsWithStatus(
Set<PullRequestParticipant> participants, PullRequestParticipantStatus status) {
Integer count = 0;
for (PullRequestParticipant participant : participants) {
for (final PullRequestParticipant participant : participants) {
if (participant.getStatus() == status) {
count++;
}
Expand Down Expand Up @@ -1331,14 +1348,14 @@ private static String cloneUrlFromRepository(
new Operation<String, RuntimeException>() {
@Override
public String perform() throws RuntimeException {
RepositoryCloneLinksRequest request =
final RepositoryCloneLinksRequest request =
new RepositoryCloneLinksRequest.Builder() //
.protocol(protocol.name()) //
.repository(repository) //
.build();
final Set<NamedLink> cloneLinks = repositoryService.getCloneLinks(request);
Set<String> allUrls = newTreeSet();
Iterator<NamedLink> itr = cloneLinks.iterator();
final Set<String> allUrls = newTreeSet();
final Iterator<NamedLink> itr = cloneLinks.iterator();
while (itr.hasNext()) {
allUrls.add(itr.next().getHref());
}
Expand Down Expand Up @@ -1382,7 +1399,7 @@ private static String getPullRequestUrl(
}

private static String iterableToString(Iterable<String> slist) {
List<String> sorted = usingToString().sortedCopy(slist);
final List<String> sorted = usingToString().sortedCopy(slist);
return on(',').join(sorted);
}

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/se/bjurr/prnfb/service/VariablesContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static se.bjurr.prnfb.service.PrnfbVariable.BUTTON_FORM_DATA;
import static se.bjurr.prnfb.service.PrnfbVariable.BUTTON_TRIGGER_TITLE;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_COMMENT_ACTION;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_COMMENT_ID;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_COMMENT_TEXT;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_MERGE_COMMIT;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_PREVIOUS_FROM_HASH;
Expand Down Expand Up @@ -80,7 +81,7 @@ public List<String> getGroups() {
}

public Map<PrnfbVariable, Supplier<String>> getVariables() {
Map<PrnfbVariable, Supplier<String>> variables = new HashMap<>();
final Map<PrnfbVariable, Supplier<String>> variables = new HashMap<>();

if (groups != null) {
variables.put(PULL_REQUEST_USER_GROUPS, Suppliers.ofInstance(Joiner.on(',').join(groups)));
Expand All @@ -96,7 +97,7 @@ public Map<PrnfbVariable, Supplier<String>> getVariables() {

if (pullRequestEvent != null) {
if (pullRequestEvent instanceof PullRequestCommentEvent) {
PullRequestCommentEvent pullRequestCommentEvent =
final PullRequestCommentEvent pullRequestCommentEvent =
(PullRequestCommentEvent) pullRequestEvent;
variables.put(
PULL_REQUEST_COMMENT_TEXT,
Expand All @@ -108,8 +109,13 @@ public Map<PrnfbVariable, Supplier<String>> getVariables() {
() -> {
return pullRequestCommentEvent.getCommentAction().name();
});
variables.put(
PULL_REQUEST_COMMENT_ID,
() -> {
return pullRequestCommentEvent.getComment().getId() + "";
});
} else if (pullRequestEvent instanceof PullRequestRescopedEvent) {
PullRequestRescopedEvent pullRequestRescopedEvent =
final PullRequestRescopedEvent pullRequestRescopedEvent =
(PullRequestRescopedEvent) pullRequestEvent;
variables.put(
PULL_REQUEST_PREVIOUS_FROM_HASH,
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<li><b>${PULL_REQUEST_VERSION}</b> Example: 1</li>
<li><b>${PULL_REQUEST_COMMENT_TEXT}</b> Example: A comment</li>
<li><b>${PULL_REQUEST_COMMENT_ACTION}</b> Example: ADDED, DELETED, EDITED, REPLIED</li>
<li><b>${PULL_REQUEST_COMMENT_ID}</b> Example: 1234</li>
<li><b>${BUTTON_TRIGGER_TITLE}</b> Example: Trigger Notification</li>
<li><b>${BUTTON_FORM_DATA}</b> The form data that was submitted</li>
<li><b>${INJECTION_URL_VALUE}</b> If injection URL configured</li>
Expand Down
27 changes: 15 additions & 12 deletions src/test/java/se/bjurr/prnfb/service/VariablesContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_COMMENT_ACTION;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_COMMENT_ID;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_COMMENT_TEXT;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_MERGE_COMMIT;

Expand All @@ -15,6 +16,10 @@
import org.junit.Test;
import org.mockito.Mock;

import se.bjurr.prnfb.service.VariablesContext.VariablesContextBuilder;
import se.bjurr.prnfb.settings.PrnfbButton;
import se.bjurr.prnfb.settings.ValidationException;

import com.atlassian.bitbucket.comment.Comment;
import com.atlassian.bitbucket.commit.MinimalCommit;
import com.atlassian.bitbucket.event.pull.PullRequestCommentAddedEvent;
Expand All @@ -23,10 +28,6 @@
import com.atlassian.bitbucket.event.pull.PullRequestMergedEvent;
import com.google.common.base.Supplier;

import se.bjurr.prnfb.service.VariablesContext.VariablesContextBuilder;
import se.bjurr.prnfb.settings.PrnfbButton;
import se.bjurr.prnfb.settings.ValidationException;

public class VariablesContextTest {

private VariablesContext sut;
Expand All @@ -41,8 +42,8 @@ public void before() throws ValidationException {

@Test
public void testThatPullRequestMergeComitIsAddedToVariables() {
PullRequestMergedEvent pullRequestEvent = mock(PullRequestMergedEvent.class);
MinimalCommit commit = mock(MinimalCommit.class);
final PullRequestMergedEvent pullRequestEvent = mock(PullRequestMergedEvent.class);
final MinimalCommit commit = mock(MinimalCommit.class);
when(pullRequestEvent.getCommit()) //
.thenReturn(commit);
when(pullRequestEvent.getCommit().getId()) //
Expand All @@ -52,7 +53,7 @@ public void testThatPullRequestMergeComitIsAddedToVariables() {
new VariablesContextBuilder() //
.setPullRequestEvent(pullRequestEvent) //
.build();
Map<PrnfbVariable, Supplier<String>> actual = sut.getVariables();
final Map<PrnfbVariable, Supplier<String>> actual = sut.getVariables();

assertThat(actual) //
.hasSize(1);
Expand All @@ -62,8 +63,8 @@ public void testThatPullRequestMergeComitIsAddedToVariables() {

@Test
public void testThatPullRequestCommentIsAddedToVariables() {
PullRequestCommentAddedEvent pullRequestEvent = mock(PullRequestCommentAddedEvent.class);
Comment comment = mock(Comment.class);
final PullRequestCommentAddedEvent pullRequestEvent = mock(PullRequestCommentAddedEvent.class);
final Comment comment = mock(Comment.class);
when(pullRequestEvent.getComment()) //
.thenReturn(comment);
when(pullRequestEvent.getComment().getText()) //
Expand All @@ -75,13 +76,15 @@ public void testThatPullRequestCommentIsAddedToVariables() {
new VariablesContextBuilder() //
.setPullRequestEvent(pullRequestEvent) //
.build();
Map<PrnfbVariable, Supplier<String>> actual = sut.getVariables();
final Map<PrnfbVariable, Supplier<String>> actual = sut.getVariables();

assertThat(actual) //
.hasSize(2);
.hasSize(3);
assertThat(actual.get(PULL_REQUEST_COMMENT_TEXT).get()) //
.isEqualTo("The comment");
assertThat(actual.get(PULL_REQUEST_COMMENT_ACTION).get()) //
.isEqualTo("ADDED");
.isEqualTo("ADDED");
assertThat(actual.get(PULL_REQUEST_COMMENT_ID).get()) //
.isEqualTo("0");
}
}

0 comments on commit 1d67435

Please sign in to comment.