1
1
package se .bjurr .prnfs .listener ;
2
2
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 ;
3
7
import static se .bjurr .prnfs .settings .SettingsStorage .getPrnfsSettings ;
4
8
5
9
import org .slf4j .Logger ;
13
17
import com .atlassian .sal .api .pluginsettings .PluginSettingsFactory ;
14
18
import com .atlassian .stash .event .pull .PullRequestEvent ;
15
19
import com .google .common .annotations .VisibleForTesting ;
20
+ import com .google .common .cache .Cache ;
16
21
17
22
public class PrnfsPullRequestEventListener {
18
23
19
24
private UrlInvoker urlInvoker = new UrlInvoker ();
20
25
private final PluginSettingsFactory pluginSettingsFactory ;
21
26
private static final Logger logger = LoggerFactory .getLogger (PrnfsPullRequestEventListener .class );
27
+ private static Cache <Object , Object > duplicateEventCache ;
22
28
23
29
public PrnfsPullRequestEventListener (PluginSettingsFactory pluginSettingsFactory ) {
24
30
this .pluginSettingsFactory = pluginSettingsFactory ;
31
+ duplicateEventCache = newBuilder ().maximumSize (1000 ).expireAfterWrite (50 , MILLISECONDS ).build ();
25
32
}
26
33
27
34
@ VisibleForTesting
@@ -31,6 +38,9 @@ public void setUrlInvoker(UrlInvoker urlInvoker) {
31
38
32
39
@ EventListener
33
40
public void anEvent (PullRequestEvent o ) {
41
+ if (dublicateEventBug (o )) {
42
+ return ;
43
+ }
34
44
try {
35
45
final PrnfsSettings settings = getPrnfsSettings (pluginSettingsFactory .createGlobalSettings ());
36
46
for (final PrnfsNotification n : settings .getNotifications ()) {
@@ -42,4 +52,16 @@ public void anEvent(PullRequestEvent o) {
42
52
logger .error ("" , e );
43
53
}
44
54
}
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
+ }
45
67
}
0 commit comments