-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathlistReposApi.ts
More file actions
56 lines (53 loc) · 2.05 KB
/
listReposApi.ts
File metadata and controls
56 lines (53 loc) · 2.05 KB
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
import { sew } from "@/actions";
import { repositoryQuerySchema } from "@/lib/schemas";
import { ListReposQueryParams } from "@/lib/types";
import { withOptionalAuthV2 } from "@/withAuthV2";
import { getBrowsePath } from "@/app/[domain]/browse/hooks/utils";
import { env } from "@sourcebot/shared";
export const listRepos = async ({ query, page, perPage, sort, direction }: ListReposQueryParams) => sew(() =>
withOptionalAuthV2(async ({ org, prisma }) => {
const skip = (page - 1) * perPage;
const orderByField = sort === 'pushed' ? 'pushedAt' : 'name';
const baseUrl = env.AUTH_URL;
const [repos, totalCount] = await Promise.all([
prisma.repo.findMany({
where: {
orgId: org.id,
...(query ? {
name: { contains: query, mode: 'insensitive' },
} : {}),
},
skip,
take: perPage,
orderBy: { [orderByField]: direction },
}),
prisma.repo.count({
where: {
orgId: org.id,
...(query ? {
name: { contains: query, mode: 'insensitive' },
} : {}),
},
}),
]);
return {
data: repos.map((repo) => repositoryQuerySchema.parse({
codeHostType: repo.external_codeHostType,
repoId: repo.id,
repoName: repo.name,
webUrl: `${baseUrl}${getBrowsePath({
repoName: repo.name,
path: '',
pathType: 'tree',
domain: org.domain,
})}`,
repoDisplayName: repo.displayName ?? undefined,
externalWebUrl: repo.webUrl ?? undefined,
imageUrl: repo.imageUrl ?? undefined,
indexedAt: repo.indexedAt ?? undefined,
pushedAt: repo.pushedAt ?? undefined,
})),
totalCount,
};
})
)