From c92ee78689c99d01a669d6111e8f120fa3fadbe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20=C3=85hsberg?= Date: Wed, 21 Oct 2020 15:22:13 +0200 Subject: [PATCH 1/2] Add support for Bitbucket Server --- lib/index.js | 19 ++++++++++++++++ test/index.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/lib/index.js b/lib/index.js index c776928..c5aa7b8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -183,7 +183,26 @@ 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; + } + + var bitbucket = /(projects|users)\/(.*?)\/repos\/(.*?)\/(?:raw\/|browse\/?)(.*?)$/; + var 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.filepath = matches[4]; + urlInfo.ref = urlInfo.query.at; + } return urlInfo; } diff --git a/test/index.js b/test/index.js index 3d03dd7..3e3180b 100644 --- a/test/index.js +++ b/test/index.js @@ -133,6 +133,66 @@ tester.describe("parse urls", test => { test.expect(res.name).toBe("name"); }); + // 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.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.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.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.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"); From 89098cd82b10b3bed26304baf19404a66fd2868a 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 2/2] Add support for Bitbucket Cloud raw and source urls --- lib/index.js | 22 ++++++++++++++++------ test/index.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index c5aa7b8..db8ae29 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,8 +204,14 @@ function gitUrlParse(url) { urlInfo.organization = urlInfo.owner; urlInfo.name = matches[3]; - urlInfo.filepath = matches[4]; - urlInfo.ref = urlInfo.query.at; + urlInfo.filepathtype = matches[4]; + urlInfo.filepath = matches[5]; + + if(urlInfo.query.at) { + urlInfo.ref = urlInfo.query.at; + } else { + urlInfo.ref = ""; + } } return urlInfo; } diff --git a/test/index.js b/test/index.js index 3e3180b..5e5802e 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,17 @@ 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"); + }); + + // 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", () => { @@ -162,6 +193,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 +202,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 +211,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", () => {