1 | import gitUrlParse from 'git-url-parse';
|
2 |
|
3 | function normalizeRawRepository({
|
4 | rawRepository
|
5 | }) {
|
6 | if (isRepository(rawRepository)) {
|
7 | return normalizeRepository({
|
8 | rawRepository
|
9 | });
|
10 | }
|
11 |
|
12 | if (typeof rawRepository === 'string') {
|
13 | return normalizeRepository({
|
14 | rawRepository: {
|
15 | url: rawRepository
|
16 | }
|
17 | });
|
18 | }
|
19 |
|
20 | return undefined;
|
21 | }
|
22 |
|
23 | function isRepository(rawRepository) {
|
24 | return rawRepository && typeof rawRepository === 'object' && typeof rawRepository['url'] === 'string' && ['string', 'undefined'].includes(typeof rawRepository['type']) && ['string', 'undefined'].includes(typeof rawRepository['directory']);
|
25 | }
|
26 |
|
27 | function normalizeRepository({
|
28 | rawRepository
|
29 | }) {
|
30 | const {
|
31 | url,
|
32 | directory: repositoryDir
|
33 | } = rawRepository;
|
34 | const info = parseGitURL({
|
35 | url
|
36 | });
|
37 |
|
38 | if (!info) {
|
39 | return undefined;
|
40 | }
|
41 |
|
42 | const {
|
43 | source,
|
44 | full_name: repositoryID,
|
45 | filepath
|
46 | } = info;
|
47 |
|
48 | const host = source.replace(/^$/, 'github.com').replace(/^github$/, 'github.com').replace(/^gitlab$/, 'gitlab.com').replace(/^bitbucket$/, 'bitbucket.org');
|
49 | const parsedDir = filepath !== '' ? filepath : undefined;
|
50 | return {
|
51 | type: 'git',
|
52 | url: `https://${host}/${repositoryID}`,
|
53 | directory: repositoryDir != null ? repositoryDir : parsedDir
|
54 | };
|
55 | }
|
56 |
|
57 | function parseGitURL({
|
58 | url
|
59 | }) {
|
60 | let info;
|
61 |
|
62 | try {
|
63 | info = gitUrlParse(url);
|
64 | } catch {}
|
65 |
|
66 | return info;
|
67 | }
|
68 |
|
69 | export { normalizeRawRepository };
|
70 |
|