diff --git a/ui/assets/js/common.js b/ui/assets/js/common.js index 35216a4f..44e32e6b 100644 --- a/ui/assets/js/common.js +++ b/ui/assets/js/common.js @@ -5,12 +5,13 @@ export function ExpandVars(template, values) { return template; }; -export function UrlToRepo(repo, path, line, rev) { +export function UrlParts(repo, path, line, rev) { var url = repo.url.replace(/\.git$/, ''), pattern = repo['url-pattern'], hostname = '', project = '', repoName = '', + path = path || '', port = '', filename = path.substring(path.lastIndexOf('/') + 1), anchor = line ? ExpandVars(pattern.anchor, { line : line, filename : filename }) : ''; @@ -44,8 +45,7 @@ export function UrlToRepo(repo, path, line, rev) { url = hostname + port + '/' + project + '/' + repoName; } - // I'm sure there is a nicer React/jsx way to do this: - return ExpandVars(pattern['base-url'], { + return { url : url, hostname: hostname, port: port, @@ -54,5 +54,13 @@ export function UrlToRepo(repo, path, line, rev) { path: path, rev: rev, anchor: anchor - }); + }; +} + +export function UrlToRepo(repo, path, line, rev) { + var urlParts = UrlParts(repo, path, line, rev), + pattern = repo['url-pattern'] + + // I'm sure there is a nicer React/jsx way to do this: + return ExpandVars(pattern['base-url'], urlParts); } diff --git a/ui/assets/js/common.test.js b/ui/assets/js/common.test.js index 6a350590..f8073028 100644 --- a/ui/assets/js/common.test.js +++ b/ui/assets/js/common.test.js @@ -1,4 +1,4 @@ -import { ExpandVars, UrlToRepo } from "./common"; +import { ExpandVars, UrlParts, UrlToRepo } from "./common"; describe("ExpandVars", () => { test("Replaces template variables with their values", () => { @@ -20,106 +20,137 @@ describe("ExpandVars", () => { }); }); -describe("UrlToRepo", () => { - test("Generate url from repo with default values", () => { +describe("UrlParts", () => { + test("Generate parts from repo with default values", () => { const repo = { url: "https://www.github.com/YourOrganization/RepoOne.git", "url-pattern": { - "base-url": "{url}/blob/{rev}/{path}{anchor}", anchor: "#L{line}" } }; const path = "test.txt" const line = null const rev = "main" - expect(UrlToRepo(repo, path, line, rev)).toBe( - "https://www.github.com/YourOrganization/RepoOne/blob/main/test.txt" - ); + expect(UrlParts(repo, path, line, rev)).toEqual(expect.objectContaining({ + url: "https://www.github.com/YourOrganization/RepoOne", + rev: rev, + path: path, + anchor: "", + })); }); - test("Generate url from repo with default values and line", () => { + test("Generate parts from repo with default values and line", () => { const repo = { url: "https://www.github.com/YourOrganization/RepoOne.git", "url-pattern": { - "base-url": "{url}/blob/{rev}/{path}{anchor}", anchor: "#L{line}" } }; const path = "test.txt" const line = "12" const rev = "main" - expect(UrlToRepo(repo, path, line, rev)).toBe( - "https://www.github.com/YourOrganization/RepoOne/blob/main/test.txt#L12" - ); + expect(UrlParts(repo, path, line, rev)).toEqual(expect.objectContaining({ + url: "https://www.github.com/YourOrganization/RepoOne", + rev: rev, + path: path, + anchor: "#L" + line, + })); }); - test("Generate url for ssh style repo with default values", () => { + test("Generate parts for ssh style repo with default values", () => { const repo = { url: "git@github.com:YourOrganization/RepoOne.git", "url-pattern": { - "base-url": "{url}/blob/{rev}/{path}{anchor}", anchor: "#L{line}" } }; const path = "test.txt" const line = null const rev = "main" - expect(UrlToRepo(repo, path, line, rev)).toBe( - "//github.com/YourOrganization/RepoOne/blob/main/test.txt" - ); + expect(UrlParts(repo, path, line, rev)).toEqual(expect.objectContaining({ + url: "//github.com/YourOrganization/RepoOne", + rev: rev, + path: path, + anchor: "", + })); }); - test("Generate url for ssh bitbucket mercurial style repo", () => { + test("Generate parts for ssh bitbucket mercurial style repo", () => { const repo = { url: "ssh://hg@bitbucket.org/YourOrganization/RepoOne", "url-pattern": { - "base-url" : "{url}/src/main/{path}{anchor}", "anchor" : "#{filename}-{line}" } }; const path = "test.txt" const line = null const rev = "main" - expect(UrlToRepo(repo, path, line, rev)).toBe( - "//bitbucket.org/YourOrganization/RepoOne/src/main/test.txt" - ); + expect(UrlParts(repo, path, line, rev)).toEqual(expect.objectContaining({ + url: "//bitbucket.org/YourOrganization/RepoOne", + path: path, + anchor: "", + })); }); - test("Generate url for ssh bitbucket style repo with port", () => { + test("Generate parts for ssh bitbucket style repo with port", () => { const repo = { url: "ssh://git@bitbucket.org:7999/YourOrganization/RepoOne", "url-pattern": { - "base-url" : "{url}/src/main/{path}{anchor}", "anchor" : "#{filename}-{line}" } }; const path = "test.txt" const line = null const rev = "main" - expect(UrlToRepo(repo, path, line, rev)).toBe( - "//bitbucket.org:7999/YourOrganization/RepoOne/src/main/test.txt" - ); + expect(UrlParts(repo, path, line, rev)).toEqual(expect.objectContaining({ + url: "//bitbucket.org:7999/YourOrganization/RepoOne", + path: path, + anchor: "", + })); }); - test("Generate url for ssh bitbucket server style repo", () => { + test("Generate parts for ssh bitbucket server style repo", () => { const repo = { url: "ssh://git@bitbucket.internal.com:7999/YourOrganization/RepoOne", "url-pattern": { - "base-url" : "{hostname}/projects/{project}/repos/{repo}/browse/{path}?at={rev}{anchor}", "anchor" : "#{line}", } }; const path = "test.txt" const line = 10 const rev = "main" + expect(UrlParts(repo, path, line, rev)).toEqual(expect.objectContaining({ + hostname: "//bitbucket.internal.com", + project: "YourOrganization", + repo: "RepoOne", + path: path, + rev: rev, + anchor: "#" + line, + })); + }); +}) + +describe("UrlToRepo", () => { + test("Generate url from repo with default values", () => { + const repo = { + url: "https://www.github.com/YourOrganization/RepoOne.git", + "url-pattern": + { + "base-url": "{url}/blob/{rev}/{path}{anchor}", + anchor: "#L{line}" + } + }; + const path = "test.txt" + const line = null + const rev = "main" expect(UrlToRepo(repo, path, line, rev)).toBe( - "//bitbucket.internal.com/projects/YourOrganization/repos/RepoOne/browse/test.txt?at=main#10" + "https://www.github.com/YourOrganization/RepoOne/blob/main/test.txt" ); }); }); diff --git a/ui/assets/js/hound.js b/ui/assets/js/hound.js index fea76ad7..e0e99a07 100644 --- a/ui/assets/js/hound.js +++ b/ui/assets/js/hound.js @@ -1,4 +1,4 @@ -import {UrlToRepo} from './common'; +import {UrlParts, UrlToRepo} from './common'; var Signal = function() { }; @@ -297,6 +297,10 @@ var Model = { UrlToRepo: function(repo, path, line, rev) { return UrlToRepo(this.repos[repo], path, line, rev); + }, + + UrlToRoot: function(repo) { + return UrlParts(this.repos[repo]).url } }; @@ -771,7 +775,9 @@ var ResultView = React.createClass({
- {Model.NameForRepo(result.Repo)} + + {Model.NameForRepo(result.Repo)} +