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.

refactor: improve GitHub app initialization and error handling

Andrej Acevski42564977dc1e
diff --git a/.husky/commit-msg b/.husky/commit-msg
index 1f42e6b07..c48ae6930 100644
--- a/.husky/commit-msg
+++ b/.husky/commit-msg
@@ -1 +1 @@
-bun x commitlint --edit $1
+pnpm dlx commitlint --edit $1
diff --git a/apps/api/package.json b/apps/api/package.json
index 347bb82a3..f4f714f47 100644
--- a/apps/api/package.json
+++ b/apps/api/package.json
@@ -4,7 +4,8 @@
"main": "./src/index.ts",
"scripts": {
"dev": "tsx watch src/index.ts",
- "build": "esbuild src/index.ts --bundle --platform=node --outdir=dist --format=esm --packages=external --external:fs --external:path --external:crypto --external:os --external:util --external:stream --external:buffer --external:events --external:url --external:querystring --external:http --external:https --external:net --external:tls --external:zlib"
+ "build": "esbuild src/index.ts --bundle --platform=node --outdir=dist --format=esm --packages=external --external:fs --external:path --external:crypto --external:os --external:util --external:stream --external:buffer --external:events --external:url --external:querystring --external:http --external:https --external:net --external:tls --external:zlib",
+ "lint": "biome check --write ."
},
"dependencies": {
"@hono/node-server": "^1.14.4",
diff --git a/apps/api/src/github-integration/controllers/create-github-integration.ts b/apps/api/src/github-integration/controllers/create-github-integration.ts
index 9db03c53b..b78d42ad2 100644
--- a/apps/api/src/github-integration/controllers/create-github-integration.ts
+++ b/apps/api/src/github-integration/controllers/create-github-integration.ts
@@ -2,7 +2,9 @@ import { eq } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import { githubIntegrationTable, projectTable } from "../../database/schema";
-import githubApp from "../utils/create-github-app";
+import createGithubApp from "../utils/create-github-app";
+
+const githubApp = createGithubApp();
async function createGithubIntegration({
projectId,
@@ -13,6 +15,12 @@ async function createGithubIntegration({
repositoryOwner: string;
repositoryName: string;
}) {
+ if (!githubApp) {
+ throw new HTTPException(500, {
+ message: "GitHub app not found",
+ });
+ }
+
const project = await db.query.projectTable.findFirst({
where: eq(projectTable.id, projectId),
});
diff --git a/apps/api/src/github-integration/controllers/list-user-repositories.ts b/apps/api/src/github-integration/controllers/list-user-repositories.ts
index e2c261d00..e7d1006ad 100644
--- a/apps/api/src/github-integration/controllers/list-user-repositories.ts
+++ b/apps/api/src/github-integration/controllers/list-user-repositories.ts
@@ -1,7 +1,15 @@
import { HTTPException } from "hono/http-exception";
-import githubApp from "../utils/create-github-app";
+import createGithubApp from "../utils/create-github-app";
+
+const githubApp = createGithubApp();
async function listUserRepositories() {
+ if (!githubApp) {
+ throw new HTTPException(500, {
+ message: "GitHub app not found",
+ });
+ }
+
try {
const { data: installations } =
await githubApp.octokit.rest.apps.listInstallations();
diff --git a/apps/api/src/github-integration/controllers/verify-github-installation.ts b/apps/api/src/github-integration/controllers/verify-github-installation.ts
index db32b1e3e..fe7e25c75 100644
--- a/apps/api/src/github-integration/controllers/verify-github-installation.ts
+++ b/apps/api/src/github-integration/controllers/verify-github-installation.ts
@@ -1,5 +1,7 @@
import { HTTPException } from "hono/http-exception";
-import githubApp from "../utils/create-github-app";
+import createGithubApp from "../utils/create-github-app";
+
+const githubApp = createGithubApp();
type VerificationResult = {
isInstalled: boolean;
@@ -22,6 +24,12 @@ async function verifyGithubInstallation({
repositoryName: string;
}): Promise<VerificationResult> {
try {
+ if (!githubApp) {
+ throw new HTTPException(500, {
+ message: "GitHub app not found",
+ });
+ }
+
const { data: installation } =
await githubApp.octokit.rest.apps.getRepoInstallation({
owner: repositoryOwner,
@@ -77,6 +85,12 @@ async function verifyGithubInstallation({
if (githubError.status === 404) {
try {
+ if (!githubApp) {
+ throw new HTTPException(500, {
+ message: "GitHub app not found",
+ });
+ }
+
await githubApp.octokit.rest.repos.get({
owner: repositoryOwner,
repo: repositoryName,
@@ -149,6 +163,12 @@ function getMissingPermissions(
async function getRepositoryId(owner: string, repo: string): Promise<number> {
try {
+ if (!githubApp) {
+ throw new HTTPException(500, {
+ message: "GitHub app not found",
+ });
+ }
+
const { data } = await githubApp.octokit.rest.repos.get({
owner,
repo,
diff --git a/apps/api/src/github-integration/index.ts b/apps/api/src/github-integration/index.ts
index 8b36ab90a..a258a6f3c 100644
--- a/apps/api/src/github-integration/index.ts
+++ b/apps/api/src/github-integration/index.ts
@@ -7,11 +7,15 @@ import deleteGithubIntegration from "./controllers/delete-github-integration";
import getGithubIntegration from "./controllers/get-github-integration";
import listUserRepositories from "./controllers/list-user-repositories";
import verifyGithubInstallation from "./controllers/verify-github-installation";
-import githubApp from "./utils/create-github-app";
+import createGithubApp from "./utils/create-github-app";
import { handleIssueOpened } from "./utils/issue-opened";
import { handleTaskCreated } from "./utils/task-created";
-githubApp.webhooks.on("issues.opened", handleIssueOpened);
+const githubApp = createGithubApp();
+
+if (githubApp) {
+ githubApp.webhooks.on("issues.opened", handleIssueOpened);
+}
subscribeToEvent<{
taskId: string;
@@ -97,6 +101,10 @@ const githubIntegration = new Hono()
)
.post("/webhook", async (c) => {
try {
+ if (!githubApp) {
+ return c.json({ error: "GitHub integration not configured" }, 400);
+ }
+
const arrayBuffer = await c.req.arrayBuffer();
const body = Buffer.from(arrayBuffer).toString("utf8");
diff --git a/apps/api/src/github-integration/utils/create-github-app.ts b/apps/api/src/github-integration/utils/create-github-app.ts
index 01b3f8125..1cc5917d6 100644
--- a/apps/api/src/github-integration/utils/create-github-app.ts
+++ b/apps/api/src/github-integration/utils/create-github-app.ts
@@ -3,12 +3,24 @@ import { App } from "octokit";
dotenv.config();
-const githubApp: App = new App({
- appId: process.env.GITHUB_APP_ID ?? "",
- privateKey: process.env.GITHUB_PRIVATE_KEY ?? "",
- webhooks: {
- secret: process.env.GITHUB_WEBHOOK_SECRET ?? "",
- },
-});
-
-export default githubApp;
+function createGithubApp(): App | null {
+ if (
+ !process.env.GITHUB_WEBHOOK_SECRET ||
+ !process.env.GITHUB_APP_ID ||
+ !process.env.GITHUB_PRIVATE_KEY
+ ) {
+ return null;
+ }
+
+ const githubApp: App = new App({
+ appId: process.env.GITHUB_APP_ID ?? "",
+ privateKey: process.env.GITHUB_PRIVATE_KEY ?? "",
+ webhooks: {
+ secret: process.env.GITHUB_WEBHOOK_SECRET ?? "",
+ },
+ });
+
+ return githubApp;
+}
+
+export default createGithubApp;
diff --git a/apps/api/src/github-integration/utils/task-created.ts b/apps/api/src/github-integration/utils/task-created.ts
index 452f9607b..26e00025a 100644
--- a/apps/api/src/github-integration/utils/task-created.ts
+++ b/apps/api/src/github-integration/utils/task-created.ts
@@ -1,5 +1,7 @@
import getGithubIntegration from "../controllers/get-github-integration";
-import githubApp from "./create-github-app";
+import createGithubApp from "./create-github-app";
+
+const githubApp = createGithubApp();
export async function handleTaskCreated(data: {
taskId: string;
@@ -11,6 +13,11 @@ export async function handleTaskCreated(data: {
number: number;
projectId: string;
}) {
+ if (!githubApp) {
+ console.log("No GitHub app found");
+ return;
+ }
+
const { taskId, userEmail, title, description, priority, status, projectId } =
data;
diff --git a/apps/web/package.json b/apps/web/package.json
index 6128f088b..639db1a19 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -6,7 +6,8 @@
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
- "preview": "vite preview"
+ "preview": "vite preview",
+ "lint": "biome check --write ."
},
"dependencies": {
"@dnd-kit/core": "^6.3.1",
diff --git a/packages/libs/package.json b/packages/libs/package.json
index 4af2725fc..658857013 100644
--- a/packages/libs/package.json
+++ b/packages/libs/package.json
@@ -5,7 +5,7 @@
"types": "./src/index.ts",
"license": "MIT",
"scripts": {
- "lint": " *.ts*"
+ "lint": "biome check --write ."
},
"devDependencies": {
"@types/react": "^19.1.3",
diff --git a/turbo.json b/turbo.json
index 0120d8c8f..ceeb3666a 100644
--- a/turbo.json
+++ b/turbo.json
@@ -11,6 +11,10 @@
"dev": {
"cache": false,
"persistent": true
+ },
+ "lint": {
+ "cache": false,
+ "persistent": true
}
}
}