mohamicorp / stash-jenkins-postreceive-webhook

Webhook used to notify Jenkins when commits are made to Stash
Other
138 stars 98 forks source link

NoSuchElementException when pushing remote merge #108

Closed bbaetz closed 9 years ago

bbaetz commented 9 years ago

When merging locally and then pushing to stash (3.6.1), the hook doesn't run properly, and instead logs an error.

the problem is that https://developer.atlassian.com/static/javadoc/stash/3.7.0/api/reference/com/atlassian/stash/event/RepositoryRefsChangedEvent.html#getRefChanges%28%29 states that "Note: While the returned collection will never be null, it may be empty. For example, a remotely merged pull request will have an empty set of changes." but the code does:

RefChange refCh = event.getRefChanges().iterator().next();

which fails when the collection is empty. (probably also related to #80, since it only pulls one ref out of the collection)

Not sure what the fix is for this - its easy to just skip empty collections (or when its a PullRequestMergedEvent AND isMergedRemotely) but I'm not sure if that would miss the push entirely if that's the only event. (If we get multiple events then it would all be good - need to test; at least a few of the examples I've look at here haven't fired the Jenkins job but I need to confirm on Monday)

to reproduce:

Stash will close the pull request as 'remotely merged' but the hook won't fire.

2015-02-27 17:08:08,562 ERROR [AtlassianEvent::thread-11] c.a.s.i.e.AsyncBatchingInvokersTransformer There was an exception thrown trying to dispatch event 'com.atlassian.stash.event.pull.PullRequestMergedEvent[source=com.atlassian.stash.internal.pull.DefaultPullRequestService@4fa699c4]' for the invoker 'SingleParameterMethodListenerInvoker{method=public void com.nerdwin15.stash.webhook.RepositoryChangeListener.onRefsChangedEvent(com.atlassian.stash.event.RepositoryRefsChangedEvent), listener=com.nerdwin15.stash.webhook.RepositoryChangeListener@56837901}'. java.lang.RuntimeException: java.util.NoSuchElementException at com.atlassian.event.internal.SingleParameterMethodListenerInvoker.invoke(SingleParameterMethodListenerInvoker.java:50) ~[atlassian-event-2.3.5.jar:na] at com.atlassian.stash.internal.event.AsyncBatchingInvokersTransformer$AsyncInvokerBatch.invoke(AsyncBatchingInvokersTransformer.java:100) ~[stash-platform-3.6.1.jar:na] at com.atlassian.event.internal.AsynchronousAbleEventDispatcher$1$1.run(AsynchronousAbleEventDispatcher.java:48) [atlassian-event-2.3.5.jar:na] at com.atlassian.sal.core.executor.ThreadLocalDelegateRunnable.run(ThreadLocalDelegateRunnable.java:38) [sal-core-2.13.3.jar:na] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_55] at java.lang.Thread.run(Thread.java:745) [na:1.7.0_55] ... 1 frame trimmed Caused by: java.util.NoSuchElementException: null at java.util.Collections$EmptyIterator.next(Collections.java:3006) ~[na:1.7.0_55] at com.nerdwin15.stash.webhook.RepositoryChangeListener.onRefsChangedEvent(RepositoryChangeListener.java:48) ~[na:na] at com.atlassian.event.internal.SingleParameterMethodListenerInvoker.invoke(SingleParameterMethodListenerInvoker.java:36) ~[atlassian-event-2.3.5.jar:na] ... 6 common frames omitted

bbaetz commented 9 years ago

If we get multiple events then it would all be good - need to test;

Confirmed that there are multiple events in that case, so just iterating over the collection is fine.

mikesir87 commented 9 years ago

Thanks for the notice and for the pull request @bbaetz!