use regex to get the project name from url
This commit is contained in:
@@ -2,43 +2,30 @@ export const IS_EDGE = navigator.userAgent.includes('Edg');
|
|||||||
export const IS_FIREFOX = navigator.userAgent.includes('Firefox');
|
export const IS_FIREFOX = navigator.userAgent.includes('Firefox');
|
||||||
export const IS_CHROME = IS_EDGE === false && IS_FIREFOX === false;
|
export const IS_CHROME = IS_EDGE === false && IS_FIREFOX === false;
|
||||||
|
|
||||||
export const generateProjectFromDevSites = (siteUrl: string): string | null => {
|
const REG_LIST = [
|
||||||
const url = new URL(siteUrl);
|
// 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
|
export const generateProjectFromDevSites = (url: string): string | null => {
|
||||||
// Example URL: https://github.com/wakatime/browser-wakatime?tab=readme-ov-file#development-instructions
|
let match: RegExpMatchArray | null = null;
|
||||||
const githubHosts = ['github.com', 'github.dev'];
|
|
||||||
for (const host of githubHosts) {
|
for (const reg of REG_LIST) {
|
||||||
if (url.host === host) {
|
match = url.match(reg);
|
||||||
return url.pathname.split('/')[2] || null;
|
if (match) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gitlab
|
return match?.[0] ?? null;
|
||||||
// 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;
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user