[Code] Improve git url parsing for github repo url with branches/refs (#37397) (#37427)

This commit is contained in:
Mengwei Ding 2019-05-29 22:18:24 -07:00 committed by GitHub
parent b533ab9298
commit 097bf8596e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View file

@ -70,6 +70,24 @@ test('Repository url parsing', () => {
org: 'foo',
protocol: 'ssh',
});
const repo8 = RepositoryUtils.buildRepository('https://github.com/apache/sqoop/a/b');
expect(repo8).toEqual({
uri: 'github.com/apache_sqoop_a/b',
url: 'https://github.com/apache/sqoop/a/b',
name: 'b',
org: 'apache_sqoop_a',
protocol: 'https',
});
const repo9 = RepositoryUtils.buildRepository('https://github.com/apache/sqoop/tree/master');
expect(repo9).toEqual({
uri: 'github.com/apache_sqoop_tree/master',
url: 'https://github.com/apache/sqoop/tree/master',
name: 'master',
org: 'apache_sqoop_tree',
protocol: 'https',
});
});
test('Repository url parsing with non standard segments', () => {
@ -104,10 +122,10 @@ test('Repository url parsing with non standard segments', () => {
test('Repository url parsing with port', () => {
const repo1 = RepositoryUtils.buildRepository('ssh://mine@mydomain.com:27017/gitolite-admin');
expect(repo1).toEqual({
uri: 'mydomain.com:27017/mine/gitolite-admin',
uri: 'mydomain.com:27017/_/gitolite-admin',
url: 'ssh://mine@mydomain.com:27017/gitolite-admin',
name: 'gitolite-admin',
org: 'mine',
org: '_',
protocol: 'ssh',
});

View file

@ -15,13 +15,23 @@ import { parseLspUrl, toCanonicalUrl } from './uri_util';
export class RepositoryUtils {
// Generate a Repository instance by parsing repository remote url
public static buildRepository(remoteUrl: string): Repository {
const repo = GitUrlParse(remoteUrl);
const repo = GitUrlParse(remoteUrl.trim());
let host = repo.source ? repo.source : '';
if (repo.port !== null) {
host = host + ':' + repo.port;
}
const name = repo.name ? repo.name : '_';
const org = repo.owner ? repo.owner.split('/').join('_') : '_';
// Remove the leading `/` and tailing `.git` if necessary.
const pathname =
repo.pathname && repo.git_suffix
? repo.pathname.substr(1, repo.pathname.length - 5)
: repo.pathname.substr(1);
// The pathname should look like `a/b/c` now.
const segs = pathname.split('/').filter(s => s.length > 0);
const org = segs.length >= 2 ? segs.slice(0, segs.length - 1).join('_') : '_';
const name = segs.length >= 1 ? segs[segs.length - 1] : '_';
const uri: RepositoryUri = host ? `${host}/${org}/${name}` : repo.full_name;
return {
uri,