2025-11-14 01:21:48 +00:00
|
|
|
import 'server-only';
|
2025-05-03 18:33:58 +00:00
|
|
|
import escapeStringRegexp from "escape-string-regexp";
|
2025-07-23 18:25:15 +00:00
|
|
|
import { fileNotFound, ServiceError, unexpectedError } from "../../lib/serviceError";
|
2025-05-03 18:33:58 +00:00
|
|
|
import { FileSourceRequest, FileSourceResponse } from "./types";
|
|
|
|
|
import { isServiceError } from "../../lib/utils";
|
|
|
|
|
import { search } from "./searchApi";
|
2025-09-20 23:51:14 +00:00
|
|
|
import { sew } from "@/actions";
|
|
|
|
|
import { withOptionalAuthV2 } from "@/withAuthV2";
|
2025-10-27 18:49:03 +00:00
|
|
|
// @todo (bkellam) #574 : We should really be using `git show <hash>:<path>` to fetch file contents here.
|
2025-05-03 18:33:58 +00:00
|
|
|
// This will allow us to support permalinks to files at a specific revision that may not be indexed
|
|
|
|
|
// by zoekt.
|
2025-06-18 17:50:36 +00:00
|
|
|
|
2025-09-20 23:51:14 +00:00
|
|
|
export const getFileSource = async ({ fileName, repository, branch }: FileSourceRequest): Promise<FileSourceResponse | ServiceError> => sew(() =>
|
|
|
|
|
withOptionalAuthV2(async () => {
|
|
|
|
|
const escapedFileName = escapeStringRegexp(fileName);
|
|
|
|
|
const escapedRepository = escapeStringRegexp(repository);
|
|
|
|
|
|
|
|
|
|
let query = `file:${escapedFileName} repo:^${escapedRepository}$`;
|
|
|
|
|
if (branch) {
|
|
|
|
|
query = query.concat(` branch:${branch}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const searchResponse = await search({
|
|
|
|
|
query,
|
|
|
|
|
matches: 1,
|
|
|
|
|
whole: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (isServiceError(searchResponse)) {
|
|
|
|
|
return searchResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const files = searchResponse.files;
|
|
|
|
|
|
|
|
|
|
if (!files || files.length === 0) {
|
|
|
|
|
return fileNotFound(fileName, repository);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const file = files[0];
|
|
|
|
|
const source = file.content ?? '';
|
|
|
|
|
const language = file.language;
|
|
|
|
|
|
|
|
|
|
const repoInfo = searchResponse.repositoryInfo.find((repo) => repo.id === file.repositoryId);
|
|
|
|
|
if (!repoInfo) {
|
|
|
|
|
// This should never happen.
|
|
|
|
|
return unexpectedError("Repository info not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
source,
|
|
|
|
|
language,
|
|
|
|
|
path: fileName,
|
|
|
|
|
repository,
|
|
|
|
|
repositoryCodeHostType: repoInfo.codeHostType,
|
|
|
|
|
repositoryDisplayName: repoInfo.displayName,
|
|
|
|
|
repositoryWebUrl: repoInfo.webUrl,
|
|
|
|
|
branch,
|
|
|
|
|
webUrl: file.webUrl,
|
|
|
|
|
} satisfies FileSourceResponse;
|
|
|
|
|
|
|
|
|
|
}));
|