Skip to content

Commit 9500430

Browse files
committed
feat: support custom user in ssh url
1 parent 881ecb4 commit 9500430

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

dist/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var normalizeUrl__default = /*#__PURE__*/_interopDefaultLegacy(normalizeUrl);
4343
const parseUrl = (url, normalize = false) => {
4444

4545
// Constants
46-
const GIT_RE = /^(?:git@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/;
46+
const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/;
4747

4848
const throwErr = msg => {
4949
const err = new Error(msg);
@@ -77,10 +77,10 @@ const parseUrl = (url, normalize = false) => {
7777
if (matched) {
7878
parsed.protocols = ["ssh"];
7979
parsed.protocol = "ssh";
80-
parsed.resource = matched[1];
81-
parsed.host = matched[1];
82-
parsed.user = "git";
83-
parsed.pathname = `/${matched[2]}`;
80+
parsed.resource = matched[2];
81+
parsed.host = matched[2];
82+
parsed.user = matched[1];
83+
parsed.pathname = `/${matched[3]}`;
8484
parsed.parse_failed = false;
8585
} else {
8686
throwErr("URL parsing failed.");

dist/index.mjs

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import normalizeUrl from 'normalize-url';
3636
const parseUrl = (url, normalize = false) => {
3737

3838
// Constants
39-
const GIT_RE = /^(?:git@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/;
39+
const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/;
4040

4141
const throwErr = msg => {
4242
const err = new Error(msg);
@@ -70,10 +70,10 @@ const parseUrl = (url, normalize = false) => {
7070
if (matched) {
7171
parsed.protocols = ["ssh"];
7272
parsed.protocol = "ssh";
73-
parsed.resource = matched[1];
74-
parsed.host = matched[1];
75-
parsed.user = "git";
76-
parsed.pathname = `/${matched[2]}`;
73+
parsed.resource = matched[2];
74+
parsed.host = matched[2];
75+
parsed.user = matched[1];
76+
parsed.pathname = `/${matched[3]}`;
7777
parsed.parse_failed = false;
7878
} else {
7979
throwErr("URL parsing failed.");

src/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import normalizeUrl from "normalize-url";
3535
const parseUrl = (url, normalize = false) => {
3636

3737
// Constants
38-
const GIT_RE = /^(?:git@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/
38+
const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/
3939

4040
const throwErr = msg => {
4141
const err = new Error(msg)
@@ -69,10 +69,10 @@ const parseUrl = (url, normalize = false) => {
6969
if (matched) {
7070
parsed.protocols = ["ssh"]
7171
parsed.protocol = "ssh"
72-
parsed.resource = matched[1]
73-
parsed.host = matched[1]
74-
parsed.user = "git"
75-
parsed.pathname = `/${matched[2]}`
72+
parsed.resource = matched[2]
73+
parsed.host = matched[2]
74+
parsed.user = matched[1]
75+
parsed.pathname = `/${matched[3]}`
7676
parsed.parse_failed = false
7777
} else {
7878
throwErr("URL parsing failed.")

test/index.mjs

+17
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@ const INPUTS = [
133133
, parse_failed: false
134134
}
135135
]
136+
, [
137+
["[email protected]:my-org/my-repo.git", false],
138+
{
139+
protocols: [ 'ssh' ]
140+
, protocol: 'ssh'
141+
, port: ''
142+
, resource: 'github.my-enterprise.com'
143+
, host: 'github.my-enterprise.com'
144+
, user: 'org-12345678'
145+
, password: ''
146+
, pathname: '/my-org/my-repo.git'
147+
, hash: ''
148+
, search: ''
149+
, query: {}
150+
, parse_failed: false
151+
}
152+
]
136153
, [
137154
["[email protected]:halup/Cloud.API.Gateway.git", false]
138155
, {

0 commit comments

Comments
 (0)