kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { HTTPException } from "hono/http-exception";
2import { normalizeGiteaBaseUrl } from "../../plugins/gitea/config";
3import {
4 createGiteaClient,
5 verifyGiteaToken,
6} from "../../plugins/gitea/utils/gitea-api";
7
8async function verifyGiteaAccess({
9 baseUrl,
10 accessToken,
11 repositoryOwner,
12 repositoryName,
13}: {
14 baseUrl: string;
15 accessToken: string;
16 repositoryOwner: string;
17 repositoryName: string;
18}) {
19 try {
20 const normalized = normalizeGiteaBaseUrl(baseUrl);
21 await verifyGiteaToken(normalized, accessToken);
22
23 const client = createGiteaClient({
24 baseUrl: normalized,
25 accessToken,
26 });
27
28 const repo = await client.getRepo(repositoryOwner, repositoryName);
29
30 const perms = repo.permissions;
31 const hasIssuesWrite = perms?.admin === true || perms?.push === true;
32
33 return {
34 isInstalled: true,
35 hasRequiredPermissions: Boolean(hasIssuesWrite),
36 repositoryExists: true,
37 repositoryPrivate: repo.private,
38 missingPermissions: hasIssuesWrite ? [] : ["issues (write)"],
39 message: hasIssuesWrite
40 ? "Token can access the repository."
41 : "Token may not have sufficient permissions to manage issues.",
42 };
43 } catch (error) {
44 const err = error as { status?: number; message?: string };
45
46 if (err.status === 404) {
47 return {
48 isInstalled: false,
49 hasRequiredPermissions: false,
50 repositoryExists: false,
51 repositoryPrivate: null,
52 missingPermissions: [] as string[],
53 message: "Repository not found or not accessible with this token.",
54 };
55 }
56
57 if (err.status === 401) {
58 throw new HTTPException(401, {
59 message: "Invalid Gitea token or unauthorized.",
60 });
61 }
62
63 throw new HTTPException(500, {
64 message:
65 error instanceof Error
66 ? error.message
67 : "Failed to verify Gitea access",
68 });
69 }
70}
71
72export default verifyGiteaAccess;