Skip to content

Commit

Permalink
Add support for Bitbucket Cloud raw and source urls
Browse files Browse the repository at this point in the history
  • Loading branch information
goober committed Oct 22, 2020
1 parent c92ee78 commit 8d5b2bc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
16 changes: 11 additions & 5 deletions 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 @@ -189,18 +193,20 @@ function gitUrlParse(url) {
urlInfo.organization = urlInfo.owner;
}

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

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

urlInfo.ref = urlInfo.query.at;
}
return urlInfo;
Expand Down
24 changes: 24 additions & 0 deletions test/index.js
Expand Up @@ -133,6 +133,26 @@ 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");
Expand All @@ -154,6 +174,7 @@ tester.describe("parse urls", test => {
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 browse file", () => {
Expand All @@ -162,6 +183,7 @@ tester.describe("parse urls", test => {
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", () => {
Expand All @@ -170,6 +192,7 @@ tester.describe("parse urls", test => {
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", () => {
Expand All @@ -178,6 +201,7 @@ tester.describe("parse urls", test => {
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", () => {
Expand Down

0 comments on commit 8d5b2bc

Please sign in to comment.