Skip to content

Commit aad3507

Browse files
plumb file weburl
1 parent 696d06b commit aad3507

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

packages/web/src/features/search/searchApi.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { parseQueryIntoLezerTree, transformLezerTreeToZoektGrpcQuery } from './q
1919
import { RepositoryInfo, SearchRequest, SearchResponse, SearchResultFile, SearchStats, SourceRange, StreamedSearchResponse } from "./types";
2020
import { FlushReason as ZoektFlushReason } from "@/proto/zoekt/webserver/v1/FlushReason";
2121
import { RevisionExpr } from "@sourcebot/query-language";
22+
import { getCodeHostBrowseFileAtBranchUrl } from "@/lib/utils";
2223

2324
const logger = createLogger("searchApi");
2425

@@ -29,7 +30,7 @@ export const search = (searchRequest: SearchRequest) => sew(() =>
2930
prisma,
3031
});
3132

32-
console.debug('zoektSearchRequest:', JSON.stringify(zoektSearchRequest, null, 2));
33+
logger.debug('zoektSearchRequest:', JSON.stringify(zoektSearchRequest, null, 2));
3334

3435
return zoektSearch(zoektSearchRequest, prisma);
3536
}));
@@ -41,6 +42,8 @@ export const streamSearch = (searchRequest: SearchRequest) => sew(() =>
4142
prisma,
4243
});
4344

45+
logger.debug('zoektStreamSearchRequest:', JSON.stringify(zoektSearchRequest, null, 2));
46+
4447
return zoektStreamSearch(zoektSearchRequest, prisma);
4548
}));
4649

@@ -304,13 +307,13 @@ const transformZoektSearchResponse = async (response: ZoektGrpcSearchResponse, r
304307
const convertRange = (range: ZoektGrpcRange): SourceRange => ({
305308
start: {
306309
byteOffset: range.start?.byte_offset ?? 0,
307-
column: range.start?.column ?? 0,
308-
lineNumber: range.start?.line_number ?? 0,
310+
column: range.start?.column ?? 1,
311+
lineNumber: range.start?.line_number ?? 1,
309312
},
310313
end: {
311314
byteOffset: range.end?.byte_offset ?? 0,
312-
column: range.end?.column ?? 0,
313-
lineNumber: range.end?.line_number ?? 0,
315+
column: range.end?.column ?? 1,
316+
lineNumber: range.end?.line_number ?? 1,
314317
}
315318
})
316319

@@ -322,8 +325,13 @@ const transformZoektSearchResponse = async (response: ZoektGrpcSearchResponse, r
322325
repository: repo.name,
323326
repositoryId: repo.id,
324327
language: file.language,
325-
// @todo: we will need to have a mechanism of forming the file's web url.
326-
webUrl: '',
328+
webUrl: getCodeHostBrowseFileAtBranchUrl({
329+
webUrl: repo.webUrl,
330+
codeHostType: repo.external_codeHostType,
331+
// If a file has multiple branches, default to the first one.
332+
branchName: file.branches?.[0] ?? 'HEAD',
333+
filePath: fileName,
334+
}),
327335
chunks: file.chunk_matches
328336
.filter((chunk) => !chunk.file_name) // filter out filename chunks.
329337
.map((chunk) => {
@@ -336,8 +344,8 @@ const transformZoektSearchResponse = async (response: ZoektGrpcSearchResponse, r
336344
lineNumber: chunk.content_start.line_number,
337345
} : {
338346
byteOffset: 0,
339-
column: 0,
340-
lineNumber: 0,
347+
column: 1,
348+
lineNumber: 1,
341349
},
342350
symbols: chunk.symbol_info.map((symbol) => {
343351
return {

packages/web/src/lib/utils.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,42 @@ export const getCodeHostBrowseAtBranchUrl = ({
376376
}
377377
}
378378

379+
export const getCodeHostBrowseFileAtBranchUrl = ({
380+
webUrl,
381+
codeHostType,
382+
branchName,
383+
filePath,
384+
}: {
385+
webUrl?: string | null,
386+
codeHostType: CodeHostType,
387+
branchName: string,
388+
filePath: string,
389+
}) => {
390+
if (!webUrl) {
391+
return undefined;
392+
}
393+
394+
switch (codeHostType) {
395+
case 'github':
396+
return `${webUrl}/blob/${branchName}/${filePath}`;
397+
case 'gitlab':
398+
return `${webUrl}/-/blob/${branchName}/${filePath}`;
399+
case 'gitea':
400+
return `${webUrl}/src/branch/${branchName}/${filePath}`;
401+
case 'azuredevops':
402+
return `${webUrl}?path=${filePath}&version=${branchName}`;
403+
case 'bitbucketCloud':
404+
return `${webUrl}/src/${branchName}/${filePath}`;
405+
case 'bitbucketServer':
406+
return `${webUrl}/browse/${filePath}?at=${branchName}`;
407+
case 'gerrit':
408+
return `${webUrl}/+/${branchName}/${filePath}`;
409+
case 'genericGitHost':
410+
return undefined;
411+
412+
}
413+
}
414+
379415
export const isAuthSupportedForCodeHost = (codeHostType: CodeHostType): boolean => {
380416
switch (codeHostType) {
381417
case "github":

0 commit comments

Comments
 (0)