Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit ports in constructed URL #383

Merged
merged 4 commits into from
Apr 12, 2021
Merged
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
10 changes: 10 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@
"url-pattern" : {
"base-url" : "{url}/{path}"
}
},
"BitbucketServerUrl" : {
"url" : "[email protected]:7999:organization/project.git",
"url-pattern" : {
"base-url" : "https://{hostname}/projects/{project}/repos/{repo}/browse/{path}?at={rev}{anchor}",
"anchor" : "#{line}"
},
"vcs-config" : {
"ref" : "main"
}
}
}
}
23 changes: 20 additions & 3 deletions ui/assets/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export function ExpandVars(template, values) {
export function UrlToRepo(repo, path, line, rev) {
var url = repo.url.replace(/\.git$/, ''),
pattern = repo['url-pattern'],
hostname = '',
project = '',
repoName = '',
port = '',
filename = path.substring(path.lastIndexOf('/') + 1),
anchor = line ? ExpandVars(pattern.anchor, { line : line, filename : filename }) : '';

Expand All @@ -25,15 +29,28 @@ export function UrlToRepo(repo, path, line, rev) {

// Regex explained: Match either `git` or `hg` followed by an `@`.
// Next, slurp up the hostname by reading until either a `:` or `/` is found.
// Finally, grab all remaining characters.
var sshParts = /(git|hg)@(.*?)(:|\/)(.*)/.exec(url);
// If a port is specified, slurp that up too. Finally, grab the project and
// repo names.
var sshParts = /(git|hg)@(.*?)(:[0-9]+)?(:|\/)(.*)(\/)(.*)/.exec(url);
if (sshParts) {
url = '//' + sshParts[2] + '/' + sshParts[4];
hostname = '//' + sshParts[2]
project = sshParts[5]
repoName = sshParts[7]
// Port is omitted in most cases. Bitbucket Server is special:
// ssh://[email protected]:7999/ATLASSIAN/jira.git
if(sshParts[3]){
port = sshParts[3]
}
url = hostname + port + '/' + project + '/' + repoName;
}

// I'm sure there is a nicer React/jsx way to do this:
return ExpandVars(pattern['base-url'], {
url : url,
hostname: hostname,
port: port,
project: project,
'repo': repoName,
path: path,
rev: rev,
anchor: anchor
Expand Down
104 changes: 104 additions & 0 deletions ui/assets/js/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,107 @@ describe("ExpandVars", () => {
expect(ExpandVars(template, {})).toBe(template);
});
});

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(
"https://www.github.com/YourOrganization/RepoOne/blob/main/test.txt"
);
});

test("Generate url 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"
);
});

test("Generate url for ssh style repo with default values", () => {
const repo = {
url: "[email protected]: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"
);
});

test("Generate url for ssh bitbucket mercurial style repo", () => {
const repo = {
url: "ssh://[email protected]/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"
);
});

test("Generate url for ssh bitbucket style repo with port", () => {
const repo = {
url: "ssh://[email protected]: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"
);
});

test("Generate url for ssh bitbucket server style repo", () => {
const repo = {
url: "ssh://[email protected]: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(UrlToRepo(repo, path, line, rev)).toBe(
"//bitbucket.internal.com/projects/YourOrganization/repos/RepoOne/browse/test.txt?at=main#10"
);
});
});
Loading