Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/url-template-suffix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"neogrok": patch
---

Fix evaluation of Zoekt URL templates that have text after the `{{URLJoinPath ...}}` block, such as the Bitbucket Server file template `{{URLJoinPath "<repo-url>/browse" .Path}}?at={{.Version}}`. Previously, any search whose results included a file from such a repository failed with `zoekt call failed: SyntaxError: Unexpected token '.' ... is not valid JSON`.
12 changes: 12 additions & 0 deletions src/lib/url-templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ describe("evaluateFileUrlTemplate", () => {
),
).toEqual("https://svn.future/r/12345/nopath");
});

it("evaluates new templates with a suffix (Bitbucket Server)", () => {
expect(
evaluateFileUrlTemplate(
'{{URLJoinPath "https://bitbucket.example.com/projects/TOOL/repos/acme/browse" .Path}}?at={{.Version}}',
"refs/heads/main",
"src/a b.go",
),
).toEqual(
"https://bitbucket.example.com/projects/TOOL/repos/acme/browse/src/a%20b.go?at=refs%2Fheads%2Fmain",
);
});
});

describe("evaluateCommitUrlTemplate", () => {
Expand Down
26 changes: 21 additions & 5 deletions src/lib/url-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
//
// These functions handle both the old and new versions of the templates.

const urlJoinPathTemplate = /^{{\s*URLJoinPath\s+(?<args>.*?)\s*}}$/;
// Zoekt may put more template text after the URLJoinPath block. Bitbucket
// Server's file template is one such case:
// {{URLJoinPath "https://host/projects/P/repos/r/browse" .Path}}?at={{.Version}}
const urlJoinPathTemplate =
/^{{\s*URLJoinPath\s+(?<args>.*?)\s*}}(?<suffix>.*)$/;

// The suffix sits outside the joined path, typically in the query string, so
// variables in it are encoded as single components rather than path segments.
const evaluateSuffix = (
suffix: string,
variables: { version: string; path?: string },
): string =>
suffix
.replaceAll("{{.Version}}", () => encodeURIComponent(variables.version))
.replaceAll("{{.Path}}", () => encodeURIComponent(variables.path ?? ""));

export const evaluateFileUrlTemplate = (
template: string,
Expand All @@ -12,7 +26,7 @@ export const evaluateFileUrlTemplate = (
): string => {
const match = template.match(urlJoinPathTemplate);
if (match?.groups) {
const { args } = match.groups;
const { args, suffix } = match.groups;
return args
.split(/\s+/)
.map((s) => {
Expand All @@ -25,7 +39,8 @@ export const evaluateFileUrlTemplate = (
return JSON.parse(s);
}
})
.join("/");
.join("/")
.concat(evaluateSuffix(suffix, { version, path }));
} else {
return (
template
Expand All @@ -44,7 +59,7 @@ export const evaluateCommitUrlTemplate = (
): string => {
const match = template.match(urlJoinPathTemplate);
if (match?.groups) {
const { args } = match.groups;
const { args, suffix } = match.groups;
return args
.split(/\s+/)
.map((s) => {
Expand All @@ -55,7 +70,8 @@ export const evaluateCommitUrlTemplate = (
return JSON.parse(s);
}
})
.join("/");
.join("/")
.concat(evaluateSuffix(suffix, { version }));
} else {
// We use the function version of replaceAll because it interprets a
// variety of characters in strings specially. Only functions guarantee
Expand Down