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

Commit

Permalink
Listening for each specific event, instead of all pull request events
Browse files Browse the repository at this point in the history
* To avoid handling same event twice
  • Loading branch information
tomasbjerre committed Apr 6, 2015
1 parent 41db3f8 commit 608e3c7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 53 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Changelog of Pull Request Notifier for Stash.

## 1.4
* Bugfix: Avoiding multiple notifications being sent from same event.

## 1.3
Same as version 1.2 but with different version number. When version 1.2 was initially rejected I fixed the issue and created a new 1.2. But a new version number was needed for a resubmission to Atlassian Marketplace.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
package se.bjurr.prnfs.listener;

import static com.google.common.cache.CacheBuilder.newBuilder;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static java.util.Collections.sort;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.regex.Pattern.compile;
import static se.bjurr.prnfs.settings.SettingsStorage.getPrnfsSettings;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import se.bjurr.prnfs.listener.PrnfsRenderer.PrnfsVariable;
import se.bjurr.prnfs.settings.PrnfsNotification;
import se.bjurr.prnfs.settings.PrnfsSettings;
import se.bjurr.prnfs.settings.ValidationException;

import com.atlassian.event.api.EventListener;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.stash.event.pull.PullRequestApprovedEvent;
import com.atlassian.stash.event.pull.PullRequestCommentAddedEvent;
import com.atlassian.stash.event.pull.PullRequestDeclinedEvent;
import com.atlassian.stash.event.pull.PullRequestEvent;
import com.atlassian.stash.event.pull.PullRequestMergedEvent;
import com.atlassian.stash.event.pull.PullRequestOpenedEvent;
import com.atlassian.stash.event.pull.PullRequestReopenedEvent;
import com.atlassian.stash.event.pull.PullRequestRescopedEvent;
import com.atlassian.stash.event.pull.PullRequestUnapprovedEvent;
import com.atlassian.stash.event.pull.PullRequestUpdatedEvent;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.Cache;

public class PrnfsPullRequestEventListener {

private UrlInvoker urlInvoker = new UrlInvoker();
private final PluginSettingsFactory pluginSettingsFactory;
private static final Logger logger = LoggerFactory.getLogger(PrnfsPullRequestEventListener.class);
private static Cache<Object, Object> duplicateEventCache;

public PrnfsPullRequestEventListener(PluginSettingsFactory pluginSettingsFactory) {
this.pluginSettingsFactory = pluginSettingsFactory;
duplicateEventCache = newBuilder().maximumSize(1000).expireAfterWrite(50, MILLISECONDS).build();
}

@VisibleForTesting
Expand All @@ -43,12 +40,53 @@ public void setUrlInvoker(UrlInvoker urlInvoker) {
}

@EventListener
public void anEvent(PullRequestEvent o) {
if (dublicateEventBug(o)) {
return;
}
public void onEvent(PullRequestApprovedEvent e) {
handleEvent(e);
}

@EventListener
public void onEvent(PullRequestCommentAddedEvent e) {
handleEvent(e);
}

@EventListener
public void onEvent(PullRequestDeclinedEvent e) {
handleEvent(e);
}

@EventListener
public void onEvent(PullRequestMergedEvent e) {
handleEvent(e);
}

@EventListener
public void onEvent(PullRequestOpenedEvent e) {
handleEvent(e);
}

@EventListener
public void onEvent(PullRequestReopenedEvent e) {
handleEvent(e);
}

@EventListener
public void onEvent(PullRequestRescopedEvent e) {
handleEvent(e);
}

@EventListener
public void onEvent(PullRequestUnapprovedEvent e) {
handleEvent(e);
}

@EventListener
public void onEvent(PullRequestUpdatedEvent e) {
handleEvent(e);
}

@VisibleForTesting
public void handleEvent(PullRequestEvent o) {
final PrnfsRenderer renderer = new PrnfsRenderer(o);
logEvent(renderer, o);
try {
final PrnfsSettings settings = getPrnfsSettings(pluginSettingsFactory.createGlobalSettings());
for (final PrnfsNotification n : settings.getNotifications()) {
Expand All @@ -64,26 +102,4 @@ public void anEvent(PullRequestEvent o) {
logger.error("", e);
}
}

private void logEvent(PrnfsRenderer renderer, PullRequestEvent o) {
final StringBuilder renderString = new StringBuilder();
final ArrayList<PrnfsVariable> variables = newArrayList(PrnfsRenderer.PrnfsVariable.values());
sort(variables);
for (final PrnfsRenderer.PrnfsVariable variable : variables) {
renderString.append(" " + variable.name() + ": ${" + variable.name() + "}");
}
logger.info(renderer.render(renderString.toString().trim()));
}

/**
* Looks like there is a bug in Stash that causes events to be fired twice.
*/
public static boolean dublicateEventBug(PullRequestEvent o) {
final String footprint = o.getPullRequest().getId() + "_" + o.getAction().name();
if (duplicateEventCache.asMap().containsKey(footprint)) {
return TRUE;
}
duplicateEventCache.put(footprint, TRUE);
return FALSE;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package se.bjurr.prnfs.admin;

import static com.atlassian.stash.pull.PullRequestAction.APPROVED;
import static com.atlassian.stash.pull.PullRequestAction.MERGED;
import static com.atlassian.stash.pull.PullRequestAction.OPENED;
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.lang.Thread.sleep;
import static java.util.Collections.sort;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static se.bjurr.prnfs.admin.utils.NotificationBuilder.notificationBuilder;
import static se.bjurr.prnfs.admin.utils.PrnfsTestBuilder.prnfsTestBuilder;
import static se.bjurr.prnfs.admin.utils.PullRequestEventBuilder.pullRequestEventBuilder;
import static se.bjurr.prnfs.admin.utils.PullRequestRefBuilder.pullRequestRefBuilder;
import static se.bjurr.prnfs.listener.PrnfsPullRequestEventListener.dublicateEventBug;

import java.io.IOException;
import java.net.URL;
Expand Down Expand Up @@ -140,14 +135,6 @@ public void testThatBasicAuthenticationHeaderIsSentIfThereIsAUser() {
.invokedUser("theuser").invokedPassword("thepassword");
}

@Test
public void testThatDuplicateEventsFiredInStashAreIgnored() throws InterruptedException {
assertEquals(FALSE, dublicateEventBug(pullRequestEventBuilder().withId(100L).withPullRequestAction(APPROVED).build()));
assertEquals(TRUE, dublicateEventBug(pullRequestEventBuilder().withId(100L).withPullRequestAction(APPROVED).build()));
sleep(100);
assertEquals(FALSE, dublicateEventBug(pullRequestEventBuilder().withId(100L).withPullRequestAction(APPROVED).build()));
}

@Test
public void testThatFieldsUsedInAdminGUIArePresentInAdminFormFields() throws IOException {
final URL resource = getResource("admin.vm");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void ivoke(String url, Optional<String> userParam, Optional<String> passw
usedPassword.add(passwordParam.or(""));
}
});
listener.anEvent(event);
listener.handleEvent(event);
return this;
}

Expand Down

0 comments on commit 608e3c7

Please sign in to comment.