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

Commit 04dd29b

Browse files
committed
Fixing multiple events issue
1 parent 3d93bb1 commit 04dd29b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package se.bjurr.prnfs.listener;
22

3+
import static com.google.common.cache.CacheBuilder.newBuilder;
4+
import static java.lang.Boolean.FALSE;
5+
import static java.lang.Boolean.TRUE;
6+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
37
import static se.bjurr.prnfs.settings.SettingsStorage.getPrnfsSettings;
48

59
import org.slf4j.Logger;
@@ -13,15 +17,18 @@
1317
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
1418
import com.atlassian.stash.event.pull.PullRequestEvent;
1519
import com.google.common.annotations.VisibleForTesting;
20+
import com.google.common.cache.Cache;
1621

1722
public class PrnfsPullRequestEventListener {
1823

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

2329
public PrnfsPullRequestEventListener(PluginSettingsFactory pluginSettingsFactory) {
2430
this.pluginSettingsFactory = pluginSettingsFactory;
31+
duplicateEventCache = newBuilder().maximumSize(1000).expireAfterWrite(50, MILLISECONDS).build();
2532
}
2633

2734
@VisibleForTesting
@@ -31,6 +38,9 @@ public void setUrlInvoker(UrlInvoker urlInvoker) {
3138

3239
@EventListener
3340
public void anEvent(PullRequestEvent o) {
41+
if (dublicateEventBug(o)) {
42+
return;
43+
}
3444
try {
3545
final PrnfsSettings settings = getPrnfsSettings(pluginSettingsFactory.createGlobalSettings());
3646
for (final PrnfsNotification n : settings.getNotifications()) {
@@ -42,4 +52,16 @@ public void anEvent(PullRequestEvent o) {
4252
logger.error("", e);
4353
}
4454
}
55+
56+
/**
57+
* Looks like there is a bug in Stash that causes events to be fired twice.
58+
*/
59+
public static boolean dublicateEventBug(PullRequestEvent o) {
60+
final String footprint = o.getPullRequest().getId() + "_" + o.getAction().name();
61+
if (duplicateEventCache.asMap().containsKey(footprint)) {
62+
return TRUE;
63+
}
64+
duplicateEventCache.put(footprint, TRUE);
65+
return FALSE;
66+
}
4567
}

src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package se.bjurr.prnfs.admin;
22

3+
import static com.atlassian.stash.pull.PullRequestAction.APPROVED;
34
import static com.atlassian.stash.pull.PullRequestAction.MERGED;
45
import static com.atlassian.stash.pull.PullRequestAction.OPENED;
6+
import static java.lang.Boolean.FALSE;
7+
import static java.lang.Boolean.TRUE;
8+
import static java.lang.Thread.sleep;
9+
import static org.junit.Assert.assertEquals;
510
import static se.bjurr.prnfs.admin.utils.NotificationBuilder.notificationBuilder;
611
import static se.bjurr.prnfs.admin.utils.PrnfsTestBuilder.prnfsTestBuilder;
712
import static se.bjurr.prnfs.admin.utils.PullRequestEventBuilder.pullRequestEventBuilder;
813
import static se.bjurr.prnfs.admin.utils.PullRequestRefBuilder.pullRequestRefBuilder;
14+
import static se.bjurr.prnfs.listener.PrnfsPullRequestEventListener.dublicateEventBug;
915

1016
import org.junit.Test;
1117
import org.slf4j.Logger;
@@ -91,4 +97,18 @@ public void testThatMultipleUrlsCanBeInvoked() {
9197
.withToRef(pullRequestRefBuilder()) //
9298
.withId(10L).withPullRequestAction(MERGED).build()).invokedOnlyUrl("http://merged.se/");
9399
}
100+
101+
@Test
102+
public void testThatDuplicateEventsFiredInStashAreIgnored() throws InterruptedException {
103+
assertEquals(FALSE, dublicateEventBug(pullRequestEventBuilder().withId(100L).withPullRequestAction(APPROVED).build()));
104+
assertEquals(TRUE, dublicateEventBug(pullRequestEventBuilder().withId(100L).withPullRequestAction(APPROVED).build()));
105+
assertEquals(FALSE, dublicateEventBug(pullRequestEventBuilder().withId(100L).withPullRequestAction(OPENED).build()));
106+
assertEquals(FALSE, dublicateEventBug(pullRequestEventBuilder().withId(101L).withPullRequestAction(APPROVED).build()));
107+
assertEquals(TRUE, dublicateEventBug(pullRequestEventBuilder().withId(100L).withPullRequestAction(OPENED).build()));
108+
assertEquals(TRUE, dublicateEventBug(pullRequestEventBuilder().withId(101L).withPullRequestAction(APPROVED).build()));
109+
sleep(20);
110+
assertEquals(TRUE, dublicateEventBug(pullRequestEventBuilder().withId(100L).withPullRequestAction(APPROVED).build()));
111+
sleep(100);
112+
assertEquals(FALSE, dublicateEventBug(pullRequestEventBuilder().withId(100L).withPullRequestAction(APPROVED).build()));
113+
}
94114
}

0 commit comments

Comments
 (0)