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

Commit

Permalink
Adding ${PULL_REQUEST_COMMENT_ACTION} variable
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Nov 14, 2016
1 parent b9a273d commit 85ae096
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 187 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### No issue
Adding ${PULL_REQUEST_COMMENT_ACTION} variable

[ade89b294fd56f8](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/ade89b294fd56f8) Tomas Bjerre *2016-11-14 19:38:16*

Format code

[b9a273d95239927](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/b9a273d95239927) Tomas Bjerre *2016-11-14 19:24:03*

Add comment text for replies

[8b13f16f92c8bbd](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/8b13f16f92c8bbd) Garret Ruh *2016-11-14 15:48:27*
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The filter text as well as the URL support variables. These are:
* ${PULL_REQUEST_TITLE} Example: Anything
* ${PULL_REQUEST_VERSION} Example: 1
* ${PULL_REQUEST_COMMENT_TEXT} Example: A comment
* ${PULL_REQUEST_COMMENT_ACTION} Example: ADDED, DELETED, EDITED, REPLIED
* ${PULL_REQUEST_ACTION} Example: OPENED
* ${PULL_REQUEST_STATE} Example: DECLINED, MERGED, OPEN
* ${BUTTON_TRIGGER_TITLE} Example: Trigger Notification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.slf4j.LoggerFactory.getLogger;
import static se.bjurr.prnfb.http.UrlInvoker.urlInvoker;
import static se.bjurr.prnfb.listener.PrnfbPullRequestAction.fromPullRequestEvent;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_COMMENT_ACTION;
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.settings.TRIGGER_IF_MERGE.ALWAYS;
Expand All @@ -20,22 +21,10 @@

import org.slf4j.Logger;

import se.bjurr.prnfb.http.ClientKeyStore;
import se.bjurr.prnfb.http.HttpResponse;
import se.bjurr.prnfb.http.Invoker;
import se.bjurr.prnfb.http.NotificationResponse;
import se.bjurr.prnfb.http.UrlInvoker;
import se.bjurr.prnfb.service.PrnfbRenderer;
import se.bjurr.prnfb.service.PrnfbRendererFactory;
import se.bjurr.prnfb.service.PrnfbVariable;
import se.bjurr.prnfb.service.SettingsService;
import se.bjurr.prnfb.settings.PrnfbHeader;
import se.bjurr.prnfb.settings.PrnfbNotification;
import se.bjurr.prnfb.settings.PrnfbSettingsData;
import se.bjurr.prnfb.settings.TRIGGER_IF_MERGE;

import com.atlassian.bitbucket.event.pull.PullRequestApprovedEvent;
import com.atlassian.bitbucket.event.pull.PullRequestCommentAddedEvent;
import com.atlassian.bitbucket.event.pull.PullRequestCommentDeletedEvent;
import com.atlassian.bitbucket.event.pull.PullRequestCommentEditedEvent;
import com.atlassian.bitbucket.event.pull.PullRequestCommentEvent;
import com.atlassian.bitbucket.event.pull.PullRequestCommentRepliedEvent;
import com.atlassian.bitbucket.event.pull.PullRequestDeclinedEvent;
Expand All @@ -53,6 +42,20 @@
import com.google.common.base.Optional;
import com.google.common.base.Supplier;

import se.bjurr.prnfb.http.ClientKeyStore;
import se.bjurr.prnfb.http.HttpResponse;
import se.bjurr.prnfb.http.Invoker;
import se.bjurr.prnfb.http.NotificationResponse;
import se.bjurr.prnfb.http.UrlInvoker;
import se.bjurr.prnfb.service.PrnfbRenderer;
import se.bjurr.prnfb.service.PrnfbRendererFactory;
import se.bjurr.prnfb.service.PrnfbVariable;
import se.bjurr.prnfb.service.SettingsService;
import se.bjurr.prnfb.settings.PrnfbHeader;
import se.bjurr.prnfb.settings.PrnfbNotification;
import se.bjurr.prnfb.settings.PrnfbSettingsData;
import se.bjurr.prnfb.settings.TRIGGER_IF_MERGE;

public class PrnfbPullRequestEventListener {

private static final Logger LOG = getLogger(PrnfbPullRequestEventListener.class);
Expand All @@ -77,16 +80,50 @@ public PrnfbPullRequestEventListener(PrnfbRendererFactory prnfbRendererFactory,
this.settingsService = settingsService;
}

private Invoker createInvoker() {
if (mockedInvoker != null) {
return mockedInvoker;
}
return new Invoker() {
@Override
public HttpResponse invoke(UrlInvoker urlInvoker) {
return urlInvoker.invoke();
}
};
}

private void handleEvent(final PullRequestEvent pullRequestEvent) {
if (pullRequestEvent.getPullRequest().isClosed() && pullRequestEvent instanceof PullRequestCommentEvent) {
return;
}
final PrnfbSettingsData settings = settingsService.getPrnfbSettingsData();
ClientKeyStore clientKeyStore = new ClientKeyStore(settings);
for (final PrnfbNotification notification : settingsService.getNotifications()) {
PrnfbPullRequestAction action = fromPullRequestEvent(pullRequestEvent, notification);
Map<PrnfbVariable, Supplier<String>> variables = populateVariables(pullRequestEvent);
PrnfbRenderer renderer = prnfbRendererFactory.create(pullRequestEvent.getPullRequest(), action, notification,
variables, pullRequestEvent.getUser());
notify(notification, action, pullRequestEvent.getPullRequest(), renderer, clientKeyStore,
settings.isShouldAcceptAnyCertificate());
}
}

@VisibleForTesting
public void handleEventAsync(final PullRequestEvent pullRequestEvent) {
this.executorService.execute(new Runnable() {
executorService.execute(new Runnable() {
@Override
public void run() {
handleEvent(pullRequestEvent);
}
});
}

@VisibleForTesting
boolean ignoreBecauseOfConflicting(TRIGGER_IF_MERGE triggerIfCanMerge, boolean isConflicted) {
return triggerIfCanMerge == NOT_CONFLICTING && isConflicted || //
triggerIfCanMerge == CONFLICTING && !isConflicted;
}

public boolean isNotificationTriggeredByAction(PrnfbNotification notification,
PrnfbPullRequestAction pullRequestAction, PrnfbRenderer renderer, PullRequest pullRequest,
ClientKeyStore clientKeyStore, Boolean shouldAcceptAnyCertificate) {
Expand Down Expand Up @@ -119,7 +156,7 @@ public boolean isNotificationTriggeredByAction(PrnfbNotification notification,

if (notification.getTriggerIfCanMerge() != ALWAYS && pullRequest.isOpen()) {
// Cannot perform canMerge unless PR is open
boolean isConflicted = this.pullRequestService
boolean isConflicted = pullRequestService
.canMerge(pullRequest.getToRef().getRepository().getId(), pullRequest.getId()).isConflicted();
if (ignoreBecauseOfConflicting(notification.getTriggerIfCanMerge(), isConflicted)) {
return FALSE;
Expand Down Expand Up @@ -177,6 +214,16 @@ public void onEvent(PullRequestCommentAddedEvent e) {
handleEventAsync(e);
}

@EventListener
public void onEvent(PullRequestCommentDeletedEvent e) {
handleEventAsync(e);
}

@EventListener
public void onEvent(PullRequestCommentEditedEvent e) {
handleEventAsync(e);
}

@EventListener
public void onEvent(PullRequestCommentRepliedEvent e) {
handleEventAsync(e);
Expand Down Expand Up @@ -217,45 +264,17 @@ public void onEvent(PullRequestUpdatedEvent e) {
handleEventAsync(e);
}

private Invoker createInvoker() {
if (mockedInvoker != null) {
return mockedInvoker;
}
return new Invoker() {
@Override
public HttpResponse invoke(UrlInvoker urlInvoker) {
return urlInvoker.invoke();
}
};
}

private void handleEvent(final PullRequestEvent pullRequestEvent) {
if (pullRequestEvent.getPullRequest().isClosed() && pullRequestEvent instanceof PullRequestCommentEvent) {
return;
}
final PrnfbSettingsData settings = this.settingsService.getPrnfbSettingsData();
ClientKeyStore clientKeyStore = new ClientKeyStore(settings);
for (final PrnfbNotification notification : this.settingsService.getNotifications()) {
PrnfbPullRequestAction action = fromPullRequestEvent(pullRequestEvent, notification);
Map<PrnfbVariable, Supplier<String>> variables = populateVariables(pullRequestEvent);
PrnfbRenderer renderer = this.prnfbRendererFactory.create(pullRequestEvent.getPullRequest(), action, notification,
variables, pullRequestEvent.getUser());
notify(notification, action, pullRequestEvent.getPullRequest(), renderer, clientKeyStore,
settings.isShouldAcceptAnyCertificate());
}
}

@VisibleForTesting
boolean ignoreBecauseOfConflicting(TRIGGER_IF_MERGE triggerIfCanMerge, boolean isConflicted) {
return triggerIfCanMerge == NOT_CONFLICTING && isConflicted || //
triggerIfCanMerge == CONFLICTING && !isConflicted;
}

@VisibleForTesting
Map<PrnfbVariable, Supplier<String>> populateVariables(final PullRequestEvent pullRequestEvent) {
Map<PrnfbVariable, Supplier<String>> variables = newHashMap();
if (pullRequestEvent instanceof PullRequestCommentEvent) {
variables.put(PULL_REQUEST_COMMENT_TEXT, () -> ((PullRequestCommentEvent) pullRequestEvent).getComment().getText());
PullRequestCommentEvent pullRequestCommentEvent = (PullRequestCommentEvent) pullRequestEvent;
variables.put(PULL_REQUEST_COMMENT_TEXT, () -> {
return pullRequestCommentEvent.getComment().getText();
});
variables.put(PULL_REQUEST_COMMENT_ACTION, () -> {
return pullRequestCommentEvent.getCommentAction().name();
});
} else if (pullRequestEvent instanceof PullRequestMergedEvent) {
variables.put(PULL_REQUEST_MERGE_COMMIT, new Supplier<String>() {
@Override
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/se/bjurr/prnfb/service/PrnfbVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ public String resolve(PullRequest pullRequest, PrnfbPullRequestAction prnfbPullR
SecurityService securityService) {
return pullRequest.getAuthor().getUser().getSlug();
}
}), PULL_REQUEST_COMMENT_ACTION(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_ACTION);
}
}), PULL_REQUEST_COMMENT_TEXT(new PrnfbVariableResolver() {
@Override
public String resolve(PullRequest pullRequest, PrnfbPullRequestAction prnfbPullRequestAction,
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 @@ -56,6 +56,7 @@
<li><b>${PULL_REQUEST_URL}</b> Example: http://localhost:7990/projects/PROJECT_1/repos/rep_1/pull-requests/1</li>
<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>${BUTTON_TRIGGER_TITLE}</b> Example: Trigger Notification</li>
<li><b>${INJECTION_URL_VALUE}</b> If injection URL configured</li>
<li><b>${PULL_REQUEST_USER_DISPLAY_NAME}</b> Example: Some User</li>
Expand Down
Loading

0 comments on commit 85ae096

Please sign in to comment.