provide a way to disconnect github repo (#47)

This commit is contained in:
Will Chen
2025-04-29 12:12:47 -07:00
committed by GitHub
parent a33e6c6ae3
commit a53d72701b
4 changed files with 89 additions and 4 deletions

View File

@@ -172,6 +172,27 @@ export function GitHubConnector({ appId, folderName }: GitHubConnectorProps) {
} }
}; };
const [isDisconnecting, setIsDisconnecting] = useState(false);
const [disconnectError, setDisconnectError] = useState<string | null>(null);
const handleDisconnectRepo = async () => {
if (!appId) return;
setIsDisconnecting(true);
setDisconnectError(null);
try {
const result = await IpcClient.getInstance().disconnectGithubRepo(appId);
if (result.success) {
refreshApp();
} else {
setDisconnectError(result.error || "Failed to disconnect repository.");
}
} catch (err: any) {
setDisconnectError(err.message || "Failed to disconnect repository.");
} finally {
setIsDisconnecting(false);
}
};
if (!settings?.githubAccessToken) { if (!settings?.githubAccessToken) {
return ( return (
<div className="mt-1 w-full"> <div className="mt-1 w-full">
@@ -313,7 +334,7 @@ export function GitHubConnector({ appId, folderName }: GitHubConnectorProps) {
> >
{app.githubOrg}/{app.githubRepo} {app.githubOrg}/{app.githubRepo}
</a> </a>
<div className="mt-2"> <div className="mt-2 flex gap-2">
<Button onClick={handleSyncToGithub} disabled={isSyncing}> <Button onClick={handleSyncToGithub} disabled={isSyncing}>
{isSyncing ? ( {isSyncing ? (
<> <>
@@ -344,11 +365,21 @@ export function GitHubConnector({ appId, folderName }: GitHubConnectorProps) {
"Sync to GitHub" "Sync to GitHub"
)} )}
</Button> </Button>
<Button
onClick={handleDisconnectRepo}
disabled={isDisconnecting}
variant="outline"
>
{isDisconnecting ? "Disconnecting..." : "Disconnect from repo"}
</Button>
</div> </div>
{syncError && <p className="text-red-600 mt-2">{syncError}</p>} {syncError && <p className="text-red-600 mt-2">{syncError}</p>}
{syncSuccess && ( {syncSuccess && (
<p className="text-green-600 mt-2">Successfully pushed to GitHub!</p> <p className="text-green-600 mt-2">Successfully pushed to GitHub!</p>
)} )}
{disconnectError && (
<p className="text-red-600 mt-2">{disconnectError}</p>
)}
</div> </div>
); );
} else { } else {

View File

@@ -422,10 +422,48 @@ async function handlePushToGithub(
} }
} }
async function handleDisconnectGithubRepo(
event: IpcMainInvokeEvent,
{ appId }: { appId: number }
) {
try {
logger.log(`Disconnecting GitHub repo for appId: ${appId}`);
// Get the app from the database
const app = await db.query.apps.findFirst({
where: eq(apps.id, appId),
});
if (!app) {
return { success: false, error: "App not found" };
}
// Update app in database to remove GitHub repo and org
await db
.update(apps)
.set({
githubRepo: null,
githubOrg: null,
})
.where(eq(apps.id, appId));
return { success: true };
} catch (error) {
logger.error(`Error disconnecting GitHub repo: ${error}`);
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
// --- Registration --- // --- Registration ---
export function registerGithubHandlers() { export function registerGithubHandlers() {
ipcMain.handle("github:start-flow", handleStartGithubFlow); ipcMain.handle("github:start-flow", handleStartGithubFlow);
ipcMain.handle("github:is-repo-available", handleIsRepoAvailable); ipcMain.handle("github:is-repo-available", handleIsRepoAvailable);
ipcMain.handle("github:create-repo", handleCreateRepo); ipcMain.handle("github:create-repo", handleCreateRepo);
ipcMain.handle("github:push", handlePushToGithub); ipcMain.handle("github:push", handlePushToGithub);
ipcMain.handle("github:disconnect", (event, args: { appId: number }) =>
handleDisconnectGithubRepo(event, args)
);
} }

View File

@@ -611,9 +611,24 @@ export class IpcClient {
): Promise<{ success: boolean; error?: string }> { ): Promise<{ success: boolean; error?: string }> {
try { try {
const result = await this.ipcRenderer.invoke("github:push", { appId }); const result = await this.ipcRenderer.invoke("github:push", { appId });
return result; return result as { success: boolean; error?: string };
} catch (error: any) { } catch (error) {
return { success: false, error: error.message || "Unknown error" }; showError(error);
throw error;
}
}
public async disconnectGithubRepo(
appId: number
): Promise<{ success: boolean; error?: string }> {
try {
const result = await this.ipcRenderer.invoke("github:disconnect", {
appId,
});
return result as { success: boolean; error?: string };
} catch (error) {
showError(error);
throw error;
} }
} }
// --- End GitHub Repo Management --- // --- End GitHub Repo Management ---

View File

@@ -38,6 +38,7 @@ const validInvokeChannels = [
"github:is-repo-available", "github:is-repo-available",
"github:create-repo", "github:create-repo",
"github:push", "github:push",
"github:disconnect",
"get-app-version", "get-app-version",
"reload-env-path", "reload-env-path",
"get-proposal", "get-proposal",