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(api) 1079 allow disabling GitHub task link comment (#1085)

Tin Sever37c85d0a6065
diff --git a/apps/api/src/github-integration/controllers/get-github-integration.ts b/apps/api/src/github-integration/controllers/get-github-integration.ts
index 404fd27d0..5cc0c5108 100644
--- a/apps/api/src/github-integration/controllers/get-github-integration.ts
+++ b/apps/api/src/github-integration/controllers/get-github-integration.ts
@@ -27,6 +27,7 @@ async function getGithubIntegration(projectId: string) {
repositoryName: config.repositoryName,
installationId: config.installationId,
branchPattern: config.branchPattern || defaultGitHubConfig.branchPattern,
+ commentTaskLinkOnGitHubIssue: config.commentTaskLinkOnGitHubIssue !== false,
isActive: integration.isActive,
createdAt: integration.createdAt,
updatedAt: integration.updatedAt,
diff --git a/apps/api/src/github-integration/index.ts b/apps/api/src/github-integration/index.ts
index 481a81ebf..f1e775292 100644
--- a/apps/api/src/github-integration/index.ts
+++ b/apps/api/src/github-integration/index.ts
@@ -6,6 +6,10 @@ import { describeRoute, resolver, validator } from "hono-openapi";
import * as v from "valibot";
import db from "../database";
import { integrationTable, projectTable } from "../database/schema";
+import {
+ type GitHubConfig,
+ validateGitHubConfig,
+} from "../plugins/github/config";
import { handleGitHubWebhook } from "../plugins/github/webhook-handler";
import { githubIntegrationSchema } from "../schemas";
import { validateWorkspaceAccess } from "../utils/validate-workspace-access";
@@ -218,24 +222,54 @@ const githubIntegration = new Hono<{
"json",
v.object({
isActive: v.optional(v.boolean()),
+ commentTaskLinkOnGitHubIssue: v.optional(v.boolean()),
}),
),
workspaceAccess.fromProject("projectId"),
async (c) => {
const { projectId } = c.req.valid("param");
- const { isActive } = c.req.valid("json");
+ const body = c.req.valid("json");
- const existingIntegration = await getGithubIntegration(projectId);
+ const row = await db.query.integrationTable.findFirst({
+ where: and(
+ eq(integrationTable.projectId, projectId),
+ eq(integrationTable.type, "github"),
+ ),
+ });
- if (!existingIntegration) {
+ if (!row) {
return c.json({ error: "Integration not found" }, 404);
}
- const [updatedIntegration] = await db
+ let config: GitHubConfig;
+ try {
+ config = JSON.parse(row.config) as GitHubConfig;
+ } catch {
+ throw new HTTPException(500, { message: "Invalid integration config" });
+ }
+
+ if (body.commentTaskLinkOnGitHubIssue !== undefined) {
+ config = {
+ ...config,
+ commentTaskLinkOnGitHubIssue: body.commentTaskLinkOnGitHubIssue,
+ };
+ }
+
+ const validation = await validateGitHubConfig(config);
+ if (!validation.valid) {
+ throw new HTTPException(400, {
+ message: validation.errors?.join(", ") ?? "Invalid config",
+ });
+ }
+
+ await db
.update(integrationTable)
.set({
+ config: JSON.stringify(config),
isActive:
- isActive !== undefined ? isActive : existingIntegration.isActive,
+ body.isActive !== undefined
+ ? body.isActive
+ : (row.isActive ?? true),
updatedAt: new Date(),
})
.where(
@@ -243,10 +277,10 @@ const githubIntegration = new Hono<{
eq(integrationTable.projectId, projectId),
eq(integrationTable.type, "github"),
),
- )
- .returning();
+ );
- return c.json(updatedIntegration, 200);
+ const updated = await getGithubIntegration(projectId);
+ return c.json(updated, 200);
},
)
.delete(
diff --git a/apps/api/src/plugins/github/config.ts b/apps/api/src/plugins/github/config.ts
index c14cbc5e8..bbf32756b 100644
--- a/apps/api/src/plugins/github/config.ts
+++ b/apps/api/src/plugins/github/config.ts
@@ -19,6 +19,7 @@ export const githubConfigSchema = v.object({
installationId: v.nullable(v.number()),
branchPattern: v.optional(v.string()),
customBranchRegex: v.optional(v.string()),
+ commentTaskLinkOnGitHubIssue: v.optional(v.boolean()),
statusTransitions: v.optional(
v.object({
onBranchPush: v.optional(v.string()),
@@ -52,6 +53,7 @@ export async function validateGitHubConfig(
export const defaultGitHubConfig: Partial<GitHubConfig> = {
branchPattern: "{slug}-{number}",
+ commentTaskLinkOnGitHubIssue: true,
statusTransitions: {
onBranchPush: "in-progress",
onPROpen: "in-review",
diff --git a/apps/api/src/plugins/github/webhooks/issue-opened.ts b/apps/api/src/plugins/github/webhooks/issue-opened.ts
index ef59db40f..630935660 100644
--- a/apps/api/src/plugins/github/webhooks/issue-opened.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-opened.ts
@@ -172,12 +172,14 @@ export async function handleIssueOpened(payload: IssueOpenedPayload) {
);
}
- await octokit.rest.issues.createComment({
- owner: repository.owner.login,
- repo: repository.name,
- issue_number: issue.number,
- body: `[${taskIdentifier}](${taskUrl})`,
- });
+ if (config.commentTaskLinkOnGitHubIssue !== false) {
+ await octokit.rest.issues.createComment({
+ owner: repository.owner.login,
+ repo: repository.name,
+ issue_number: issue.number,
+ body: `[${taskIdentifier}](${taskUrl})`,
+ });
+ }
} catch (error) {
console.error("Failed to process GitHub issue:", error);
}
diff --git a/apps/api/src/schemas.ts b/apps/api/src/schemas.ts
index fbffa19d8..85663c638 100644
--- a/apps/api/src/schemas.ts
+++ b/apps/api/src/schemas.ts
@@ -101,6 +101,8 @@ export const githubIntegrationSchema = v.object({
repositoryOwner: v.string(),
repositoryName: v.string(),
installationId: v.nullable(v.number()),
+ branchPattern: v.optional(v.string()),
+ commentTaskLinkOnGitHubIssue: v.optional(v.boolean()),
isActive: v.nullable(v.boolean()),
createdAt: v.date(),
updatedAt: v.date(),
diff --git a/apps/web/src/components/project/github-integration-settings.tsx b/apps/web/src/components/project/github-integration-settings.tsx
index d07409ae8..c09225155 100644
--- a/apps/web/src/components/project/github-integration-settings.tsx
+++ b/apps/web/src/components/project/github-integration-settings.tsx
@@ -27,6 +27,7 @@ import {
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Separator } from "@/components/ui/separator";
+import { Switch } from "@/components/ui/switch";
import type { VerifyGithubInstallationResponse } from "@/fetchers/github-integration/verify-github-installation";
import {
useCreateGithubIntegration,
@@ -34,6 +35,7 @@ import {
useVerifyGithubInstallation,
} from "@/hooks/mutations/github-integration/use-create-github-integration";
import useImportGithubIssues from "@/hooks/mutations/github-integration/use-import-github-issues";
+import { useUpdateGithubIntegration } from "@/hooks/mutations/github-integration/use-update-github-integration";
import useGetGithubIntegration from "@/hooks/queries/github-integration/use-get-github-integration";
import { cn } from "@/lib/cn";
import { toast } from "@/lib/toast";
@@ -65,6 +67,8 @@ export function GitHubIntegrationSettings({
useVerifyGithubInstallation();
const { mutateAsync: importIssues, isPending: isImporting } =
useImportGithubIssues();
+ const { mutateAsync: updateGithubSettings, isPending: isUpdatingSettings } =
+ useUpdateGithubIntegration();
const [verificationResult, setVerificationResult] =
React.useState<VerifyGithubInstallationResponse | null>(null);
@@ -297,6 +301,42 @@ export function GitHubIntegrationSettings({
</a>
</div>
</div>
+
+ <Separator />
+ <div className="flex items-center justify-between gap-4">
+ <div className="min-w-0 flex-1 space-y-0.5">
+ <p className="text-sm font-medium">
+ Comment Kaneo link on new issues
+ </p>
+ <p className="text-xs text-muted-foreground">
+ When enabled, Kaneo posts a comment on each new GitHub issue
+ with a link to the task.
+ </p>
+ </div>
+ <Switch
+ checked={integration.commentTaskLinkOnGitHubIssue !== false}
+ onCheckedChange={async (checked) => {
+ try {
+ await updateGithubSettings({
+ projectId,
+ json: { commentTaskLinkOnGitHubIssue: checked },
+ });
+ toast.success(
+ checked
+ ? "Kaneo will comment with a task link on new issues"
+ : "Task link comments on new issues are turned off",
+ );
+ } catch (error) {
+ toast.error(
+ error instanceof Error
+ ? error.message
+ : "Failed to update GitHub integration",
+ );
+ }
+ }}
+ disabled={isUpdatingSettings}
+ />
+ </div>
</>
)}
diff --git a/apps/web/src/fetchers/github-integration/update-github-integration.ts b/apps/web/src/fetchers/github-integration/update-github-integration.ts
new file mode 100644
index 000000000..e34c12965
--- /dev/null
+++ b/apps/web/src/fetchers/github-integration/update-github-integration.ts
@@ -0,0 +1,27 @@
+import { client } from "@kaneo/libs";
+import type { InferRequestType } from "hono";
+
+export type UpdateGithubIntegrationRequest = InferRequestType<
+ (typeof client)["github-integration"]["project"][":projectId"]["$patch"]
+>["json"];
+
+async function updateGithubIntegration(
+ projectId: string,
+ json: UpdateGithubIntegrationRequest,
+) {
+ const response = await client["github-integration"].project[
+ ":projectId"
+ ].$patch({
+ param: { projectId },
+ json,
+ });
+
+ if (!response.ok) {
+ const error = await response.text();
+ throw new Error(error);
+ }
+
+ return response.json();
+}
+
+export default updateGithubIntegration;
diff --git a/apps/web/src/hooks/mutations/github-integration/use-update-github-integration.ts b/apps/web/src/hooks/mutations/github-integration/use-update-github-integration.ts
new file mode 100644
index 000000000..ac98a6ba0
--- /dev/null
+++ b/apps/web/src/hooks/mutations/github-integration/use-update-github-integration.ts
@@ -0,0 +1,23 @@
+import { useMutation, useQueryClient } from "@tanstack/react-query";
+import updateGithubIntegration, {
+ type UpdateGithubIntegrationRequest,
+} from "@/fetchers/github-integration/update-github-integration";
+
+export function useUpdateGithubIntegration() {
+ const queryClient = useQueryClient();
+
+ return useMutation({
+ mutationFn: ({
+ projectId,
+ json,
+ }: {
+ projectId: string;
+ json: UpdateGithubIntegrationRequest;
+ }) => updateGithubIntegration(projectId, json),
+ onSuccess: (_, { projectId }) => {
+ void queryClient.invalidateQueries({
+ queryKey: ["github-integration", projectId],
+ });
+ },
+ });
+}