|
36 | 36 | from commoncode import fileutils |
37 | 37 |
|
38 | 38 | from packagedcode import models |
| 39 | +from packagedcode.utils import parse_repo_url |
39 | 40 |
|
40 | 41 | """ |
41 | 42 | Handle Node.js NPM packages |
@@ -299,79 +300,6 @@ def repository_mapper(repo, package): |
299 | 300 | return package |
300 | 301 |
|
301 | 302 |
|
302 | | -VCS_URLS = ( |
303 | | - 'https://', |
304 | | - 'http://', |
305 | | - 'git://', |
306 | | - 'git+git://', |
307 | | - 'hg+https://', |
308 | | - 'hg+http://', |
309 | | - 'git+https://', |
310 | | - 'git+http://', |
311 | | - 'svn+https://', |
312 | | - 'svn+http://', |
313 | | - 'svn://', |
314 | | -) |
315 | | - |
316 | | - |
317 | | -def parse_repo_url(repo_url): |
318 | | - """ |
319 | | - Validate a repo_ulr and handle shortcuts for GitHub, GitHub gist, |
320 | | - Bitbucket, or GitLab repositories (same syntax as npm install): |
321 | | -
|
322 | | - See https://docs.npmjs.com/files/package.json#repository |
323 | | - This is done here in npm: |
324 | | - https://github.com/npm/npm/blob/d3c858ce4cfb3aee515bb299eb034fe1b5e44344/node_modules/hosted-git-info/git-host-info.js |
325 | | -
|
326 | | - These should be resolved: |
327 | | - npm/npm |
328 | | - gist:11081aaa281 |
329 | | - bitbucket:example/repo |
330 | | - gitlab:another/repo |
331 | | - expressjs/serve-static |
332 | | - git://github.com/angular/di.js.git |
333 | | - git://github.com/hapijs/boom |
334 | | - [email protected]:balderdashy/waterline-criteria.git |
335 | | - http://github.com/ariya/esprima.git |
336 | | - http://github.com/isaacs/nopt |
337 | | - https://github.com/chaijs/chai |
338 | | - https://github.com/christkv/kerberos.git |
339 | | - https://gitlab.com/foo/private.git |
340 | | - |
341 | | - """ |
342 | | - |
343 | | - # TODO: Improve this and use outside of NPMs |
344 | | - is_vcs_url = repo_url.startswith(VCS_URLS) |
345 | | - if is_vcs_url: |
346 | | - # TODO: ensure the .git suffix is present if needed |
347 | | - return repo_url |
348 | | - |
349 | | - if repo_url.startswith('git@'): |
350 | | - left, right = repo_url.split('@', 1) |
351 | | - host, repo = right.split(':', 1) |
352 | | - # may be we should |
353 | | - if any(h in host for h in ['github', 'bitbucket', 'gitlab']): |
354 | | - return 'https://%(host)s/%(repo)s' % locals() |
355 | | - else: |
356 | | - return repo_url |
357 | | - |
358 | | - if repo_url.startswith('gist:'): |
359 | | - return repo_url |
360 | | - |
361 | | - elif repo_url.startswith(('bitbucket:', 'gitlab:', 'github:')): |
362 | | - hoster_urls = { |
363 | | - 'bitbucket:': 'https://bitbucket.org/%(repo)s', |
364 | | - 'github:': 'https://github.com/%(repo)s', |
365 | | - 'gitlab:': 'https://gitlab.com/%(repo)s', |
366 | | - } |
367 | | - hoster, repo = repo_url.split(':', 1) |
368 | | - return hoster_urls[hoster] % locals() |
369 | | - elif len(repo_url.split('/')) == 2: |
370 | | - # implicit github |
371 | | - return 'https://github.com/%(repo_url)s' % locals() |
372 | | - return repo_url |
373 | | - |
374 | | - |
375 | 303 | def url_mapper(url, package): |
376 | 304 | """ |
377 | 305 | In a package.json, the "url" field is a redirection to a package download |
|
0 commit comments