Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(gitea-web): tighten verification and repository browser behavior

Tine0fc19cca822
diff --git a/apps/web/src/components/project/gitea-integration-settings.tsx b/apps/web/src/components/project/gitea-integration-settings.tsx
index c7abbb746..58e0a035e 100644
--- a/apps/web/src/components/project/gitea-integration-settings.tsx
+++ b/apps/web/src/components/project/gitea-integration-settings.tsx
@@ -47,6 +47,29 @@ type GiteaIntegrationFormValues = {
repositoryName: string;
};
+type GiteaVerificationSnapshot = {
+ baseUrl: string;
+ accessToken: string;
+ repositoryOwner: string;
+ repositoryName: string;
+};
+
+type GiteaVerificationState = {
+ result: VerifyGiteaAccessResponse;
+ verified: GiteaVerificationSnapshot;
+};
+
+function createVerificationSnapshot(
+ values: GiteaIntegrationFormValues,
+): GiteaVerificationSnapshot {
+ return {
+ baseUrl: values.baseUrl.trim(),
+ accessToken: values.accessToken.trim(),
+ repositoryOwner: values.repositoryOwner.trim(),
+ repositoryName: values.repositoryName.trim(),
+ };
+}
+
export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
const { t } = useTranslation();
@@ -83,7 +106,12 @@ export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
[t],
);
- const { data: integration, isLoading } = useGetGiteaIntegration(projectId);
+ const {
+ data: integration,
+ isLoading,
+ error: integrationError,
+ refetch: refetchIntegration,
+ } = useGetGiteaIntegration(projectId);
const { mutateAsync: createIntegration, isPending: isCreating } =
useCreateGiteaIntegration();
const { mutateAsync: deleteIntegration, isPending: isDeleting } =
@@ -96,7 +124,7 @@ export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
useUpdateGiteaIntegration();
const [verificationResult, setVerificationResult] =
- React.useState<VerifyGiteaAccessResponse | null>(null);
+ React.useState<GiteaVerificationState | null>(null);
const [showRepositoryBrowser, setShowRepositoryBrowser] =
React.useState(false);
@@ -118,6 +146,7 @@ export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
repositoryOwner: integration.repositoryOwner,
repositoryName: integration.repositoryName,
});
+ setVerificationResult(null);
}
}, [integration, form]);
@@ -135,13 +164,17 @@ export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
return;
}
try {
+ const snapshot = createVerificationSnapshot(data);
const result = await verifyAccess({
- baseUrl: data.baseUrl,
- accessToken: token,
- repositoryOwner: data.repositoryOwner,
- repositoryName: data.repositoryName,
+ baseUrl: snapshot.baseUrl,
+ accessToken: snapshot.accessToken,
+ repositoryOwner: snapshot.repositoryOwner,
+ repositoryName: snapshot.repositoryName,
+ });
+ setVerificationResult({
+ result,
+ verified: snapshot,
});
- setVerificationResult(result);
if (showToast) {
if (result.isInstalled && result.hasRequiredPermissions) {
toast.success(t("settings:giteaIntegration.toast.verifyOk"));
@@ -169,6 +202,35 @@ export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
const accessToken = form.watch("accessToken");
const repositoryOwner = form.watch("repositoryOwner");
const repositoryName = form.watch("repositoryName");
+ const currentVerificationSnapshot = React.useMemo(
+ () =>
+ createVerificationSnapshot({
+ baseUrl,
+ accessToken,
+ repositoryOwner,
+ repositoryName,
+ }),
+ [baseUrl, accessToken, repositoryOwner, repositoryName],
+ );
+
+ React.useEffect(() => {
+ setVerificationResult((current) => {
+ if (!current) {
+ return current;
+ }
+
+ const stillMatches =
+ current.verified.baseUrl === currentVerificationSnapshot.baseUrl &&
+ current.verified.accessToken ===
+ currentVerificationSnapshot.accessToken &&
+ current.verified.repositoryOwner ===
+ currentVerificationSnapshot.repositoryOwner &&
+ current.verified.repositoryName ===
+ currentVerificationSnapshot.repositoryName;
+
+ return stillMatches ? current : null;
+ });
+ }, [currentVerificationSnapshot]);
React.useEffect(() => {
if (
@@ -200,12 +262,22 @@ export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
return;
}
- if (data.accessToken.trim()) {
+ const snapshot = createVerificationSnapshot(data);
+ const hasMatchingVerification =
+ verificationResult?.result.isInstalled &&
+ verificationResult.result.hasRequiredPermissions &&
+ verificationResult.verified.baseUrl === snapshot.baseUrl &&
+ verificationResult.verified.accessToken === snapshot.accessToken &&
+ verificationResult.verified.repositoryOwner ===
+ snapshot.repositoryOwner &&
+ verificationResult.verified.repositoryName === snapshot.repositoryName;
+
+ if (data.accessToken.trim() && !hasMatchingVerification) {
const verification = await verifyAccess({
- baseUrl: data.baseUrl,
- accessToken: data.accessToken.trim(),
- repositoryOwner: data.repositoryOwner,
- repositoryName: data.repositoryName,
+ baseUrl: snapshot.baseUrl,
+ accessToken: snapshot.accessToken,
+ repositoryOwner: snapshot.repositoryOwner,
+ repositoryName: snapshot.repositoryName,
});
if (!verification.isInstalled || !verification.hasRequiredPermissions) {
@@ -295,11 +367,46 @@ export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
);
}
+ if (integrationError) {
+ return (
+ <div className="space-y-4 border border-destructive/25 rounded-md p-4 bg-sidebar">
+ <div className="flex items-start justify-between gap-4">
+ <div className="space-y-1">
+ <p className="text-sm font-medium text-destructive">
+ {t("common:error.title")}
+ </p>
+ <p className="text-sm text-muted-foreground">
+ {integrationError instanceof Error
+ ? integrationError.message
+ : t("settings:giteaIntegration.toast.updateError")}
+ </p>
+ </div>
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ onClick={() => refetchIntegration()}
+ >
+ {t("settings:giteaIntegration.retry")}
+ </Button>
+ </div>
+ </div>
+ );
+ }
+
const isConnected = !!integration && integration.isActive;
- const canImport =
- isConnected &&
- verificationResult?.isInstalled &&
- verificationResult?.hasRequiredPermissions;
+ const hasVerifiedCurrentValues =
+ verificationResult?.result.isInstalled &&
+ verificationResult.result.hasRequiredPermissions &&
+ verificationResult.verified.baseUrl ===
+ currentVerificationSnapshot.baseUrl &&
+ verificationResult.verified.accessToken ===
+ currentVerificationSnapshot.accessToken &&
+ verificationResult.verified.repositoryOwner ===
+ currentVerificationSnapshot.repositoryOwner &&
+ verificationResult.verified.repositoryName ===
+ currentVerificationSnapshot.repositoryName;
+ const canImport = isConnected && Boolean(hasVerifiedCurrentValues);
const repoUrl =
integration?.baseUrl && integration.repositoryOwner
@@ -610,10 +717,7 @@ export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
isCreating ||
isDeleting ||
!form.formState.isValid ||
- (verificationResult
- ? !verificationResult.isInstalled ||
- !verificationResult.hasRequiredPermissions
- : false)
+ (verificationResult ? !hasVerifiedCurrentValues : false)
}
className="gap-2"
>
@@ -647,24 +751,26 @@ export function GiteaIntegrationSettings({ projectId }: { projectId: string }) {
<div
className={cn(
"flex items-start gap-3 p-3 border rounded-md text-sm",
- verificationResult.isInstalled &&
- verificationResult.hasRequiredPermissions
+ verificationResult.result.isInstalled &&
+ verificationResult.result.hasRequiredPermissions
? "border-success/25 bg-success/10"
- : verificationResult.repositoryExists
+ : verificationResult.result.repositoryExists
? "border-warning/25 bg-warning/10"
: "border-destructive/25 bg-destructive/10",
)}
>
- {verificationResult.isInstalled &&
- verificationResult.hasRequiredPermissions ? (
+ {verificationResult.result.isInstalled &&
+ verificationResult.result.hasRequiredPermissions ? (
<CheckCircle className="mt-0.5 h-4 w-4 flex-shrink-0 text-success-foreground" />
- ) : verificationResult.repositoryExists ? (
+ ) : verificationResult.result.repositoryExists ? (
<AlertTriangle className="mt-0.5 h-4 w-4 flex-shrink-0 text-warning-foreground" />
) : (
<XCircle className="mt-0.5 h-4 w-4 flex-shrink-0 text-destructive-foreground" />
)}
<div className="flex-1">
- <p className="font-medium">{verificationResult.message}</p>
+ <p className="font-medium">
+ {verificationResult.result.message}
+ </p>
</div>
</div>
</>
diff --git a/apps/web/src/components/project/gitea-repository-browser-modal.tsx b/apps/web/src/components/project/gitea-repository-browser-modal.tsx
index f64ecb98d..ff767ed92 100644
--- a/apps/web/src/components/project/gitea-repository-browser-modal.tsx
+++ b/apps/web/src/components/project/gitea-repository-browser-modal.tsx
@@ -41,7 +41,7 @@ export function GiteaRepositoryBrowserModal({
open && baseUrl.trim().length > 0 && accessToken.trim().length > 0;
const { data, isLoading, error, refetch } = useQuery({
- queryKey: ["gitea-repositories", baseUrl, accessToken],
+ queryKey: ["gitea-repositories", baseUrl],
queryFn: () => listGiteaRepositories({ baseUrl, accessToken }),
enabled: canFetch,
});
@@ -64,7 +64,7 @@ export function GiteaRepositoryBrowserModal({
owner: repository.owner.login,
name: repository.name,
});
- onOpenChange(false);
+ resetAndCloseModal(false);
};
const resetAndCloseModal = (next: boolean) => {
@@ -129,35 +129,36 @@ export function GiteaRepositoryBrowserModal({
<ul className="space-y-1">
{filteredRepositories.map((repo) => (
<li key={repo.id}>
- <button
- type="button"
- onClick={() => handleSelectRepository(repo)}
- className={cn(
- "w-full flex items-center justify-between gap-3 rounded-md px-3 py-2 text-left text-sm hover:bg-muted/80 transition-colors",
- selectedRepository === repo.full_name && "bg-muted",
- )}
- >
- <span className="font-medium truncate">
- {repo.full_name}
- </span>
- <div className="flex items-center gap-2 shrink-0">
- {repo.private ? (
- <Lock className="size-3.5 text-muted-foreground" />
- ) : null}
- <Badge variant="secondary" className="text-xs">
- {repo.owner.login}
- </Badge>
- <a
- href={repo.html_url}
- target="_blank"
- rel="noopener noreferrer"
- className="text-primary"
- onClick={(e) => e.stopPropagation()}
- >
- <ExternalLink className="size-3.5" />
- </a>
- </div>
- </button>
+ <div className="flex items-center gap-2">
+ <button
+ type="button"
+ onClick={() => handleSelectRepository(repo)}
+ className={cn(
+ "flex-1 flex items-center justify-between gap-3 rounded-md px-3 py-2 text-left text-sm hover:bg-muted/80 transition-colors",
+ selectedRepository === repo.full_name && "bg-muted",
+ )}
+ >
+ <span className="font-medium truncate">
+ {repo.full_name}
+ </span>
+ <div className="flex items-center gap-2 shrink-0">
+ {repo.private ? (
+ <Lock className="size-3.5 text-muted-foreground" />
+ ) : null}
+ <Badge variant="secondary" className="text-xs">
+ {repo.owner.login}
+ </Badge>
+ </div>
+ </button>
+ <a
+ href={repo.html_url}
+ target="_blank"
+ rel="noopener noreferrer"
+ className="rounded-md p-2 text-primary hover:bg-muted/80 transition-colors"
+ >
+ <ExternalLink className="size-3.5" />
+ </a>
+ </div>
</li>
))}
</ul>
diff --git a/apps/web/src/fetchers/gitea-integration/create-gitea-integration.ts b/apps/web/src/fetchers/gitea-integration/create-gitea-integration.ts
index 90e457af6..f396a2193 100644
--- a/apps/web/src/fetchers/gitea-integration/create-gitea-integration.ts
+++ b/apps/web/src/fetchers/gitea-integration/create-gitea-integration.ts
@@ -19,8 +19,17 @@ async function createGiteaIntegration(
});
if (!response.ok) {
- const error = await response.text();
- throw new Error(error);
+ const error = await response
+ .clone()
+ .json()
+ .catch(async () => ({
+ message: (await response.text()) || "Request failed",
+ }));
+ throw new Error(
+ typeof error === "object" && error && "message" in error
+ ? String(error.message)
+ : "Request failed",
+ );
}
return response.json();
diff --git a/apps/web/src/fetchers/gitea-integration/update-gitea-integration.ts b/apps/web/src/fetchers/gitea-integration/update-gitea-integration.ts
index 9fce1996a..1b268f446 100644
--- a/apps/web/src/fetchers/gitea-integration/update-gitea-integration.ts
+++ b/apps/web/src/fetchers/gitea-integration/update-gitea-integration.ts
@@ -17,8 +17,17 @@ async function updateGiteaIntegration(
});
if (!response.ok) {
- const error = await response.text();
- throw new Error(error);
+ const error = await response
+ .clone()
+ .json()
+ .catch(async () => ({
+ message: (await response.text()) || "Request failed",
+ }));
+ throw new Error(
+ typeof error === "object" && error && "message" in error
+ ? String(error.message)
+ : "Request failed",
+ );
}
return response.json();
diff --git a/apps/web/src/hooks/mutations/gitea-integration/use-import-gitea-issues.ts b/apps/web/src/hooks/mutations/gitea-integration/use-import-gitea-issues.ts
index 47b672d0e..c4b96fb0d 100644
--- a/apps/web/src/hooks/mutations/gitea-integration/use-import-gitea-issues.ts
+++ b/apps/web/src/hooks/mutations/gitea-integration/use-import-gitea-issues.ts
@@ -7,7 +7,7 @@ export default function useImportGiteaIssues() {
return useMutation({
mutationFn: (projectId: string) => importGiteaIssues(projectId),
onSuccess: (_, projectId) => {
- queryClient.invalidateQueries({ queryKey: ["task"] });
+ queryClient.invalidateQueries({ queryKey: ["tasks", projectId] });
queryClient.invalidateQueries({ queryKey: ["project", projectId] });
},
});