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): commit inbound pauses and retry failed removals

Tinc4847f1238bd
diff --git a/apps/api/src/plugins/gitea/events/task-created.ts b/apps/api/src/plugins/gitea/events/task-created.ts
index 741d1359f..3c1787861 100644
--- a/apps/api/src/plugins/gitea/events/task-created.ts
+++ b/apps/api/src/plugins/gitea/events/task-created.ts
@@ -121,7 +121,7 @@ async function createTaskIssue(
"initialization",
async ({ add, remove }, write) => {
for (const name of remove)
- await removeLabelGitea(config, issueNumber, name, write);
+ await removeLabelGitea(config, issueNumber, name, write, true);
await addLabelsToIssueGitea(config, issueNumber, add, true, write);
},
),
diff --git a/apps/api/src/plugins/gitea/events/task-priority-changed.ts b/apps/api/src/plugins/gitea/events/task-priority-changed.ts
index 8445b37fc..d18772154 100644
--- a/apps/api/src/plugins/gitea/events/task-priority-changed.ts
+++ b/apps/api/src/plugins/gitea/events/task-priority-changed.ts
@@ -45,7 +45,7 @@ export async function handleTaskPriorityChanged(
"priority",
async ({ add, remove }, write) => {
for (const name of remove)
- await removeLabelGitea(config, issueNumber, name, write);
+ await removeLabelGitea(config, issueNumber, name, write, true);
if (add.length)
await addLabelsToIssueGitea(config, issueNumber, add, true, write);
},
diff --git a/apps/api/src/plugins/gitea/events/task-status-changed.ts b/apps/api/src/plugins/gitea/events/task-status-changed.ts
index 9d9f3293e..b3f2a2cdd 100644
--- a/apps/api/src/plugins/gitea/events/task-status-changed.ts
+++ b/apps/api/src/plugins/gitea/events/task-status-changed.ts
@@ -50,7 +50,7 @@ export async function handleTaskStatusChanged(
"status",
async ({ add, remove }, write) => {
for (const name of remove)
- await removeLabelGitea(config, issueNumber, name, write);
+ await removeLabelGitea(config, issueNumber, name, write, true);
if (add.length)
await addLabelsToIssueGitea(config, issueNumber, add, true, write);
},
diff --git a/apps/api/src/plugins/gitea/utils/labels.ts b/apps/api/src/plugins/gitea/utils/labels.ts
index 8d8ba2dec..75964f223 100644
--- a/apps/api/src/plugins/gitea/utils/labels.ts
+++ b/apps/api/src/plugins/gitea/utils/labels.ts
@@ -113,6 +113,7 @@ export async function removeLabelGitea(
issueIndex: number,
labelName: string,
write: IssueWrite = (send) => send(),
+ requireSuccess = false,
) {
const client = createGiteaClient(config);
let labels: GiteaLabel[];
@@ -122,6 +123,7 @@ export async function removeLabelGitea(
config.repositoryName,
);
} catch (error) {
+ if (requireSuccess) throw error;
console.error("Failed to list Gitea labels for removal:", {
repositoryOwner: config.repositoryOwner,
repositoryName: config.repositoryName,
@@ -145,6 +147,7 @@ export async function removeLabelGitea(
),
);
} catch (error) {
+ if (requireSuccess) throw error;
console.error("Failed to remove label from Gitea issue:", {
repositoryOwner: config.repositoryOwner,
repositoryName: config.repositoryName,
diff --git a/apps/api/src/plugins/github/services/with-integration-link.ts b/apps/api/src/plugins/github/services/with-integration-link.ts
index fc4f9307a..5f085a8b7 100644
--- a/apps/api/src/plugins/github/services/with-integration-link.ts
+++ b/apps/api/src/plugins/github/services/with-integration-link.ts
@@ -45,7 +45,17 @@ export function withIntegrationLink<T>(
)
return;
try {
- return await apply(database, afterCommit, lockedLink);
+ const result = await apply(database, afterCommit, lockedLink);
+ // Inbound label mutations can remove the predicate that admitted this
+ // callback. Commit its paused link with the mutation, before events run.
+ if (lockedLink.resourceType === "issue")
+ await canSyncTask(
+ link.taskId,
+ integration.id,
+ database,
+ expectedBinding?.config,
+ );
+ return result;
} catch (error) {
if (!(error instanceof PendingEcho)) throw error;
if (error.context && error.intentId && error.updatedAt) {
diff --git a/tests/api-integration/integration-sync-field-labels.test.ts b/tests/api-integration/integration-sync-field-labels.test.ts
index 7bdf42f5c..e6361e218 100644
--- a/tests/api-integration/integration-sync-field-labels.test.ts
+++ b/tests/api-integration/integration-sync-field-labels.test.ts
@@ -14,6 +14,8 @@ const mocks = vi.hoisted(() => ({
labels: new Set<string>(),
read: vi.fn(),
write: vi.fn(),
+ list: vi.fn(),
+ remove: vi.fn(),
}));
const names = [
"status:planned",
@@ -53,13 +55,17 @@ vi.mock("../../apps/api/src/plugins/github/utils/github-app", () => ({
vi.mock("../../apps/api/src/plugins/gitea/utils/gitea-api", () => ({
createGiteaClient: () => ({
getIssue: async () => mocks.read(),
- listLabels: async () => available(),
+ listLabels: async () => {
+ await mocks.list();
+ return available();
+ },
removeLabelFromIssue: async (
_owner: string,
_repo: string,
_number: number,
id: number,
) => {
+ await mocks.remove();
mocks.labels.delete(names[id - 1]);
},
addLabelsToIssue: async (
@@ -95,6 +101,8 @@ beforeEach(async () => {
await resetTestDatabase();
mocks.write.mockReset().mockResolvedValue(undefined);
mocks.read.mockReset().mockImplementation(() => issue());
+ mocks.list.mockReset().mockResolvedValue(undefined);
+ mocks.remove.mockReset().mockResolvedValue(undefined);
mocks.labels = new Set(["status:planned", "priority:low", "keep"]);
});
@@ -109,6 +117,9 @@ it.each(
"read-error",
"initialization",
"initialization-waits",
+ ...(provider === "gitea"
+ ? ["initialization-list-error", "initialization-remove-error"]
+ : []),
]
: [false, true, "initialization", "initialization-waits"]
).map((overlap) => ({ provider, field, overlap })),
@@ -123,8 +134,12 @@ it.each(
});
const clearing = overlap === "clear";
const initializing =
- overlap === "initialization" || overlap === "initialization-waits";
- const overlapping = overlap === true || initializing;
+ typeof overlap === "string" && overlap.startsWith("initialization");
+ const initializationFailure =
+ overlap === "initialization-list-error" ||
+ overlap === "initialization-remove-error";
+ const overlapping =
+ overlap === true || (initializing && !initializationFailure);
const first =
field === "status" ? "in-progress" : clearing ? "no-priority" : "high";
const last = field === "status" ? "in-review" : "urgent";
@@ -228,6 +243,42 @@ it.each(
},
context,
);
+ if (initializationFailure) {
+ const log = vi.spyOn(console, "error").mockImplementation(() => {});
+ (overlap === "initialization-list-error"
+ ? mocks.list
+ : mocks.remove
+ ).mockRejectedValueOnce(new Error("Temporary provider failure"));
+ try {
+ await initialize();
+ const failed = await db.query.externalLinkTable.findFirst({
+ where: eq(schema.externalLinkTable.id, link.id),
+ });
+ expect(JSON.parse(failed!.metadata!)).toMatchObject({
+ syncInitializationPending: true,
+ });
+ expect(JSON.parse(failed!.metadata!).syncInitializedLabels).not.toBe(
+ true,
+ );
+ expect(mocks.write).not.toHaveBeenCalled();
+ await initialize();
+ const retried = await db.query.externalLinkTable.findFirst({
+ where: eq(schema.externalLinkTable.id, link.id),
+ });
+ expect(JSON.parse(retried!.metadata!)).toMatchObject({
+ syncInitializedLabels: true,
+ syncInitializationPending: false,
+ });
+ expect([...mocks.labels].sort()).toEqual([
+ "keep",
+ "priority:high",
+ "status:in-progress",
+ ]);
+ } finally {
+ log.mockRestore();
+ }
+ return;
+ }
let release!: () => void;
let started!: () => void;
const gate = new Promise<void>((resolve) => {
diff --git a/tests/api-integration/integration-sync-providers.test.ts b/tests/api-integration/integration-sync-providers.test.ts
index d381e04d5..8f8794eea 100644
--- a/tests/api-integration/integration-sync-providers.test.ts
+++ b/tests/api-integration/integration-sync-providers.test.ts
@@ -226,6 +226,74 @@ describe.each(["github", "gitea", "gitlab"] as const)(
gitea: mocks.giteaCreate,
gitlab: mocks.gitlabCreate,
}[type];
+ it("commits an inbound scope-label removal with a paused link before publishing", async () => {
+ const f = await setup(type);
+ await f.assign();
+ await reconcileTaskSync(f.project.id, f.task.id);
+ const link = (await db.query.externalLinkTable.findMany())[0]!;
+ const publish = vi
+ .spyOn(events, "publishEvent")
+ .mockResolvedValue(undefined);
+ try {
+ if (type === "github")
+ await handleIssueLabeled({
+ action: "unlabeled",
+ installation: { id: 2 },
+ repository: { id: 1, owner: { login: "team" }, name: "repo" },
+ issue: { number: 12, labels: [] },
+ label: { name: "export", color: "123456" },
+ });
+ else if (type === "gitea")
+ await handleGiteaIssueLabeled(
+ {
+ action: "label_updated",
+ repository: {
+ owner: { login: "team" },
+ name: "repo",
+ html_url: "https://git.example/team/repo",
+ },
+ issue: { number: 12, labels: [] },
+ },
+ f.integration.id,
+ );
+ else
+ await handleGitlabIssueUpdated(
+ {
+ object_attributes: {
+ iid: 12,
+ title: "Export task",
+ description: "",
+ state: "opened",
+ url: "https://gitlab.example/team/repo/-/issues/12",
+ },
+ changes: {
+ labels: { previous: [{ title: "export" }], current: [] },
+ },
+ project: {
+ name: "repo",
+ path_with_namespace: "team/repo",
+ web_url: "https://gitlab.example/team/repo",
+ },
+ },
+ f.integration.id,
+ );
+ expect(
+ await db.query.labelTable.findMany({
+ where: eq(schema.labelTable.taskId, f.task.id),
+ }),
+ ).toEqual([]);
+ const paused = await db.query.externalLinkTable.findFirst({
+ where: eq(schema.externalLinkTable.id, link.id),
+ });
+ expect(JSON.parse(paused!.metadata!)).toMatchObject({
+ syncFilterPaused: true,
+ });
+ await f.assign();
+ expect(await canSyncTask(f.task.id, f.integration.id)).toBe(false);
+ } finally {
+ publish.mockRestore();
+ }
+ });
it("exports equivalent formatted configurations while rejecting real changes", async () => {
const f = await setup(type);
await f.assign();