AnAction from Swing component that invokes a Git4Idea call

Answered

For reference, I've tried the solution from here and here, without success as the Git4Idea call fails.

I have an AnAction (that does some setup when a PR comes in) that I can trigger successfully from a balloon notification. When the action is run, the system first fetches from the appropriate repo, which is successful when in the context of a balloon.

I also need to trigger it from a double-click in a JList containing the names of the PRs. I can successfully trigger the AnAction, using the methods in the linked posts via ActionUtil, however when I call the fetch operation within the AnAction I get an assertion error in `BuiltInServerManagerImpl#waitForStart`, where it checks if we're in a dispatch thread.

java.lang.Throwable: Assertion failed
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:197)
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:206)
at org.jetbrains.ide.BuiltInServerManagerImpl.waitForStart(BuiltInServerManagerImpl.kt:107)
at git4idea.rebase.GitRebaseEditorService.getEditorCommand(GitRebaseEditorService.java:81)
at git4idea.rebase.GitHandlerRebaseEditorManager.prepareEditor(GitHandlerRebaseEditorManager.java:42)
at git4idea.rebase.GitHandlerRebaseEditorManager.lambda$prepareEditor$0(GitHandlerRebaseEditorManager.java:28)
at git4idea.GitUtil.tryRunOrClose(GitUtil.java:1040)
at git4idea.rebase.GitHandlerRebaseEditorManager.prepareEditor(GitHandlerRebaseEditorManager.java:27)
at git4idea.commands.GitImplBase.prepareGeneralPurposeEditor(GitImplBase.java:186)
at git4idea.commands.GitImplBase.run(GitImplBase.java:170)
at git4idea.commands.GitImplBase.runCommand(GitImplBase.java:71)
at git4idea.config.GitConfigUtil.getValue(GitConfigUtil.java:77)
at git4idea.config.GitConfigUtil.getValue(GitConfigUtil.java:70)
at git4idea.fetch.GitFetchSupportImpl.isStoreCredentialsHelperUsed(GitFetchSupportImpl.kt:182)
at git4idea.fetch.GitFetchSupportImpl.getMaxThreads(GitFetchSupportImpl.kt:174)
at git4idea.fetch.GitFetchSupportImpl.fetchInParallel(GitFetchSupportImpl.kt:140)
at git4idea.fetch.GitFetchSupportImpl.access$fetchInParallel(GitFetchSupportImpl.kt:45)
at git4idea.fetch.GitFetchSupportImpl$fetch$1.invoke(GitFetchSupportImpl.kt:104)
at git4idea.fetch.GitFetchSupportImpl$fetch$1.invoke(GitFetchSupportImpl.kt:45)
at git4idea.fetch.GitFetchSupportImpl.withIndicator(GitFetchSupportImpl.kt:211)
at git4idea.fetch.GitFetchSupportImpl.fetch(GitFetchSupportImpl.kt:101)
at git4idea.fetch.GitFetchSupportImpl.fetch(GitFetchSupportImpl.kt:91)

This does all work successfully when it's invoked directly as an action from a `com.intellij.notification.Notification` balloon popup.

How can I get my Swing event to trigger the action in the same context as the Notification does? 

8 comments
Comment actions Permalink

Please clarify what is the ActionID you're calling here, ideally also snippet showing invocation.

0
Comment actions Permalink

My action doesn't have an action ID as it's added to a com.intellij.notification.Notification (in the case where it's working).

In the case where it isn't working, I execute it as:

openPRList.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
String selectedPullRequest = openPrModel.getElementAt(openPRList.locationToIndex(e.getPoint()));
if (selectedPullRequest != null)
{
for (PullRequest pullRequest : pullRequestsForProject)
{
if (pullRequest.getTitle().equals(selectedPullRequest))
{
JList checkBox = (JList) e.getSource();
ApplicationManager.getApplication().invokeLater(() -> {
DataContext dataContext = ActionToolbar.getDataContextFor(checkBox);
SwitchToPRBranchAction action = new SwitchToPRBranchAction(pullRequest);
ActionUtil.invokeAction(action, dataContext, "BitbucketPR", null, null);
});
0
Comment actions Permalink

Please try invoking in non-EDT way using com.intellij.openapi.progress.ProgressManager

0
Comment actions Permalink

Invoking per:

ProgressManager.getInstance().executeNonCancelableSection(() -> {
DataContext dataContext = ActionToolbar.getDataContextFor(checkBox);
SwitchToPRBranchAction action = new SwitchToPRBranchAction(pullRequest);
ActionUtil.invokeAction(action, dataContext, "BitbucketPR", null, null);
});

Still produces

java.lang.Throwable: Assertion failed
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:197)
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:206)
at org.jetbrains.ide.BuiltInServerManagerImpl.waitForStart(BuiltInServerManagerImpl.kt:107)
at org.jetbrains.git4idea.ssh.GitXmlRpcHandlerService.getXmlRcpPort(GitXmlRpcHandlerService.java:68)
at git4idea.commands.GitHandlerAuthenticationManager.prepareHttpAuth(GitHandlerAuthenticationManager.java:93)
at git4idea.commands.GitHandlerAuthenticationManager.lambda$prepare$0(GitHandlerAuthenticationManager.java:60)
at git4idea.GitUtil.tryRunOrClose(GitUtil.java:1040)
at git4idea.commands.GitHandlerAuthenticationManager.prepare(GitHandlerAuthenticationManager.java:59)
at git4idea.commands.GitImplBase.run(GitImplBase.java:169)
at git4idea.commands.GitImplBase.runCommand(GitImplBase.java:71)
at git4idea.config.GitConfigUtil.getValue(GitConfigUtil.java:77)
at git4idea.config.GitConfigUtil.getValue(GitConfigUtil.java:70)
at git4idea.fetch.GitFetchSupportImpl.isStoreCredentialsHelperUsed(GitFetchSupportImpl.kt:182)
at git4idea.fetch.GitFetchSupportImpl.getMaxThreads(GitFetchSupportImpl.kt:174)
at git4idea.fetch.GitFetchSupportImpl.fetchInParallel(GitFetchSupportImpl.kt:140)
at git4idea.fetch.GitFetchSupportImpl.access$fetchInParallel(GitFetchSupportImpl.kt:45)
at git4idea.fetch.GitFetchSupportImpl$fetch$1.invoke(GitFetchSupportImpl.kt:104)
at git4idea.fetch.GitFetchSupportImpl$fetch$1.invoke(GitFetchSupportImpl.kt:45)
at git4idea.fetch.GitFetchSupportImpl.withIndicator(GitFetchSupportImpl.kt:211)
at git4idea.fetch.GitFetchSupportImpl.fetch(GitFetchSupportImpl.kt:101)
at git4idea.fetch.GitFetchSupportImpl.fetch(GitFetchSupportImpl.kt:91)

It doesn't seem to stop the flow though, not sure what's happening under the hood but my subsequent lines are executed - but it does produce the error log saying that my plugin produced an exception.

The subsequent lines are:

GitFetchSupport.fetchSupport(project).fetch(gitRepository, gitRepository.getRemotes().iterator().next()); // produces the assertion exception

GitBrancher.getInstance(project).checkout(targetBranch, false, Collections.singletonList(gitRepository), () -> {
0
Comment actions Permalink

executeNonCancelableSection() runs on invoking thread and thus is not working, please try runProcessWithProgressSynchronously() or background Task

0
Comment actions Permalink

I may need a different way to execute the action then? Executing it as I currently am produces this:

Access is allowed from event dispatch thread with IW lock only.

com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: EventQueue.isDispatchThread()=false Toolkit.getEventQueue()=com.intellij.ide.IdeEventQueue@4fab9b55
Current thread: Thread[ApplicationImpl pooled thread 1,4,Idea Thread Group] 1684960391
SystemEventQueueThread: Thread[AWT-EventQueue-0,6,Idea Thread Group] 631249080
at com.intellij.openapi.application.impl.ApplicationImpl.throwThreadAccessException(ApplicationImpl.java:1047)
at com.intellij.openapi.application.impl.ApplicationImpl.assertIsDispatchThread(ApplicationImpl.java:1022)
at com.intellij.ide.impl.DataManagerImpl.getDataContext(DataManagerImpl.java:167)
at com.intellij.openapi.actionSystem.ActionToolbar.getDataContextFor(ActionToolbar.java:171)
at za.co.discoverylife.bitbucket.BitbucketPRViewer$1.lambda$mouseClicked$0(BitbucketPRViewer.java:53)

Which is from the first line of this block:

DataContext dataContext = ActionToolbar.getDataContextFor(checkBox);
SwitchToPRBranchAction action = new SwitchToPRBranchAction(pullRequest);
ActionUtil.invokeAction(action, dataContext, "BitbucketPR", null, null);
0
Comment actions Permalink
ActionToolbar.getDataContextFor(checkBox) must be executed in EDT
0
Comment actions Permalink

Hey - sorry, yes, was being dumb :)

I ended up having to execute the action in the EDT, and moved the runProcessWithProgressSynchronously deeper into the action itself, and all seems good now, thanks.

0

Please sign in to leave a comment.