fix(github): drain piled-up PR/issue review tasks via cleanup policy + manual sweep (#929)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useCallback } from "react";
|
||||
import { IconBrandGithub, IconPlus } from "@tabler/icons-react";
|
||||
import { IconBrandGithub, IconPlus, IconTrashX } from "@tabler/icons-react";
|
||||
import { Button } from "@kandev/ui/button";
|
||||
import { Card, CardContent } from "@kandev/ui/card";
|
||||
import { Separator } from "@kandev/ui/separator";
|
||||
@@ -19,8 +19,52 @@ import { PRStatsPanel } from "./pr-stats";
|
||||
import { useReviewWatches } from "@/hooks/domains/github/use-review-watches";
|
||||
import { useIssueWatches } from "@/hooks/domains/github/use-issue-watches";
|
||||
import { WorkspaceScopedSection } from "@/components/integrations/workspace-scoped-section";
|
||||
import { cleanupMergedReviewTasks, cleanupClosedIssueTasks } from "@/lib/api/domains/github-api";
|
||||
import type { ReviewWatch, IssueWatch } from "@/lib/types/github";
|
||||
|
||||
// CleanupNowButton runs a manual global sweep over the dedup tables. Useful
|
||||
// for users who upgraded with a pile of legacy merged-PR / closed-issue
|
||||
// review tasks that pre-date the cleanup_policy work — clicking once drains
|
||||
// them according to each watch's policy.
|
||||
function CleanupNowButton({
|
||||
label,
|
||||
run,
|
||||
}: {
|
||||
label: string;
|
||||
run: () => Promise<{ deleted: number }>;
|
||||
}) {
|
||||
const { toast } = useToast();
|
||||
const [busy, setBusy] = useState(false);
|
||||
return (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
disabled={busy}
|
||||
onClick={async () => {
|
||||
setBusy(true);
|
||||
try {
|
||||
const { deleted } = await run();
|
||||
toast({
|
||||
description:
|
||||
deleted === 0
|
||||
? "No tasks to clean up"
|
||||
: `Deleted ${deleted} task${deleted === 1 ? "" : "s"}`,
|
||||
variant: "success",
|
||||
});
|
||||
} catch {
|
||||
toast({ description: "Cleanup failed", variant: "error" });
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
<IconTrashX className="h-4 w-4 mr-1" />
|
||||
{busy ? "Cleaning…" : label}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
function useWatchActions(workspaceId?: string | null) {
|
||||
const { items: watches, create, update, remove, trigger } = useReviewWatches(workspaceId);
|
||||
const { toast } = useToast();
|
||||
@@ -205,17 +249,20 @@ function ReviewWatchSection() {
|
||||
title="Review Watches"
|
||||
description="Automatically create tasks for PRs that need your review."
|
||||
action={
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setEditingWatch(null);
|
||||
setDialogOpen(true);
|
||||
}}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
<IconPlus className="h-4 w-4 mr-1" />
|
||||
Add Watch
|
||||
</Button>
|
||||
<div className="flex items-center gap-2">
|
||||
<CleanupNowButton label="Clean up merged" run={cleanupMergedReviewTasks} />
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setEditingWatch(null);
|
||||
setDialogOpen(true);
|
||||
}}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
<IconPlus className="h-4 w-4 mr-1" />
|
||||
Add Watch
|
||||
</Button>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<Card>
|
||||
@@ -266,17 +313,20 @@ function IssueWatchSection() {
|
||||
title="Issue Watches"
|
||||
description="Automatically create tasks for GitHub issues matching your criteria."
|
||||
action={
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setEditingIssueWatch(null);
|
||||
setDialogOpen(true);
|
||||
}}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
<IconPlus className="h-4 w-4 mr-1" />
|
||||
Add Watch
|
||||
</Button>
|
||||
<div className="flex items-center gap-2">
|
||||
<CleanupNowButton label="Clean up closed" run={cleanupClosedIssueTasks} />
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setEditingIssueWatch(null);
|
||||
setDialogOpen(true);
|
||||
}}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
<IconPlus className="h-4 w-4 mr-1" />
|
||||
Add Watch
|
||||
</Button>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<Card>
|
||||
|
||||
@@ -35,6 +35,7 @@ import type {
|
||||
IssueWatch,
|
||||
CreateIssueWatchRequest,
|
||||
UpdateIssueWatchRequest,
|
||||
CleanupPolicy,
|
||||
} from "@/lib/types/github";
|
||||
|
||||
type IssueWatchDialogProps = {
|
||||
@@ -61,10 +62,29 @@ type FormState = {
|
||||
customQuery: string;
|
||||
enabled: boolean;
|
||||
pollInterval: number;
|
||||
cleanupPolicy: CleanupPolicy;
|
||||
};
|
||||
|
||||
const DEFAULT_QUERY = "type:issue state:open";
|
||||
|
||||
const CLEANUP_POLICY_OPTIONS: Array<{ id: CleanupPolicy; label: string; description: string }> = [
|
||||
{
|
||||
id: "auto",
|
||||
label: "Auto (recommended)",
|
||||
description: "Delete closed-issue tasks unless you typed a message in them.",
|
||||
},
|
||||
{
|
||||
id: "always",
|
||||
label: "Always delete",
|
||||
description: "Delete on close even if you engaged with the task.",
|
||||
},
|
||||
{
|
||||
id: "never",
|
||||
label: "Never auto-delete",
|
||||
description: "Keep all tasks. Delete them manually from the task list.",
|
||||
},
|
||||
];
|
||||
|
||||
function makeDefaultForm(workspaceId: string): FormState {
|
||||
return {
|
||||
workspaceId,
|
||||
@@ -79,6 +99,7 @@ function makeDefaultForm(workspaceId: string): FormState {
|
||||
customQuery: DEFAULT_QUERY,
|
||||
enabled: true,
|
||||
pollInterval: 300,
|
||||
cleanupPolicy: "auto",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -97,6 +118,7 @@ function formStateFromWatch(watch: IssueWatch): FormState {
|
||||
customQuery: watch.custom_query || DEFAULT_QUERY,
|
||||
enabled: watch.enabled,
|
||||
pollInterval: watch.poll_interval_seconds,
|
||||
cleanupPolicy: watch.cleanup_policy ?? "auto",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -354,6 +376,16 @@ function IssueSettingsFields({
|
||||
className="cursor-pointer"
|
||||
/>
|
||||
</div>
|
||||
<SelectField
|
||||
label="Cleanup behavior"
|
||||
description={
|
||||
CLEANUP_POLICY_OPTIONS.find((p) => p.id === form.cleanupPolicy)?.description ?? ""
|
||||
}
|
||||
value={form.cleanupPolicy}
|
||||
onChange={(v) => setForm((prev) => ({ ...prev, cleanupPolicy: v as CleanupPolicy }))}
|
||||
placeholder="Auto"
|
||||
items={CLEANUP_POLICY_OPTIONS.map((p) => ({ id: p.id, label: p.label }))}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -418,6 +450,7 @@ export function IssueWatchDialog({
|
||||
custom_query: form.customQuery,
|
||||
enabled: form.enabled,
|
||||
poll_interval_seconds: form.pollInterval,
|
||||
cleanup_policy: form.cleanupPolicy,
|
||||
};
|
||||
if (watch) {
|
||||
await onUpdate(watch.id, payload);
|
||||
|
||||
@@ -33,6 +33,7 @@ import type {
|
||||
ReviewWatch,
|
||||
CreateReviewWatchRequest,
|
||||
UpdateReviewWatchRequest,
|
||||
CleanupPolicy,
|
||||
} from "@/lib/types/github";
|
||||
|
||||
type ReviewWatchDialogProps = {
|
||||
@@ -63,6 +64,7 @@ type FormState = {
|
||||
customQuery: string;
|
||||
enabled: boolean;
|
||||
pollInterval: number;
|
||||
cleanupPolicy: CleanupPolicy;
|
||||
};
|
||||
|
||||
function makeDefaultForm(workspaceId: string): FormState {
|
||||
@@ -78,6 +80,7 @@ function makeDefaultForm(workspaceId: string): FormState {
|
||||
customQuery: QUERY_TEMPLATES.meAndTeams,
|
||||
enabled: true,
|
||||
pollInterval: 300,
|
||||
cleanupPolicy: "auto",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -95,9 +98,28 @@ function formStateFromWatch(watch: ReviewWatch): FormState {
|
||||
customQuery: watch.custom_query || QUERY_TEMPLATES.meAndTeams,
|
||||
enabled: watch.enabled,
|
||||
pollInterval: watch.poll_interval_seconds,
|
||||
cleanupPolicy: watch.cleanup_policy ?? "auto",
|
||||
};
|
||||
}
|
||||
|
||||
const CLEANUP_POLICY_OPTIONS: Array<{ id: CleanupPolicy; label: string; description: string }> = [
|
||||
{
|
||||
id: "auto",
|
||||
label: "Auto (recommended)",
|
||||
description: "Delete merged/closed PR tasks unless you typed a message in them.",
|
||||
},
|
||||
{
|
||||
id: "always",
|
||||
label: "Always delete",
|
||||
description: "Delete on merge/close even if you engaged with the task.",
|
||||
},
|
||||
{
|
||||
id: "never",
|
||||
label: "Never auto-delete",
|
||||
description: "Keep all tasks. Delete them manually from the task list.",
|
||||
},
|
||||
];
|
||||
|
||||
// --- Generic select field with description ---
|
||||
|
||||
type SelectFieldProps = {
|
||||
@@ -482,6 +504,16 @@ function SettingsFields({
|
||||
className="cursor-pointer"
|
||||
/>
|
||||
</div>
|
||||
<SelectField
|
||||
label="Cleanup behavior"
|
||||
description={
|
||||
CLEANUP_POLICY_OPTIONS.find((p) => p.id === form.cleanupPolicy)?.description ?? ""
|
||||
}
|
||||
value={form.cleanupPolicy}
|
||||
onChange={(v) => setForm((prev) => ({ ...prev, cleanupPolicy: v as CleanupPolicy }))}
|
||||
placeholder="Auto"
|
||||
items={CLEANUP_POLICY_OPTIONS.map((p) => ({ id: p.id, label: p.label }))}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -539,6 +571,7 @@ export function ReviewWatchDialog({
|
||||
custom_query: form.customQuery,
|
||||
enabled: form.enabled,
|
||||
poll_interval_seconds: form.pollInterval,
|
||||
cleanup_policy: form.cleanupPolicy,
|
||||
};
|
||||
if (watch) {
|
||||
await onUpdate(watch.id, payload);
|
||||
|
||||
Reference in New Issue
Block a user