-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle breaking change in zoekt repo URL templates
:shakefist: Ref: sourcegraph/zoekt@5687809
- Loading branch information
Showing
10 changed files
with
196 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"neogrok": patch | ||
--- | ||
|
||
Handle breaking change in zoekt repo URL templates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { | ||
evaluateFileUrlTemplate, | ||
evaluateCommitUrlTemplate, | ||
} from "./url-templates"; | ||
|
||
describe("evaluateFileUrlTemplate", () => { | ||
it("evaluates old templates", () => { | ||
expect( | ||
evaluateFileUrlTemplate( | ||
"https://github.com/hanwen/go-fuse/blob/{{.Version}}/{{.Path}}", | ||
"notify", | ||
"genversion.sh", | ||
), | ||
).toEqual("https://github.com/hanwen/go-fuse/blob/notify/genversion.sh"); | ||
expect( | ||
evaluateFileUrlTemplate( | ||
"https://svn.future/r/{{.Version}}/nopath", | ||
"12345", | ||
"ignored", | ||
), | ||
).toEqual("https://svn.future/r/12345/nopath"); | ||
}); | ||
|
||
it("evaluates new templates", () => { | ||
expect( | ||
evaluateFileUrlTemplate( | ||
'{{URLJoinPath "https://github.com/hanwen/go-fuse/blob" .Version .Path}}', | ||
"notify", | ||
"genversion.sh", | ||
), | ||
).toEqual("https://github.com/hanwen/go-fuse/blob/notify/genversion.sh"); | ||
expect( | ||
evaluateFileUrlTemplate( | ||
'{{ URLJoinPath "https://github.com/hanwen/go-fuse/blob" .Version .Path }}', | ||
"notify", | ||
"genversion.sh", | ||
), | ||
).toEqual("https://github.com/hanwen/go-fuse/blob/notify/genversion.sh"); | ||
expect( | ||
evaluateFileUrlTemplate( | ||
'{{URLJoinPath "https://svn.future/r" .Version "nopath"}}', | ||
"12345", | ||
"ignored", | ||
), | ||
).toEqual("https://svn.future/r/12345/nopath"); | ||
}); | ||
}); | ||
|
||
describe("evaluateCommitUrlTemplate", () => { | ||
it("evaluates old templates", () => { | ||
expect( | ||
evaluateCommitUrlTemplate( | ||
"https://github.com/hanwen/go-fuse/commit/{{.Version}}", | ||
"deadbeef", | ||
), | ||
).toEqual("https://github.com/hanwen/go-fuse/commit/deadbeef"); | ||
expect( | ||
evaluateCommitUrlTemplate("https://svn.future/r/{{.Version}}", "12345"), | ||
).toEqual("https://svn.future/r/12345"); | ||
}); | ||
|
||
it("evaluates new templates", () => { | ||
expect( | ||
evaluateCommitUrlTemplate( | ||
'{{URLJoinPath "https://github.com/hanwen/go-fuse/commit" .Version}}', | ||
"deadbeef", | ||
), | ||
).toEqual("https://github.com/hanwen/go-fuse/commit/deadbeef"); | ||
expect( | ||
evaluateCommitUrlTemplate( | ||
'{{ URLJoinPath "https://github.com/hanwen/go-fuse/commit" .Version }}', | ||
"deadbeef", | ||
), | ||
).toEqual("https://github.com/hanwen/go-fuse/commit/deadbeef"); | ||
expect( | ||
evaluateCommitUrlTemplate( | ||
'{{URLJoinPath "https://svn.future/r" .Version}}', | ||
"12345", | ||
), | ||
).toEqual("https://svn.future/r/12345"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// CommitURLTemplate and FileURLTemplate used to be simple. No longer: | ||
// https://github.com/sourcegraph/zoekt/commit/5687809315075882a8e7413bdb17b042f3394c02 | ||
// | ||
// These functions handle both the old and new versions of the templates. | ||
|
||
const urlJoinPathTemplate = /^{{\s*URLJoinPath\s+(?<args>.*?)\s*}}$/; | ||
|
||
export const evaluateFileUrlTemplate = ( | ||
template: string, | ||
version: string, | ||
path: string, | ||
): string => { | ||
const match = template.match(urlJoinPathTemplate); | ||
if (match?.groups) { | ||
const { args } = match.groups; | ||
return args | ||
.split(/\s+/) | ||
.map((s) => { | ||
if (s === ".Version") { | ||
return version; | ||
} else if (s === ".Path") { | ||
return path; | ||
} else { | ||
// It's a quoted string: https://pkg.go.dev/strconv#Quote. | ||
return JSON.parse(s); | ||
} | ||
}) | ||
.join("/"); | ||
} else { | ||
return template | ||
.replaceAll("{{.Version}}", version) | ||
.replaceAll("{{.Path}}", path); | ||
} | ||
}; | ||
|
||
export const evaluateCommitUrlTemplate = ( | ||
template: string, | ||
version: string, | ||
): string => { | ||
const match = template.match(urlJoinPathTemplate); | ||
if (match?.groups) { | ||
const { args } = match.groups; | ||
return args | ||
.split(/\s+/) | ||
.map((s) => { | ||
if (s === ".Version") { | ||
return version; | ||
} else { | ||
// It's a quoted string: https://pkg.go.dev/strconv#Quote. | ||
return JSON.parse(s); | ||
} | ||
}) | ||
.join("/"); | ||
} else { | ||
return template.replaceAll("{{.Version}}", version); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters