/ src / lib / utils / build-repo-url.ts
build-repo-url.ts
 1  import { BASE_URL } from './base-url';
 2  import { isValidGitUrl } from './is-valid-git-url';
 3  
 4  export function isDripsProjectUrl(value: string): boolean {
 5    return value.includes(`${BASE_URL}/app/projects/`);
 6  }
 7  
 8  export function buildRepositoryURL(url: string): string {
 9    const parsedUrl = new URL(url);
10    const pathSegments = parsedUrl.pathname.split('/').filter((segment) => segment.length > 0);
11  
12    const forgeIndex = pathSegments.findIndex((segment) =>
13      ['github', 'gitlab', 'bitbucket'].includes(segment),
14    );
15  
16    if (forgeIndex !== -1) {
17      const forge = pathSegments[forgeIndex];
18      const repoPath = pathSegments.slice(forgeIndex + 1).join('/');
19  
20      if (!repoPath) {
21        throw new Error('Repository path is incomplete.');
22      }
23  
24      if (forge === 'github') {
25        const githubUrl = `https://github.com/${repoPath}`;
26  
27        if (isValidGitUrl(githubUrl)) {
28          return `https://github.com/${repoPath}`;
29        }
30  
31        throw new Error(`Invalid GitHub URL: ${githubUrl}.`);
32      } else {
33        throw new Error(`Unsupported forge: ${forge}.`);
34      }
35    }
36  
37    throw new Error('Forge not found in the URL path.');
38  }