Skip to content

Commit

Permalink
fix(github-resolver): support for semver hash (#5858)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenkan authored and imsnif committed May 29, 2018
1 parent c640c45 commit 643deaa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 85 deletions.
72 changes: 23 additions & 49 deletions __tests__/resolvers/exotics/hosted-git-resolver.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,30 @@
/* @flow */

import {explodeHostedGitFragment} from '../../../src/resolvers/exotics/hosted-git-resolver.js';
import type {ExplodedFragment} from '../../../src/resolvers/exotics/hosted-git-resolver.js';
import * as reporters from '../../../src/reporters/index.js';
const reporter = new reporters.NoopReporter({});

test('explodeHostedGitFragment should allow for hashes as part of the branch name', () => {
const fragmentString = 'jure/lens#fix-issue-#96';

const expectedFragment: ExplodedFragment = {
user: 'jure',
repo: 'lens',
hash: 'fix-issue-#96',
};

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

test('explodeHostedGitFragment should work for branch names without hashes', () => {
const fragmentString = 'jure/lens#feature/fix-issue';

const expectedFragment: ExplodedFragment = {
user: 'jure',
repo: 'lens',
hash: 'feature/fix-issue',
};

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

test('explodeHostedGitFragment should work identical with and without .git suffix', () => {
const fragmentWithGit = 'jure/lens.git#feature/fix-issue';
const fragmentWithoutGit = 'jure/lens#feature/fix-issue';

const expectedFragment: ExplodedFragment = {
user: 'jure',
repo: 'lens',
hash: 'feature/fix-issue',
};

expect(explodeHostedGitFragment(fragmentWithoutGit, reporter)).toEqual(expectedFragment);
expect(explodeHostedGitFragment(fragmentWithGit, reporter)).toEqual(expectedFragment);
});

test('explodeHostedGitFragment should allow the project name to contain .git', () => {
const fragmentString = 'jure/lens.github';

const expectedFragment: ExplodedFragment = {
user: 'jure',
repo: 'lens.github',
hash: '',
};

expect(explodeHostedGitFragment(fragmentString, reporter)).toEqual(expectedFragment);
[
{fragment: 'jure/lens#fix-issue-#96', user: 'jure', repo: 'lens', hash: 'fix-issue-#96'},
{fragment: 'jure/lens#feature/fix-issue', user: 'jure', repo: 'lens', hash: 'feature/fix-issue'},
{fragment: 'jure/lens.github', user: 'jure', repo: 'lens.github', hash: ''},
{fragment: 'jure/lens#semver:^3.0.0', user: 'jure', repo: 'lens', hash: 'semver:^3.0.0'},
{fragment: 'github:jure/lens', user: 'jure', repo: 'lens', hash: ''},
{fragment: 'bitbucket:jure/lens', user: 'jure', repo: 'lens', hash: ''},
{fragment: 'git+ssh://git@github.com:jure/lens', user: 'jure', repo: 'lens', hash: ''},
{fragment: 'git+ssh://git@github.com:jure/lens.git', user: 'jure', repo: 'lens', hash: ''},
{fragment: 'git://git@github.com:jure/lens.git', user: 'jure', repo: 'lens', hash: ''},
{fragment: 'git+https://login@github.com/jure/lens.git', user: 'jure', repo: 'lens', hash: ''},
{fragment: 'git+ssh://git@github.com/jure/lens.git#semver:^3.1.0', user: 'jure', repo: 'lens', hash: 'semver:^3.1.0'},
{fragment: 'git+http://login@github.com/jure/lens.git', user: 'jure', repo: 'lens', hash: ''},
].forEach(({fragment, user, repo, hash}) => {
test(`explodeHostedGitFragment ${fragment} -> user: '${user}'`, () => {
expect(explodeHostedGitFragment(fragment, reporter).user).toEqual(user);
});
test(`explodeHostedGitFragment ${fragment} -> repo: '${repo}'`, () => {
expect(explodeHostedGitFragment(fragment, reporter).repo).toEqual(repo);
});
test(`explodeHostedGitFragment ${fragment} -> hash: '${hash}'`, () => {
expect(explodeHostedGitFragment(fragment, reporter).hash).toEqual(hash);
});
});
57 changes: 21 additions & 36 deletions src/resolvers/exotics/hosted-git-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,37 @@ export type ExplodedFragment = {
hash: string,
};

function parseHash(fragment: string): string {
const hashPosition = fragment.indexOf('#');
return hashPosition === -1 ? '' : fragment.substr(hashPosition + 1);
}

export function explodeHostedGitFragment(fragment: string, reporter: Reporter): ExplodedFragment {
const hash = parseHash(fragment);

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

const parts = 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 parts = fragment
.replace(/(.*?)#.*/, '$1') // Strip hash
.replace(/.*:(.*)/, '$1') // Strip prefixed protocols
.replace(/.git$/, '') // Strip the .git suffix
.split('/');

const user = userParts.shift();
const repoParts = userParts.join('/').split(/(?:[.]git)?#(.*)/);
const user = parts[parts.length - 2];
const repo = parts[parts.length - 1];

if (repoParts.length <= 3) {
return {
user,
repo: repoParts[0].replace(/\.git$/, ''),
hash: repoParts[1] || '',
};
}
if (user === undefined || repo === undefined) {
throw new MessageError(reporter.lang('invalidHostedGitFragment', fragment));
}

throw new MessageError(reporter.lang('invalidHostedGitFragment', fragment));
return {
user,
repo,
hash,
};
}

export default class HostedGitResolver extends ExoticResolver {
Expand Down

0 comments on commit 643deaa

Please sign in to comment.