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(integrations): preserve legacy imports and paged rule drafts

Tin1dd0f1777a71
diff --git a/apps/api/src/github-integration/controllers/import-issues.ts b/apps/api/src/github-integration/controllers/import-issues.ts
index 32973a0b0..d8e418760 100644
--- a/apps/api/src/github-integration/controllers/import-issues.ts
+++ b/apps/api/src/github-integration/controllers/import-issues.ts
@@ -1,6 +1,7 @@
import { acceptsIssue, readSyncRules } from "../../plugins/sync/rules";
import { canSyncTask } from "../../plugins/sync/eligibility";
import { importIssueLabels } from "../../plugins/sync/issue-labels";
+import { parseLinkMetadata } from "../../plugins/github/utils/parse-link-metadata";
import { createId } from "@paralleldrive/cuid2";
import { and, asc, eq, isNull } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
@@ -399,7 +400,11 @@ async function applyPage(
)
.for("no key update");
const [linked] = await tx
- .select({ id: externalLinkTable.id })
+ .select({
+ id: externalLinkTable.id,
+ metadata: externalLinkTable.metadata,
+ createdAt: externalLinkTable.createdAt,
+ })
.from(externalLinkTable)
.where(
and(
@@ -410,6 +415,18 @@ async function applyPage(
),
)
.for("update");
+ if (linked && current.isNewTask === undefined) {
+ const metadata = parseLinkMetadata<{ createdFrom?: string }>(
+ linked.metadata,
+ {
+ externalLinkId: linked.id,
+ source: "github_import_continuation",
+ },
+ );
+ current.isNewTask =
+ metadata.createdFrom === "github-import" &&
+ linked.createdAt.getTime() >= Date.parse(state.startedAt);
+ }
const eligible =
task && linked && (await canSyncTask(current.taskId, integrationId, tx));
// A newly admitted issue's initial history belongs to the explicit import,
diff --git a/apps/web/src/components/project/sync-rules/sync-rules-section.tsx b/apps/web/src/components/project/sync-rules/sync-rules-section.tsx
index 6869944b4..ffa8ef918 100644
--- a/apps/web/src/components/project/sync-rules/sync-rules-section.tsx
+++ b/apps/web/src/components/project/sync-rules/sync-rules-section.tsx
@@ -162,7 +162,7 @@ export function SyncRulesSection({ projectId, provider }: SyncParams) {
<Button
size="xs"
variant="outline"
- disabled={!pages.length}
+ disabled={!pages.length || query.isPlaceholderData}
onClick={() => setPages((current) => current.slice(0, -1))}
>
{t("settings:syncRules.previous")}
@@ -170,7 +170,7 @@ export function SyncRulesSection({ projectId, provider }: SyncParams) {
<Button
size="xs"
variant="outline"
- disabled={!data.pausedNextCursor}
+ disabled={!data.pausedNextCursor || query.isPlaceholderData}
onClick={() => {
if (data.pausedNextCursor)
setPages((current) => [...current, data.pausedNextCursor!]);
diff --git a/apps/web/src/components/project/sync-rules/sync-rules.test.tsx b/apps/web/src/components/project/sync-rules/sync-rules.test.tsx
index 91d237d05..6ebeeac53 100644
--- a/apps/web/src/components/project/sync-rules/sync-rules.test.tsx
+++ b/apps/web/src/components/project/sync-rules/sync-rules.test.tsx
@@ -107,6 +107,119 @@ function mount(node = <SyncRulesSection {...param} />) {
}
describe("advanced sync settings", () => {
+ it("keeps a dirty rule draft while loading another paused-task page", async () => {
+ useUserPreferencesStore.setState({ advancedSettings: true });
+ const initial = {
+ ...saved,
+ labels: [
+ ...saved.labels,
+ { id: "label-2", name: "later", color: "#654321" },
+ ],
+ paused: 30,
+ pausedNextCursor: "next-page",
+ };
+ let release!: () => void;
+ const gate = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ mocks.get.mockImplementation(async (_params, after?: string) => {
+ if (after) {
+ await gate;
+ return { ...initial, pausedNextCursor: null };
+ }
+ return initial;
+ });
+ mocks.save.mockResolvedValue(initial);
+ mount();
+ fireEvent.click(await screen.findByRole("checkbox", { name: "later" }));
+ await waitFor(() =>
+ expect(
+ screen.getByRole("button", { name: "settings:syncRules.apply" }),
+ ).toBeEnabled(),
+ );
+ fireEvent.click(
+ screen.getByRole("button", { name: "settings:syncRules.next" }),
+ );
+ try {
+ await waitFor(() =>
+ expect(mocks.get).toHaveBeenCalledWith(param, "next-page"),
+ );
+ expect(screen.getByRole("checkbox", { name: "later" })).toBeChecked();
+ expect(
+ screen.getByRole("button", { name: "settings:syncRules.next" }),
+ ).toBeDisabled();
+ } finally {
+ await act(async () => {
+ release();
+ });
+ }
+ await waitFor(() =>
+ expect(
+ screen.getByRole("button", { name: "settings:syncRules.next" }),
+ ).toBeDisabled(),
+ );
+ expect(screen.getByRole("checkbox", { name: "later" })).toBeChecked();
+ fireEvent.click(
+ screen.getByRole("button", { name: "settings:syncRules.apply" }),
+ );
+ await waitFor(() =>
+ expect(mocks.save).toHaveBeenCalledWith(
+ param,
+ {
+ ...saved.rules,
+ outgoing: {
+ mode: "labels",
+ match: "any",
+ labels: ["label-1", "label-2"],
+ },
+ },
+ "b".repeat(64),
+ ),
+ );
+ });
+
+ it.each([
+ { projectId: "another-project", provider: "gitea" as const },
+ { projectId: param.projectId, provider: "github" as const },
+ ])(
+ "does not show a previous integration's data while changing scope to %j",
+ async (nextParam) => {
+ useUserPreferencesStore.setState({ advancedSettings: true });
+ let release!: () => void;
+ const gate = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ mocks.get.mockImplementation(async (scope) => {
+ if (
+ scope.projectId === nextParam.projectId &&
+ scope.provider === nextParam.provider
+ )
+ await gate;
+ return saved;
+ });
+ const { client, rerender } = mount();
+ await screen.findByRole("checkbox", { name: "sync" });
+ rerender(
+ <QueryClientProvider client={client}>
+ <SyncRulesSection {...nextParam} />
+ </QueryClientProvider>,
+ );
+ try {
+ await waitFor(() =>
+ expect(mocks.get).toHaveBeenCalledWith(nextParam, undefined),
+ );
+ expect(screen.queryByRole("checkbox", { name: "sync" })).toBeNull();
+ expect(screen.getByRole("status")).toHaveTextContent(
+ "settings:syncRules.loading",
+ );
+ } finally {
+ await act(async () => {
+ release();
+ });
+ }
+ },
+ );
+
it.each(["apply", "reload"])(
"keeps a dirty draft after shared rules change until %s",
async (choice) => {
diff --git a/apps/web/src/hooks/queries/integration-sync/use-sync-rules.ts b/apps/web/src/hooks/queries/integration-sync/use-sync-rules.ts
index b9a38f23b..c0068dc03 100644
--- a/apps/web/src/hooks/queries/integration-sync/use-sync-rules.ts
+++ b/apps/web/src/hooks/queries/integration-sync/use-sync-rules.ts
@@ -11,5 +11,10 @@ export function useSyncRules(param: SyncParams, after?: string) {
after ?? "",
],
queryFn: () => getSyncRules(param, after),
+ placeholderData: (previousData, previousQuery) =>
+ previousQuery?.queryKey[1] === param.projectId &&
+ previousQuery.queryKey[2] === param.provider
+ ? previousData
+ : undefined,
});
}
diff --git a/tests/api-integration/github-import-bounds.test.ts b/tests/api-integration/github-import-bounds.test.ts
index 6635df1b6..e3a4fe298 100644
--- a/tests/api-integration/github-import-bounds.test.ts
+++ b/tests/api-integration/github-import-bounds.test.ts
@@ -300,88 +300,147 @@ describe("bounded resumable GitHub import", () => {
});
});
- it("finishes a new paused issue's comment history across import requests", async () => {
- const { member, project, integration, config } = await setup();
- const [root] = await db
- .insert(schema.labelTable)
- .values({
- workspaceId: member.workspace.id,
- name: "export",
- color: "red",
- })
- .returning();
- await db
- .update(schema.integrationTable)
- .set({
- config: JSON.stringify({
- ...config,
- syncRules: {
- outgoing: { mode: "labels", match: "any", labels: [root.id] },
- incoming: { mode: "all" },
- },
- }),
- })
- .where(eq(schema.integrationTable.id, integration.id));
- mocks.verify.mockResolvedValue({
- graphql: mocks.graphql,
- rest: { issues: { get: async () => ({ data: { labels: [] } }) } },
- });
- const comments = Array.from({ length: 85 }, (_, index) => comment(index));
- mocks.graphql.mockImplementation(
- async (query: string, vars: { cursor: string | null }) => {
- if (query.includes("query ImportIssues("))
- return issuePage([
- issue(1, {
- comments: connection(
- comments.slice(0, 20),
- true,
- "20",
- comments.length,
- ),
- }),
- ]);
- if (query.includes("query ImportIssueComments(")) {
- const start = Number(vars.cursor);
- const end = Math.min(start + 20, comments.length);
- return {
- repository: {
- databaseId: 2,
- issue: {
+ it.each(["new", "legacy-new", "legacy-existing"])(
+ "preserves %s issue history across import requests",
+ async (history) => {
+ const { member, project, integration, config } = await setup();
+ const existing = history === "legacy-existing";
+ if (existing) {
+ const [task] = await db
+ .insert(schema.taskTable)
+ .values({
+ projectId: project.id,
+ number: 1,
+ title: "Existing imported task",
+ status: "to-do",
+ })
+ .returning();
+ await db.insert(schema.externalLinkTable).values({
+ taskId: task.id,
+ integrationId: integration.id,
+ resourceType: "issue",
+ externalId: "1",
+ url: "https://github.com/example/repo/issues/1",
+ createdAt: new Date(old),
+ metadata: JSON.stringify({ createdFrom: "github-import" }),
+ });
+ }
+ const [root] = await db
+ .insert(schema.labelTable)
+ .values({
+ workspaceId: member.workspace.id,
+ name: "export",
+ color: "red",
+ })
+ .returning();
+ await db
+ .update(schema.integrationTable)
+ .set({
+ config: JSON.stringify({
+ ...config,
+ syncRules: {
+ outgoing: existing
+ ? { mode: "all" }
+ : { mode: "labels", match: "any", labels: [root.id] },
+ incoming: { mode: "all" },
+ },
+ }),
+ })
+ .where(eq(schema.integrationTable.id, integration.id));
+ mocks.verify.mockResolvedValue({
+ graphql: mocks.graphql,
+ rest: { issues: { get: async () => ({ data: { labels: [] } }) } },
+ });
+ const comments = Array.from({ length: 85 }, (_, index) => comment(index));
+ mocks.graphql.mockImplementation(
+ async (query: string, vars: { cursor: string | null }) => {
+ if (query.includes("query ImportIssues("))
+ return issuePage([
+ issue(1, {
comments: connection(
- comments.slice(start, end),
- end < comments.length,
- String(end),
+ comments.slice(0, 20),
+ true,
+ "20",
comments.length,
),
+ }),
+ ]);
+ if (query.includes("query ImportIssueComments(")) {
+ const start = Number(vars.cursor);
+ const end = Math.min(start + 20, comments.length);
+ return {
+ repository: {
+ databaseId: 2,
+ issue: {
+ comments: connection(
+ comments.slice(start, end),
+ end < comments.length,
+ String(end),
+ comments.length,
+ ),
+ },
},
- },
- };
- }
- return emptyPulls();
- },
- );
- let result = await importIssues(project.id);
- expect(result).toMatchObject({ pending: true, imported: 1, skipped: 0 });
- expect(
- JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!),
- ).toMatchObject({ syncFilterPaused: true });
- result = await importIssues(project.id, result.runId);
- expect(result).toMatchObject({ pending: false, imported: 1, skipped: 0 });
- expect(await db.query.activityTable.findMany()).toHaveLength(
- comments.length,
- );
- expect(
- JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!),
- ).toMatchObject({ syncFilterPaused: true });
- expect(await importIssues(project.id)).toMatchObject({
- pending: false,
- imported: 0,
- skipped: 1,
- });
- expect(await db.query.activityTable.findMany()).toHaveLength(
- comments.length,
- );
- });
+ };
+ }
+ return emptyPulls();
+ },
+ );
+ let result = await importIssues(project.id);
+ expect(result).toMatchObject({
+ pending: true,
+ imported: existing ? 0 : 1,
+ updated: existing ? 1 : 0,
+ skipped: 0,
+ });
+ expect(
+ JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!)
+ .syncFilterPaused === true,
+ ).toBe(!existing);
+ const initialComments = (await db.query.activityTable.findMany()).length;
+ if (history !== "new") {
+ const persisted = (await saved())!;
+ delete persisted.state.currentIssue!.isNewTask;
+ await db
+ .update(schema.githubImportTable)
+ .set({ state: persisted.state })
+ .where(eq(schema.githubImportTable.integrationId, integration.id));
+ }
+ if (existing)
+ await db
+ .update(schema.integrationTable)
+ .set({
+ config: JSON.stringify({
+ ...config,
+ syncRules: {
+ outgoing: { mode: "labels", match: "any", labels: [root.id] },
+ incoming: { mode: "all" },
+ },
+ }),
+ })
+ .where(eq(schema.integrationTable.id, integration.id));
+ result = await importIssues(project.id, result.runId);
+ expect(result).toMatchObject({
+ pending: false,
+ imported: existing ? 0 : 1,
+ updated: existing ? 1 : 0,
+ skipped: existing ? 1 : 0,
+ });
+ expect(await db.query.activityTable.findMany()).toHaveLength(
+ existing ? initialComments : comments.length,
+ );
+ expect(
+ JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!),
+ ).toMatchObject({ syncFilterPaused: true });
+ expect(await importIssues(project.id)).toMatchObject({
+ pending: false,
+ imported: 0,
+ skipped: 1,
+ });
+ expect(await db.query.activityTable.findMany()).toHaveLength(
+ existing ? initialComments : comments.length,
+ );
+ },
+ );
it("rejects an incoming rule change while fetching a page before creating tasks or consuming its cursor", async () => {
const { integration, config, request } = await setup();
const started = deferred();