From 5f2f4da9640bc7bc45e1d14ad9113dc1bf3e2f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20=C3=85hsberg?= Date: Wed, 21 Oct 2020 21:46:29 +0200 Subject: [PATCH] Add support for Bitbucket Cloud raw and source urls --- lib/index.js | 6 +++++- test/index.js | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index c5aa7b8..3969988 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) { diff --git a/test/index.js b/test/index.js index 3e3180b..faa3327 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");