Skip to content

Commit e4fff42

Browse files
authored
Fix 'getProjectNameFromGitUrl' method (#23616)
1 parent e4e9400 commit e4fff42

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

tests/e2e/utils/StringUtil.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,21 @@ export class StringUtil {
3030
static getProjectNameFromGitUrl(url: string): string {
3131
Logger.debug(`Original URL: ${url}`);
3232

33-
try {
34-
// remove query and hash fragments
35-
url = url.split('?')[0].split('#')[0];
33+
// remove query and hash fragments
34+
url = url.split('?')[0].split('#')[0];
3635

37-
// remove branch/tree path fragments for major providers
38-
url = url
39-
.replace(/\/-?\/tree\/[^/]+$/, '') // gitLab, GitHub
40-
.replace(/\/src\/[^/]+.*$/, ''); // bitbucket
36+
// remove branch/tree fragments for GitHub, GitLab, Bitbucket
37+
if (url.includes('/tree/') || url.includes('/-/tree/') || url.includes('/src/')) {
38+
url = url.split('/').slice(0, -2).join('/');
39+
}
4140

42-
// take the last segment of the path and strip ".git"
43-
const projectName: string = (url.split('/').filter(Boolean).pop() || '').replace(/\.git$/, '');
41+
const projectName: string = url
42+
.split(/[\/.]/)
43+
.filter((e: string): boolean => !['git', ''].includes(e))
44+
.reverse()[0];
4445

45-
Logger.debug(`Extracted project name: ${projectName}`);
46-
return projectName;
47-
} catch (err) {
48-
Logger.error(`Failed to extract project name from URL ${url}: ${err}`);
49-
return '';
50-
}
46+
Logger.debug(`Extracted project name: ${projectName}`);
47+
return projectName;
5148
}
5249

5350
static sanitizeTitle(arg: string): string {

0 commit comments

Comments
 (0)