-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathgetFileSourceApi.ts
More file actions
97 lines (87 loc) · 3.38 KB
/
getFileSourceApi.ts
File metadata and controls
97 lines (87 loc) · 3.38 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
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
import { sew } from '@/actions';
import { getBrowsePath } from '@/app/[domain]/browse/hooks/utils';
import { SINGLE_TENANT_ORG_DOMAIN } from '@/lib/constants';
import { detectLanguageFromFilename } from '@/lib/languageDetection';
import { ServiceError, notFound, fileNotFound, invalidGitRef, unexpectedError } from '@/lib/serviceError';
import { getCodeHostBrowseFileAtBranchUrl } from '@/lib/utils';
import { withOptionalAuthV2 } from '@/withAuthV2';
import { getRepoPath } from '@sourcebot/shared';
import simpleGit from 'simple-git';
import z from 'zod';
import { isGitRefValid, isPathValid } from './utils';
import { CodeHostType } from '@sourcebot/db';
export const fileSourceRequestSchema = z.object({
path: z.string(),
repo: z.string(),
ref: z.string().optional(),
});
export type FileSourceRequest = z.infer<typeof fileSourceRequestSchema>;
export const fileSourceResponseSchema = z.object({
source: z.string(),
language: z.string(),
path: z.string(),
repo: z.string(),
repoCodeHostType: z.nativeEnum(CodeHostType),
repoDisplayName: z.string().optional(),
repoExternalWebUrl: z.string().optional(),
webUrl: z.string(),
externalWebUrl: z.string().optional(),
});
export type FileSourceResponse = z.infer<typeof fileSourceResponseSchema>;
export const getFileSource = async ({ path: filePath, repo: repoName, ref }: FileSourceRequest): Promise<FileSourceResponse | ServiceError> => sew(() => withOptionalAuthV2(async ({ org, prisma }) => {
const repo = await prisma.repo.findFirst({
where: { name: repoName, orgId: org.id },
});
if (!repo) {
return notFound(`Repository "${repoName}" not found.`);
}
if (!isPathValid(filePath)) {
return fileNotFound(filePath, repoName);
}
if (ref !== undefined && !isGitRefValid(ref)) {
return invalidGitRef(ref);
}
const { path: repoPath } = getRepoPath(repo);
const git = simpleGit().cwd(repoPath);
const gitRef = ref ??
repo.defaultBranch ??
'HEAD';
let source: string;
try {
source = await git.raw(['show', `${gitRef}:${filePath}`]);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage.includes('does not exist') || errorMessage.includes('fatal: path')) {
return fileNotFound(filePath, repoName);
}
if (errorMessage.includes('unknown revision') || errorMessage.includes('bad revision') || errorMessage.includes('invalid object name')) {
return unexpectedError(`Invalid git reference: ${gitRef}`);
}
throw error;
}
const language = detectLanguageFromFilename(filePath);
const webUrl = getBrowsePath({
repoName: repo.name,
revisionName: ref,
path: filePath,
pathType: 'blob',
domain: SINGLE_TENANT_ORG_DOMAIN,
});
const externalWebUrl = getCodeHostBrowseFileAtBranchUrl({
webUrl: repo.webUrl,
codeHostType: repo.external_codeHostType,
branchName: gitRef,
filePath,
});
return {
source,
language,
path: filePath,
repo: repoName,
repoCodeHostType: repo.external_codeHostType,
repoDisplayName: repo.displayName ?? undefined,
repoExternalWebUrl: repo.webUrl ?? undefined,
webUrl,
externalWebUrl,
} satisfies FileSourceResponse;
}));