Skip to content

Commit e5da821

Browse files
committed
adding a fork check worker to replace fork and bramch worker by making it into seperate functions
1 parent 7592283 commit e5da821

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package org.pathvisio.githubplugin.worker;
2+
3+
import org.pathvisio.githubplugin.service.GitHubForkService;
4+
import org.pathvisio.githubplugin.service.GitHubForkSyncService;
5+
import org.pathvisio.githubplugin.service.GitHubForkSyncService.SyncResult;
6+
7+
import javax.swing.SwingWorker;
8+
import java.io.IOException;
9+
import java.util.List;
10+
import java.util.concurrent.ExecutionException;
11+
12+
/**
13+
* Confirms the user's fork exists and is synced with upstream, off the EDT.
14+
* Does NOT create or check any branch — see {@link BranchReuseWorker} for
15+
* that, which now runs separately on Save click rather than on dialog open.
16+
*/
17+
public class ForkCheckWorker extends SwingWorker<Void, String>
18+
{
19+
private final String accessToken;
20+
private final String authenticatedUsername;
21+
private final String upstreamRepo;
22+
private final ForkCheckCallback callback;
23+
24+
private static class ForkConflictException extends IOException
25+
{
26+
ForkConflictException(String message) { super(message); }
27+
}
28+
29+
public ForkCheckWorker(String accessToken, String authenticatedUsername,
30+
String upstreamRepo, ForkCheckCallback callback)
31+
{
32+
this.accessToken = accessToken;
33+
this.authenticatedUsername = authenticatedUsername;
34+
this.upstreamRepo = upstreamRepo;
35+
this.callback = callback;
36+
}
37+
38+
@Override
39+
protected Void doInBackground() throws Exception
40+
{
41+
GitHubForkService forkService =
42+
new GitHubForkService(accessToken, authenticatedUsername, upstreamRepo);
43+
GitHubForkSyncService syncService =
44+
new GitHubForkSyncService(accessToken, authenticatedUsername, upstreamRepo);
45+
46+
publish("Checking fork...");
47+
boolean forkReady = forkService.ensureForkExists();
48+
if (!forkReady)
49+
{
50+
throw new IOException("Fork creation timed out before becoming ready.");
51+
}
52+
publish("Fork ready");
53+
54+
publish("Syncing with upstream...");
55+
SyncResult syncResult = syncService.syncWithUpstreamMain();
56+
if (syncResult == SyncResult.CONFLICT)
57+
{
58+
throw new ForkConflictException("Fork's main has diverged and could not be auto-synced.");
59+
}
60+
publish(syncResult == SyncResult.UPDATED ? "Fork synced" : "Fork already up to date");
61+
62+
return null;
63+
}
64+
65+
@Override
66+
protected void process(List<String> chunks)
67+
{
68+
for (String message : chunks)
69+
{
70+
callback.onStatusUpdate(message);
71+
}
72+
}
73+
74+
@Override
75+
protected void done()
76+
{
77+
try
78+
{
79+
get();
80+
callback.onSuccess();
81+
}
82+
catch (InterruptedException e)
83+
{
84+
Thread.currentThread().interrupt();
85+
callback.onFailure(e.getMessage());
86+
}
87+
catch (java.util.concurrent.CancellationException e)
88+
{
89+
callback.onFailure("Operation was cancelled.");
90+
}
91+
catch (ExecutionException e)
92+
{
93+
Throwable cause = e.getCause();
94+
if (cause instanceof ForkConflictException)
95+
{
96+
callback.onConflict();
97+
}
98+
else if (cause != null)
99+
{
100+
callback.onFailure(cause.getMessage());
101+
}
102+
else
103+
{
104+
callback.onFailure(e.getMessage());
105+
}
106+
}
107+
}
108+
109+
public interface ForkCheckCallback
110+
{
111+
void onStatusUpdate(String message);
112+
void onConflict();
113+
void onSuccess();
114+
void onFailure(String errorMessage);
115+
}
116+
}

0 commit comments

Comments
 (0)