diff --git a/lib/index.js b/lib/index.js index c5aa7b8..eb5b5d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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('/'); @@ -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) { @@ -189,10 +193,10 @@ 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]; @@ -200,7 +204,9 @@ function gitUrlParse(url) { 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; diff --git a/test/index.js b/test/index.js index 3e3180b..9e1ca01 100644 --- a/test/index.js +++ b/test/index.js @@ -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"); @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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", () => {