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

Add support for Bitbucket Server #113

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/index.js
Expand Up @@ -148,10 +148,14 @@ function gitUrlParse(url) {
const blobIndex = splits.indexOf("blob", 2);
const treeIndex = splits.indexOf("tree", 2);
const commitIndex = splits.indexOf("commit", 2);
const srcIndex = splits.indexOf("src", 2);
const rawIndex = splits.indexOf("raw", 2);
nameIndex = dashIndex > 0 ? dashIndex - 1
: blobIndex > 0 ? blobIndex - 1
: treeIndex > 0 ? treeIndex - 1
: commitIndex > 0 ? commitIndex - 1
: srcIndex > 0 ? srcIndex - 1
: rawIndex > 0 ? rawIndex - 1
: nameIndex;

urlInfo.owner = splits.slice(0, nameIndex).join('/');
Expand All @@ -165,7 +169,7 @@ function gitUrlParse(url) {
urlInfo.filepathtype = "";
urlInfo.filepath = "";
const offsetNameIndex = splits.length > nameIndex && splits[nameIndex+1] === "-" ? nameIndex + 1 : nameIndex;
if ((splits.length > offsetNameIndex + 2) && (["blob", "tree"].indexOf(splits[offsetNameIndex + 1]) >= 0)) {
if ((splits.length > offsetNameIndex + 2) && (["raw","src","blob", "tree"].indexOf(splits[offsetNameIndex + 1]) >= 0)) {
urlInfo.filepathtype = splits[offsetNameIndex + 1];
urlInfo.ref = splits[offsetNameIndex + 2];
if (splits.length > offsetNameIndex + 3) {
Expand All @@ -183,7 +187,32 @@ function gitUrlParse(url) {
urlInfo.full_name += urlInfo.name;
}
}
// Bitbucket Server
if(urlInfo.owner.startsWith("scm/")) {
urlInfo.owner = urlInfo.owner.replace("scm/","");
urlInfo.organization = urlInfo.owner;
}

const bitbucket = /(projects|users)\/(.*?)\/repos\/(.*?)\/(raw|browse)\/(.*?)$/
const matches = bitbucket.exec(urlInfo.pathname)
if(matches != null) {
if (matches[1] === "users") {
urlInfo.owner = "~" + matches[2];
} else {
urlInfo.owner = matches[2];
}

urlInfo.organization = urlInfo.owner;
urlInfo.name = matches[3];
urlInfo.filepathtype = matches[4];
urlInfo.filepath = matches[5];

if(urlInfo.query.at) {
urlInfo.ref = urlInfo.query.at;
} else {
urlInfo.ref = "";
}
}
return urlInfo;
}

Expand Down
94 changes: 94 additions & 0 deletions test/index.js
Expand Up @@ -133,6 +133,100 @@ tester.describe("parse urls", test => {
test.expect(res.name).toBe("name");
});

// bitbucket cloud src file
test.should("parse Bitbucket Cloud src file", () => {
var res = gitUrlParse("https://bitbucket.org/owner/name/src/master/README.md");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
test.expect(res.filepath).toBe("README.md");
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("src");
});

// bitbucket cloud raw file
test.should("parse Bitbucket Cloud raw file", () => {
var res = gitUrlParse("https://bitbucket.org/owner/name/raw/master/README.md");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
test.expect(res.filepath).toBe("README.md");
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("raw");
});

// https bitbucket server
test.should("parse Bitbucket Server clone over http", () => {
var res = gitUrlParse("https://user@bitbucket.companyname.com/scm/owner/name.git");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
});

// ssh bitbucket server
test.should("parse Bitbucket Server clone over ssh", () => {
var res = gitUrlParse("ssh://git@bitbucket.companyname.com/owner/name.git");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
});

// bitbucket server raw file
test.should("parse Bitbucket Server raw file", () => {
var res = gitUrlParse("https://bitbucket.mycompany.com/projects/owner/repos/name/raw/README.md?at=master");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
test.expect(res.filepath).toBe("README.md")
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("raw");
});

// bitbucket server raw file
test.should("parse Bitbucket Server raw file without ref", () => {
var res = gitUrlParse("https://bitbucket.mycompany.com/projects/owner/repos/name/raw/README.md");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
test.expect(res.filepath).toBe("README.md")
test.expect(res.ref).toBe("");
test.expect(res.filepathtype).toBe("raw");
});

test.should("parse Bitbucket server browse file", () => {
var res = gitUrlParse("https://bitbucket.mycompany.com/projects/owner/repos/name/browse/README.md?at=master");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
test.expect(res.filepath).toBe("README.md");
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("browse");
});

test.should("parse Bitbucket Server personal repository browse url", () => {
var res = gitUrlParse("https://bitbucket.mycompany.com/users/owner/repos/name/browse/README.md?at=master");
test.expect(res.owner).toBe("~owner");
test.expect(res.name).toBe("name");
test.expect(res.filepath).toBe("README.md");
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("browse");
});

test.should("parse Bitbucket Server personal repository raw url", () => {
var res = gitUrlParse("https://bitbucket.mycompany.com/users/owner/repos/name/raw/README.md?at=master");
test.expect(res.owner).toBe("~owner");
test.expect(res.name).toBe("name");
test.expect(res.filepath).toBe("README.md");
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("raw");
});

test.should("parse Bitbucket Server personal repository clone over ssh", () => {
var res = gitUrlParse("ssh://git@bitbucket.mycompany.com/~owner/name.git");
test.expect(res.owner).toBe("~owner");
test.expect(res.name).toBe("name");
});

test.should("parse Bitbucket Server personal repository clone over http", () => {
var res = gitUrlParse("https://bitbucket.mycompany.com/scm/~owner/name.git");
test.expect(res.owner).toBe("~owner");
test.expect(res.organization).toBe("~owner")
test.expect(res.name).toBe("name");
});

// https cloudforge
test.should("parse CloudForge urls", () => {
var res = gitUrlParse("https://owner@organization.git.cloudforge.com/name.git");
Expand Down