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.

feat: enhance task status management with new utility functions and improved status update logic

Andrej48a90bf3465d
diff --git a/apps/api/src/plugins/github/services/task-service.ts b/apps/api/src/plugins/github/services/task-service.ts
index 73723a725..996ed9b54 100644
--- a/apps/api/src/plugins/github/services/task-service.ts
+++ b/apps/api/src/plugins/github/services/task-service.ts
@@ -6,6 +6,8 @@ import {
taskTable,
} from "../../../database/schema";
+const NON_COLUMN_STATUSES = new Set(["planned", "archived"]);
+
export async function findTaskByNumber(projectId: string, taskNumber: number) {
return db.query.taskTable.findFirst({
where: and(
@@ -26,15 +28,26 @@ export async function updateTaskStatus(taskId: string, newStatus: string) {
where: eq(taskTable.id, taskId),
});
+ if (!task) {
+ return;
+ }
+
let columnId: string | null = null;
- if (task) {
- const column = await db.query.columnTable.findFirst({
- where: and(
- eq(columnTable.projectId, task.projectId),
- eq(columnTable.slug, newStatus),
- ),
- });
- columnId = column?.id ?? null;
+
+ const column = await db.query.columnTable.findFirst({
+ where: and(
+ eq(columnTable.projectId, task.projectId),
+ eq(columnTable.slug, newStatus),
+ ),
+ });
+
+ if (column) {
+ columnId = column.id;
+ } else if (!NON_COLUMN_STATUSES.has(newStatus)) {
+ console.warn(
+ `[GitHub] Skipping status update for task ${taskId}: column "${newStatus}" not found in project ${task.projectId}`,
+ );
+ return;
}
await db
@@ -43,6 +56,38 @@ export async function updateTaskStatus(taskId: string, newStatus: string) {
.where(eq(taskTable.id, taskId));
}
+export async function isTaskInFinalState(task: {
+ projectId: string;
+ status: string;
+ columnId: string | null;
+}): Promise<boolean> {
+ if (task.columnId) {
+ const columnById = await db.query.columnTable.findFirst({
+ where: and(
+ eq(columnTable.id, task.columnId),
+ eq(columnTable.projectId, task.projectId),
+ ),
+ });
+
+ if (columnById) {
+ return columnById.isFinal;
+ }
+ }
+
+ const columnByStatus = await db.query.columnTable.findFirst({
+ where: and(
+ eq(columnTable.projectId, task.projectId),
+ eq(columnTable.slug, task.status),
+ ),
+ });
+
+ if (columnByStatus) {
+ return columnByStatus.isFinal;
+ }
+
+ return task.status === "done";
+}
+
export async function getIntegrationWithProject(integrationId: string) {
return db.query.integrationTable.findFirst({
where: eq(integrationTable.id, integrationId),
diff --git a/apps/api/src/plugins/github/utils/extract-priority.ts b/apps/api/src/plugins/github/utils/extract-priority.ts
index 576e1a59a..ba6ef3753 100644
--- a/apps/api/src/plugins/github/utils/extract-priority.ts
+++ b/apps/api/src/plugins/github/utils/extract-priority.ts
@@ -1,17 +1,10 @@
type GitHubLabel = string | { name?: string };
const VALID_PRIORITIES = ["low", "medium", "high", "urgent"] as const;
-const VALID_STATUSES = [
- "to-do",
- "in-progress",
- "in-review",
- "done",
- "planned",
- "archived",
-] as const;
+const STATUS_SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
export type ValidPriority = (typeof VALID_PRIORITIES)[number];
-export type ValidStatus = (typeof VALID_STATUSES)[number];
+export type ValidStatus = string;
/**
* Extract priority from GitHub issue labels.
@@ -51,8 +44,6 @@ export function extractIssueStatus(
const firstStatusLabel = statusLabels[0];
if (!firstStatusLabel) return null;
- const status = firstStatusLabel.replace("status:", "");
- return VALID_STATUSES.includes(status as ValidStatus)
- ? (status as ValidStatus)
- : null;
+ const status = firstStatusLabel.replace("status:", "").trim().toLowerCase();
+ return STATUS_SLUG_PATTERN.test(status) ? status : null;
}
diff --git a/apps/api/src/plugins/github/webhooks/issue-labeled.ts b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
index 18b1e5f10..f8a0b8291 100644
--- a/apps/api/src/plugins/github/webhooks/issue-labeled.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
@@ -2,7 +2,10 @@ import { eq } from "drizzle-orm";
import db from "../../../database";
import { labelTable, taskTable } from "../../../database/schema";
import { findExternalLink } from "../services/link-manager";
-import { findAllIntegrationsByRepo } from "../services/task-service";
+import {
+ findAllIntegrationsByRepo,
+ updateTaskStatus,
+} from "../services/task-service";
import {
extractIssuePriority,
extractIssueStatus,
@@ -46,23 +49,17 @@ export async function handleIssueLabeled(payload: IssueLabeledPayload) {
const priority = extractIssuePriority(issue.labels);
const status = extractIssueStatus(issue.labels);
- const updateData: Record<string, unknown> = {};
-
if (priority) {
- updateData.priority = priority;
- }
-
- if (status) {
- updateData.status = status;
- }
-
- if (Object.keys(updateData).length > 0) {
await db
.update(taskTable)
- .set(updateData)
+ .set({ priority })
.where(eq(taskTable.id, existingLink.taskId));
}
+ if (status) {
+ await updateTaskStatus(existingLink.taskId, status);
+ }
+
if (!addedLabel) {
return;
}
diff --git a/apps/api/src/plugins/github/webhooks/pull-request-opened.ts b/apps/api/src/plugins/github/webhooks/pull-request-opened.ts
index 304d8d137..70fd53ad1 100644
--- a/apps/api/src/plugins/github/webhooks/pull-request-opened.ts
+++ b/apps/api/src/plugins/github/webhooks/pull-request-opened.ts
@@ -3,6 +3,7 @@ import { createExternalLink, findExternalLink } from "../services/link-manager";
import {
findAllIntegrationsByRepo,
findTaskByNumber,
+ isTaskInFinalState,
updateTaskStatus,
} from "../services/task-service";
import { extractTaskNumber } from "../utils/branch-matcher";
@@ -96,7 +97,9 @@ export async function handlePullRequestOpened(payload: PROpenedPayload) {
config.statusTransitions?.onPROpen || "in-review",
);
- if (task.status !== targetStatus && task.status !== "done") {
+ const isTaskFinal = await isTaskInFinalState(task);
+
+ if (task.status !== targetStatus && !isTaskFinal) {
await updateTaskStatus(task.id, targetStatus);
}
diff --git a/apps/api/src/plugins/github/webhooks/push.ts b/apps/api/src/plugins/github/webhooks/push.ts
index 1a84e6444..bb0053205 100644
--- a/apps/api/src/plugins/github/webhooks/push.ts
+++ b/apps/api/src/plugins/github/webhooks/push.ts
@@ -3,6 +3,7 @@ import { createOrUpdateExternalLink } from "../services/link-manager";
import {
findAllIntegrationsByRepo,
findTaskByNumber,
+ isTaskInFinalState,
updateTaskStatus,
} from "../services/task-service";
import { extractTaskNumberFromBranch } from "../utils/branch-matcher";
@@ -127,7 +128,9 @@ export async function handlePush(payload: PushPayload) {
`[Push] Target status: ${targetStatus}, current: ${task.status}`,
);
- if (task.status !== targetStatus && task.status !== "done") {
+ const isTaskFinal = await isTaskInFinalState(task);
+
+ if (task.status !== targetStatus && !isTaskFinal) {
console.log(
`[Push] Updating task ${task.id} status from ${task.status} to ${targetStatus}`,
);