From 274a660369f429e3e86777711e1222644fb86128 Mon Sep 17 00:00:00 2001 From: Rohid Date: Mon, 19 Aug 2024 15:12:25 +0600 Subject: [PATCH 1/3] extract project name from gitlab --- src/utils/index.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 9b88e46..1ff6cf9 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,13 +4,26 @@ export const IS_CHROME = IS_EDGE === false && IS_FIREFOX === false; export const generateProjectFromDevSites = (siteUrl: string): string | null => { const url = new URL(siteUrl); - const githubUrls = ['github.com', 'github.dev']; - for (const githubUrl of githubUrls) { - if (url.host === githubUrl) { + + // Github + const githubHosts = ['github.com', 'github.dev']; + for (const host of githubHosts) { + if (url.host === host) { // input: https://github.com/wakatime/browser-wakatime?tab=readme-ov-file#development-instructions // output: browser-wakatime return url.pathname.split('/')[2] || null; } } + + // Gitlab + const gitlabHosts = ['gitlab.com']; + for (const host of gitlabHosts) { + if (url.host === host) { + // input: https://gitlab.com/wakatime/browser-wakatime?tab=readme-ov-file#development-instructions + // output: browser-wakatime + return url.pathname.split('/')[2] || null; + } + } + return null; }; From 8b3ff4482de476d9528f701befc557d2ff964eab Mon Sep 17 00:00:00 2001 From: Rohid Date: Mon, 19 Aug 2024 15:33:43 +0600 Subject: [PATCH 2/3] extract project name from BitBucket and Travis CI --- src/utils/index.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 1ff6cf9..24fde52 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -6,24 +6,39 @@ export const generateProjectFromDevSites = (siteUrl: string): string | null => { const url = new URL(siteUrl); // Github + // Example URL: https://github.com/wakatime/browser-wakatime?tab=readme-ov-file#development-instructions const githubHosts = ['github.com', 'github.dev']; for (const host of githubHosts) { if (url.host === host) { - // input: https://github.com/wakatime/browser-wakatime?tab=readme-ov-file#development-instructions - // output: browser-wakatime return url.pathname.split('/')[2] || null; } } // Gitlab + // Example URL: https://gitlab.com/wakatime/browser-wakatime?tab=readme-ov-file#development-instructions const gitlabHosts = ['gitlab.com']; for (const host of gitlabHosts) { if (url.host === host) { - // input: https://gitlab.com/wakatime/browser-wakatime?tab=readme-ov-file#development-instructions - // output: browser-wakatime return url.pathname.split('/')[2] || null; } } + // BitBucket + // Example URL: https://bitbucket.org/rohidul209/my-test-repo/src + const bitbucketHosts = ['bitbucket.org']; + for (const host of bitbucketHosts) { + if (url.host === host) { + return url.pathname.split('/')[2] || null; + } + } + + // Travis CI + // Example URL: https://app.travis-ci.com/github/iam-rohid/ai-expense-tracker/no-build?serverType=git + const travisHosts = ['app.travis-ci.com']; + for (const host of travisHosts) { + if (url.host === host) { + return url.pathname.split('/')[3] || null; + } + } return null; }; From 332682a37ca1686fdce328196777f6d765c8b19f Mon Sep 17 00:00:00 2001 From: Rohid Date: Mon, 19 Aug 2024 16:32:47 +0600 Subject: [PATCH 3/3] use regex to get the project name from url --- src/utils/index.ts | 57 ++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 24fde52..2b65339 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,43 +2,30 @@ export const IS_EDGE = navigator.userAgent.includes('Edg'); export const IS_FIREFOX = navigator.userAgent.includes('Firefox'); export const IS_CHROME = IS_EDGE === false && IS_FIREFOX === false; -export const generateProjectFromDevSites = (siteUrl: string): string | null => { - const url = new URL(siteUrl); +const REG_LIST = [ + // GitHub. Eg. URL: https://github.com/workspace-name/project-name + /(?<=github\.(?:com|dev)\/[^/]+\/)([^/?#]+)/, + // Gitlab. Eg. URL: https://gitlab.com/workspace-name/project-name + /(?<=gitlab\.com\/[^/]+\/)([^/?#]+)/, + // BitBucket. Eg. URL: https://bitbucket.org/workspace-name/project-name/src + /(?<=bitbucket\.org\/[^/]+\/)([^/?#]+)/, + // Travis CI. Eg. URL: https://app.travis-ci.com/github/workspace-name/project-name/no-build?serverType=git + /(?<=app\.travis-ci\.com\/[^/]+\/[^/]+\/)([^/?#]+)/, + // Circle CI. Eg. URL: https://app.circleci.com/projects/project-setup/github/workspace-name/project-name/ + /(?<=app\.circleci\.com\/projects\/[^/]+\/[^/]+\/[^/]+\/)([^/?#]+)/, + // Vercel. Eg. URL: http://vercel.com/team-name/project-name + /(?<=vercel\.com\/[^/]+\/)([^/?#]+)/, +]; - // Github - // Example URL: https://github.com/wakatime/browser-wakatime?tab=readme-ov-file#development-instructions - const githubHosts = ['github.com', 'github.dev']; - for (const host of githubHosts) { - if (url.host === host) { - return url.pathname.split('/')[2] || null; +export const generateProjectFromDevSites = (url: string): string | null => { + let match: RegExpMatchArray | null = null; + + for (const reg of REG_LIST) { + match = url.match(reg); + if (match) { + break; } } - // Gitlab - // Example URL: https://gitlab.com/wakatime/browser-wakatime?tab=readme-ov-file#development-instructions - const gitlabHosts = ['gitlab.com']; - for (const host of gitlabHosts) { - if (url.host === host) { - return url.pathname.split('/')[2] || null; - } - } - - // BitBucket - // Example URL: https://bitbucket.org/rohidul209/my-test-repo/src - const bitbucketHosts = ['bitbucket.org']; - for (const host of bitbucketHosts) { - if (url.host === host) { - return url.pathname.split('/')[2] || null; - } - } - - // Travis CI - // Example URL: https://app.travis-ci.com/github/iam-rohid/ai-expense-tracker/no-build?serverType=git - const travisHosts = ['app.travis-ci.com']; - for (const host of travisHosts) { - if (url.host === host) { - return url.pathname.split('/')[3] || null; - } - } - return null; + return match?.[0] ?? null; };