Swadhin Biswas Feat/federation (#82) · 8/19/2026
c0437ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Org-aware repository owner resolution.
*
* Repositories may be owned by a user (ownerType='user') or an
* organization (ownerType='organization', ownerId = org id). Many routes
* look up owners via the users table only, which 404s every org-owned
* repo. Use `resolveOwnerRepo` instead of the naive lookup.
*/
import { getDatabase, schema } from "@/db";
import { eq, and, inArray } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
export async function resolveOwnerRepo(
ownerName: string,
repoName: string,
): Promise<{ repository: any; ownerType: "user" | "organization" } | null> {
const db = getDatabase() as NodePgDatabase<typeof schema>;
const ownerUser = await db.query.users.findFirst({
where: eq(schema.users.username, ownerName),
});
if (ownerUser) {
const repository = await db.query.repositories.findFirst({
where: and(
eq(schema.repositories.ownerId, ownerUser.id),
eq(schema.repositories.name, repoName),
),
});
if (repository) return { repository, ownerType: "user" };
}
const org = await db.query.organizations.findFirst({
where: eq(schema.organizations.name, ownerName),
});
if (org) {
const repository = await db.query.repositories.findFirst({
where: and(
eq(schema.repositories.ownerId, org.id),
eq(schema.repositories.ownerType, "organization"),
eq(schema.repositories.name, repoName),
),
});
if (repository) return { repository, ownerType: "organization" };
}
return null;
}
/**
* Fill in a synthetic `owner.username` for org-owned repos.
* Queries that use `with: { owner: true }` get a null owner for org repos
* (ownerId = org id), which crashes `repo.owner.username` renders.
*/
export async function attachOwnerNames<T extends { ownerId: string; ownerType?: string | null; owner?: any }>(
repos: T[],
): Promise<T[]> {
if (repos.length === 0) return repos;
const orgIds = repos
.filter((r) => r.ownerType === "organization" && !r.owner)
.map((r) => r.ownerId);
if (orgIds.length === 0) return repos;
const db = getDatabase() as NodePgDatabase<typeof schema>;
const orgs = await db.query.organizations.findMany({
where: inArray(schema.organizations.id, orgIds),
columns: { id: true, name: true },
});
const nameById = new Map(orgs.map((o) => [o.id, o.name]));
return repos.map((r) =>
r.ownerType === "organization" && !r.owner
? { ...r, owner: { username: nameById.get(r.ownerId) ?? "org" } }
: r,
);
}
/**
* Deep org-owner fix for list pages. Walks query results and fills a
* synthetic `owner.username` on any org-owned repo found at the top level
* or nested under `item.repository` (the `with: { owner: true }` relation
* is null for org repos).
>>>>>>> origin/main
*/
export async function resolveOrgOwners<T>(items: T[]): Promise<T[]> {
const candidates: any[] = [];
const walk = (obj: any) => {
if (!obj || typeof obj !== "object") return;
if (obj.ownerType === "organization" && !obj.owner && obj.ownerId) {
candidates.push(obj);
}
if (
obj.repository &&
obj.repository.ownerType === "organization" &&
!obj.repository.owner &&
obj.repository.ownerId
) {
candidates.push(obj.repository);
}
for (const key of Object.keys(obj)) {
const value = obj[key];
if (Array.isArray(value)) value.forEach(walk);
}
};
items.forEach(walk);
if (candidates.length === 0) return items;
const db = getDatabase() as NodePgDatabase<typeof schema>;
const orgs = await db.query.organizations.findMany({
where: inArray(schema.organizations.id, candidates.map((c) => c.ownerId)),
columns: { id: true, name: true },
});
const nameById = new Map(orgs.map((o) => [o.id, o.name]));
for (const c of candidates) {
c.owner = { username: nameById.get(c.ownerId) ?? "org" };
}
return items;
}