Skip to content

Commit

Permalink
Add hyperlinks to repository root (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrarmstro authored Jul 2, 2021
1 parent 70e1229 commit c9a7667
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 63 deletions.
16 changes: 12 additions & 4 deletions ui/assets/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) : '';
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
91 changes: 61 additions & 30 deletions ui/assets/js/common.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExpandVars, UrlToRepo } from "./common";
import { ExpandVars, UrlParts, UrlToRepo } from "./common";

describe("ExpandVars", () => {
test("Replaces template variables with their values", () => {
Expand All @@ -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"
);
});
});
10 changes: 8 additions & 2 deletions ui/assets/js/hound.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {UrlToRepo} from './common';
import {UrlParts, UrlToRepo} from './common';

var Signal = function() {
};
Expand Down Expand Up @@ -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
}

};
Expand Down Expand Up @@ -771,7 +775,9 @@ var ResultView = React.createClass({
<div className="repo">
<div className="title">
<span className="mega-octicon octicon-repo"></span>
<span className="name">{Model.NameForRepo(result.Repo)}</span>
<span className="name">
<a href={Model.UrlToRoot(result.Repo)}>{Model.NameForRepo(result.Repo)}</a>
</span>
</div>
<FilesView matches={result.Matches}
rev={result.Rev}
Expand Down
Loading

0 comments on commit c9a7667

Please sign in to comment.