const urlRegex = /(https?:\/\/)?([\da-z\.-]+\.[a-z\.]{2,6})/; // eslint-disable-line no-useless-escape

const getUrl = (str) => urlRegex.exec(str);
const regex = /([^\/\s]+)\/?(?:(\w+))?\/([^\/]+?)$/; // eslint-disable-line no-useless-escape

/**
* Gitlab repository
* @typedef {Object} GitlabParsedRepository
* @property {String} repo full repository name, including owner & group
* @property {String} repository same as .repo
* @property {String} owner repo owner
* @property {String} group repo group, if any
* @property {String} name repo name
*/

/**
 * Gitlab repository parser
 * @param  {String} str input
 * @return {GitlabParsedRepository}     output
 */
module.exports = (str) => {
  if (!str || typeof str !== 'string' || !str.length) return {};

  const url = getUrl(str);
  const domain = url && url[2];
  const parsed = regex.exec(str.replace(urlRegex, ''));

  return parsed ? {
    host: domain,
    isGitlab: !domain || domain === 'gitlab.com',
    repo: parsed[0],
    repository: parsed[0],
    owner: parsed[1],
    group: parsed[2],
    name: parsed[3],
  } : {};
};

@@ -11,16 +11,17 @@
 egex = /
+%5E
 (https?:
@@ -53,16 +53,19 @@
 .%5D%7B2,6%7D)
+%5C/?
 /; // es
@@ -160,16 +160,17 @@
 egex = /
+%5E
 (%5B%5E%5C/%5Cs%5D
@@ -179,17 +179,24 @@
 %5C/?(
+(
 ?:
-(
+%5C/?(?:
 %5Cw+))
-?
+*)
 %5C/(%5B
@@ -202,16 +202,26 @@
 %5B%5E%5C/%5D+?)
+(?:%5C.git)?
 $/; // e

rework parser to support .git and multiple organization groups