Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reroll #1816 into a new PR + fix for test failures #2384

Merged
merged 4 commits into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion __tests__/resolvers/exotics/bitbucket-resolver.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* @flow */

import {explodeHostedGitFragment} from '../../../src/resolvers/exotics/hosted-git-resolver.js';
import BitBucketResolver from '../../../src/resolvers/exotics/bitbucket-resolver.js';
import type {ExplodedFragment} from '../../../src/resolvers/exotics/hosted-git-resolver.js';
import Git from '../../../src/util/git.js';
import * as reporters from '../../../src/reporters/index.js';

const url = require('url');
const _bitBucketBase = 'https://bitbucket.org/';
const reporter = new reporters.NoopReporter({});

test('hostname should be "bitbucket.org" ', () => {
expect(BitBucketResolver.hostname).toBe('bitbucket.org');
Expand All @@ -15,6 +17,54 @@ test('protocol should be "bitbucket" ', () => {
expect(BitBucketResolver.protocol).toBe('bitbucket');
});

test('explodeHostedGitFragment should work for colon separator after host', () => {
const fragmentString = '[email protected]:team2/repo.git';

const expectedFragment: ExplodedFragment = {
user: 'team2',
repo: 'repo',
hash: '',
};

expect(explodeHostedGitFragment(fragmentString, reporter)).toEqual(expectedFragment);
});

test('explodeHostedGitFragment should work for colon separator after host and with protocol before', () => {
const fragmentString = 'git+ssh://[email protected]:team/repo.git';

const expectedFragment: ExplodedFragment = {
user: 'team',
repo: 'repo',
hash: '',
};

expect(explodeHostedGitFragment(fragmentString, reporter)).toEqual(expectedFragment);
});

test('explodeHostedGitFragment should work for slash separator after host', () => {
const fragmentString = '[email protected]/team/repo.git';

const expectedFragment: ExplodedFragment = {
user: 'team',
repo: 'repo',
hash: '',
};

expect(explodeHostedGitFragment(fragmentString, reporter)).toEqual(expectedFragment);
});

test('explodeHostedGitFragment should work for package name and colon separator after host', () => {
const fragmentString = 'package@[email protected]:team/repo.git';

const expectedFragment: ExplodedFragment = {
user: 'team',
repo: 'repo',
hash: '',
};

expect(explodeHostedGitFragment(fragmentString, reporter)).toEqual(expectedFragment);
});

test('getTarballUrl should return the correct bitbucket tarball url', () => {
const fragment: ExplodedFragment = {
user: 'foo',
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/exotics/git-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const urlParse = require('url').parse;
// we purposefully omit https and http as those are only valid if they end in the .git extension
const GIT_PROTOCOLS = ['git:', 'git+ssh:', 'git+https:', 'ssh:'];

const GIT_HOSTS = ['github.com', 'gitlab.com', 'bitbucket.com'];
const GIT_HOSTS = ['github.com', 'gitlab.com', 'bitbucket.com', 'bitbucket.org'];

export default class GitResolver extends ExoticResolver {
constructor(request: PackageRequest, fragment: string) {
Expand Down
34 changes: 29 additions & 5 deletions src/resolvers/exotics/hosted-git-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,45 @@ export type ExplodedFragment = {
hash: string,
};

export function explodeHostedGitFragment(fragment: string, reporter: Reporter): ExplodedFragment {
// TODO: make sure this only has a length of 2
export function explodeHostedGitFragment(fragment: string, reporter: Reporter): ExplodedFragment {

const preParts = fragment.split('@');
if (preParts.length > 2) {
fragment = preParts[1] + '@' + preParts[2];
}

const parts = fragment.split(':');
fragment = parts.pop();

const userParts = fragment.split('/');
if (parts.length == 3) { // protocol + host + folder
parts[1] = parts[1].indexOf('//') >= 0 ? parts[1].substr(2) : parts[1];
fragment = parts[1] + '/' + parts[2];
} else if (parts.length == 2) {
if (parts[0].indexOf('@') == -1) { // protocol + host
fragment = parts[1];
} else { // host + folder
fragment = parts[0] + '/' + parts[1];
}
} else if (parts.length == 1) {
fragment = parts[0];
} else {
throw new MessageError(reporter.lang('invalidHostedGitFragment', fragment));
}

const userParts = fragment.split('/');

if (userParts.length >= 2) {

if (userParts[0].indexOf('@') >= 0) {
userParts.shift();
}

const user = userParts.shift();
const repoParts = userParts.join('/').split(/(?:[.]git)?#(.*)/);

if (repoParts.length <= 3) {
return {
user,
repo: repoParts[0],
repo: repoParts[0].replace('.git', ''),
hash: repoParts[1] || '',
};
}
Expand Down